}
//Architecture
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel();
HierarchicalGraph graph = null;
switch (container.getEdgeDefault()) {
case DIRECTED:
graph = graphModel.getHierarchicalDirectedGraph();
break;
case UNDIRECTED:
graph = graphModel.getHierarchicalUndirectedGraph();
break;
case MIXED:
graph = graphModel.getHierarchicalMixedGraph();
break;
default:
graph = graphModel.getHierarchicalMixedGraph();
break;
}
GraphFactory factory = graphModel.factory();
//Attributes - Manually merge models with new dynamic cols
attributeModel = Lookup.getDefault().lookup(AttributeController.class).getModel();
AttributeTable nodeTable = container.getAttributeModel().getNodeTable();
AttributeTable edgeTable = container.getAttributeModel().getEdgeTable();
for (AttributeColumn column : nodeTable.getColumns()) {
AttributeColumn existingCol = attributeModel.getNodeTable().getColumn(column.getTitle());
if (existingCol == null) {
if (!column.getOrigin().equals(AttributeOrigin.PROPERTY)) {
AttributeType dynamicType = TypeConvertor.getDynamicType(column.getType());
if (dynamicType != null && !column.getType().isDynamicType()) {
attributeModel.getNodeTable().addColumn(column.getId(), column.getTitle(), dynamicType, column.getOrigin(), null);
} else {
attributeModel.getNodeTable().addColumn(column.getId(), column.getTitle(), column.getType(), column.getOrigin(), column.getDefaultValue());
}
}
}
}
for (AttributeColumn column : edgeTable.getColumns()) {
AttributeColumn existingCol = attributeModel.getEdgeTable().getColumn(column.getTitle());
if (existingCol == null) {
if (!column.getOrigin().equals(AttributeOrigin.PROPERTY)) {
AttributeType dynamicType = TypeConvertor.getDynamicType(column.getType());
if (dynamicType != null && !column.getType().isDynamicType()) {
attributeModel.getEdgeTable().addColumn(column.getId(), column.getTitle(), dynamicType, column.getOrigin(), null);
} else {
attributeModel.getEdgeTable().addColumn(column.getId(), column.getTitle(), column.getType(), column.getOrigin(), column.getDefaultValue());
}
}
} else if (PropertiesColumn.EDGE_WEIGHT.getId().equals(column.getId()) && !existingCol.getType().isDynamicType()) {
attributeModel.getEdgeTable().replaceColumn(attributeModel.getEdgeTable().getColumn(PropertiesColumn.EDGE_WEIGHT.getIndex()), PropertiesColumn.EDGE_WEIGHT.getId(), PropertiesColumn.EDGE_WEIGHT.getTitle(), AttributeType.DYNAMIC_FLOAT, AttributeOrigin.PROPERTY, null);
}
}
//Get Time Interval Column
AttributeColumn nodeDynamicColumn = attributeModel.getNodeTable().getColumn(DynamicModel.TIMEINTERVAL_COLUMN);
AttributeColumn edgeDynamicColumn = attributeModel.getEdgeTable().getColumn(DynamicModel.TIMEINTERVAL_COLUMN);
if (nodeDynamicColumn == null) {
nodeDynamicColumn = attributeModel.getNodeTable().addColumn(DynamicModel.TIMEINTERVAL_COLUMN, "Time Interval", AttributeType.TIME_INTERVAL, AttributeOrigin.PROPERTY, null);
}
if (edgeDynamicColumn == null) {
edgeDynamicColumn = attributeModel.getEdgeTable().addColumn(DynamicModel.TIMEINTERVAL_COLUMN, "Time Interval", AttributeType.TIME_INTERVAL, AttributeOrigin.PROPERTY, null);
}
//Get Time stamp
if (dateMode) {
try {
point = DynamicUtilities.getDoubleFromXMLDateString(date);
} catch (Exception e) {
throw new RuntimeException("The entered date can't be parsed");
}
} else {
point = Double.parseDouble(date);
}
DynamicController dynamicController = Lookup.getDefault().lookup(DynamicController.class);
dynamicController.setTimeFormat(dateMode ? DynamicModel.TimeFormat.DATE : DynamicModel.TimeFormat.DOUBLE);
//Index existing graph
Map<String, Node> map = new HashMap<String, Node>();
for (Node n : graph.getNodes()) {
String id = n.getNodeData().getId();
if (id != null && !labelmatching && !id.equalsIgnoreCase(String.valueOf(n.getId()))) {
map.put(id, n);
}
if (n.getNodeData().getLabel() != null && !n.getNodeData().getLabel().isEmpty() && labelmatching) {
map.put(n.getNodeData().getLabel(), n);
}
}
//Create all nodes
Set<Node> nodesInDraft = new HashSet<Node>();
int newNodeCount = 0;
for (NodeDraftGetter draftNode : container.getNodes()) {
Node node = null;
String id = draftNode.getId();
String label = draftNode.getLabel();
if (!draftNode.isAutoId() && id != null && map.get(id) != null && !labelmatching) {
node = map.get(id);
} else if (label != null && map.get(label) != null && labelmatching) {
node = map.get(label);
}
TimeInterval timeInterval = null;
if (node == null) {
//Node is new
node = factory.newNode(draftNode.isAutoId() ? null : draftNode.getId());
flushToNode(draftNode, node);
draftNode.setNode(node);
newNodeCount++;
} else {
timeInterval = (TimeInterval) node.getNodeData().getAttributes().getValue(nodeDynamicColumn.getIndex());
flushToNodeAttributes(draftNode, node);
draftNode.setNode(node);
}
nodesInDraft.add(node);
//Add Point
node.getNodeData().getAttributes().setValue(nodeDynamicColumn.getIndex(), addPoint(timeInterval, point));
}
//Push nodes in data structure
for (NodeDraftGetter draftNode : container.getNodes()) {
Node n = draftNode.getNode();
NodeDraftGetter[] parents = draftNode.getParents();
if (parents != null) {
for (int i = 0; i < parents.length; i++) {
Node parent = parents[i].getNode();
graph.addNode(n, parent);
}
} else {
graph.addNode(n);
}
}
//Remove point from all nodes not in draft
for (Node node : graph.getNodes()) {
if (!nodesInDraft.contains(node)) {
TimeInterval timeInterval = (TimeInterval) node.getNodeData().getAttributes().getValue(nodeDynamicColumn.getIndex());
node.getNodeData().getAttributes().setValue(nodeDynamicColumn.getIndex(), removePoint(timeInterval, point));
}
}
//Create all edges and push to data structure
Set<Edge> edgesInDraft = new HashSet<Edge>();
int newEdgeCount = 0;
for (EdgeDraftGetter draftEdge : container.getEdges()) {
Node source = draftEdge.getSource().getNode();
Node target = draftEdge.getTarget().getNode();
Edge edge = graph.getEdge(source, target);
TimeInterval timeInterval = null;
if (edge == null) {
//Edge is new
switch (container.getEdgeDefault()) {
case DIRECTED:
edge = factory.newEdge(draftEdge.isAutoId() ? null : draftEdge.getId(), source, target, draftEdge.getWeight(), true);
break;
case UNDIRECTED:
edge = factory.newEdge(draftEdge.isAutoId() ? null : draftEdge.getId(), source, target, draftEdge.getWeight(), false);
break;
case MIXED:
edge = factory.newEdge(draftEdge.isAutoId() ? null : draftEdge.getId(), source, target, draftEdge.getWeight(), draftEdge.getType().equals(EdgeType.UNDIRECTED) ? false : true);
break;
}
newEdgeCount++;
graph.addEdge(edge);
flushToEdge(draftEdge, edge);
} else {
timeInterval = (TimeInterval) edge.getEdgeData().getAttributes().getValue(edgeDynamicColumn.getIndex());
flushToEdgeAttributes(draftEdge, edge);
}
edgesInDraft.add(edge);
//Add Point
edge.getEdgeData().getAttributes().setValue(edgeDynamicColumn.getIndex(), addPoint(timeInterval, point));
}
//Remove point from all edges not in draft
for (Edge edge : graph.getEdges()) {
if (!edgesInDraft.contains(edge)) {
TimeInterval timeInterval = (TimeInterval) edge.getEdgeData().getAttributes().getValue(edgeDynamicColumn.getIndex());
edge.getEdgeData().getAttributes().setValue(edgeDynamicColumn.getIndex(), removePoint(timeInterval, point));
}
}