Java Code Examples for org.apache.jena.rdf.model.Resource#listProperties()

The following examples show how to use org.apache.jena.rdf.model.Resource#listProperties() . 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
/**
 * 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 2
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 3
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 4
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 5
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 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: DB2Closure.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
private static void closureNoTest(Resource r, Model closureBlob,
		Collection<Resource> visited, ClosureTest test, Collection<String> resources) {
	visited.add(r);
	String gid = ((DB2Graph) r.getModel().getGraph()).getGraphID();
	String key = null;
	if (r.isAnon()) {
		key = gid + ":" + r.getId();
	} else {
		key = gid + ":" + r.getURI();
	}
	if (resources.contains(key)) {
		return;
	}
	resources.add(key);

	StmtIterator sIter = r.listProperties();

	for (; sIter.hasNext();) {
		Statement stmt = sIter.nextStatement();
		closure(stmt, closureBlob, visited, test, resources);
	}
}
 
Example 8
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 9
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 10
Source File: Mapping.java    From FCA-Map with GNU General Public License v3.0 6 votes vote down vote up
public String listMappingCellSPO(MappingCell mc) {
  if (mc == null || m_source == null || m_target == null) return "null";

  StringBuilder sb = new StringBuilder();
  Resource r1 = m_source.getResource(mc.getEntity1());
  sb.append(String.format("%n<<<<<<<"));
  sb.append(String.format("%n* %s%n", r1.getURI()));
  for (StmtIterator stmt = r1.listProperties(); stmt.hasNext(); ) {
    sb.append(String.format("** %s%n", stmt.nextStatement()));
  }

  sb.append(String.format("======="));

  Resource r2 = m_target.getResource(mc.getEntity2());
  sb.append(String.format("%n* %s%n", r2.getURI()));
  for (StmtIterator stmt = r2.listProperties(); stmt.hasNext(); ) {
    sb.append(String.format("** %s%n", stmt.nextStatement()));
  }
  sb.append(String.format(">>>>>>>%n"));

  return sb.toString();
}
 
Example 11
Source File: JenaUtil.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static List<Resource> getResourceProperties(Resource subject, Property predicate) {
	List<Resource> results = new LinkedList<>();
	StmtIterator it = subject.listProperties(predicate);
	while(it.hasNext()) {
		Statement s = it.next();
		if(s.getObject().isResource()) {
			results.add(s.getResource());
		}
	}
	return results;
}
 
Example 12
Source File: JenaUtil.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static List<Resource> getURIResourceProperties(Resource subject, Property predicate) {
	List<Resource> results = new LinkedList<>();
	StmtIterator it = subject.listProperties(predicate);
	while(it.hasNext()) {
		Statement s = it.next();
		if(s.getObject().isURIResource()) {
			results.add(s.getResource());
		}
	}
	return results;
}
 
Example 13
Source File: LexicalMatcherAlphaImpl.java    From FCA-Map with GNU General Public License v3.0 5 votes vote down vote up
private static Set<String> acquireAllLiteralsLexicalFormsWith(Resource resource, Property property) {
  Set<String> s = new HashSet<>();
  for (StmtIterator it = resource.listProperties(property); it.hasNext(); ) {
    Statement stmt = it.nextStatement();
    RDFNode object = stmt.getObject();

    if (!object.isLiteral()) continue;

    String lb = object.asLiteral().getString();
    if (lb != null && !lb.isEmpty() && !lb.trim().equals("")) {
      s.add(lb);
    }
  }
  return s;
}
 
Example 14
Source File: JenaUtil.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static List<Literal> getLiteralProperties(Resource subject, Property predicate) {
	List<Literal> results = new LinkedList<>();
	StmtIterator it = subject.listProperties(predicate);
	while(it.hasNext()) {
		Statement s = it.next();
		if(s.getObject().isLiteral()) {
			results.add(s.getLiteral());
		}
	}
	return results;
}
 
Example 15
Source File: JenaUtil.java    From shacl with Apache License 2.0 5 votes vote down vote up
private static void listAllProperties(Resource subject, Property predicate, Set<Property> reached,
		List<Statement> results) {
	reached.add(predicate);
	StmtIterator sit;
	Model model;
	if (subject != null) {
		sit = subject.listProperties(predicate);
		model = subject.getModel();
	}
	else {
		model = predicate.getModel();
		sit = model.listStatements(null, predicate, (RDFNode)null);
	}
	while (sit.hasNext()) {
		results.add(sit.next());
	}

	// Iterate into direct subproperties
	StmtIterator it = model.listStatements(null, RDFS.subPropertyOf, predicate);
	while (it.hasNext()) {
		Statement sps = it.next();
		if (!reached.contains(sps.getSubject())) {
			Property subProperty = asProperty(sps.getSubject());
			listAllProperties(subject, subProperty, reached, results);
		}
	}
}
 
Example 16
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 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: RDFToTopicMapConverter.java    From ontopia with Apache License 2.0 5 votes vote down vote up
private void doConversion(Model model) throws JenaException {
  StatementHandler totm = new ToTMStatementHandler();
  AResourceWrapper subjw = new AResourceWrapper();
  AResourceWrapper propw = new AResourceWrapper();
  AResourceWrapper objtw = new AResourceWrapper();
  ALiteralWrapper litlw = new ALiteralWrapper();

  ResIterator it = model.listSubjects();
  while (it.hasNext()) {
    Resource subject = (Resource) it.next();

    StmtIterator it2 = subject.listProperties(); // get all statements
    while (it2.hasNext()) {
      Statement stmt = (Statement) it2.next();

      subjw.resource = stmt.getSubject();
      propw.resource = stmt.getPredicate();

      RDFNode obj = stmt.getObject();
      if (obj instanceof Resource) {
        objtw.resource = (Resource) obj;
        totm.statement(subjw, propw, objtw);
      } else {
        litlw.literal = (Literal) obj;
        totm.statement(subjw, propw, litlw);
      }
    }
  }
}
 
Example 19
Source File: AdditionalPropertyMatcherImpl.java    From FCA-Map with GNU General Public License v3.0 5 votes vote down vote up
private static final void deriveClassMappingsFromResource(Resource r, Map<Resource, Set<MappingCell>> m, Set<MappingCell> s) {
  for (StmtIterator it = r.listProperties(RDF.type); it.hasNext(); ) {
    Statement stmt = it.nextStatement();
    if (stmt.getObject().isResource()) {
      Set<MappingCell> v = m.get(stmt.getObject());
      if (v != null) {
        s.addAll(s);
      }
    }
  }
}
 
Example 20
Source File: JenaResource.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
private StatementIterator(Model model, Resource resource, String uri) {
  stmtIterator = resource.listProperties(model.createProperty(uri));
}