/** * Get the root of the taxonomic graph contained in the graph associated to * the engine. An exception will be thrown if the taxonomic graph contains * multiple roots. * * @return the class corresponding to the root. * @throws slib.utils.ex.SLIB_Ex_Critic */ public synchronized URI getRoot() throws SLIB_Ex_Critic { if (root == null) { URI rooturi = new ValidatorDAG().getUniqueTaxonomicRoot(graph); root = rooturi; } return root; }
/** * * @param graph * @return true if the graph contains a taxonomic graph. * @throws SLIB_Ex_Critic */ public boolean containsTaxonomicDag(G graph) throws SLIB_Ex_Critic { WalkConstraint wct = new WalkConstraintGeneric(RDFS.SUBCLASSOF, Direction.IN); return isDag(graph, wct); }
/** * @param g * @param root * @return true if the graph contains a taxonomic graph which is rooted by a * unique vertex. * @throws SLIB_Ex_Critic */ public boolean isUniqueRootedTaxonomicDag(G g, URI root) throws SLIB_Ex_Critic { WalkConstraint wc = new WalkConstraintGeneric(RDFS.SUBCLASSOF, Direction.IN); return isUniqueRootedDagRoot(g, root, wc); }
/** * Root the underlying taxonomic DAG of the specified graph. * * @param g * @param rootUri * @return the URI of the root * @throws SLIB_Ex_Critic */ public static URI rootUnderlyingTaxonomicDAG(G g, URI rootUri) throws SLIB_Ex_Critic { Logger logger = LoggerFactory.getLogger(RooterDAG.class); logger.info("Rooting taxonomic Graph using " + rootUri); ValidatorDAG validator = new ValidatorDAG(); if (!validator.containsTaxonomicDagWithUniqueRoot(g)) { return rootUnderlyingDAG(g, rootUri, new WalkConstraintGeneric(RDFS.SUBCLASSOF,Direction.OUT),true); } else { return validator.getUniqueTaxonomicRoot(g); } } }
private void performDFS(URI v) { if (!valid) { return; } if (!verticesColors.containsKey(v)) { verticesColors.put(v, Color.ORANGE); Set<E> edges = graph.getE(v, wc); for (E e : edges) { if (!valid) { return; } URI target = e.getTarget(); if (target.equals(v)) { // IN target = e.getSource(); } if (verticesColors.get(target) != Color.RED) { currentPath.addEdge(e); lastEdge = e; performDFS(target); } } if (!valid) { return; } currentPath.removeLastEdge(); verticesColors.put(v, Color.RED); } else if (verticesColors.get(v) == Color.ORANGE) { valid = false; } }
currentPath = new Path(); performDFS(rootUri); logger.info("current path :" + currentPath.toString()); logger.info("Cycle detected adding : " + lastEdge + " to path");
/** * Return the vertices which root the taxonomic graph. * * @param g * @return the vertices which can be considered as a root, i.e. all the * vertices which are not subsumed by an other vertex through a taxonomic * relationship. */ public Set<URI> getTaxonomicRoots(G g) { return getDAGRoots(g, new WalkConstraintGeneric(RDFS.SUBCLASSOF, Direction.OUT)); }
/** * Do not check if the graph is a DAG * * @param g * @param root * @param wc * @return true if the graph is a DAG and rooted by a unique vertex. * @throws SLIB_Ex_Critic */ public boolean isUniqueRootedDagRoot(G g, URI root, WalkConstraint wc) throws SLIB_Ex_Critic { if (isDag(g, wc)) { Set<URI> roots = getDAGRoots(g, WalkConstraintUtils.getInverse(wc, false)); logger.debug("roots: " + roots); if (roots.size() == 1 && roots.iterator().next().equals(root)) { return true; } } return false; }
/** * Root the underlying taxonomic DAG of the specified graph. * * @param g * @param rootUri * @return the URI of the root * @throws SLIB_Ex_Critic */ public static URI rootUnderlyingTaxonomicDAG(G g, URI rootUri) throws SLIB_Ex_Critic { Logger logger = LoggerFactory.getLogger(RooterDAG.class); logger.info("Rooting taxonomic Graph using " + rootUri); ValidatorDAG validator = new ValidatorDAG(); if (!validator.containsTaxonomicDagWithUniqueRoot(g)) { return rootUnderlyingDAG(g, rootUri, new WalkConstraintGeneric(RDFS.SUBCLASSOF,Direction.OUT),true); } else { return validator.getUniqueTaxonomicRoot(g); } } }
private void performDFS(URI v) { if (!valid) { return; } if (!vertexColor.containsKey(v)) { vertexColor.put(v, Color.ORANGE); Set<E> edges = graph.getE(v, wc); for (E e : edges) { if (!valid) { return; } URI target = e.getTarget(); if (target.equals(v)) { // IN target = e.getSource(); } if (vertexColor.get(target) != Color.RED) { currentPath.addEdge(e); lastEdge = e; performDFS(target); } } if (!valid) { return; } currentPath.removeLastEdge(); vertexColor.put(v, Color.RED); } else if (vertexColor.get(v) == Color.ORANGE) { valid = false; } }
currentPath = new Path(); performDFS(rootUri); if (!valid) { logger.info("current path :" + currentPath.toString()); logger.info("Cycle detected adding : " + lastEdge + " to path");
/** * Return the vertices which root the taxonomic graph. * * @param g * @return the vertices which can be considered as a root, i.e. all the * vertices which are not subsumed by an other vertex through a taxonomic * relationship. */ public Set<URI> getTaxonomicRoots(G g) { return getDAGRoots(g, new WalkConstraintGeneric(RDFS.SUBCLASSOF, Direction.OUT)); }
/** * * @param graph * @return true if the graph contains a taxonomic graph. * @throws SLIB_Ex_Critic */ public boolean containsTaxonomicDag(G graph) throws SLIB_Ex_Critic { WalkConstraint wct = new WalkConstraintGeneric(RDFS.SUBCLASSOF, Direction.IN); return isDag(graph, wct); }
/** * @param g * @param root * @return true if the graph contains a taxonomic graph which is rooted by a * unique vertex. * @throws SLIB_Ex_Critic */ public boolean isUniqueRootedTaxonomicDag(G g, URI root) throws SLIB_Ex_Critic { WalkConstraint wc = new WalkConstraintGeneric(RDFS.SUBCLASSOF, Direction.IN); return isUniqueRootedDagRoot(g, root, wc); }
/** * Root the underlying taxonomic DAG of the specified graph. * * @param g * @param rootUri * @return the URI of the root * @throws SLIB_Ex_Critic */ public static URI rootUnderlyingTaxonomicDAG(G g, URI rootUri) throws SLIB_Ex_Critic { Logger logger = LoggerFactory.getLogger(RooterDAG.class); logger.info("Rooting taxonomic Graph using " + rootUri); ValidatorDAG validator = new ValidatorDAG(); if (!validator.containsTaxonomicDagWithUniqueRoot(g)) { return rootUnderlyingDAG(g, rootUri, new WalkConstraintGeneric(RDFS.SUBCLASSOF,Direction.OUT),true); } else { return validator.getUniqueTaxonomicRoot(g); } } }
/** * Test if the given URI corresponds to a root of the graph build according * to the specified edge types. * * @param g the graph to consider * @param rootURI the URI to test * @param wc the edge type to consider only OUT direction is considered * @return true if the graph defined by the specified constraint is rooted * by the given URI */ public boolean containsRootedDagRoot(G g, URI rootURI, WalkConstraint wc) { for (URI v : getDAGRoots(g, wc)) { if (v.equals(rootURI)) { return true; } } return false; }
/** * Check if the underlying graph defined by the edges of the given edge * types and build using a traversal starting from the given root node is a * DAG. shortcut of {@link ValidatorDAG#isDag(G, WalkConstraint)} only * considering the given set of edge types * * @param graph the graph on which the evaluation has to be made * @param rootURI * @param wc * @return true if the the (underlying) graph reduction is a DAG * @throws SLIB_Ex_Critic * */ public boolean isDag(G graph, URI rootURI, WalkConstraint wc) throws SLIB_Ex_Critic { return isDag(graph, SetUtils.buildSet(rootURI), wc); }
/** * Root the underlying taxonomic DAG of the specified graph. * * @param g * @param rootUri * @return the URI of the root * @throws SLIB_Ex_Critic */ public static URI rootUnderlyingTaxonomicDAG(G g, URI rootUri) throws SLIB_Ex_Critic { Logger logger = LoggerFactory.getLogger(RooterDAG.class); logger.info("Rooting taxonomic Graph using " + rootUri); ValidatorDAG validator = new ValidatorDAG(); if (!validator.containsTaxonomicDagWithUniqueRoot(g)) { return rootUnderlyingDAG(g, rootUri, new WalkConstraintGeneric(RDFS.SUBCLASSOF,Direction.OUT),true); } else { return validator.getUniqueTaxonomicRoot(g); } } }
/** * Test if the given URI corresponds to a root of the graph build according * to the specified edge types. * * @param g the graph to consider * @param rootURI the URI to test * @param wc the edge type to consider only OUT direction is considered * @return true if the graph defined by the specified constraint is rooted * by the given URI */ public boolean containsRootedDagRoot(G g, URI rootURI, WalkConstraint wc) { for (URI v : getDAGRoots(g, wc)) { if (v.equals(rootURI)) { return true; } } return false; }
/** * Check if the underlying graph defined by the edges of the given edge * types and build using a traversal starting from the given root node is a * DAG. shortcut of {@link ValidatorDAG#isDag(G, WalkConstraint)} only * considering the given set of edge types * * @param graph the graph on which the evaluation has to be made * @param rootURI * @param wc * @return true if the the (underlying) graph reduction is a DAG * @throws SLIB_Ex_Critic * */ public boolean isDag(G graph, URI rootURI, WalkConstraint wc) throws SLIB_Ex_Critic { return isDag(graph, SetUtils.buildSet(rootURI), wc); }