/** * Returns all triple patterns in given SPARQL query that have the given node in subject position, i.e. the outgoing * triple patterns. * @param query The SPARQL query. * @param node the node * @return */ public Set<Triple> extractOutgoingTriplePatterns(Query query, Node node){ return extractTriplePattern(query, false).stream() .filter(t -> t.subjectMatches(node)) .collect(Collectors.toSet()); }
/** * Returns all triple patterns in given SPARQL query that have the given * node in subject position, i.e. the outgoing triple patterns. * * @param query The SPARQL query. */ public Set<Triple> extractOutgoingTriplePatterns(final Query query, final Node node) { Set<Triple> triplePatterns = extractTriplePattern(query, false); // remove triple patterns not containing triple patterns with given node // in subject position for (Iterator<Triple> iterator = triplePatterns.iterator(); iterator.hasNext();) { Triple triple = iterator.next(); if (!triple.subjectMatches(node)) { iterator.remove(); } } return triplePatterns; }
/** * Check if there is an RDF-path from source to target. * * @param source the source node * @param target the target node * @param triples the set of triples in the graph * @return whether both nodes are RDF-connected by the given set of triples, i.e. if there is an RDF-path from * source to target. */ public static boolean isRDFConnected(Node source, Node target, Set<Triple> triples) { // trivial case: node is always RDF-connected to itself if(source.equals(target)) { return true; } // other case: for (Triple t : triples) { if(t.subjectMatches(source) && (isRDFConnected(t.getPredicate(), target, triples) || isRDFConnected(t.getObject(), target, triples)) ) { return true; } } return false; }
@Override public int compare(Triple o1, Triple o2) { boolean same = o1.subjectMatches(o2.getSubject()) && o2.predicateMatches(o2.getPredicate()) && o1.getObject().isVariable() && o2.getObject().isVariable(); // && !var2OutgoingTPs.containsKey(o1.getObject()); if (same) return 0; return comp.compare(o1, o2); } });
@Override public int compare(Triple o1, Triple o2) { boolean same = o1.subjectMatches(o2.getSubject()) && o2.predicateMatches(o2.getPredicate()) && o1.getObject().isVariable() && o2.getObject().isVariable(); // && !var2IncomingTPs.containsKey(o1.getObject()); if (same) return 0; return comp.compare(o1, o2); } });
public void testElementMatches() { assertTrue( NodeCreateUtils.createTriple( "S P O" ).subjectMatches( node( "S" ) ) ); assertTrue( NodeCreateUtils.createTriple( "S P O" ).predicateMatches( node( "P" ) ) ); assertTrue( NodeCreateUtils.createTriple( "S P O" ).objectMatches( node( "O" ) ) ); /* */ assertFalse( NodeCreateUtils.createTriple( "S P O" ).subjectMatches( node( "Z" ) ) ); assertFalse( NodeCreateUtils.createTriple( "S P O" ).predicateMatches( node( "Q" ) ) ); assertFalse( NodeCreateUtils.createTriple( "S P O" ).objectMatches( node( "I" ) ) ); /* */ assertTrue( NodeCreateUtils.createTriple( "?? P O" ).subjectMatches( node( "SUB" ) ) ); assertTrue( NodeCreateUtils.createTriple( "S ?? O" ).predicateMatches( node( "PRED" ) ) ); assertTrue( NodeCreateUtils.createTriple( "S P ??" ).objectMatches( node( "OBJ" ) ) ); }
public void testElementMatches() { assertTrue( NodeCreateUtils.createTriple( "S P O" ).subjectMatches( node( "S" ) ) ); assertTrue( NodeCreateUtils.createTriple( "S P O" ).predicateMatches( node( "P" ) ) ); assertTrue( NodeCreateUtils.createTriple( "S P O" ).objectMatches( node( "O" ) ) ); /* */ assertFalse( NodeCreateUtils.createTriple( "S P O" ).subjectMatches( node( "Z" ) ) ); assertFalse( NodeCreateUtils.createTriple( "S P O" ).predicateMatches( node( "Q" ) ) ); assertFalse( NodeCreateUtils.createTriple( "S P O" ).objectMatches( node( "I" ) ) ); /* */ assertTrue( NodeCreateUtils.createTriple( "?? P O" ).subjectMatches( node( "SUB" ) ) ); assertTrue( NodeCreateUtils.createTriple( "S ?? O" ).predicateMatches( node( "PRED" ) ) ); assertTrue( NodeCreateUtils.createTriple( "S P ??" ).objectMatches( node( "OBJ" ) ) ); }