Java Code Examples for org.apache.jena.sparql.expr.NodeValue#compare()

The following examples show how to use org.apache.jena.sparql.expr.NodeValue#compare() . 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: AbstractClusiveConstraintExecutor.java    From shacl with Apache License 2.0 6 votes vote down vote up
@Override
public void executeConstraint(Constraint constraint, ValidationEngine engine, Collection<RDFNode> focusNodes) {
	long startTime = System.currentTimeMillis();
	NodeValue cmpValue = NodeValue.makeNode(constraint.getParameterValue().asNode());
	for(RDFNode focusNode : focusNodes) {
		for(RDFNode valueNode : engine.getValueNodes(constraint, focusNode)) {				
			try {
		        NodeValue value = NodeValue.makeNode(valueNode.asNode());
				int c = NodeValue.compare(cmpValue, value);
				if (c == Expr.CMP_INDETERMINATE) {
					engine.createValidationResult(constraint, focusNode, valueNode, () -> "Indeterminant comparison with " + engine.getLabelFunction().apply(constraint.getParameterValue())); 
				}
				else if(!condition.test(c)) {
					engine.createValidationResult(constraint, focusNode, valueNode, () -> "Value is not " + operator + " " + engine.getLabelFunction().apply(constraint.getParameterValue())); 
				}
			}
			catch (ExprNotComparableException ex) {
				engine.createValidationResult(constraint, focusNode, valueNode, () -> "Cannot compare with " + engine.getLabelFunction().apply(constraint.getParameterValue())); 
			}
		}
		engine.checkCanceled();
	}
	addStatistics(constraint, startTime);
}
 
Example 2
Source File: AbstractLessThanConstraintExecutor.java    From shacl with Apache License 2.0 5 votes vote down vote up
@Override
public void executeConstraint(Constraint constraint, ValidationEngine engine, Collection<RDFNode> focusNodes) {
	long startTime = System.currentTimeMillis();
	Property predicate = constraint.getParameterValue().as(Property.class);
	for(RDFNode focusNode : focusNodes) {
		if(focusNode instanceof Resource) {
			Collection<RDFNode> valueNodes = engine.getValueNodes(constraint, focusNode);
			Set<RDFNode> otherNodes = ((Resource)focusNode).listProperties(predicate).mapWith(s -> s.getObject()).toSet();
			for(RDFNode valueNode : valueNodes) {
				NodeValue v = NodeValue.makeNode(valueNode.asNode());
		        for(RDFNode otherNode : otherNodes) {
		        	NodeValue o = NodeValue.makeNode(otherNode.asNode());
		        	try {
		        		int cmp = NodeValue.compare(v, o);
		        		if(test.test(cmp)) {
			        		engine.createValidationResult(constraint, focusNode, valueNode, () -> "Value is not " + operator + " " + engine.getLabelFunction().apply(otherNode));			        			
		        		}
		        	} 
		        	catch (ExprNotComparableException ex) {
		        		engine.createValidationResult(constraint, focusNode, valueNode, () -> "Cannot compare with " + engine.getLabelFunction().apply(otherNode));
		        	}
				}
			}
		}
		engine.checkCanceled();
	}
	addStatistics(constraint, startTime);
}
 
Example 3
Source File: SHACLObject.java    From shacl with Apache License 2.0 5 votes vote down vote up
public Integer compareNodes(JSTerm node1, JSTerm node2) {
	try {
		if(node1.isURI() && node2.isURI()) {
			return node1.getUri().compareTo(node2.getUri());
		}
		else {
			return NodeValue.compare(NodeValue.makeNode(node1.getNode()), NodeValue.makeNode(node2.getNode()));
		}
	}
	catch(ExprNotComparableException ex) {
		return null;
	}
}