Java Code Examples for org.apache.jena.graph.Graph#find()

The following examples show how to use org.apache.jena.graph.Graph#find() . 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: LangTurtleStarTest.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
@Test
public void stringParse5() {
       final String x = "<<<s> <p> <o>>> <p2> <o2> , <o3> .";
       final Graph g = RDFStarUtils.createGraphFromTurtleStarSnippet(x);

       assertEquals( 2, g.size() );

       final Iterator<Triple> it = g.find();

       final Triple t1 = it.next();
       assertTrue( t1.getSubject() instanceof Node_Triple );
       assertFalse( t1.getPredicate() instanceof Node_Triple );
       assertFalse( t1.getObject() instanceof Node_Triple );

       final Triple t2 = it.next();
       assertTrue( t2.getSubject() instanceof Node_Triple );
       assertFalse( t2.getPredicate() instanceof Node_Triple );
       assertFalse( t2.getObject() instanceof Node_Triple );

       final Triple et1 = ( (Node_Triple) t1.getSubject() ).get();
       final Triple et2 = ( (Node_Triple) t2.getSubject() ).get();
       assertTrue( et1 == et2 );
}
 
Example 2
Source File: ClassPropertyMetadata.java    From shacl with Apache License 2.0 6 votes vote down vote up
ClassPropertyMetadata(Node classNode, Node predicate, boolean inverse, Graph graph) {
	
	this.inverse = inverse;
	this.predicate = predicate;
	
	// Init from SHACL shapes
	if(SHACLUtil.exists(graph)) {
		if(JenaNodeUtil.isInstanceOf(classNode, SH.Shape.asNode(), graph)) {
			initFromShape(classNode, graph);
		}
		ExtendedIterator<Triple> it = graph.find(null, SH.targetClass.asNode(), classNode);
		while(it.hasNext()) {
			Node shape = it.next().getSubject();
			initFromShape(shape, graph);
		}
	}
	
	if(!inverse) {
		for(Plugin plugin : plugins) {
			plugin.init(this, classNode, graph);
		}
	}
}
 
Example 3
Source File: ClassMetadata.java    From shacl with Apache License 2.0 6 votes vote down vote up
public synchronized Set<PathMetadata> getGroupPaths(Node group, Graph graph) {
	if(groupPaths == null) {
		groupPaths = new HashMap<>();
		if(JenaNodeUtil.isInstanceOf(classNode, SH.Shape.asNode(), graph)) {
			addGroupProperties(classNode, graph, SH.parameter.asNode());
			addGroupProperties(classNode, graph, SH.property.asNode());
		}
		ExtendedIterator<Triple> it = graph.find(null, SH.targetClass.asNode(), classNode);
		while(it.hasNext()) {
			Node shape = it.next().getSubject();
			addGroupProperties(shape, graph, SH.parameter.asNode());
			addGroupProperties(shape, graph, SH.property.asNode());
		}
	}
	return groupPaths.get(group);
}
 
Example 4
Source File: localname.java    From xcurator with Apache License 2.0 6 votes vote down vote up
private QueryIterator execAllNodes(Var subjVar, Node nodeLocalname,  Binding input, ExecutionContext execCxt)
{
    if ( ! nodeLocalname.isVariable() )
    {
        if ( ! nodeLocalname.isLiteral() )
            // Not a variable, not a literal=> can't match
            return QueryIterNullIterator.create(execCxt) ;
    
        if( ! NodeUtils.isSimpleString(nodeLocalname) )
            return QueryIterNullIterator.create(execCxt) ;
    }
    
    //Set bindings = new HashSet() ;    // Use a Set if you want unique results. 
    List<Binding> bindings = new ArrayList<Binding>() ;   // Use a list if you want counting results. 
    Graph graph = execCxt.getActiveGraph() ;
    
    ExtendedIterator<Triple>iter = graph.find(Node.ANY, Node.ANY, Node.ANY) ;
    for ( ; iter.hasNext() ; )
    {
        Triple t = iter.next() ;
        slot(bindings, input, t.getSubject(),   subjVar, nodeLocalname) ;
        slot(bindings, input, t.getPredicate(), subjVar, nodeLocalname) ;
        slot(bindings, input, t.getObject(),    subjVar, nodeLocalname) ;
    }
    return new QueryIterPlainWrapper(bindings.iterator(), execCxt) ;
}
 
