Java Code Examples for org.apache.jena.sparql.core.TriplePath#asTriple()

The following examples show how to use org.apache.jena.sparql.core.TriplePath#asTriple() . 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: ParserSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@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 2
Source File: ParserSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@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 3
Source File: ParserSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@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 4
Source File: ParserSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@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 5
Source File: ParserSPARQLStarTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
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 6
Source File: SPARQLExtFormatterElement.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@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;
    }
}