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

The following examples show how to use org.apache.jena.rdf.model.Resource#getProperty() . 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 8 votes vote down vote up
public static String getStringProperty(Resource subject, Property predicate) {
	Statement s = subject.getProperty(predicate);
	if(s != null && s.getObject().isLiteral()) {
		return s.getString();
	}
	else {
		return null;
	}
}
 
Example 2
Source File: SolidContactsExport.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
private static Statement getValueStatement(Resource r) {
  Statement valueStatement = r.getProperty(VCARD4.hasValue);
  if (valueStatement == null) {
    valueStatement = r.getProperty(VCARD4.value);
  }
  if (valueStatement == null) {
    throw new IllegalStateException("Couldn't find value property in: " + r);
  }
  return valueStatement;
}
 
Example 3
Source File: SystemTriples.java    From shacl with Apache License 2.0 5 votes vote down vote up
private static List<Resource> collectMissingSuperClasses(Resource metaClass,
		Resource superClass) {
	List<Resource> toAdd = new ArrayList<Resource>();
	StmtIterator it = vocabulary.listStatements(null, RDF.type, metaClass);
	while (it.hasNext()) {
		Resource c = it.nextStatement().getSubject();
		if (!c.equals(superClass)) {
			if (c.getProperty(RDFS.subClassOf) == null) {
				toAdd.add(c);
			}
		}
	}
	return toAdd;
}
 
Example 4
Source File: JenaUtil.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static Integer getIntegerProperty(Resource subject, Property predicate) {
	Statement s = subject.getProperty(predicate);
	if(s != null && s.getObject().isLiteral()) {
		return s.getInt();
	}
	else {
		return null;
	}
}
 
Example 5
Source File: JenaUtil.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static RDFList getListProperty(Resource subject, Property predicate) {
	Statement s = subject.getProperty(predicate);
	if(s != null && s.getObject().canAs(RDFList.class)) {
		return s.getResource().as(RDFList.class);
	}
	else {
		return null;
	}
}
 
Example 6
Source File: JenaUtil.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static RDFNode getProperty(Resource subject, Property predicate) {
	Statement s = subject.getProperty(predicate);
	if(s != null) {
		return s.getObject();
	}
	else {
		return null;
	}
}
 
Example 7
Source File: JenaUtil.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static Resource getURIResourceProperty(Resource subject, Property predicate) {
	Statement s = subject.getProperty(predicate);
	if(s != null && s.getObject().isURIResource()) {
		return s.getResource();
	}
	else {
		return null;
	}
}
 
Example 8
Source File: JenaUtil.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static boolean getBooleanProperty(Resource subject, Property predicate) {
	Statement s = subject.getProperty(predicate);
	if(s != null && s.getObject().isLiteral()) {
		return s.getBoolean();
	}
	else {
		return false;
	}
}
 
Example 9
Source File: JenaUtil.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static Double getDoubleProperty(Resource subject, Property predicate) {
	Statement s = subject.getProperty(predicate);
	if(s != null && s.getObject().isLiteral()) {
		return s.getDouble();
	}
	else {
		return null;
	}
}
 
Example 10
Source File: SHACLCWriter.java    From shacl with Apache License 2.0 5 votes vote down vote up
private void writeProperty(IndentedWriter out, Resource property) {
	out.print(getPathString(property));
	out.print(" ");
	
	out.print(getPropertyTypes(property));
	
	// Count block
	out.print(" ");
	out.print("[");
	Statement minCountS = property.getProperty(SH.minCount);
	if(minCountS != null) {
		out.print("" + minCountS.getInt());
	}
	else {
		out.print("0");
	}
	out.print("..");
	Statement maxCountS = property.getProperty(SH.maxCount);
	if(maxCountS != null) {
		out.print("" + maxCountS.getInt());
	}
	else {
		out.print("*");
	}
	out.print("]");
	
	writeExtraStatements(out, property, specialPropertyProperties, false);
	
	writeNestedShapes(out, property);
	
	out.println(" .");
}
 
Example 11
Source File: TripleRule.java    From shacl with Apache License 2.0 5 votes vote down vote up
private NodeExpression createNodeExpression(Resource resource, Property predicate) {
	Statement s = resource.getProperty(predicate);
	if(s == null) {
		throw new IllegalArgumentException("Triple rule without " + predicate.getLocalName());
	}
	return NodeExpressionFactory.get().create(s.getObject());
}
 