Example 5
Source File: LangTurtleStarTest.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
@Test
public void stringParse7() {
       final String x = "<s2> <p2> <o2> , <<<s> <p> <o>>> .";
       final Graph g = RDFStarUtils.createGraphFromTurtleStarSnippet(x);

       assertEquals( 2, g.size() );

       final Iterator<Triple> it = g.find();

       final Triple t1 = it.next();
       assertFalse( t1.getSubject() instanceof Node_Triple );
       assertFalse( t1.getPredicate() instanceof Node_Triple );

       final Triple t2 = it.next();
       assertFalse( t2.getSubject() instanceof Node_Triple );
       assertFalse( t2.getPredicate() instanceof Node_Triple );

       if ( t1.getObject() instanceof Node_Triple )
       	assertFalse( t2.getObject() instanceof Node_Triple );
       else
       	assertTrue( t2.getObject() instanceof Node_Triple );
}
 
Example 6
Source File: LangTurtleStarTest.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
@Test
public void stringParse6() {
       final String x = "<<<s> <p> <o>>> <p2> <o2> ; <p3> <o3> .";
       final Graph g = RDFStarUtils.createGraphFromTurtleStarSnippet(x);

       assertEquals( 2, g.size() );

       final Iterator<Triple> it = g.find();

       final Triple t1 = it.next();
       assertTrue( t1.getSubject() instanceof Node_Triple );
       assertFalse( t1.getPredicate() instanceof Node_Triple );
       assertFalse( t1.getObject() instanceof Node_Triple );

       final Triple t2 = it.next();
       assertTrue( t2.getSubject() instanceof Node_Triple );
       assertFalse( t2.getPredicate() instanceof Node_Triple );
       assertFalse( t2.getObject() instanceof Node_Triple );

       final Triple et1 = ( (Node_Triple) t1.getSubject() ).get();
       final Triple et2 = ( (Node_Triple) t2.getSubject() ).get();
       assertTrue( et1 == et2 );
}
 
Example 7
Source File: QueryIterTripleStarPattern.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
public TripleMapper( Binding binding, Triple tp, ExecutionContext cxt )
{
    super(cxt) ;
    this.s = ExtendedSubstitute.substitute( tp.getSubject(),   binding );
    this.p = ExtendedSubstitute.substitute( tp.getPredicate(), binding );
    this.o = ExtendedSubstitute.substitute( tp.getObject(),    binding );
    this.sIsTripleWithVars = isTripleWithVars(s);
    this.oIsTripleWithVars = isTripleWithVars(o);
    this.binding = binding;

    final Node s2 = sIsTripleWithVars ? Node.ANY : convertToBeUsedForFind(s);
    final Node p2 = convertToBeUsedForFind(p);
    final Node o2 = oIsTripleWithVars ? Node.ANY : convertToBeUsedForFind(o);

    final Graph graph = cxt.getActiveGraph();
    this.graphIter = graph.find(s2, p2, o2);
}
 
Example 8
Source File: RDFStar2RDFTest.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
protected void verifyNoNesting(Graph g)
{		
	final Iterator<Triple> iter = g.find();
	
	while (iter.hasNext()) {
		final Triple t = iter.next();
		assertFalse( t.getSubject()   instanceof Node_Triple );
		assertFalse( t.getPredicate() instanceof Node_Triple );
		assertFalse( t.getObject()    instanceof Node_Triple );
	}
}
 
Example 9
Source File: EntityIterator.java    From Stargraph with MIT License 5 votes vote down vote up
private Iterator<Node> createIterator() {
    Model model = core.getGraphModel();
    Graph g = model.getGraph();
    ExtendedIterator<Triple> exIt = g.find(Node.ANY, null, null);
    ExtendedIterator<Node> subjIt = exIt.mapWith(Triple::getSubject);
    exIt = g.find(null, null, Node.ANY);
    ExtendedIterator<Node> objIt = exIt.mapWith(Triple::getObject);
    return Iterators.concat(subjIt, objIt);
}
 
Example 10
Source File: JenaNodeUtil.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static Node getObject(Node subject, Node predicate, Graph graph) {
    ExtendedIterator<Triple> it = graph.find(subject, predicate, Node.ANY);
    try { 
           return it.hasNext() 
               ? it.next().getObject() 
               : null;
    } finally { 
        it.close();
    }
}
 
Example 11
Source File: JenaNodeUtil.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static List<Node> getSubjects(Node predicate, Node object, Graph graph) {
	List<Node> results = new LinkedList<>();
	ExtendedIterator<Triple> it = graph.find(Node.ANY, predicate, object);
	while(it.hasNext()) {
		results.add(it.next().getSubject());
	}
	return results;
}
 
