@Override
public Set<InternalNode> addModel(SemanticModel model) {
HashMap<Node, Node> visitedNodes;
Node source, target;
Node n1, n2;
// adding the patterns to the graph
if (model == null)
return null;
String modelId = model.getId();
if (this.graphBuilder.getModelIds().contains(modelId)) {
// FIXME
// we need to somehow update the graph, but I don't know how to do that yet.
// so, we rebuild the whole graph from scratch.
logger.info("the graph already includes the model and needs to be updated, we re-initialize the graph from the repository!");
initializeFromJsonRepository();
return null;
}
visitedNodes = new HashMap<Node, Node>();
for (LabeledLink e : model.getGraph().edgeSet()) {
source = e.getSource();
target = e.getTarget();
n1 = visitedNodes.get(source);
n2 = visitedNodes.get(target);
if (n1 == null) {
if (source instanceof InternalNode) {
String id = this.nodeIdFactory.getNodeId(source.getLabel().getUri());
InternalNode node = new InternalNode(id, new Label(source.getLabel()));
if (this.graphBuilder.addNode(node)) {
n1 = node;
} else continue;
}
else {
String id = new RandomGUID().toString();
ColumnNode node = new ColumnNode(id, id, ((ColumnNode)target).getColumnName(), null);
if (this.graphBuilder.addNode(node)) {
n1 = node;
} else continue;
}
visitedNodes.put(source, n1);
}
if (n2 == null) {
if (target instanceof InternalNode) {
String id = nodeIdFactory.getNodeId(target.getLabel().getUri());
InternalNode node = new InternalNode(id, new Label(target.getLabel()));
if (this.graphBuilder.addNode(node)) {
n2 = node;
} else continue;
}
else if(target instanceof LiteralNode) {
LiteralNode lTarget = (LiteralNode)target;
String id = nodeIdFactory.getNodeId(lTarget.getValue());
LiteralNode node = new LiteralNode(id, lTarget.getValue(), new Label(target.getLabel()), lTarget.isUri());
if (this.graphBuilder.addNode(node)) {
n2 = node;
} else continue;
}
else {
String id = new RandomGUID().toString();
ColumnNode node = new ColumnNode(id, id, ((ColumnNode)target).getColumnName(), null);
if (this.graphBuilder.addNode(node)) {
n2 = node;
} else continue;
}
visitedNodes.put(target, n2);
}
LabeledLink link;
String id = LinkIdFactory.getLinkId(e.getLabel().getUri(), n1.getId(), n2.getId());
if (e instanceof DataPropertyLink)
link = new DataPropertyLink(id, e.getLabel());
else if (e instanceof ObjectPropertyLink)
link = new ObjectPropertyLink(id, e.getLabel(), ((ObjectPropertyLink)e).getObjectPropertyType());
else if (e instanceof SubClassLink)
link = new SubClassLink(id);
else if (e instanceof ClassInstanceLink)
link = new ClassInstanceLink(id, e.getKeyType());
else if (e instanceof ColumnSubClassLink)
link = new ColumnSubClassLink(id);
else if (e instanceof DataPropertyOfColumnLink)
link = new DataPropertyOfColumnLink(id,
((DataPropertyOfColumnLink)e).getSpecializedColumnHNodeId(),
((DataPropertyOfColumnLink)e).getSpecializedLinkId()
);
else if (e instanceof ObjectPropertySpecializationLink)
link = new ObjectPropertySpecializationLink(id, ((ObjectPropertySpecializationLink)e).getSpecializedLinkId());
else {
logger.error("cannot instanciate a link from the type: " + e.getType().toString());
continue;
}
link.getModelIds().add(modelId);
if (this.graphBuilder.addLink(n1, n2, link)) {
this.graphBuilder.changeLinkWeight(link, ModelingParams.PATTERN_LINK_WEIGHT);
}
if (!n1.getModelIds().contains(modelId))
n1.getModelIds().add(modelId);
if (!n2.getModelIds().contains(modelId))
n2.getModelIds().add(modelId);
}