Java Code Examples for org.apache.jena.graph.Triple#getPredicate()

The following examples show how to use org.apache.jena.graph.Triple#getPredicate() . 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: Node_TripleStarPattern.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
static public Triple asTripleWithNode_TripleStarPatterns( Triple t )
{
	final Node s = t.getSubject();
	final Node s2;
	if ( s instanceof Node_Triple )
		s2 = asNode_TripleStarPattern( (Node_Triple) s );
	else
		s2 = s;

	final Node o = t.getObject();
	final Node o2;
	if ( o instanceof Node_Triple )
		o2 = asNode_TripleStarPattern( (Node_Triple) o );
	else
		o2 = o;

	if ( s == s2 && o == o2 )
		return t;
	else
		return new Triple(s2, t.getPredicate(), o2);
}
 
Example 2
Source File: TraversalBuilder.java    From sparql-gremlin with Apache License 2.0 6 votes vote down vote up
public static GraphTraversal<?, ?> transform(final Triple triple) {
    final GraphTraversal<Vertex, ?> matchTraversal = __.as(triple.getSubject().getName());
    final Node predicate = triple.getPredicate();
    final String uri = predicate.getURI();
    final String uriValue = Prefixes.getURIValue(uri);
    final String prefix = Prefixes.getPrefix(uri);
    switch (prefix) {
        case "edge":
            return matchTraversal.out(uriValue).as(triple.getObject().getName());
        case "property":
            return matchProperty(matchTraversal, uriValue, PropertyType.PROPERTY, triple.getObject());
        case "value":
            return matchProperty(matchTraversal, uriValue, PropertyType.VALUE, triple.getObject());
        default:
            throw new IllegalStateException(String.format("Unexpected predicate: %s", predicate));
    }
}
 
Example 3
Source File: TraversalBuilder.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
static GraphTraversal<?, ?> transform(final Triple triple) {
    final GraphTraversal<Vertex, ?> matchTraversal = __.as(triple.getSubject().getName());
    
    final Node predicate = triple.getPredicate();
    final String uri = predicate.getURI();
    final String uriValue = Prefixes.getURIValue(uri);
    final String prefix = Prefixes.getPrefix(uri);

    switch (prefix) {
        case "edge":
            return matchTraversal.out(uriValue).as(triple.getObject().getName());
        case "property":
            return matchProperty(matchTraversal, uriValue, PropertyType.PROPERTY, triple.getObject());
        case "value":
            return matchProperty(matchTraversal, uriValue, PropertyType.VALUE, triple.getObject());
        default:
            throw new IllegalStateException(String.format("Unexpected predicate: %s", predicate));
    }
}
 
Example 4
Source File: OWLQLCompiler.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
protected String getKey(Triple t) {
	Node subj = t.getSubject();
	Node obj = t.getObject();
	Node pred = t.getPredicate();
	if (!pred.isURI()) {
		return null;
	}
	if (!subj.isVariable() && !obj.isVariable()) {
		return null;
	}
	if (pred.getURI().equals(RDFConstants.RDF_TYPE)) {
		return obj.isURI()? obj.getURI() : null;
	} else {
		return pred.getURI();
	}
	
}
 
Example 5
Source File: ElementTransformSPARQLStar.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
protected void unNestBindClause( Triple tp, ElementPathBlock epb, Var var )
{
	Node s = tp.getSubject();
	Node p = tp.getPredicate();
	Node o = tp.getObject();

	if ( s instanceof Node_Triple )
	{
		final Triple sTP = ( (Node_Triple) s ).get();
		s = unNestTriplePattern(sTP, epb, true);
	}

	if ( o instanceof Node_Triple )
	{
		final Triple oTP = ( (Node_Triple) o ).get();
		o = unNestTriplePattern(oTP, epb, true);
	}

	final Triple nonnestedTP = new Triple(s, p, o);

	if ( ! doneNested.containsKey(nonnestedTP) )
	{
		epb.addTriple(nonnestedTP);
		epb.addTriple( new Triple(var, RDF.Nodes.type,      RDF.Nodes.Statement) );
		epb.addTriple( new Triple(var, RDF.Nodes.subject,   s) );
		epb.addTriple( new Triple(var, RDF.Nodes.predicate, p) );
		epb.addTriple( new Triple(var, RDF.Nodes.object,    o) );
		doneNested.put(nonnestedTP, var);
	}
}
 
