private static Optional<String> getValue(final Graph graph, final Node subject, final Node predicate) { final Iterator<Node> objects = listObjects(graph, subject, predicate); return Optional.ofNullable(objects.hasNext() ? objects.next().getLiteralValue().toString() : null); }
/** * Determines whether the subject is of type memento:Memento. * * @param graph the graph * @param subject the subject * @return whether the subject is a versioned node */ public boolean isVersionedNode(final Graph graph, final Node subject) { return listObjects(graph, subject, RDF.type.asNode()).toList().stream().map(Node::getURI) .anyMatch((MEMENTO_TYPE)::equals); }
/** * Get the number of child resources associated with the arg 'subject' as specified by the triple found in the arg * 'graph' with the predicate RdfLexicon.HAS_CHILD_COUNT. * * @param graph of triples * @param subject for which child resources is sought * @return number of child resources */ public int getNumChildren(final Graph graph, final Node subject) { LOGGER.trace("Getting number of children: s:{}, g:{}", subject, graph); return (int) asStream(listObjects(graph, subject, CONTAINS.asNode())).count(); }
@Override public Object readValue(Graph graph, Node subject, Node predicate) { List<Node> os = GraphUtil.listObjects(graph, subject, predicate).toList(); Object result = null; if(!os.isEmpty()) { Node o = os.iterator().next(); if(o.isURI()) { result = o.getURI(); } } return result; } }
@Override public Object readValue(Graph graph, Node subject, Node predicate) { List<Node> os = GraphUtil.listObjects(graph, subject, predicate).toList(); Object result = null; if(!os.isEmpty()) { Node o = os.iterator().next(); if(o.isURI()) { result = o.getURI(); } } return result; } }
/** * Return an iterator of Triples for versions in order that * they were created. * * @param g the graph * @param subject the subject * @param predicate the predicate * @return iterator */ public Iterator<Node> getOrderedVersions(final Graph g, final Node subject, final Resource predicate) { final List<Node> vs = listObjects(g, subject, predicate.asNode()).toList(); vs.sort(Comparator.comparing(v -> getVersionDate(g, v))); return vs.iterator(); }
@Override public Object readValue(Graph graph, Node subject, Node predicate) { List<Node> nodes = GraphUtil.listObjects(graph, subject, predicate).toList(); Object result = null; if(!nodes.isEmpty()) { Node node = nodes.iterator().next(); if(node != null) { result = node.equals(nullValue) ? null : node.getLiteralValue() ; } } return result; } }
/** * Get the canonical title of a subject from the graph * * @param graph the graph * @param sub the subject * @return canonical title of the subject in the graph */ public String getObjectTitle(final Graph graph, final Node sub) { if (sub == null) { return ""; } final Optional<String> title = TITLE_PROPERTIES.stream().map(Property::asNode).flatMap(p -> listObjects( graph, sub, p).toList().stream()).filter(Node::isLiteral).map(Node::getLiteral).map( LiteralLabel::toString).findFirst(); return title.orElse(sub.isURI() ? sub.getURI() : sub.isBlank() ? sub.getBlankNodeLabel() : sub.toString()); }
@Override public Object readValue(Graph graph, Node subject, Node predicate) { List<Node> nodes = GraphUtil.listObjects(graph, subject, predicate).toList(); Object result = null; if(!nodes.isEmpty()) { Node node = nodes.iterator().next(); if(node != null) { result = node.equals(nullValue) ? null : node.getLiteralValue() ; } } return result; } }
private NodeIterator listObjectsFor( RDFNode s, RDFNode p ) { ClosableIterator<Node> xit = GraphUtil.listObjects(graph, asNode( s ), asNode( p ) ) ; return IteratorFactory.asRDFNodeIterator( xit, this ); }
protected Set<Node> listObjects( Graph g ) { return GraphUtil.listObjects( g, Node.ANY, Node.ANY ).toSet(); }
private NodeIterator listObjectsFor( RDFNode s, RDFNode p ) { ClosableIterator<Node> xit = GraphUtil.listObjects(graph, asNode( s ), asNode( p ) ) ; return IteratorFactory.asRDFNodeIterator( xit, this ); }
protected Set<Node> listObjects( Graph g ) { return GraphUtil.listObjects( g, Node.ANY, Node.ANY ).toSet(); }
/** * Get the string version of the object that matches the given subject and * predicate * * @param graph the graph * @param subject the subject * @param predicate the predicate * @param uriAsLink the boolean value of uri as link * @return string version of the object */ public String getObjectsAsString(final Graph graph, final Node subject, final Resource predicate, final boolean uriAsLink) { LOGGER.trace("Getting Objects as String: s:{}, p:{}, g:{}", subject, predicate, graph); final Iterator<Node> iterator = listObjects(graph, subject, predicate.asNode()); if (iterator.hasNext()) { final Node obj = iterator.next(); if (obj.isLiteral()) { final String lit = obj.getLiteralValue().toString(); return lit.isEmpty() ? "<empty>" : lit; } return uriAsLink ? "<<a href=\"" + obj.getURI() + "\">" + obj.getURI() + "</a>>" : obj.getURI(); } return ""; }
public void testListObjectsNoRemove() { Graph g = getGraphWith( "a P b; b Q c; c R a" ); Iterator<Node> it = GraphUtil.listObjects( g, Node.ANY, Node.ANY ); it.next(); try { it.remove(); fail( "listObjects for " + g.getClass() + " should not support .remove()" ); } catch (UnsupportedOperationException e) { pass(); } }
public void testListObjectsNoRemove() { Graph g = getGraphWith( "a P b; b Q c; c R a" ); Iterator<Node> it = GraphUtil.listObjects( g, Node.ANY, Node.ANY ); it.next(); try { it.remove(); fail( "listObjects for " + g.getClass() + " should not support .remove()" ); } catch (UnsupportedOperationException e) { pass(); } }
protected void testObjects( Graph g, Collection<Node> exclude, Node s, Node p, Node... expected ) { List<Node> X = GraphUtil.listObjects( g, s, p ).toList(); assertSameUnordered(X, exclude, expected) ; }
protected void testObjects( Graph g, Collection<Node> exclude, Node s, Node p, Node... expected ) { List<Node> X = GraphUtil.listObjects( g, s, p ).toList(); assertSameUnordered(X, exclude, expected) ; }
public void testObjectsFor() { // First get the answer from the empty graph (not empty for an inf graph) Graph b = getGraphWith( "" ); Set<Node> B = GraphUtil.listObjects( b, Node.ANY, Node.ANY ).toSet(); Graph g = getGraphWith( "b P a; c Q a; d P a; x P b; y Q c" ); testObjects( g, B, Node.ANY, Node.ANY, node("a"), node("b"), node("c") ); testObjects( g, B, Node.ANY, node( "P" ), node("a"), node("b") ); testObjects( g, B, node( "c" ), node( "Q" ), node("a") ); testObjects( g, B, node( "y" ), node( "Q" ), node("c") ); testObjects( g, B, node( "a" ), node( "Q" )); testObjects( g, B, node( "z" ), node( "Q" )); }
public void testObjectsFor() { // First get the answer from the empty graph (not empty for an inf graph) Graph b = getGraphWith( "" ); Set<Node> B = GraphUtil.listObjects( b, Node.ANY, Node.ANY ).toSet(); Graph g = getGraphWith( "b P a; c Q a; d P a; x P b; y Q c" ); testObjects( g, B, Node.ANY, Node.ANY, node("a"), node("b"), node("c") ); testObjects( g, B, Node.ANY, node( "P" ), node("a"), node("b") ); testObjects( g, B, node( "c" ), node( "Q" ), node("a") ); testObjects( g, B, node( "y" ), node( "Q" ), node("c") ); testObjects( g, B, node( "a" ), node( "Q" )); testObjects( g, B, node( "z" ), node( "Q" )); }