Example 12
Source File: ClassMetadata.java    From shacl with Apache License 2.0 5 votes vote down vote up
private void addGroupProperties(Node nodeShape, Graph graph, Node systemPredicate) {
	ExtendedIterator<Triple> it = graph.find(nodeShape, systemPredicate, Node.ANY);
	while(it.hasNext()) {
		Node propertyShape = it.next().getObject();
		if(!graph.contains(propertyShape, SH.deactivated.asNode(), JenaDatatypes.TRUE.asNode())) {
			Node group = JenaNodeUtil.getObject(propertyShape, SH.group.asNode(), graph);
			if(group != null) {
				Node path = JenaNodeUtil.getObject(propertyShape, SH.path.asNode(), graph);
				if(path != null) {
					Set<PathMetadata> paths = groupPaths.get(group);
					if(paths == null) {
						paths = new HashSet<>();
						groupPaths.put(group, paths);
					}
					if(path.isURI()) {
						paths.add(new PathMetadata(path, false));
					}
					else {
						Node inverse = JenaNodeUtil.getObject(path, SH.inversePath.asNode(), graph);
						if(inverse != null && inverse.isURI()) {
							paths.add(new PathMetadata(inverse, true));
						}
					}
				}
			}
		}
	}
}
 
Example 13
Source File: ClassMetadata.java    From shacl with Apache License 2.0 5 votes vote down vote up
public synchronized Iterable<ClassMetadata> getSuperClasses(Graph graph) {
	if(superClasses == null) {
		superClasses = new LinkedList<>();
		ExtendedIterator<Triple> it = graph.find(classNode, RDFS.subClassOf.asNode(), Node.ANY);
		while(it.hasNext()) {
			Node superClass = it.next().getObject();
			superClasses.add(OntologyOptimizations.get().getClassMetadata(superClass, graph, graphKey));
		}
	}
	return superClasses;
}
 
Example 14
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 15
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 16
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 17
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 18
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 19
Source File: ClassPropertyMetadata.java    From shacl with Apache License 2.0 4 votes vote down vote up
private void initFromShape(Node shape, Node systemPredicate, Graph graph) {
	ExtendedIterator<Triple> it = graph.find(shape, systemPredicate, Node.ANY);
	while(it.hasNext()) {
		Node propertyShape = it.next().getObject();
		if(!propertyShape.isLiteral()) {
			if(hasMatchingPath(propertyShape, graph)) {
				if(!graph.contains(propertyShape, SH.deactivated.asNode(), JenaDatatypes.TRUE.asNode())) {
					if(description == null) {
						description = JenaNodeUtil.getObject(propertyShape, SH.description.asNode(), graph);
					}
					if(editWidget == null) {
						editWidget = JenaNodeUtil.getObject(propertyShape, TOSH.editWidget.asNode(), graph);
					}
					if(localRange == null) {
						if(inverse) {
							// Maybe: support inverse ranges
						}
						else {
							localRange = SHACLUtil.walkPropertyShapesHelper(propertyShape, graph);
						}
					}
					if(maxCount == null) {
						Node maxCountNode = JenaNodeUtil.getObject(propertyShape, SH.maxCount.asNode(), graph);
						if(maxCountNode != null && maxCountNode.isLiteral()) {
							Object value = maxCountNode.getLiteralValue();
							if(value instanceof Number) {
								maxCount = ((Number) value).intValue();
							}
						}
					}
					if(name == null) {
						name = JenaNodeUtil.getObject(propertyShape, SH.name.asNode(), graph);
					}
					if(order == null) {
						order = JenaNodeUtil.getObject(propertyShape, SH.order.asNode(), graph);
					}
					if(viewWidget == null) {
						viewWidget = JenaNodeUtil.getObject(propertyShape, TOSH.viewWidget.asNode(), graph);
					}
				}
			}
		}
	}
}
 
Example 20
Source File: ExRIOT_5.java    From xcurator with Apache License 2.0 4 votes vote down vote up
private void read(Item item, String baseURI, ContentType ct, StreamRDF output, Context context) {
    Graph graph = BuilderGraph.buildGraph(item) ;
    Iterator<Triple> iter = graph.find(null, null, null) ;
    for ( ; iter.hasNext() ; )
        output.triple(iter.next()) ;
}