Example 6
Source File: SinkTripleStarOutput.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
@Override
public void send(Triple triple) {
    Node s = triple.getSubject() ;
    Node p = triple.getPredicate() ;
    Node o = triple.getObject() ;

    nodeFmt.format(writer, s) ;
    writer.print(" ") ;
    nodeFmt.format(writer, p) ;
    writer.print(" ") ;
    nodeFmt.format(writer, o) ;
    writer.print(" .\n") ;
}
 
Example 7
Source File: NormalizedOWLQLTbox.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isTripleAbsentFromAbox(Triple qt) {
	Node pred = qt.getPredicate();
	if (pred.isURI() && isGeneratedRole(pred.getURI())) {
		return true;
	} else if (pred.isURI() && pred.getURI().equals(RDFConstants.RDF_TYPE)) {
		Node obj = qt.getObject();
		if (obj.isURI() && isGeneratedClass(obj.getURI())) {
			return true;
		}
	}
	return false;
}
 
Example 8
Source File: ElementTransformSPARQLStar.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
protected Node unNestTriplePattern( Triple tp, ElementPathBlock epb, boolean hasParent )
{	
	Node s = tp.getSubject();
	Node p = tp.getPredicate();
	Node o = tp.getObject();

	if ( s instanceof Node_Triple )
	{
		final Triple sTP = ( (Node_Triple) s ).get();
		s = unNestTriplePattern(sTP, epb, true);
	}

	if ( o instanceof Node_Triple )
	{
		final Triple oTP = ( (Node_Triple) o ).get();
		o = unNestTriplePattern(oTP, epb, true);
	}

	final Triple nonnestedTP = new Triple(s, p, o);
	final boolean seenBefore = doneNested.containsKey(nonnestedTP);

	final Node var;
	if ( seenBefore ) {
		var = doneNested.get(nonnestedTP);
	}
	else
	{
		var = createFreshAnonVarForReifiedTriple();
		epb.addTriple(nonnestedTP);

		if ( hasParent ) {
			epb.addTriple( new Triple(var, RDF.Nodes.type,      RDF.Nodes.Statement) );
			epb.addTriple( new Triple(var, RDF.Nodes.subject,   s) );
			epb.addTriple( new Triple(var, RDF.Nodes.predicate, p) );
			epb.addTriple( new Triple(var, RDF.Nodes.object,    o) );
			doneNested.put(nonnestedTP, var);
		}
	}

	return var;
}
 
Example 9
Source File: RDFStar2RDFTest.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
@Test
public void nestedSubject()
{
	final String filename = "nestedSubject.ttls";
       final Graph g = convertAndLoadIntoGraph(filename);

       assertEquals( 6, g.size() );

       verifyNoNesting(g);

       int cntTypeStmt = 0;
       int cntSubjectStmt = 0;
       int cntPredicateStmt = 0;
       int cntObjectStmt = 0;
       int cntReifiedStmt = 0;
       int cntMetaStmt = 0;

       final Iterator<Triple> it = g.find();
       while ( it.hasNext() )
       {
       	final Triple t = it.next();
       	final Node p = t.getPredicate();
       	final Node o = t.getObject();

       	if ( p.equals(RDF.type.asNode()) && o.equals(RDF.Statement.asNode()) )
       		cntTypeStmt++;
       	else if ( p.equals(RDF.subject.asNode()) )
       		cntSubjectStmt++;
       	else if ( p.equals(RDF.predicate.asNode()) && o.equals(DCTerms.creator.asNode()) )
       		cntPredicateStmt++;
       	else if ( p.equals(RDF.object.asNode()) )
       		cntObjectStmt++;
       	else if ( p.equals(DCTerms.creator.asNode()) )
       		cntReifiedStmt++;
       	else if ( p.equals(DCTerms.source.asNode()) )
       		cntMetaStmt++;
       }

       assertEquals( 1, cntTypeStmt );
       assertEquals( 1, cntSubjectStmt );
       assertEquals( 1, cntPredicateStmt );
       assertEquals( 1, cntObjectStmt );
       assertEquals( 1, cntReifiedStmt );
       assertEquals( 1, cntMetaStmt );
}
 
