org.apache.jena.sparql.core.TriplePath Java Examples
The following examples show how to use
org.apache.jena.sparql.core.TriplePath.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConjunctiveQuery.java From quetzal with Eclipse Public License 2.0 | 6 votes |
public void setQueryPattern(Element graphPattern) { if (graphPattern instanceof ElementTriplesBlock) { query.setQueryPattern((ElementTriplesBlock)graphPattern); } else if (graphPattern instanceof ElementGroup) { ElementGroup group = (ElementGroup) graphPattern; for (Element e: flatten(group)) { if (e instanceof ElementPathBlock) { ElementPathBlock epb = (ElementPathBlock) e; for (TriplePath p:epb.getPattern().getList()) { if (!p.isTriple()) { throw new IllegalArgumentException("Conjunctive query only accepts a basic graph patterns. No triple path: "+ p+"\n"+graphPattern ); } } } else if (!(e instanceof ElementTriplesBlock) && !(e instanceof ElementFilter)) { throw new IllegalArgumentException("Conjunctive query only accepts a basic graph patterns: "+ e+"\n"+graphPattern ); } } query.setQueryPattern(graphPattern); } else { throw new IllegalArgumentException("Conjunctive query only accepts a basic graph patterns: "+ graphPattern); } }
Example #2
Source File: PropertyPathRewrite.java From quetzal with Eclipse Public License 2.0 | 6 votes |
public Element transform(TriplePath tp,boolean bestEffort, NewVariableGenerator vargen, boolean[] resultHasPropertyPaths) { Triple t = tp.asTriple(); if (t!=null) { // this is a simple triple ElementTriplesBlock ret =new ElementTriplesBlock(); ret.addTriple(t); return ret; } assert !tp.isTriple(); assert tp.getPredicate()==null: tp; JenaPathRewrite pr = new JenaPathRewrite(tp.getSubject(), tp.getObject(), bestEffort, vargen); tp.getPath().visit(pr); if (resultHasPropertyPaths!=null) { resultHasPropertyPaths[0] = pr.resultWithPropertyPaths(); } return pr.getResult(); }
Example #3
Source File: RuleSystemToUnionQuery.java From quetzal with Eclipse Public License 2.0 | 6 votes |
public TriplePath copySubstitute(TriplePath triple) { Node newSubj = replace(triple.getSubject()); Node newObj = replace(triple.getObject()); Path path = triple.getPath(); TriplePath newTriple; if (path!=null) { newTriple = new TriplePath(newSubj, path, newObj); } else { Node pred = replace(triple.getPredicate()); assert pred!=null : triple; //assert pred.isURI() : "predicate must be an IRI: "+ triple; newTriple = new TriplePath(new Triple(newSubj, pred, newObj)); } return newTriple; }
Example #4
Source File: OCUtils.java From quetzal with Eclipse Public License 2.0 | 5 votes |
public static ElementTriplesBlock toTriples(ElementPathBlock pb) { ElementTriplesBlock ret = new ElementTriplesBlock(); for (TriplePath tp :pb.getPattern().getList()) { if (tp.isTriple()) { ret.addTriple(tp.asTriple()); } else { throw new RuntimeException("Path query not supported"); } } return ret; }
Example #5
Source File: ElementTransformSPARQLStarTest.java From RDFstarTools with Apache License 2.0 | 5 votes |
protected void checkForEquivalence( ElementPathBlock expectedEPB, ElementPathBlock testEPB ) { final IsomorphismMap isoMap = new IsomorphismMap(); final Iterator<TriplePath> eit = expectedEPB.getPattern().iterator(); while ( eit.hasNext() ) { final Triple expcTP = eit.next().asTriple(); boolean found = false; final Iterator<TriplePath> rit = testEPB.getPattern().iterator(); while ( rit.hasNext() ) { final Triple testTP = rit.next().asTriple(); if ( isoMap.canBeIsomorphic(expcTP.getSubject(), testTP.getSubject()) && isoMap.canBeIsomorphic(expcTP.getPredicate(), testTP.getPredicate()) && isoMap.canBeIsomorphic(expcTP.getObject(), testTP.getObject()) ) found = true; } if ( ! found ) System.err.println( "Expected triple pattern not found (" + expcTP.toString() + ")" ); assertTrue(found); } }
Example #6
Source File: PropertyPathRewrite.java From quetzal with Eclipse Public License 2.0 | 5 votes |
@Override public void visit(P_ZeroOrMoreN p) { if (bestEffort) { ElementPathBlock e = new ElementPathBlock(); e.addTriple(new TriplePath(subject, p, object)); result = e; resultHasPropertyPaths = true; } else { result = null; } }
Example #7
Source File: PropertyPathRewrite.java From quetzal with Eclipse Public License 2.0 | 5 votes |
@Override public void visit(P_ZeroOrMore1 p) { if (bestEffort) { ElementPathBlock e = new ElementPathBlock(); e.addTriple(new TriplePath(subject, p, object)); result = e; resultHasPropertyPaths = true; } else { result = null; } }
Example #8
Source File: PropertyPathRewrite.java From quetzal with Eclipse Public License 2.0 | 5 votes |
@Override public void visit(P_OneOrMoreN p) { if (bestEffort) { ElementPathBlock e = new ElementPathBlock(); e.addTriple(new TriplePath(subject, p, object)); result = e; resultHasPropertyPaths = true; } else { result = null; } }
Example #9
Source File: PropertyPathRewrite.java From quetzal with Eclipse Public License 2.0 | 5 votes |
@Override public void visit(P_OneOrMore1 p) { if (bestEffort) { ElementPathBlock e = new ElementPathBlock(); e.addTriple(new TriplePath(subject, p, object)); result = e; resultHasPropertyPaths = true; } else { result = null; } }
Example #10
Source File: PropertyPathRewrite.java From quetzal with Eclipse Public License 2.0 | 5 votes |
@Override public void visit(P_Inverse p) { TriplePath newtp = new TriplePath(object, p.getSubPath(), subject); boolean[] resHasPropertyPaths = new boolean[]{false}; result = transform(newtp, bestEffort, vargen, resHasPropertyPaths); if (!resultHasPropertyPaths) { resultHasPropertyPaths = resHasPropertyPaths[0]; } }
Example #11
Source File: PropertyPathToRules.java From quetzal with Eclipse Public License 2.0 | 5 votes |
public RuleSystem toRules(TriplePath tp, boolean rightRecursion, String triplePredicateName, String negatedPropertySetPredName) { String predPrefix = "P"; Set<String> predNames = HashSetFactory.make(); predNames.add(triplePredicateName); predNames.add(negatedPropertySetPredName); int startSuffix = OCUtils.nextAvailableSuffixVariable(predNames, predPrefix)+1; NewVariableGenerator predgen = new NewVariableGenerator(predPrefix, startSuffix); PathToRules visitor = new PathToRules(predgen, triplePredicateName, negatedPropertySetPredName, rightRecursion);; tp.getPath().visit(visitor); return visitor.getResult(); }
Example #12
Source File: FindAllVariables.java From quetzal with Eclipse Public License 2.0 | 5 votes |
protected void addVars(TriplePath t) { addVar(t.getSubject()); if (t.getPredicate()!=null) { addVar(t.getPredicate()); } addVar(t.getObject()); }
Example #13
Source File: ParserSPARQLStarTest.java From RDFstarTools with Apache License 2.0 | 5 votes |
@Test public void parseNestedTPsSameSubject2() { final String queryString = "SELECT * WHERE { <<?s ?p ?o>> ?p2 ?o2 , ?o3 }"; final ElementPathBlock epb = getElementPathBlock(queryString); assertEquals( 2, epb.getPattern().size() ); assertTrue( epb.getPattern().get(0) instanceof TriplePath ); assertTrue( epb.getPattern().get(1) instanceof TriplePath ); final TriplePath tp1 = epb.getPattern().get(0); assertTrue( tp1.isTriple() ); final Triple t1 = tp1.asTriple(); final TriplePath tp2 = epb.getPattern().get(1); assertTrue( tp2.isTriple() ); final Triple t2 = tp2.asTriple(); assertTrue( t1.getSubject() instanceof Node_Triple ); assertFalse( t1.getPredicate() instanceof Node_Triple ); assertFalse( t1.getObject() instanceof Node_Triple ); assertTrue( t2.getSubject() instanceof Node_Triple ); assertFalse( t2.getPredicate() instanceof Node_Triple ); assertFalse( t2.getObject() instanceof Node_Triple ); assertTrue( t1.getSubject().equals(t2.getSubject()) ); assertTrue( t1.getPredicate().equals(t2.getPredicate()) ); assertFalse( t1.getObject().equals(t2.getObject()) ); }
Example #14
Source File: ElementTransformSPARQLStar.java From RDFstarTools with Apache License 2.0 | 5 votes |
@Override public Element transform( ElementPathBlock el ) { final ElementPathBlock epb = new ElementPathBlock(); for ( TriplePath tp : el.getPattern() ) { unNestTriplePattern(tp.asTriple(), epb, false); } return epb; }
Example #15
Source File: ParserSPARQLStarTest.java From RDFstarTools with Apache License 2.0 | 5 votes |
@Test public void parseNestedTPsSameSubject1() { final String queryString = "SELECT * WHERE { <<?s ?p ?o>> ?p2 ?o2 ; ?p3 ?o3 }"; final ElementPathBlock epb = getElementPathBlock(queryString); assertEquals( 2, epb.getPattern().size() ); assertTrue( epb.getPattern().get(0) instanceof TriplePath ); assertTrue( epb.getPattern().get(1) instanceof TriplePath ); final TriplePath tp1 = epb.getPattern().get(0); assertTrue( tp1.isTriple() ); final Triple t1 = tp1.asTriple(); final TriplePath tp2 = epb.getPattern().get(1); assertTrue( tp2.isTriple() ); final Triple t2 = tp2.asTriple(); assertTrue( t1.getSubject() instanceof Node_Triple ); assertFalse( t1.getPredicate() instanceof Node_Triple ); assertFalse( t1.getObject() instanceof Node_Triple ); assertTrue( t2.getSubject() instanceof Node_Triple ); assertFalse( t2.getPredicate() instanceof Node_Triple ); assertFalse( t2.getObject() instanceof Node_Triple ); assertTrue( t1.getSubject().equals(t2.getSubject()) ); assertFalse( t1.getPredicate().equals(t2.getPredicate()) ); assertFalse( t1.getObject().equals(t2.getObject()) ); }
Example #16
Source File: RuleSystemToUnionQuery.java From quetzal with Eclipse Public License 2.0 | 5 votes |
@Override public void visit(ElementPathBlock pb) { ElementPathBlock ret = new ElementPathBlock(); for (Iterator<TriplePath> it= pb.patternElts();it.hasNext(); ) { TriplePath tp = it.next(); ret.addTriplePath(copySubstitute(tp)); } result = ret; }
Example #17
Source File: TriplePatternExtractor.java From NLIWOD with GNU Affero General Public License v3.0 | 5 votes |
@Override public void visit(final ElementPathBlock el) { for (Iterator<TriplePath> iterator = el.patternElts(); iterator.hasNext();) { TriplePath tp = iterator.next(); if (inOptionalClause) { optionalTriplePattern.add(tp.asTriple()); } else { if (tp.isTriple()) { triplePattern.add(tp.asTriple()); } } } }
Example #18
Source File: ParserSPARQLStarTest.java From RDFstarTools with Apache License 2.0 | 5 votes |
protected Triple getTriplePattern( String queryString ) { final ElementPathBlock epb = getElementPathBlock(queryString); assertEquals( 1, epb.getPattern().size() ); assertTrue( "unexpected type (" + epb.getPattern().get(0).getClass() + ")", epb.getPattern().get(0) instanceof TriplePath ); final TriplePath tp = epb.getPattern().get(0); assertTrue( tp.isTriple() ); return tp.asTriple(); }
Example #19
Source File: ParserSPARQLStarTest.java From RDFstarTools with Apache License 2.0 | 5 votes |
@Test public void parseNestedTPsOneOfTwoObjects2() { final String queryString = "SELECT * WHERE { ?s1 ?p1 ?o1 , <<?s ?p ?o>> }"; final ElementPathBlock epb = getElementPathBlock(queryString); assertEquals( 2, epb.getPattern().size() ); assertTrue( epb.getPattern().get(0) instanceof TriplePath ); assertTrue( epb.getPattern().get(1) instanceof TriplePath ); final TriplePath tp1 = epb.getPattern().get(0); assertTrue( tp1.isTriple() ); final Triple t1 = tp1.asTriple(); final TriplePath tp2 = epb.getPattern().get(1); assertTrue( tp2.isTriple() ); final Triple t2 = tp2.asTriple(); assertFalse( t1.getSubject() instanceof Node_Triple ); assertFalse( t1.getPredicate() instanceof Node_Triple ); assertFalse( t1.getObject() instanceof Node_Triple ); assertFalse( t2.getSubject() instanceof Node_Triple ); assertFalse( t2.getPredicate() instanceof Node_Triple ); assertTrue( t2.getObject() instanceof Node_Triple ); assertTrue( t1.getSubject().equals(t2.getSubject()) ); assertTrue( t1.getPredicate().equals(t2.getPredicate()) ); assertFalse( t1.getObject().equals(t2.getObject()) ); }
Example #20
Source File: ParserSPARQLStarTest.java From RDFstarTools with Apache License 2.0 | 5 votes |
@Test public void parseNestedTPsOneOfTwoObjects1() { final String queryString = "SELECT * WHERE { ?s1 ?p1 ?o1 ; ?p2 <<?s ?p ?o>> }"; final ElementPathBlock epb = getElementPathBlock(queryString); assertEquals( 2, epb.getPattern().size() ); assertTrue( epb.getPattern().get(0) instanceof TriplePath ); assertTrue( epb.getPattern().get(1) instanceof TriplePath ); final TriplePath tp1 = epb.getPattern().get(0); assertTrue( tp1.isTriple() ); final Triple t1 = tp1.asTriple(); final TriplePath tp2 = epb.getPattern().get(1); assertTrue( tp2.isTriple() ); final Triple t2 = tp2.asTriple(); assertFalse( t1.getSubject() instanceof Node_Triple ); assertFalse( t1.getPredicate() instanceof Node_Triple ); assertFalse( t1.getObject() instanceof Node_Triple ); assertFalse( t2.getSubject() instanceof Node_Triple ); assertFalse( t2.getPredicate() instanceof Node_Triple ); assertTrue( t2.getObject() instanceof Node_Triple ); assertTrue( t1.getSubject().equals(t2.getSubject()) ); assertFalse( t1.getPredicate().equals(t2.getPredicate()) ); assertFalse( t1.getObject().equals(t2.getObject()) ); }
Example #21
Source File: QueryPatternSimplification.java From quetzal with Eclipse Public License 2.0 | 4 votes |
protected void add(ElementPathBlock targetPathBlock, ElementPathBlock newPaths) { for (TriplePath tp:newPaths.getPattern().getList()) { targetPathBlock.addTriple(tp); } }
Example #22
Source File: FindAllVariables.java From quetzal with Eclipse Public License 2.0 | 4 votes |
@Override public void visit(ElementPathBlock e) { for (TriplePath t:e.getPattern().getList()) { addVars(t); } }
Example #23
Source File: PropertyPathToRules.java From quetzal with Eclipse Public License 2.0 | 4 votes |
public static void main(String[] args) throws Exception{ PropertyPathToRules toRules = new PropertyPathToRules(); String q = "select * where { " + "?x (^<http://example.org/p>|<http://example.org/s>?)/(<http://example.org/q>| <http://example.org/r>)+/!(^<http://example.org/t>| ^<http://example.org/u>|<http://example.org/v> )?y ." + "?x <http://example.org/s>/<http://example.org/r>/^<http://example.org/t> ?y ." + "?x <http://example.org/s>/<http://example.org/r>+/^<http://example.org/t> ?y ." + "?x (<http://example.org/s>/<http://example.org/r>+/^<http://example.org/t>)+ ?y ." + "?x (<http://example.org/s>/(^(<http://example.org/r>+|<http://example.org/p>))+/(^<http://example.org/t>)+)+ ?y ." + "?x <http://example.org/s>/<http://example.org/r>/<http://example.org/t> ?y ." + "?x !(a|<http://example.org/s>|<http://example.org/r>| <http://example.org/t> ) ?y ." + "?x !(<http://example.org/r>|^<http://example.org/t>| ^a|<http://example.org/s> ) ?y ." + "}"; // ( iRIref | 'a' ) com.ibm.research.rdf.store.sparql11.model.Query db2rdfQuery = SparqlParserUtilities.parseSparqlString(q); System.out.println("Query:\n"+db2rdfQuery.toString()); //System.exit(0); Query query = QueryFactory.create(q, Syntax.syntaxSPARQL_11); System.out.println("Parsed Query:\n\t"+query); ElementGroup group = (ElementGroup) query.getQueryPattern(); ElementPathBlock p = (ElementPathBlock) group.getElements().get(0); for (TriplePath path : p.getPattern().getList()) { System.out.println("Path: "+path); RuleSystem rs = toRules.toRules(path, true, "Triple", "NegatedPropertySetTriple"); System.out.println("Equivalent Rule System (Main formula: "+rs.getMainHeadFormula()+"):\n"+rs); rs = rs.simplify(Collections.singleton(rs.getMainHeadFormula().getPredicate())); System.out.println("Equivalent Rule System after simplification (Main formula: "+rs.getMainHeadFormula()+"):\n" +rs); /*Graph<Predicate> graph = DatalogEngine.buildDependencyGraph(rs); LinkedList<Set<Predicate>> sccs =DatalogEngine.topologicalSortOfSCC(graph); System.out.println("Predicate dependency graph:\n"+graph); System.out.println("Topological sort of predicates:"); for (Set<Predicate> scc: sccs) { System.out.println("\t"+scc); }*/ AtomicFormula goal = new AtomicFormula(rs.getMainHeadFormula().getPredicate(), new ConstantExpr(0), new VariableExpr("Y")); RuleSystem mgrs = rs.magicSetTransformation(goal, true, true, false); Set<Predicate> predicatesToKeep = HashSetFactory.make(); predicatesToKeep.add(goal.getPredicate()); for (Predicate pred: mgrs.getHeadPredicates()) { if (pred instanceof MagicSetPredicate) { //predicatesToKeep.add(pred); /*Set<Predicate> preds = HashSetFactory.make(); preds.add(goal.getPredicate()); preds.add(pred); RuleSystem rules = mgrs.simplify(preds); System.out.println("Rule System For Magic set predicate: "+pred+"\n"+rules); */ } } mgrs = mgrs.simplify(predicatesToKeep); System.out.println("Rule System after magic set transformation (goal: "+goal+"):\n"+mgrs); /*graph = DatalogEngine.buildDependencyGraph(mgrs); sccs =DatalogEngine.topologicalSortOfSCC(graph); System.out.println("Predicate dependency graph:\n"+graph); System.out.println("Topological sort of predicates:"); for (Set<Predicate> scc: sccs) { System.out.println("\t"+scc); }*/ } }
Example #24
Source File: PropertyPathToRules.java From quetzal with Eclipse Public License 2.0 | 4 votes |
public RuleSystem toRules(TriplePath tp, String triplePredicateName, String negatedPropertySetPredName) { return toRules(tp, true, triplePredicateName, negatedPropertySetPredName); }
Example #25
Source File: SPARQLExtFormatterElement.java From sparql-generate with Apache License 2.0 | 4 votes |
@Override public void visit(ElementPathBlock el) { // Write path block - don't put in a final trailing "." if (el.isEmpty()) { out.println("# Empty BGP"); return; } // Split into BGP-path-BGP-... // where the BGPs may be empty. PathBlock pBlk = el.getPattern(); BasicPattern bgp = new BasicPattern(); boolean first = true; // Has anything been output? for (TriplePath tp : pBlk) { if (tp.isTriple()) { Triple t = tp.asTriple(); Node s = t.getSubject(); if(s.isVariable()) { s = Var.alloc(Var.canonical(((Var)s).getVarName())); } Node p = t.getPredicate(); Node o = t.getObject(); if(o.isVariable()) { o = Var.alloc(Var.canonical(((Var)o).getVarName())); } bgp.add(new Triple(s, p, o)); continue; } if (!bgp.isEmpty()) { if (!first) { out.println(" ."); } flush(bgp); first = false; } if (!first) { out.println(" ."); } // Path printSubject(tp.getSubject()); out.print(" "); SPARQLExtPathWriter.write(out, tp.getPath(), context); out.print(" "); printObject(tp.getObject()); first = false; } // Flush any stored triple patterns. if (!bgp.isEmpty()) { if (!first) { out.println(" ."); } flush(bgp); first = false; } }
Example #26
Source File: ElementGenerateTriplesBlock.java From sparql-generate with Apache License 2.0 | 4 votes |
@Override public void addTriplePath(int index, TriplePath path) { throw new ARQException("Triples-only collector"); }
Example #27
Source File: ElementGenerateTriplesBlock.java From sparql-generate with Apache License 2.0 | 4 votes |
@Override public void addTriplePath(TriplePath path) { throw new ARQException("Triples-only collector"); }
Example #28
Source File: PropertyPathRewrite.java From quetzal with Eclipse Public License 2.0 | 4 votes |
@Override public void visit(P_ZeroOrOne p) { if (bestEffort) { ElementPathBlock e = new ElementPathBlock(); e.addTriple(new TriplePath(subject, p, object)); result = e; resultHasPropertyPaths = true; } else { result = null; } // (subj p? obj) is semantically equivalent to a union of // 1) subj == obj // 2) subj p obj /*TriplePath newtp = new TriplePath(subject, p.getSubPath(), object); boolean[] resHasPropertyPaths = new boolean[]{false}; Element newElt = transform(newtp, bestEffort, vargen, resHasPropertyPaths); if (!subject.isVariable() && !object.isVariable()) { if (subject.sameValueAs(object)) { // trivially satisfied result = new ElementGroup(); } else { // the zero branch cannot be satified // so we test only the one branch result = newElt; if (!resultHasPropertyPaths) { resultHasPropertyPaths = resHasPropertyPaths[0]; } } } else { if (!resultHasPropertyPaths) { resultHasPropertyPaths = resHasPropertyPaths[0]; } ElementUnion union = new ElementUnion(); union.addElement(newElt); ElementGroup eqGroup = new ElementGroup(); ElementBind bind; if (subject.isVariable()) { bind = new ElementBind( Var.alloc(subject.getName()), getExpr(object)); } else { assert object.isVariable() : object; bind = new ElementBind(Var.alloc(object.getName()), getExpr(subject)); } eqGroup.addElement(bind); union.addElement(eqGroup); result = union; } */ }