Example 12
Source File: AbstractRule.java    From shacl with Apache License 2.0 5 votes vote down vote up
protected AbstractRule(Resource resource) {
	this.resource = resource;
	order = 0;
	Statement s = resource.getProperty(SH.order);
	if(s != null && s.getObject().isLiteral()) {
		order = (Number) s.getLiteral().getValue();
	}
}
 
Example 13
Source File: OrderComparator.java    From shacl with Apache License 2.0 5 votes vote down vote up
private Double getOrder(Resource subject) {
	Statement s = subject.getProperty(SH.order);
	if(s != null && s.getObject().isLiteral() && s.getLiteral().getValue() instanceof Number) {
		return ((Number)s.getLiteral().getValue()).doubleValue();
	}
	return 0.0;
}
 
Example 14
Source File: SystemTriples.java    From shacl with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the system ontology (a shared copy).
 * @return the system ontology
 */
public static synchronized Model getVocabularyModel() {
	if (vocabulary == null) {
		vocabulary = JenaUtil.createDefaultModel();
		org.topbraid.jenax.util.JenaUtil.initNamespaces(vocabulary.getGraph());
		vocabulary.setNsPrefix("xsd", XSD.getURI());
		InputStream ttl = SystemTriples.class.getResourceAsStream("/rdf/system-triples.ttl");
		vocabulary.read(ttl, "urn:x:dummy", FileUtils.langTurtle);
		ensureSuperClasses(RDFS.Class, RDFS.Resource);
		ensureSuperClasses(OWL.Class, OWL.Thing);
		
		// Remove owl imports rdfs which only causes trouble
		vocabulary.removeAll(null, OWL.imports, null); 
		
		vocabulary.add(OWL.Thing, RDFS.subClassOf, RDFS.Resource);
		vocabulary.add(OWL.inverseOf, RDF.type, OWL.SymmetricProperty);
		vocabulary.add(OWL.equivalentClass, RDF.type, OWL.SymmetricProperty);
		vocabulary.add(OWL.equivalentProperty, RDF.type, OWL.SymmetricProperty);
		vocabulary.add(OWL.equivalentProperty, RDFS.range, RDF.Property);
		vocabulary.add(OWL.differentFrom, RDF.type, OWL.SymmetricProperty);
		vocabulary.add(OWL.sameAs, RDF.type, OWL.SymmetricProperty);
		vocabulary.add(OWL.disjointWith, RDF.type, OWL.SymmetricProperty);
		Resource xml = vocabulary.getResource(XMLLiteralType.theXMLLiteralType.getURI());
		vocabulary.add(xml, RDFS.subClassOf, RDFS.Resource);
		for(String uri : JenaDatatypes.getDatatypeURIs()) {
			Resource r = vocabulary.getResource(uri);
			if (r.getProperty(RDF.type) == null) {
				vocabulary.add(r, RDF.type, RDFS.Datatype);
				vocabulary.add(r, RDFS.subClassOf, RDFS.Literal);
			}
		}
		
		vocabulary.add(RDF.HTML, RDFS.label, "HTML");
		
		// Triples were formally in OWL 1, but dropped from OWL 2
		vocabulary.add(RDFS.comment, RDF.type, OWL.AnnotationProperty);
		vocabulary.add(RDFS.label, RDF.type, OWL.AnnotationProperty);
		vocabulary.add(RDFS.isDefinedBy, RDF.type, OWL.AnnotationProperty);
		vocabulary.add(RDFS.seeAlso, RDF.type, OWL.AnnotationProperty);
		
		// Add rdfs:labels for XSD types
		for(Resource datatype : vocabulary.listSubjectsWithProperty(RDF.type, RDFS.Datatype).toList()) {
			datatype.addProperty(RDFS.label, datatype.getLocalName());
           }
		vocabulary = JenaUtil.asReadOnlyModel(vocabulary);
	}
	return vocabulary;
}
 
