Java Code Examples for org.apache.jena.rdf.model.StmtIterator#close()

The following examples show how to use org.apache.jena.rdf.model.StmtIterator#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: JenaUtil.java    From shacl with Apache License 2.0 6 votes vote down vote up
private static void addTransitiveSubjects(Set<Resource> reached, Resource object,
		Property predicate, ProgressMonitor monitor) {
	if (object != null) {
		reached.add(object);
		StmtIterator it = object.getModel().listStatements(null, predicate, object);
		try {
			while (it.hasNext()) {
				if (monitor != null && monitor.isCanceled()) {
					it.close();
					return;
				}
				Resource subject = it.next().getSubject();
				if (!reached.contains(subject)) {
					addTransitiveSubjects(reached, subject, predicate, monitor);
				}
			}
		}
		finally {
			it.close();
		}
	}
}
 
Example 2
Source File: JenaUtil.java    From shacl with Apache License 2.0 6 votes vote down vote up
private static Resource getFirstRange(Resource property, Set<Resource> reached) {
	Resource directRange = getFirstDirectRange(property);
	if(directRange != null) {
		return directRange;
	}
	StmtIterator it = property.listProperties(RDFS.subPropertyOf);
	while (it.hasNext()) {
		Statement ss = it.next();
		if (ss.getObject().isURIResource()) {
			Resource superProperty = ss.getResource();
			if (!reached.contains(superProperty)) {
				reached.add(superProperty);
				Resource r = getFirstRange(superProperty, reached);
				if (r != null) {
					it.close();
					return r;
				}
			}
		}
	}
	return null;
}
 
Example 3
Source File: JenaUtil.java    From shacl with Apache License 2.0 6 votes vote down vote up
private static <T> T getNearest(Resource cls, java.util.function.Function<Resource,T> function, Set<Resource> reached) {
	reached.add(cls);
	StmtIterator it = cls.listProperties(RDFS.subClassOf);
	while(it.hasNext()) {
		Statement s = it.next();
		if(s.getObject().isResource() && !reached.contains(s.getResource())) {
			T result = function.apply(s.getResource());
			if(result == null) {
				result = getNearest(s.getResource(), function, reached);
			}
			if(result != null) {
				it.close();
				return result;
			}
		}
	}
	return null;
}
 
Example 4
Source File: JenaUtil.java    From shacl with Apache License 2.0 6 votes vote down vote up
private static void addTransitiveObjects(Set<Resource> resources, Set<Resource> reached,
		Resource subject, Property predicate) {
	resources.add(subject);
	reached.add(subject);
	StmtIterator it = subject.listProperties(predicate);
	try {
		while (it.hasNext()) {
			RDFNode object = it.next().getObject();
			if (object instanceof Resource) {
				if (!reached.contains(object)) {
					addTransitiveObjects(resources, reached, (Resource)object, predicate);
				}
			}
		}
	}
	finally {
		it.close();
	}
}
 
Example 5
Source File: JenaUtil.java    From shacl with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether a given Resource is an instance of a given type, or
 * a subclass thereof.  Make sure that the expectedType parameter is associated
 * with the right Model, because the system will try to walk up the superclasses
 * of expectedType.  The expectedType may have no Model, in which case
 * the method will use the instance's Model.
 * @param instance  the Resource to test
 * @param expectedType  the type that instance is expected to have
 * @return true if resource has rdf:type expectedType
 */
public static boolean hasIndirectType(Resource instance, Resource expectedType) {
	
	if(expectedType.getModel() == null) {
		expectedType = expectedType.inModel(instance.getModel());
	}
	
	StmtIterator it = instance.listProperties(RDF.type);
	while(it.hasNext()) {
		Statement s = it.next();
		if(s.getObject().isResource()) {
			Resource actualType = s.getResource();
			if(actualType.equals(expectedType) || JenaUtil.hasSuperClass(actualType, expectedType)) {
				it.close();
				return true;
			}
		}
	}
	return false;
}
 
Example 6
Source File: JenaUtil.java    From shacl with Apache License 2.0 6 votes vote down vote up
private static boolean hasSuperClass(Resource subClass, Resource superClass, Set<Resource> reached) {
	StmtIterator it = subClass.listProperties(RDFS.subClassOf);
	while(it.hasNext()) {
		Statement s = it.next();
		if(superClass.equals(s.getObject())) {
			it.close();
			return true;
		}
		else if(!reached.contains(s.getResource())) {
			reached.add(s.getResource());
			if(hasSuperClass(s.getResource(), superClass, reached)) {
				it.close();
				return true;
			}
		}
	}
	return false;
}
 
