private GraphBuilder cloneGraphBuilder(GraphBuilder graphBuilder) { GraphBuilder clonedGraphBuilder = null; if (graphBuilder == null || graphBuilder.getGraph() == null) { clonedGraphBuilder = new GraphBuilder(this.ontologyManager, false); } else { clonedGraphBuilder = new GraphBuilder(this.ontologyManager, graphBuilder.getGraph(), false); } this.nodeIdFactory = clonedGraphBuilder.getNodeIdFactory(); return clonedGraphBuilder; }
if (this.steinerNodes != null) Collections.sort(this.steinerNodes); this.graphBuilder = cloneGraphBuilder(gb); // create a copy of the graph builder this.nodeIdFactory = this.graphBuilder.getNodeIdFactory(); if (steinerNodes != null) { for (Node n : steinerNodes) { if (this.graphBuilder.getIdToNodeMap().get(n.getId()) == null) { this.graphBuilder.addNodeAndUpdate(n); Node source = l.getSource(); Node target = l.getTarget(); if (!this.graphBuilder.addLink(source, target, l)) { LabeledLink existingLink = this.graphBuilder.getIdToLinkMap().get(l.getId()); if (existingLink != null) { // the link already exist, but it may not be forced by user this.graphBuilder.changeLinkStatus(existingLink, LinkStatus.ForcedByUser);
/** * returns all the new nodes that should be added to the graph due to adding the input node * @param node * @param closure: contains all the nodes that are connected to the input node * by ObjectProperty or SubClass links * @return */ private void addNodeClosure(Node node, Set<Node> newAddedNodes) { logger.debug("<enter"); if (newAddedNodes == null) newAddedNodes = new HashSet<>(); String uri = node.getUri(); if (this.uriClosure.get(uri) != null) // the closure is already computed and added to the graph. return; Set<String> uriClosure = computeUriClosure(uri); for (String c : uriClosure) { Set<Node> nodesOfSameUri = this.uriToNodesMap.get(c); if (nodesOfSameUri == null || nodesOfSameUri.isEmpty()) { // the internal node is not added to the graph before Node nn = new InternalNode(nodeIdFactory.getNodeId(c), ontologyManager.getUriLabel(c)); if (addNode(nn)) newAddedNodes.add(nn); } } logger.debug("exit>"); }
public boolean addLink(Node source, Node target, DefaultLink link, Double weight) { if (addLink(source, target, link)) { if (weight != null) changeLinkWeight(link, weight); return true; } return false; }
protected ModelLearningGraph(OntologyManager ontologyManager, boolean emptyInstance, ModelLearningGraphType type) { this.ontologyManager = ontologyManager; if (type == ModelLearningGraphType.Compact) this.graphBuilder = new GraphBuilderTopK(ontologyManager, false); else this.graphBuilder = new GraphBuilder(ontologyManager, false); this.nodeIdFactory = this.graphBuilder.getNodeIdFactory(); this.lastUpdateTime = System.currentTimeMillis(); }
for (LabeledLink l : idSortedLinks) { key = l.getSource().getUri() + l.getUri() + l.getTarget().getUri(); if (this.graphBuilder.getPatternLinks().get(key) != null) { sortedLinks.add(l); matchedLinks = this.graphBuilder.getPatternLinks().get(key); if (matchedLinks != null && !matchedLinks.isEmpty()) { Collections.sort(matchedLinks); matchedLinks = this.graphBuilder.getPatternLinks().get(key); if (matchedLinks != null && !matchedLinks.isEmpty()) { for (LabeledLink l : matchedLinks) { matchedLinks = this.graphBuilder.getPatternLinks().get(key); if (matchedLinks != null && !matchedLinks.isEmpty()) { for (LabeledLink l : matchedLinks) { if (sortedMatchedNodes == null) { sortedMatchedNodes = new LinkedList<>(); Set<Node> matchedNodes = this.graphBuilder.getUriToNodesMap().get(sourceUri); if (matchedNodes != null && !matchedNodes.isEmpty()) { sortedMatchedNodes.addAll(matchedNodes); Set<DefaultLink> outLinks = this.getGraphBuilder().getGraph().outgoingEdgesOf(sortedMatchedNodes.get(0)); boolean okLink = true; if (outLinks != null) { id = this.nodeIdFactory.getNodeId(sourceUri); n1 = new InternalNode(id, new Label(sourceUri));
Set<SemanticTypeMapping> semanticTypeMatches = this.graphBuilder.getSemanticTypeMatches().get(domainUri + propertyUri); if (semanticTypeMatches != null) { for (SemanticTypeMapping stm : semanticTypeMatches) { Set<Node> nodesWithSameUriOfDomain = this.graphBuilder.getUriToNodesMap().get(domainUri); if (nodesWithSameUriOfDomain != null) { for (Node source : nodesWithSameUriOfDomain) { count = this.graphBuilder.getNodeDataPropertyCount().get(source.getId() + propertyUri); if (!this.graphBuilder.addNode(target)) continue;; addedNodes.add(target); if (!this.graphBuilder.addLink(source, target, link)) continue;;
Node domain = l.getSource(); LabeledLink incomingLink = l; Set<Node> matchedNodes = this.graphBuilder.getUriToNodesMap().get(domain.getUri()); if (matchedNodes == null || matchedNodes.isEmpty()) { logger.error("no match found for the node " + domain.getUri() + " in the graph"); Set<Node> dataPropertyColumnNodes = this.graphBuilder.getNodeDataProperties().get(graphKey); Integer graphDataPropertyCount = this.graphBuilder.getNodeDataPropertyCount().get(graphKey); if (graphDataPropertyCount == null) graphDataPropertyCount = 0; if (dataPropertyColumnNodes != null) { matches.add(cn); graphNodeDomains.put(cn, GraphUtil.getDomainLinksInDefaultGraph(this.graphBuilder.getGraph(), (ColumnNode)cn)); return null; if (this.graphBuilder.addNode(newNode)) { String linkId = LinkIdFactory.getLinkId(incomingLink.getUri(), m.getId(), newNode.getId()); DataPropertyLink link = new DataPropertyLink(linkId, new Label(incomingLink.getLabel())); this.graphBuilder.addLink(m, newNode, link, ModelingParams.PATTERN_LINK_WEIGHT); matches.add(newNode); graphNodeDomains.put(newNode, GraphUtil.getDomainLinksInDefaultGraph(this.graphBuilder.getGraph(), (ColumnNode)newNode));
for (DefaultLink link : this.graphBuilder.getGraph().edgeSet()) { linkUri = link.getUri(); if (!linkUri.equalsIgnoreCase(Uris.DEFAULT_LINK_URI)) { if (link.getTarget() instanceof InternalNode && !linkUri.equalsIgnoreCase(Uris.RDFS_SUBCLASS_URI)) { key = "domain:" + link.getSource().getLabel().getUri() + ",link:" + linkUri + ",range:" + link.getTarget().getLabel().getUri(); Integer count = this.graphBuilder.getLinkCountMap().get(key); if (count != null) this.graphBuilder.changeLinkWeight(link, ModelingParams.PATTERN_LINK_WEIGHT - ((double)count / (double)this.graphBuilder.getNumberOfModelLinks()) ); lf1 = this.graphBuilder.getMoreFrequentLinkBetweenNodes(link.getSource().getLabel().getUri(), link.getTarget().getLabel().getUri()); sourceTargetLinkFrequency.put(key1, lf1); lf2 = this.graphBuilder.getMoreFrequentLinkBetweenNodes(link.getTarget().getLabel().getUri(), link.getSource().getLabel().getUri()); sourceTargetLinkFrequency.put(key2, lf2); this.graphBuilder.getGraph().removeEdge(link); this.graphBuilder.addLink(sources.get(i), targets.get(i), newLink); this.graphBuilder.changeLinkWeight(newLink, weights.get(i));
ColumnNode columnNode = (ColumnNode)node; String columnName = getClassName(columnNode); List<LabeledLink> links = alignment.getGraphBuilder().getIncomingLinks(node.getId()); for(LabeledLink link : links) { String property = "`" + getPropertyName(link.getLabel()) + "`";
LabeledLink l = this.graphBuilder.getIdToLinkMap().get(id); if (l != null) { int numOfPatterns = l.getModelIds().size(); double newW = model.getGraph().getEdgeWeight(e); if (newW < currentW) this.graphBuilder.changeLinkWeight(l, newW); } else if (weightSystem == PatternWeightSystem.JWSPaperFormula) { if (n2 instanceof InternalNode) { this.graphBuilder.changeLinkWeight(l, ModelingParams.PATTERN_LINK_WEIGHT - ((double) (numOfPatterns + 1) / (double) (this.totalNumberOfKnownModels + 1) )); this.graphBuilder.changeLinkWeight(l, ModelingParams.PATTERN_LINK_WEIGHT); this.graphBuilder.changeLinkWeight(l, ModelingParams.PATTERN_LINK_WEIGHT); if (!this.graphBuilder.addLink(n1, n2, link, model.getGraph().getEdgeWeight(e))) continue; } else { if (!this.graphBuilder.addLink(n1, n2, link, ModelingParams.PATTERN_LINK_WEIGHT)) continue; DefaultLink[] graphLinks = this.graphBuilder.getGraph().edgeSet().toArray(new DefaultLink[0]); for (DefaultLink e : graphLinks) { source = e.getSource(); e instanceof LabeledLink) { LabeledLink l = (LabeledLink)e; Set<Node> nodesWithSourceUri = this.graphBuilder.getUriToNodesMap().get(source.getUri()); Set<Node> nodesWithTargetUri = this.graphBuilder.getUriToNodesMap().get(target.getUri());
List<LabeledLink> incomingLinks, outgoingLinks; incomingLinks = ModelLearningGraph.getInstance(ontologyManager, ModelLearningGraphType.Compact). getGraphBuilder().getIncomingLinks(nodeId); outgoingLinks = ModelLearningGraph.getInstance(ontologyManager, ModelLearningGraphType.Compact). getGraphBuilder().getOutgoingLinks(nodeId); if (incomingLinks != null) candidateLinks.addAll(incomingLinks); if (outgoingLinks != null) candidateLinks.addAll(outgoingLinks);
if (this.graphBuilder.getModelIds().contains(modelId)) { LabeledLink l = this.graphBuilder.getIdToLinkMap().get(id); if (l != null) { int numOfPatterns = l.getModelIds().size(); double newW = model.getGraph().getEdgeWeight(e); if (newW < currentW) this.graphBuilder.changeLinkWeight(l, newW); } else if (weightSystem == PatternWeightSystem.JWSPaperFormula) { if (n2 instanceof InternalNode) { this.graphBuilder.changeLinkWeight(l, ModelingParams.PATTERN_LINK_WEIGHT - ((double) (numOfPatterns + 1) / (double) (this.totalNumberOfKnownModels + 1) )); this.graphBuilder.changeLinkWeight(l, ModelingParams.PATTERN_LINK_WEIGHT); this.graphBuilder.changeLinkWeight(l, ModelingParams.PATTERN_LINK_WEIGHT); if (!this.graphBuilder.addLink(n1, n2, link, model.getGraph().getEdgeWeight(e))) continue; } else { if (!this.graphBuilder.addLink(n1, n2, link, ModelingParams.PATTERN_LINK_WEIGHT)) continue;
if (!this.graphBuilder.addNodeAndUpdate(source, addedNodes)) return null; if (!this.graphBuilder.addNode(target)) return null; addedNodes.add(target); link = new DataPropertyLink(linkId, label); if (!this.graphBuilder.addLink(source, target, link)) return null;
if (this.graphBuilder.getModelIds().contains(modelId)) { 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; String id = new RandomGUID().toString(); ColumnNode node = new ColumnNode(id, id, ((ColumnNode)target).getColumnName(), null, null); if (this.graphBuilder.addNode(node)) { n1 = node; } else continue; 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; String id = nodeIdFactory.getNodeId(lTarget.getValue()); LiteralNode node = new LiteralNode(id, lTarget.getValue(), new Label(target.getLabel()), lTarget.getLanguage(), lTarget.isUri()); if (this.graphBuilder.addNode(node)) { n2 = node; } else continue; String id = new RandomGUID().toString(); ColumnNode node = new ColumnNode(id, id, ((ColumnNode)target).getColumnName(), null, null); if (this.graphBuilder.addNode(node)) {
public GraphBuilder getGraphBuilderClone() { GraphBuilder clonedGraphBuilder = null; if (this instanceof ModelLearningGraphSparse) { clonedGraphBuilder = new GraphBuilder(this.ontologyManager, this.getGraphBuilder().getGraph(), false); } else if (this instanceof ModelLearningGraphCompact) { clonedGraphBuilder = new GraphBuilderTopK(this.ontologyManager, this.getGraphBuilder().getGraph()); } return clonedGraphBuilder; }
public void changeLinkStatus(LabeledLink link, LinkStatus newStatus) { if (link == null) return; LabeledLink graphLink = this.getIdToLinkMap().get(link.getId()); // use the graph link, not the tree link sent by the alignment (steiner tree has a copy of the graph link) if (graphLink == null) return; LinkStatus oldStatus = graphLink.getStatus(); if (newStatus == oldStatus) return; graphLink.setStatus(newStatus); link.setStatus(newStatus); this.changeLinkWeight(graphLink, computeWeight(graphLink)); Set<LabeledLink> linksWithOldStatus = this.statusToLinksMap.get(oldStatus); if (linksWithOldStatus != null) linksWithOldStatus.remove(graphLink); if (newStatus == LinkStatus.Normal) // we don't need to index normal links return; Set<LabeledLink> linksWithNewStatus = this.statusToLinksMap.get(newStatus); if (linksWithNewStatus == null) { linksWithNewStatus = new HashSet<>(); statusToLinksMap.put(newStatus, linksWithNewStatus); } linksWithNewStatus.add(graphLink); }
graph = new GraphBuilder() .createNode(3).setLabel("3").setDescription("null").add() .createNode(2).setLabel("2").setDescription("null").add() .createNode(1).setLabel("1").setDescription("Accepted").add() .createNode(0).setLabel("0").setDescription("Initial").add() // unimplemented start .createEdge(2, 1).setLabel("0").add() .createEdge(2, 2).setLabel("1").add() .createEdge(1, 1).setLabel("0").add() .createEdge(1, 3).setLabel("1").add() .createEdge(0, 1).setLabel("0").add() .createEdge(0, 2).setLabel("1").add() // unimplemented end .build();
public ObjectPropertyLink addObjectPropertyLink(Node source, Node target, Label label) { String id = LinkIdFactory.getLinkId(label.getUri(), source.getId(), target.getId()); ObjectPropertyLink link = new ObjectPropertyLink(id, label, this.graphBuilder.getOntologyManager().getObjectPropertyType(source.getLabel().getUri(), target.getLabel().getUri(), label.getUri())); if (this.graphBuilder.addLink(source, target, link)) { return link; } else if(this.graphBuilder.getIdToLinkMap().containsKey(link.getId())) { return (ObjectPropertyLink) this.graphBuilder.getIdToLinkMap().get(link.getId()); } else { return null; } }
public void changeLinkStatus(String linkId, LinkStatus newStatus) { logger.debug("changing the status of link " + linkId + " to " + newStatus.name()); LabeledLink link = this.getLinkById(linkId); if (link == null) { if (newStatus == LinkStatus.ForcedByUser) { Node source = this.getNodeById(LinkIdFactory.getLinkSourceId(linkId)); Node target = this.getNodeById(LinkIdFactory.getLinkTargetId(linkId)); String linkUri = LinkIdFactory.getLinkUri(linkId); LabeledLink newLink; if (linkUri.equalsIgnoreCase(Uris.RDFS_SUBCLASS_URI)) newLink = new SubClassLink(linkId); else newLink = new ObjectPropertyLink(linkId, this.graphBuilder.getOntologyManager().getUriLabel(linkUri), this.graphBuilder.getOntologyManager().getObjectPropertyType(source.getLabel().getUri(), target.getLabel().getUri(), linkUri)); newLink.setStatus(LinkStatus.ForcedByUser); this.graphBuilder.addLink(source, target, newLink); } } else this.graphBuilder.changeLinkStatus(link, newStatus); }