private static List<Resource> equivalentTypes(Resource type) { List<Resource> types = new ArrayList<Resource>(); types.add(type); for (StmtIterator it = type.getModel().listStatements(ANY, OWL.equivalentClass, type); it.hasNext(); ) types.add(it.nextStatement().getSubject()); return types; }
/** * Answer the shortest path from the <code>start</code> resource to the <code>end</code> RDF node, * such that every step on the path is accepted by the given filter. A path is a {@link List} of * RDF {@link Statement}s. The subject of the first statement in the list is <code>start</code>, * and the object of the last statement in the list is <code>end</code>. * * <p>The <code>onPath</code> argument is a {@link Filter}, which accepts a statement and returns * true if the statement should be considered to be on the path. To search for an unconstrained * path, pass {@link Filter#any} as an argument. To search for a path whose predicates match a * fixed restricted set of property names, pass an instance of {@link PredicatesFilter}. * * <p>If there is more than one path of minimal length from <code>start</code> to <code>end</code> * , this method returns an arbitrary one. The algorithm is blind breadth-first search, with loop * detection. * * @param m The model in which we are seeking a path * @param start The starting resource * @param end The end, or goal, node * @param onPath A filter which determines whether a given statement can be considered part of the * path * @return A path, consisting of a list of statements whose first subject is <code>start</code>, * and whose last object is <code>end</code>, or null if no such path exists. */ public static Path findShortestPath( Model m, Resource start, RDFNode end, Filter<Statement> onPath) { List<Path> bfs = new LinkedList<Path>(); Set<Resource> seen = new HashSet<Resource>(); // initialise the paths for (Iterator<Statement> i = m.listStatements(start, null, (RDFNode) null).filterKeep(onPath); i.hasNext(); ) { bfs.add(new Path().append(i.next())); } // search Path solution = null; while (solution == null && !bfs.isEmpty()) { Path candidate = bfs.remove(0); if (candidate.hasTerminus(end)) { solution = candidate; } else { Resource terminus = candidate.getTerminalResource(); if (terminus != null) { seen.add(terminus); // breadth-first expansion for (Iterator<Statement> i = terminus.listProperties().filterKeep(onPath); i.hasNext(); ) { Statement link = i.next(); // no looping allowed, so we skip this link if it takes us to a node we've seen if (!seen.contains(link.getObject())) { bfs.add(candidate.append(link)); } } } } } return solution; }
public Collection<URI> getSupportedFacets(URI needUri) throws NoSuchNeedException { List<URI> ret = new LinkedList<URI>(); Need need = DataAccessUtils.loadNeed(needRepository, needUri); Model content = rdfStorageService.loadContent(need); if (content == null) return ret; Resource baseRes = content.getResource(content.getNsPrefixURI("")); StmtIterator stmtIterator = baseRes.listProperties(WON.HAS_FACET); while (stmtIterator.hasNext()) { RDFNode object = stmtIterator.nextStatement().getObject(); if (object.isURIResource()) { ret.add(URI.create(object.toString())); } } return ret; }
private static ResultSetRewindable unique(ResultSetRewindable results) { // VERY crude. Utilises the fact that bindings have value equality. List<Binding> x = new ArrayList<Binding>(); Set<Binding> seen = new HashSet<Binding>(); for (; results.hasNext(); ) { Binding b = results.nextBinding(); if (seen.contains(b)) continue; seen.add(b); x.add(b); } QueryIterator qIter = new QueryIterPlainWrapper(x.iterator()); ResultSet rs = new ResultSetStream(results.getResultVars(), ModelFactory.createDefaultModel(), qIter); return ResultSetFactory.makeRewindable(rs); }
private ResultSetRewindable convertToStrings(ResultSetRewindable resultsActual) { List<Binding> bindings = new ArrayList<Binding>(); while (resultsActual.hasNext()) { Binding b = resultsActual.nextBinding(); BindingMap b2 = BindingFactory.create(); for (String vn : resultsActual.getResultVars()) { Var v = Var.alloc(vn); Node n = b.get(v); String s; if (n == null) s = ""; else if (n.isBlank()) s = "_:" + n.getBlankNodeLabel(); else s = NodeFunctions.str(n); b2.add(v, NodeFactory.createLiteral(s)); } bindings.add(b2); } ResultSet rs = new ResultSetStream( resultsActual.getResultVars(), null, new QueryIterPlainWrapper(bindings.iterator())); return ResultSetFactory.makeRewindable(rs); }