Example 7
Source File: Skolemizer.java    From Processor with Apache License 2.0 6 votes vote down vote up
protected Resource getResource(Resource resource, String name)
{
    if (resource == null) throw new IllegalArgumentException("Resource cannot be null");
    
    StmtIterator it = resource.listProperties();
    try
    {
        while (it.hasNext())
        {
            Statement stmt = it.next();
            if (stmt.getObject().isAnon() && stmt.getPredicate().getLocalName().equals(name))
            {
                if (log.isTraceEnabled()) log.trace("Found Resource {} for property name: {} ", stmt.getResource(), name);
                return stmt.getResource();
            }
        }
    }
    finally
    {
        it.close();
    }
    
    return null;
}
 
Example 8
Source File: OrConstraintExecutor.java    From shacl with Apache License 2.0 6 votes vote down vote up
private boolean hasOnlyDatatypeConstraints() {
	if(shapes.size() == 0) {
		return false;
	}
	for(Resource shape : shapes) {
		StmtIterator mit = shape.listProperties();
		if(mit.hasNext()) {
			Statement s = mit.next();
			if(!SH.datatype.equals(s.getPredicate()) || mit.hasNext() || !s.getObject().isURIResource()) {
				mit.close();
				return false;
			}
		}
		else {
			return false;
		}
	}
	return true;
}
 
Example 9
Source File: SPARQLConstraintExecutor.java    From shacl with Apache License 2.0 6 votes vote down vote up
@Override
protected String getSPARQL(Constraint constraint) {
	SHSPARQLConstraint sc = SHFactory.asSPARQLConstraint(constraint.getParameterValue());
	String select = JenaUtil.getStringProperty(sc, SH.select);
	if(select == null) {
		String message = "Missing " + SH.PREFIX + ":" + SH.select.getLocalName() + " of " + RDFLabels.get().getLabel(sc);
		if(sc.isAnon()) {
			StmtIterator it = sc.getModel().listStatements(null, null, sc);
			if(it.hasNext()) {
				Statement s = it.next();
				it.close();
				message += " at " + RDFLabels.get().getLabel(s.getSubject());
				message += " via " + RDFLabels.get().getLabel(s.getPredicate());
			}
		}
		throw new SHACLException(message);
	}
	return SPARQLSubstitutions.withPrefixes(select, sc);
}
 
Example 10
Source File: ValidationEngine.java    From shacl with Apache License 2.0 6 votes vote down vote up
public void updateConforms() {
	boolean conforms = true;
	StmtIterator it = report.listProperties(SH.result);
	while(it.hasNext()) {
		Statement s = it.next();
		if(s.getResource().hasProperty(RDF.type, SH.ValidationResult)) {
			conforms = false;
			it.close();
			break;
		}
	}
	if(report.hasProperty(SH.conforms)) {
		report.removeAll(SH.conforms);
	}
	report.addProperty(SH.conforms, conforms ? JenaDatatypes.TRUE : JenaDatatypes.FALSE);
}
 
Example 11
Source File: SHParameterizableImpl.java    From shacl with Apache License 2.0 6 votes vote down vote up
@Override
public List<SHParameter> getParameters() {
	List<SHParameter> results = new LinkedList<SHParameter>();
	StmtIterator it = null;
	JenaUtil.setGraphReadOptimization(true);
	try {
		Set<Resource> classes = JenaUtil.getAllSuperClasses(this);
		classes.add(this);
		for(Resource cls : classes) {
			it = cls.listProperties(SH.parameter);
			while(it.hasNext()) {
				Resource param = it.next().getResource();
				results.add(param.as(SHParameter.class));
			}
		}
	}
	finally {
		if (it != null) {
			it.close();
		}
		JenaUtil.setGraphReadOptimization(false);
	}
	return results;
}
 
Example 12
Source File: ExecutionPlatform.java    From shacl with Apache License 2.0 6 votes vote down vote up
public static boolean canExecute(Resource executable) {
	StmtIterator it = executable.listProperties(DASH.requiredExecutionPlatform);
	if(!it.hasNext()) {
		return true;
	}
	else {
		while(it.hasNext()) {
			Statement s = it.next();
			if(s.getObject().isResource() && isCompatibleWith(s.getResource())) {
				it.close();
				return true;
			}
		}
		return false;
	}
}
 
Example 13
Source File: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 5 votes vote down vote up
private void buildMappings(Model model) throws MalformedURLException {
  mappings = new HashMap();
  Property mapsTo = model.createProperty(RTM_MAPSTO);
  StmtIterator it = model.listStatements(null, mapsTo, (RDFNode) null);
  while (it.hasNext()) {
    Statement stmt = (Statement) it.next();
    StatementHandler mapper = getMapper(stmt.getSubject(), stmt.getObject(), model);
    mappings.put(stmt.getSubject().getURI(), mapper);
  }
  it.close();
}
 