Example 15
Source File: FunctionTestCaseType.java    From shacl with Apache License 2.0 4 votes vote down vote up
@Override
public void run(Model results) {
	Resource testCase = getResource();
	
	FunctionRegistry oldFR = FunctionRegistry.get();
	CurrentThreadFunctionRegistry threadFR = new CurrentThreadFunctionRegistry(oldFR);
	FunctionRegistry.set(ARQ.getContext(), threadFR);
	CurrentThreadFunctions old = CurrentThreadFunctionRegistry.register(testCase.getModel());

	try {
		for(TestCaseContextFactory contextFactory : contextFactories) {
			TestCaseContext context = contextFactory.createContext();
			String expression = JenaUtil.getStringProperty(testCase, DASH.expression);
			Statement expectedResultS = testCase.getProperty(DASH.expectedResult);
			String queryString = "SELECT (" + expression + " AS ?result) WHERE {}";
			Query query = ARQFactory.get().createQuery(testCase.getModel(), queryString);
			context.setUpTestContext();
			try(QueryExecution qexec = ARQFactory.get().createQueryExecution(query, testCase.getModel())) {
			    ResultSet rs = qexec.execSelect();
			    if(!rs.hasNext()) {
			        if(expectedResultS != null) {
			            createFailure(results,
			                          "Expression returned no result, but expected: " + expectedResultS.getObject(),
			                          context);
			            return;
			        }
			    }
			    else {
			        RDFNode actual = rs.next().get("result");
			        if(expectedResultS == null) {
			            if(actual != null) {
			                createFailure(results,
			                              "Expression returned a result, but none expected: " + actual, context);
			                return;
			            }
			        }
			        else if(testCase.hasProperty(DASH.expectedResultIsTTL, JenaDatatypes.TRUE)) {
			        	Graph expectedGraph = parseGraph(expectedResultS.getObject());
			        	Graph actualGraph = parseGraph(actual);
			        	if(!expectedGraph.isIsomorphicWith(actualGraph)) {
				            createFailure(results,
			                          "Mismatching result graphs. Expected: " + expectedResultS.getObject() + ". Found: " + actual, context);
				            return;
			        	}
			        }
			        else if(!expectedResultS.getObject().equals(actual)) {
			            createFailure(results,
			                          "Mismatching result. Expected: " + expectedResultS.getObject() + ". Found: " + actual, context);
			            return;
			        }
			    }
			}
			finally {
				context.tearDownTestContext();
			}
		}
	}
	finally {
		CurrentThreadFunctionRegistry.unregister(old);
		FunctionRegistry.set(ARQ.getContext(), oldFR);
	}
	
	createResult(results, DASH.SuccessTestCaseResult);
}
 
Example 16
Source File: JSTestCaseType.java    From shacl with Apache License 2.0 4 votes vote down vote up
@Override
public void run(Model results) {
	Resource testCase = getResource();
	
	FunctionRegistry oldFR = FunctionRegistry.get();
	CurrentThreadFunctionRegistry threadFR = new CurrentThreadFunctionRegistry(oldFR);
	FunctionRegistry.set(ARQ.getContext(), threadFR);

	CurrentThreadFunctions old = CurrentThreadFunctionRegistry.register(testCase.getModel());
	Statement expectedResultS = testCase.getProperty(DASH.expectedResult);
	String queryString = "SELECT (<" + getResource() + ">() AS ?result) WHERE {}";
	Query query = ARQFactory.get().createQuery(testCase.getModel(), queryString);
	try(QueryExecution qexec = ARQFactory.get().createQueryExecution(query, testCase.getModel())) {
	    ResultSet rs = qexec.execSelect();
	    if(!rs.hasNext()) {
	        if(expectedResultS != null) {
	            createFailure(results,
	                          "Expression returned no result, but expected: " + expectedResultS.getObject());
	            return;
	        }
	    }
	    else {
	        RDFNode result = rs.next().get("result");
	        if(expectedResultS == null) {
	            if(result != null) {
	                createFailure(results,
	                              "Expression returned a result, but none expected: " + result);
	                return;
	            }
	        }
	        else if(!expectedResultS.getObject().equals(result)) {
	            createFailure(results,
	                          "Mismatching result. Expected: " + expectedResultS.getObject() + ". Found: " + result);
	            return;
	        }
	    }
	}
	finally {
		CurrentThreadFunctionRegistry.unregister(old);
		FunctionRegistry.set(ARQ.getContext(), oldFR);
	}
	
	createResult(results, DASH.SuccessTestCaseResult);
}