protected void addNodes( Integer graphId, Connection con, ContainerLoader cl, Map<String, Object> graphParam) throws SQLException { String urlbaseTmp = (String) graphParam.get("url_base"); final ElementDraft.Factory elementDraftFactory = cl.factory(); final Pattern tagSplitter = Pattern.compile(","); final String urlbase = urlbaseTmp == null ? "/" : urlbaseTmp; graphDataSource.populateNodesForGraph( con, graphId, (num, name, tag) -> { String[] tags = tag != null ? tagSplitter.split(tag) : new String[] {}; NodeDraft nd = elementDraftFactory.newNodeDraft(num.toString()); nd.setLabel(name); nd.setValue(KEY_URL, urlbase + num); nd.setValue(KEY_TAG, tags); cl.addNode(nd); return true; }); }
public void convertToNetwork() throws IOException, InvalidFormatException { container = MyFileImporter.container; container.setEdgeDefault(EdgeDefault.UNDIRECTED); String firstDelimiter; String secondDelimiter; firstDelimiter = Utils.getCharacter(MyFileImporter.firstConnectorDelimiter); secondDelimiter = Utils.getCharacter(MyFileImporter.secondConnectorDelimiter); boolean oneTypeOfAgent = MyFileImporter.getFirstConnectedAgent().equals(MyFileImporter.getSecondConnectedAgent()); nbColumnFirstAgent = MyFileImporter.firstConnectedAgentIndex; nbColumnSecondAgent = MyFileImporter.secondConnectedAgentIndex; Integer lineCounter = 0; InputStream inp; inp = new FileInputStream(fileName); Workbook wb = WorkbookFactory.create(inp); Row row; Sheet sheet = wb.getSheet(sheetName); int startingRow; if (MyFileImporter.headersPresent) { startingRow = 1; } else { startingRow = 0; } Set<String> linesFirstAgent = new HashSet(); Set<String> linesSecondAgent = new HashSet(); for (int i = startingRow; i <= sheet.getLastRowNum(); i++) { row = sheet.getRow(i); if (row == null) { break; } Cell cell = row.getCell(nbColumnFirstAgent); if (cell == null) { Issue issue = new Issue( "problem with line " + lineCounter + " (empty column " + MyFileImporter.getFirstConnectedAgent() + "). It was skipped in the conversion", Issue.Level.WARNING); MyFileImporter.getStaticReport().logIssue(issue); continue; } String firstAgent = row.getCell(nbColumnFirstAgent).getStringCellValue(); if (firstAgent == null || firstAgent.isEmpty()) { Issue issue = new Issue( "problem with line " + lineCounter + " (empty column " + MyFileImporter.getFirstConnectedAgent() + "). It was skipped in the conversion", Issue.Level.WARNING); MyFileImporter.getStaticReport().logIssue(issue); continue; } if (MyFileImporter.removeDuplicates) { boolean newLine = linesFirstAgent.add(firstAgent); if (!newLine) { continue; } } String secondAgent = null; if (!oneTypeOfAgent) { cell = row.getCell(nbColumnSecondAgent); if (cell == null) { Issue issue = new Issue( "problem with line " + lineCounter + " (empty column " + MyFileImporter.getFirstConnectedAgent() + "). It was skipped in the conversion", Issue.Level.WARNING); MyFileImporter.getStaticReport().logIssue(issue); continue; } secondAgent = row.getCell(nbColumnSecondAgent).getStringCellValue(); if (secondAgent == null || secondAgent.isEmpty()) { Issue issue = new Issue( "problem with line " + lineCounter + " (empty column " + MyFileImporter.getSecondConnectedAgent() + "). It was skipped in the conversion", Issue.Level.WARNING); MyFileImporter.getStaticReport().logIssue(issue); continue; } if (MyFileImporter.removeDuplicates) { boolean newLine = linesFirstAgent.add(firstAgent); if (!newLine) { continue; } } } lineCounter++; String[] firstAgentSplit; String[] secondAgentSplit; if (firstDelimiter != null) { firstAgentSplit = firstAgent.trim().split(firstDelimiter); } else { firstAgentSplit = new String[1]; firstAgentSplit[0] = firstAgent; } for (String node : firstAgentSplit) { nodesFirst.add(node.trim()); } if (!oneTypeOfAgent) { if (secondDelimiter != null) { secondAgentSplit = secondAgent.trim().split(secondDelimiter); } else { secondAgentSplit = new String[1]; secondAgentSplit[0] = secondAgent; } for (String node : secondAgentSplit) { nodesSecond.add(node.trim()); } } else { secondAgentSplit = null; } String[] both = ArrayUtils.addAll(firstAgentSplit, secondAgentSplit); // let's find all connections between all the tags for this picture Utils usefulTools = new Utils(); List<String> connections = usefulTools.getListOfLinks(both, MyFileImporter.removeSelfLoops); edges.addAll(connections); } NodeDraft node; AttributeTable atNodes = container.getAttributeModel().getNodeTable(); AttributeColumn acFrequency = atNodes.addColumn("frequency", AttributeType.INT); AttributeColumn acType = atNodes.addColumn("type", AttributeType.STRING); for (String n : nodesFirst.elementSet()) { node = container.factory().newNodeDraft(); node.setId(n); node.setLabel(n); node.addAttributeValue(acFrequency, nodesFirst.count(n)); node.addAttributeValue(acType, MyFileImporter.getFirstConnectedAgent()); container.addNode(node); } for (String n : nodesSecond.elementSet()) { node = container.factory().newNodeDraft(); node.setId(n); node.setLabel(n); node.addAttributeValue(acFrequency, nodesSecond.count(n)); node.addAttributeValue(acType, MyFileImporter.getSecondConnectedAgent()); container.addNode(node); } // loop for edges Integer idEdge = 0; EdgeDraft edge; for (String e : edges.elementSet()) { System.out.println("edge: " + e); String sourceNode = e.split("\\|")[0]; String targetNode = e.split("\\|")[1]; if (!MyFileImporter.innerLinksIncluded) { if ((nodesFirst.contains(sourceNode) & nodesFirst.contains(targetNode)) || (nodesSecond.contains(sourceNode) & nodesSecond.contains(targetNode))) { continue; } } edge = container.factory().newEdgeDraft(); idEdge = idEdge + 1; edge.setSource(container.getNode(sourceNode)); edge.setTarget(container.getNode(targetNode)); edge.setWeight((float) edges.count(e)); edge.setId(String.valueOf(idEdge)); edge.setType(EdgeDraft.EdgeType.UNDIRECTED); container.addEdge(edge); } }
protected void process(ContainerUnloader container, Workspace workspace) { // Architecture GraphController graphController = Lookup.getDefault().lookup(GraphController.class); graphModel = graphController.getGraphModel(workspace); // Get graph Graph graph = graphModel.getGraph(); GraphFactory factory = graphModel.factory(); // Time Format & Time zone graphModel.setTimeFormat(container.getTimeFormat()); graphModel.setTimeZone(container.getTimeZone()); // Progress Progress.start(progressTicket, container.getNodeCount() + container.getEdgeCount()); // Attributes - Creates columns for properties flushColumns(container); // Counters int addedNodes = 0, addedEdges = 0; // Create all nodes ElementIdType elementIdType = container.getElementIdType(); for (NodeDraft draftNode : container.getNodes()) { String idString = draftNode.getId(); Object id = toElementId(elementIdType, idString); Node node = graph.getNode(id); if (node == null) { node = factory.newNode(id); addedNodes++; } flushToNode(draftNode, node); graph.addNode(node); Progress.progress(progressTicket); } // Create all edges and push to data structure for (EdgeDraft draftEdge : container.getEdges()) { String idString = draftEdge.getId(); Object id = toElementId(elementIdType, idString); String sourceId = draftEdge.getSource().getId(); String targetId = draftEdge.getTarget().getId(); Node source = graph.getNode(toElementId(elementIdType, sourceId)); Node target = graph.getNode(toElementId(elementIdType, targetId)); Object type = draftEdge.getType(); int edgeType = graphModel.addEdgeType(type); Edge edge = graph.getEdge(source, target, edgeType); if (edge == null) { switch (container.getEdgeDefault()) { case DIRECTED: edge = factory.newEdge(id, source, target, edgeType, draftEdge.getWeight(), true); break; case UNDIRECTED: edge = factory.newEdge(id, source, target, edgeType, draftEdge.getWeight(), false); break; case MIXED: boolean directed = draftEdge.getDirection() == null || !draftEdge.getDirection().equals(EdgeDirection.UNDIRECTED); edge = factory.newEdge(id, source, target, edgeType, draftEdge.getWeight(), directed); break; } addedEdges++; } flushToEdge(draftEdge, edge); graph.addEdge(edge); Progress.progress(progressTicket); } // Report int touchedNodes = container.getNodeCount(); int touchedEdges = container.getEdgeCount(); if (touchedNodes != addedNodes || touchedEdges != addedEdges) { Logger.getLogger(getClass().getSimpleName()) .log( Level.INFO, "# Nodes loaded: {0} ({1} added)", new Object[] {touchedNodes, addedNodes}); Logger.getLogger(getClass().getSimpleName()) .log( Level.INFO, "# Edges loaded: {0} ({1} added)", new Object[] {touchedEdges, addedEdges}); } else { Logger.getLogger(getClass().getSimpleName()) .log(Level.INFO, "# Nodes loaded: {0}", new Object[] {touchedNodes}); Logger.getLogger(getClass().getSimpleName()) .log(Level.INFO, "# Edges loaded: {0}", new Object[] {touchedEdges}); } Progress.finish(progressTicket); }