Example 10
Source File: RDFStar2RDFTest.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
@Test
public void nestedObject()
{
	final String filename = "nestedObject.ttls";
       final Graph g = convertAndLoadIntoGraph(filename);

       assertEquals( 6, g.size() );

       verifyNoNesting(g);

       int cntTypeStmt = 0;
       int cntSubjectStmt = 0;
       int cntPredicateStmt = 0;
       int cntObjectStmt = 0;
       int cntReifiedStmt = 0;
       int cntMetaStmt = 0;

       final Iterator<Triple> it = g.find();
       while ( it.hasNext() )
       {
       	final Triple t = it.next();
       	final Node p = t.getPredicate();
       	final Node o = t.getObject();

       	if ( p.equals(RDF.type.asNode()) && o.equals(RDF.Statement.asNode()) )
       		cntTypeStmt++;
       	else if ( p.equals(RDF.subject.asNode()) )
       		cntSubjectStmt++;
       	else if ( p.equals(RDF.predicate.asNode()) && o.equals(DCTerms.creator.asNode()) )
       		cntPredicateStmt++;
       	else if ( p.equals(RDF.object.asNode()) )
       		cntObjectStmt++;
       	else if ( p.equals(DCTerms.creator.asNode()) )
       		cntReifiedStmt++;
       	else if ( p.equals(DCTerms.source.asNode()) )
       		cntMetaStmt++;
       }

       assertEquals( 1, cntTypeStmt );
       assertEquals( 1, cntSubjectStmt );
       assertEquals( 1, cntPredicateStmt );
       assertEquals( 1, cntObjectStmt );
       assertEquals( 1, cntReifiedStmt );
       assertEquals( 1, cntMetaStmt );
}
 
Example 11
Source File: RDFStar2RDFTest.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
@Test
public void nestedSubjectAndObject()
{
	final String filename = "nestedSubjectAndObject.ttls";
       final Graph g = convertAndLoadIntoGraph(filename);

       assertEquals( 11, g.size() );

       verifyNoNesting(g);

       int cntTypeStmt = 0;
       int cntSubjectStmt = 0;
       int cntPredicateStmt1 = 0;
       int cntPredicateStmt2 = 0;
       int cntObjectStmt = 0;
       int cntReifiedStmt1 = 0;
       int cntReifiedStmt2 = 0;
       int cntMetaStmt = 0;

       final Iterator<Triple> it = g.find();
       while ( it.hasNext() )
       {
       	final Triple t = it.next();
       	final Node p = t.getPredicate();
       	final Node o = t.getObject();

       	if ( p.equals(RDF.type.asNode()) && o.equals(RDF.Statement.asNode()) )
       		cntTypeStmt++;
       	else if ( p.equals(RDF.subject.asNode()) )
       		cntSubjectStmt++;
       	else if ( p.equals(RDF.predicate.asNode()) && o.equals(DCTerms.creator.asNode()) )
       		cntPredicateStmt1++;
       	else if ( p.equals(RDF.predicate.asNode()) && o.equals(DCTerms.created.asNode()) )
       		cntPredicateStmt2++;
       	else if ( p.equals(RDF.object.asNode()) )
       		cntObjectStmt++;
       	else if ( p.equals(DCTerms.creator.asNode()) )
       		cntReifiedStmt1++;
       	else if ( p.equals(DCTerms.created.asNode()) )
       		cntReifiedStmt2++;
       	else if ( p.equals(DCTerms.requires.asNode()) )
       		cntMetaStmt++;
       }

       assertEquals( 2, cntTypeStmt );
       assertEquals( 2, cntSubjectStmt );
       assertEquals( 1, cntPredicateStmt1 );
       assertEquals( 1, cntPredicateStmt2 );
       assertEquals( 2, cntObjectStmt );
       assertEquals( 1, cntReifiedStmt1 );
       assertEquals( 1, cntReifiedStmt2 );
       assertEquals( 1, cntMetaStmt );
}
 