Example 14
Source File: Skolemizer.java    From Processor with Apache License 2.0 5 votes vote down vote up
protected Literal getLiteral(Resource resource, String namePath)
{
    if (resource == null) throw new IllegalArgumentException("Resource cannot be null");

    if (namePath.contains("."))
    {
        String name = namePath.substring(0, namePath.indexOf("."));
        String nameSubPath = namePath.substring(namePath.indexOf(".") + 1);
        Resource subResource = getResource(resource, name);
        if (subResource != null) return getLiteral(subResource, nameSubPath);
    }
    
    StmtIterator it = resource.listProperties();
    try
    {
        while (it.hasNext())
        {
            Statement stmt = it.next();
            if (stmt.getObject().isLiteral() && stmt.getPredicate().getLocalName().equals(namePath))
            {
                if (log.isTraceEnabled()) log.trace("Found Literal {} for property name: {} ", stmt.getLiteral(), namePath);
                return stmt.getLiteral();
            }
        }
    }
    finally
    {
        it.close();
    }
    
    return null;
}
 
Example 15
Source File: TemplateImpl.java    From Processor with Apache License 2.0 5 votes vote down vote up
@Override
public final boolean hasSuperTemplate(Template superTemplate)
{
    if (superTemplate == null) throw new IllegalArgumentException("Template cannot be null");
    
    StmtIterator it = listProperties(LDT.extends_);
    try
    {
        while (it.hasNext())
        {
            Statement stmt = it.next();
            if (!stmt.getObject().isResource() || !stmt.getObject().asResource().canAs(Template.class))
            {
                if (log.isErrorEnabled()) log.error("Template's '{}' ldt:extends value '{}' is not an LDT Template", getURI(), stmt.getObject());
                throw new OntologyException("Template's '" + getURI() + "' ldt:extends value '" + stmt.getObject() + "' is not an LDT Template");
            }

            Template nextTemplate = stmt.getObject().as(Template.class);
            if (nextTemplate.equals(superTemplate) || nextTemplate.hasSuperTemplate(superTemplate))
                return true;
        }
    }
    finally
    {
        it.close();
    }
    
    return false;
}
 
Example 16
Source File: TemplateImpl.java    From Processor with Apache License 2.0 5 votes vote down vote up
protected Map<Property, Parameter> addSuperParameters(Template template, Map<Property, Parameter> params)
{
    if (template == null) throw new IllegalArgumentException("Template Set cannot be null");
    if (params == null) throw new IllegalArgumentException("Parameter Map cannot be null");
    
    StmtIterator it = template.listProperties(LDT.extends_);
    try
    {
        while (it.hasNext())
        {
            Statement stmt = it.next();
            if (!stmt.getObject().isResource() || !stmt.getObject().asResource().canAs(Template.class))
            {
                if (log.isErrorEnabled()) log.error("Template's '{}' ldt:extends value '{}' is not an LDT Template", getURI(), stmt.getObject());
                throw new OntologyException("Template's '" + getURI() + "' ldt:extends value '" + stmt.getObject() + "' is not an LDT Template");
            }

            Template superTemplate = stmt.getObject().as(Template.class);
            Map<Property, Parameter> superArgs = superTemplate.getLocalParameters();
            Iterator<Entry<Property, Parameter>> entryIt = superArgs.entrySet().iterator();
            while (entryIt.hasNext())
            {
                Entry<Property, Parameter> entry = entryIt.next();
                params.putIfAbsent(entry.getKey(), entry.getValue()); // reject Parameters for existing predicates
            }

            addSuperParameters(superTemplate, params);  // recursion to super class
        }
    }
    finally
    {
        it.close();
    }
    
    return params;
}
 
Example 17
Source File: ClassesCache.java    From shacl with Apache License 2.0 5 votes vote down vote up
@Override
public boolean test(Resource instance) {
	StmtIterator it = instance.listProperties(RDF.type);
	while(it.hasNext()) {
		if(classes.contains(it.next().getObject())) {
			it.close();
			return true;
		}
	}
	return false;
}
 
Example 18
Source File: DeclarativeFunctionDrivers.java    From shacl with Apache License 2.0 5 votes vote down vote up
private DeclarativeFunctionDriver getDirectDriver(Resource spinFunction) {
	if(!spinFunction.hasProperty(SPIN_ABSTRACT, JenaDatatypes.TRUE) &&
		!spinFunction.hasProperty(DASH.abstract_, JenaDatatypes.TRUE)) {
		StmtIterator it = spinFunction.listProperties();
		while(it.hasNext()) {
			Statement s = it.next();
			final DeclarativeFunctionDriver driver = drivers.get(s.getPredicate());
			if(driver != null) {
				it.close();
				return driver;
			}
		}
	}
	return null;
}
 
Example 19
Source File: JenaUtil.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static Resource getResourcePropertyWithType(Resource subject, Property predicate, Resource type) {
	StmtIterator it = subject.listProperties(predicate);
	while(it.hasNext()) {
		Statement s = it.next();
		if(s.getObject().isResource() && JenaUtil.hasIndirectType(s.getResource(), type)) {
			it.close();
			return s.getResource();
		}
	}
	return null;
}