use of org.gephi.io.importer.api.ElementIdType 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(container, draftNode, node);
if (newNode) {
graph.addNode(node);
}
Progress.progress(progressTicket);
}
final EdgeMergeStrategy edgesMergeStrategy = containers[0].getEdgesMergeStrategy();
// 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);
boolean createDirected = true;
switch(container.getEdgeDefault()) {
case DIRECTED:
createDirected = true;
break;
case UNDIRECTED:
createDirected = false;
break;
case MIXED:
createDirected = draftEdge.getDirection() != EdgeDirection.UNDIRECTED;
draftEdge.setDirection(createDirected ? EdgeDirection.DIRECTED : EdgeDirection.UNDIRECTED);
break;
}
Edge edge = graph.getEdge(source, target, edgeType);
if (edge != null && edgesMergeStrategy == EdgeMergeStrategy.NO_MERGE) {
// Undirected and directed edges are incompatible, check for them or we could get an exception:
final Edge incompatibleEdge = findIncompatibleEdge(graph, source, target, createDirected, edgeType);
if (incompatibleEdge == null) {
// Force create, no merge
edge = null;
} else {
String message = NbBundle.getMessage(DefaultProcessor.class, "DefaultProcessor.error.incompatibleEdges", String.format("[%s -> %s; %s, type %s]", sourceId, targetId, createDirected ? "Directed" : "Undirected", type), String.format("[%s -> %s; %s; type: %s; id: %s]", incompatibleEdge.getSource().getId(), incompatibleEdge.getTarget().getId(), incompatibleEdge.isDirected() ? "Directed" : "Undirected", incompatibleEdge.getTypeLabel(), incompatibleEdge.getId()));
report.logIssue(new Issue(message, Issue.Level.WARNING));
Progress.progress(progressTicket);
continue;
}
}
boolean newEdge = edge == null;
if (newEdge) {
if (!graph.hasEdge(id)) {
edge = factory.newEdge(id, source, target, edgeType, draftEdge.getWeight(), createDirected);
} else {
// The id is already in use by a different edge, generate a new id:
edge = factory.newEdge(source, target, edgeType, draftEdge.getWeight(), createDirected);
}
addedEdges++;
}
flushToEdge(container, draftEdge, edge, newEdge);
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);
}
Aggregations