Java Code Examples for org.apache.jena.util.iterator.ExtendedIterator#close()

The following examples show how to use org.apache.jena.util.iterator.ExtendedIterator#close() . 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: DiffGraph.java    From shacl with Apache License 2.0 6 votes vote down vote up
@Override
public boolean contains(Triple t) {
	if(addedGraph.contains(t)) {
		return true;
	}
	else {
		ExtendedIterator<Triple> it = base.find(t);
		while(it.hasNext()) {
			Triple n = it.next();
			if(!deletedTriples.contains(n)) {
				it.close();
				return true;
			}
		}
		return false;
	}
}
 
Example 2
Source File: DiffGraph.java    From shacl with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isEmpty() {
	if (!addedGraph.isEmpty()) {
		return false;
	}
	if (deletedTriples.isEmpty()) {
		return base.isEmpty();
	}
	ExtendedIterator<Triple> it = find(Triple.ANY);
	try {
		return !it.hasNext();
	}
	finally {
		it.close();
	}
}
 
Example 3
Source File: IfExpression.java    From shacl with Apache License 2.0 6 votes vote down vote up
@Override
public ExtendedIterator<RDFNode> eval(RDFNode focusNode, NodeExpressionContext context) {
	ExtendedIterator<RDFNode> it = evalInput(focusNode, context);
	try {
		if(it.hasNext() && JenaDatatypes.TRUE.equals(it.next()) && !it.hasNext()) {
			if(then != null) {
				return then.eval(focusNode, context);
			}
		}
		else {
			if(else_ != null) {
				return else_.eval(focusNode, context);
			}
		}
		return WrappedIterator.emptyIterator();
	}
	finally {
		it.close();
	}
}
 
Example 4
Source File: SumExpression.java    From shacl with Apache License 2.0 6 votes vote down vote up
@Override
public ExtendedIterator<RDFNode> eval(RDFNode focusNode, NodeExpressionContext context) {
	ExtendedIterator<RDFNode> it = evalInput(focusNode, context);
	NodeValue total = NodeValue.nvZERO;
	while(it.hasNext()) {
		RDFNode n = it.next();
		NodeValue nv = NodeValue.makeNode(n.asNode());
		if (nv.isNumber()) {
			total = XSDFuncOp.numAdd(nv, total);
		}
		else {
			it.close();
			return WrappedIterator.emptyIterator();
		}
	}
	RDFNode result = focusNode.getModel().asRDFNode(total.asNode());
	List<RDFNode> results = Collections.singletonList(result);
	return WrappedIterator.create(results.iterator());
}
 
Example 5
Source File: DiffGraph.java    From shacl with Apache License 2.0 5 votes vote down vote up
protected boolean containsByEquals(Graph g,Triple t) {
	ExtendedIterator<Triple> it = g.find(t);
	try {
		while (it.hasNext()) {
			if (t.equals(it.next())) 
				return true;
		}
	}
	finally {
		it.close();
	}
	return false;
}
 
Example 6
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 7
Source File: OWLClassPropertyMetadataPlugin.java    From shacl with Apache License 2.0 5 votes vote down vote up
@Override
public void init(ClassPropertyMetadata cpm, Node classNode, Graph graph) {
	ExtendedIterator<Triple> it = graph.find(classNode, RDFS.subClassOf.asNode(), Node.ANY);
	while(it.hasNext()) {
		Node superClass = it.next().getObject();
		if(superClass.isBlank() && graph.contains(superClass, OWL.onProperty.asNode(), cpm.getPredicate())) {
			if(cpm.getLocalRange() == null) {
				Node localRange = JenaNodeUtil.getObject(superClass, OWL.allValuesFrom.asNode(), graph);
				if(localRange != null) {
					cpm.setLocalRange(localRange);
					it.close();
					break;
				}
			}
			if(cpm.getMaxCount() == null) {
				Node maxCountNode = JenaNodeUtil.getObject(superClass, OWL.maxCardinality.asNode(), graph);
				if(maxCountNode == null) {
					maxCountNode = JenaNodeUtil.getObject(superClass, OWL.cardinality.asNode(), graph);
				}
				if(maxCountNode != null && maxCountNode.isLiteral()) {
					Object value = maxCountNode.getLiteralValue();
					if(value instanceof Number) {
						cpm.setMaxCount(((Number) value).intValue());
					}
				}
			}
		}
	}
}
 
Example 8
Source File: ExistsExpression.java    From shacl with Apache License 2.0 5 votes vote down vote up
@Override
public ExtendedIterator<RDFNode> eval(RDFNode focusNode, NodeExpressionContext context) {
	ExtendedIterator<RDFNode> it = evalInput(focusNode, context);
	RDFNode result = it.hasNext() ? JenaDatatypes.TRUE : JenaDatatypes.FALSE;
	it.close();
	return WrappedIterator.create(Collections.singletonList(result).iterator());
}
 
