private void writeAttributes(XMLStreamWriter xmlWriter, AttributeTable table) throws Exception { List<AttributeColumn> staticCols = new ArrayList<AttributeColumn>(); List<AttributeColumn> dynamicCols = new ArrayList<AttributeColumn>(); String attClass = table == attributeModel.getNodeTable() ? "node" : "edge"; for (AttributeColumn col : table.getColumns()) { if (!col.getOrigin().equals(AttributeOrigin.PROPERTY)) { if (exportDynamic && col.getType().isDynamicType()) { dynamicCols.add(col); } else { staticCols.add(col); } } else if (exportDynamic && col.getType().isDynamicType() && col.getType() != AttributeType.TIME_INTERVAL && col.getOrigin().equals(AttributeOrigin.PROPERTY) && col.getIndex() == PropertiesColumn.EDGE_WEIGHT.getIndex()) { dynamicCols.add(col); } } if (!staticCols.isEmpty()) { writeAttributes(xmlWriter, staticCols.toArray(new AttributeColumn[0]), "static", attClass); } if (!dynamicCols.isEmpty()) { writeAttributes(xmlWriter, dynamicCols.toArray(new AttributeColumn[0]), "dynamic", attClass); } }
@Override public void execute(GraphModel graphModel, AttributeModel attributeModel) { // Graph graph = graphModel.getGraphVisible(); HierarchicalGraph graph = null; // get visible graph if (directed) { graph = graphModel.getHierarchicalDirectedGraphVisible(); } else { graph = graphModel.getHierarchicalUndirectedGraphVisible(); } // lock graph graph.readLock(); try { Progress.start(progressTicket, graph.getNodeCount()); // all coefficients nodeCoefficients = new double[graph.getNodeCount()]; // attribute column AttributeTable nodeTable = attributeModel.getNodeTable(); AttributeColumn clusteringColumn = nodeTable.getColumn("newClusteringCoefficient"); if (clusteringColumn == null) { clusteringColumn = nodeTable.addColumn( "newClusteringCoefficient", "Local Clustering Coefficient", AttributeType.DOUBLE, AttributeOrigin.COMPUTED, 0.0); } int i = 0; // for each node for (Node e : graph.getNodes()) { // compute coefficient double coeficient = 0.0; double denominator = (graph.getDegree(e) * (graph.getDegree(e) - 1)); if (!directed) { denominator /= 2; } double numerator = 0.0; // get neighbors as list List<Node> n2 = Arrays.asList(graph.getNeighbors(e).toArray()); List<Node> neighbors2 = new ArrayList<Node>(n2); for (Node neighbor1 : graph.getNeighbors(e)) { neighbors2.remove(neighbor1); // count edges betwwen neighbors for (Node neighbor2 : neighbors2) { if (graph.getEdge(neighbor1, neighbor2) != null || graph.getEdge(neighbor2, neighbor1) != null) { numerator++; } } } // compute coefficient if (denominator > 0) { coeficient = numerator / denominator; } else { coeficient = 0.0; } averageCoefficient += coeficient; nodeCoefficients[i] = coeficient; i++; // set attribute AttributeRow row = (AttributeRow) e.getNodeData().getAttributes(); row.setValue(clusteringColumn, coeficient); Progress.progress(progressTicket); if (cancel) { break; } } if (graph.getNodeCount() > 0) { averageCoefficient = averageCoefficient / graph.getNodeCount(); } graph.readUnlockAll(); } catch (Exception e) { e.printStackTrace(); // Unlock graph graph.readUnlockAll(); } }
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); } }