use of org.gephi.io.importer.api.NodeDraft in project gephi by gephi.
the class DefaultProcessor method process.
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);
boolean newNode = false;
if (node == null) {
node = factory.newNode(id);
addedNodes++;
newNode = true;
}
flushToNode(draftNode, node);
if (newNode) {
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);
boolean newEdge = false;
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++;
newEdge = true;
}
flushToEdge(draftEdge, edge);
if (newEdge) {
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);
}
use of org.gephi.io.importer.api.NodeDraft in project gephi by gephi.
the class ImporterPajek method importData.
private void importData(LineNumberReader reader) throws Exception {
//Progress
Progress.start(progressTicket);
try {
// ignore everything until we see '*Vertices'
String curLine = skip(reader, "*vertices");
if (// no vertices in the graph; return empty graph
curLine == null) {
report.logIssue(new Issue(NbBundle.getMessage(ImporterPajek.class, "importerNET_error_dataformat1"), Issue.Level.CRITICAL));
}
// create appropriate number of vertices
StringTokenizer stringTokenizer = new StringTokenizer(curLine);
// skip past "*vertices";
stringTokenizer.nextToken();
int num_vertices = Integer.parseInt(stringTokenizer.nextToken());
//Progress
Progress.switchToDeterminate(progressTicket, num_vertices);
verticesArray = new NodeDraft[num_vertices];
for (int i = 0; i < num_vertices; i++) {
String label = "" + (i + 1);
NodeDraft node = container.factory().newNodeDraft(label);
node.setLabel(label);
verticesArray[i] = node;
}
curLine = null;
while (reader.ready()) {
if (cancel) {
reader.close();
return;
}
curLine = reader.readLine();
if (curLine == null || curLine.startsWith("*")) {
break;
}
if (curLine.isEmpty()) {
// skip blank lines
report.logIssue(new Issue(NbBundle.getMessage(ImporterPajek.class, "importerNET_error_dataformat2", reader.getLineNumber()), Issue.Level.WARNING));
continue;
}
try {
readVertex(curLine, num_vertices);
} catch (IllegalArgumentException iae) {
reader.close();
throw iae;
}
Progress.progress(progressTicket);
}
//Append nodes
for (NodeDraft node : verticesArray) {
container.addNode(node);
}
//Get arcs
curLine = readArcsOrEdges(curLine, reader);
//Get edges
readArcsOrEdges(curLine, reader);
reader.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
Progress.finish(progressTicket);
}
use of org.gephi.io.importer.api.NodeDraft in project gephi by gephi.
the class ImporterTGF method addEdge.
private void addEdge(String source, String target, String label) {
NodeDraft sourceNode;
if (!container.nodeExists(source)) {
sourceNode = container.factory().newNodeDraft(source);
container.addNode(sourceNode);
} else {
sourceNode = container.getNode(source);
}
NodeDraft targetNode;
if (!container.nodeExists(target)) {
targetNode = container.factory().newNodeDraft(target);
container.addNode(targetNode);
} else {
targetNode = container.getNode(target);
}
if (!container.edgeExists(sourceNode.getId(), targetNode.getId())) {
EdgeDraft edge = container.factory().newEdgeDraft();
edge.setSource(sourceNode);
edge.setTarget(targetNode);
edge.setLabel(label);
container.addEdge(edge);
}
}
use of org.gephi.io.importer.api.NodeDraft in project gephi by gephi.
the class ImporterTGF method addNode.
private void addNode(String id, String label) {
NodeDraft node;
if (!container.nodeExists(id)) {
node = container.factory().newNodeDraft(id);
node.setLabel(label);
container.addNode(node);
}
}
use of org.gephi.io.importer.api.NodeDraft in project gephi by gephi.
the class ImporterTLP method parseEdge.
private void parseEdge(String[] tokens, int cptLine) {
if (tokens.length != 4) {
report.logIssue(new Issue(NbBundle.getMessage(ImporterGDF.class, "importerTPL_error_dataformat1", cptLine), Issue.Level.WARNING));
}
String id = tokens[1];
EdgeDraft edge = container.factory().newEdgeDraft(id);
NodeDraft source = container.getNode(tokens[2]);
NodeDraft target = container.getNode(tokens[3]);
edge.setSource(source);
edge.setTarget(target);
container.addEdge(edge);
}
Aggregations