Example 9
Source File: TemplateImpl.java    From Processor with Apache License 2.0 5 votes vote down vote up
protected List<Locale> getLanguages(Property property)
{
    if (property == null) throw new IllegalArgumentException("Property cannot be null");
    
    List<Locale> languages = new ArrayList<>();
    Resource langs = getPropertyResourceValue(property);
    if (langs != null)
    {
        if (!langs.canAs(RDFList.class))
        {
            if (log.isErrorEnabled()) log.error("ldt:lang value is not an rdf:List on template '{}'", getURI());
            throw new OntologyException("ldt:lang value is not an rdf:List on template  '" + getURI() +"'");
        }

        // could use list order as priority (quality value q=)
        RDFList list = langs.as(RDFList.class);
        ExtendedIterator<RDFNode> it = list.iterator();
        try
        {
            while (it.hasNext())
            {
                RDFNode langTag = it.next();
                if (!langTag.isLiteral())
                {
                    if (log.isErrorEnabled()) log.error("Non-literal language tag (ldt:lang member) on template '{}'", getURI());
                    throw new OntologyException("Non-literal language tag (ldt:lang member) on template '" + getURI() +"'");
                }

                languages.add(Locale.forLanguageTag(langTag.asLiteral().getString()));
            }
        }
        finally
        {
            it.close();
        }
    }
    
    return languages;
}
 
Example 10
Source File: OntologyProvider.java    From Processor with Apache License 2.0 5 votes vote down vote up
public void check(Ontology ontology)
{
    if (ontology == null) throw new IllegalArgumentException("Ontology cannot be null");
    
    marked.put(ontology, Boolean.TRUE);
    onStack.put(ontology, Boolean.TRUE);

    ExtendedIterator<OntResource> it = ontology.listImports();
    try
    {
        while (it.hasNext())
        {
            OntResource importRes = it.next();
            if (importRes.canAs(Ontology.class))
            {
                Ontology imported = importRes.asOntology();
                if (marked.get(imported) == null)
                    check(imported);
                else if (onStack.get(imported))
                {
                    cycleOntology = imported;
                    return;
                }
            }
        }

        onStack.put(ontology, Boolean.FALSE);
    }
    finally
    {
        it.close();
    }
}
 
Example 11
Source File: DB2Dataset.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public Iterator<Quad> find(Node g, final Node s, final Node p, final Node o)
{

ExtendedIterator<Triple> it;

if ((g == null) || g.equals(Node.ANY))
   {
   it = DB2Graph.find(store, new Triple(s, p, o), null, connection, false, /* not reification */
         true /* search all graphs */);
   }
else
   {

   if (g.getURI().equalsIgnoreCase(Constants.DEFAULT_GRAPH_MONIKER))
      {
      it = defaultModel.getGraph().find(s, p, o);
      }
   else
      {
      it = getGraph(g).find(s, p, o);
      }

   }

Set<Quad> sq = new HashSet<Quad>();
while (it.hasNext())
   {
   sq.add(new Quad(g, it.next()));
   }
it.close();

return sq.iterator();

}
 
Example 12
Source File: Skolemizer.java    From Processor with Apache License 2.0 4 votes vote down vote up
public SortedSet<ClassPrecedence> match(Ontology ontology, Resource resource, Property property, int level)
{
    if (ontology == null) throw new IllegalArgumentException("Ontology cannot be null");
    if (resource == null) throw new IllegalArgumentException("Resource cannot be null");
    if (property == null) throw new IllegalArgumentException("Property cannot be null");

    SortedSet<ClassPrecedence> matchedClasses = new TreeSet<>();
    ResIterator it = ontology.getOntModel().listResourcesWithProperty(LDT.path);
    try
    {
        while (it.hasNext())
        {
            Resource ontClassRes = it.next();
            OntClass ontClass = ontology.getOntModel().getOntResource(ontClassRes).asClass();
            // only match templates defined in this ontology - maybe reverse loops?
            if (ontClass.getIsDefinedBy() != null && ontClass.getIsDefinedBy().equals(ontology) &&
                    resource.hasProperty(property, ontClass))
            {
                ClassPrecedence precedence = new ClassPrecedence(ontClass, level * -1);
                if (log.isTraceEnabled()) log.trace("Resource {} matched OntClass {}", resource, ontClass);
                matchedClasses.add(precedence);
            } 
        }
    }
    finally
    {
        it.close();
    }

    ExtendedIterator<OntResource> imports = ontology.listImports();
    try
    {
        while (imports.hasNext())
        {
            OntResource importRes = imports.next();
            if (importRes.canAs(Ontology.class))
            {
                Ontology importedOntology = importRes.asOntology();
                // traverse imports recursively
                Set<ClassPrecedence> matchedImportClasses = match(importedOntology, resource, property, level + 1);
                matchedClasses.addAll(matchedImportClasses);
            }
        }
    }
    finally
    {
        imports.close();
    }
    
    return matchedClasses;
}