Example 12
Source File: RDFStar2RDFTest.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
@Test
public void doubleNestedSubject()
{
	final String filename = "doubleNestedSubject.ttls";
       final Graph g = convertAndLoadIntoGraph(filename);

       assertEquals( 11, g.size() );

       verifyNoNesting(g);

       int cntTypeStmt = 0;
       int cntSubjectStmt = 0;
       int cntPredicateStmt1 = 0;
       int cntPredicateStmt2 = 0;
       int cntObjectStmt = 0;
       int cntReifiedStmt1 = 0;
       int cntReifiedStmt2 = 0;
       int cntMetaStmt = 0;

       final Iterator<Triple> it = g.find();
       while ( it.hasNext() )
       {
       	final Triple t = it.next();
       	final Node p = t.getPredicate();
       	final Node o = t.getObject();

       	if ( p.equals(RDF.type.asNode()) && o.equals(RDF.Statement.asNode()) )
       		cntTypeStmt++;
       	else if ( p.equals(RDF.subject.asNode()) )
       		cntSubjectStmt++;
       	else if ( p.equals(RDF.predicate.asNode()) && o.equals(FOAF.knows.asNode()) )
       		cntPredicateStmt1++;
       	else if ( p.equals(RDF.predicate.asNode()) && o.equals(DCTerms.created.asNode()) )
       		cntPredicateStmt2++;
       	else if ( p.equals(RDF.object.asNode()) )
       		cntObjectStmt++;
       	else if ( p.equals(FOAF.knows.asNode()) )
       		cntReifiedStmt1++;
       	else if ( p.equals(DCTerms.created.asNode()) )
       		cntReifiedStmt2++;
       	else if ( p.equals(DCTerms.source.asNode()) )
       		cntMetaStmt++;
       }

       assertEquals( 2, cntTypeStmt );
       assertEquals( 2, cntSubjectStmt );
       assertEquals( 1, cntPredicateStmt1 );
       assertEquals( 1, cntPredicateStmt2 );
       assertEquals( 2, cntObjectStmt );
       assertEquals( 1, cntReifiedStmt1 );
       assertEquals( 1, cntReifiedStmt2 );
       assertEquals( 1, cntMetaStmt );
}
 
Example 13
Source File: RDFStar2RDFTest.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
@Test
public void doubleNestedObject()
{
	final String filename = "doubleNestedObject.ttls";
       final Graph g = convertAndLoadIntoGraph(filename);

       assertEquals( 11, g.size() );

       verifyNoNesting(g);

       int cntTypeStmt = 0;
       int cntSubjectStmt = 0;
       int cntPredicateStmt1 = 0;
       int cntPredicateStmt2 = 0;
       int cntObjectStmt = 0;
       int cntReifiedStmt1 = 0;
       int cntReifiedStmt2 = 0;
       int cntMetaStmt = 0;

       final Iterator<Triple> it = g.find();
       while ( it.hasNext() )
       {
       	final Triple t = it.next();
       	final Node s = t.getSubject();
       	final Node p = t.getPredicate();
       	final Node o = t.getObject();

       	if ( p.equals(RDF.type.asNode()) && o.equals(RDF.Statement.asNode()) )
       		cntTypeStmt++;
       	else if ( p.equals(RDF.subject.asNode()) )
       		cntSubjectStmt++;
       	else if ( p.equals(RDF.predicate.asNode()) && o.equals(FOAF.knows.asNode()) )
       		cntPredicateStmt1++;
       	else if ( p.equals(RDF.predicate.asNode()) && o.equals(DCTerms.created.asNode()) )
       		cntPredicateStmt2++;
       	else if ( p.equals(RDF.object.asNode()) )
       		cntObjectStmt++;
       	else if ( p.equals(FOAF.knows.asNode()) && s.getURI().contains("alice") )
       		cntReifiedStmt1++;
       	else if ( p.equals(DCTerms.created.asNode()) )
       		cntReifiedStmt2++;
       	else if ( p.equals(FOAF.knows.asNode()) )
       		cntMetaStmt++;
       }

       assertEquals( 2, cntTypeStmt );
       assertEquals( 2, cntSubjectStmt );
       assertEquals( 1, cntPredicateStmt1 );
       assertEquals( 1, cntPredicateStmt2 );
       assertEquals( 2, cntObjectStmt );
       assertEquals( 1, cntReifiedStmt1 );
       assertEquals( 1, cntReifiedStmt2 );
       assertEquals( 1, cntMetaStmt );
}
 
Example 14
Source File: LogUtils.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
public static Triple compress(Triple t) {
    Node o = compress(t.getObject());
    return new Triple(t.getSubject(), t.getPredicate(), o);
}
 
Example 15
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;
    }
}