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

The following examples show how to use org.apache.jena.sparql.expr.NodeValue#makeNode() . 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: SHACLSPARQLARQFunction.java    From shacl with Apache License 2.0 6 votes vote down vote up
@Override
   public NodeValue executeBody(Dataset dataset, Model defaultModel, QuerySolution bindings) {
    try( QueryExecution qexec = createQueryExecution(dataset, defaultModel, bindings) ) {
        if(arqQuery.isAskType()) {
            boolean result = qexec.execAsk();
            return NodeValue.makeBoolean(result);
        }
        else {
            ResultSet rs = qexec.execSelect();
            if(rs.hasNext()) {
                QuerySolution s = rs.nextSolution();
                List<String> resultVars = rs.getResultVars();
                String varName = resultVars.get(0);
                RDFNode resultNode = s.get(varName);
                if(resultNode != null) {
                    return NodeValue.makeNode(resultNode.asNode());
                }
            }
            throw new ExprEvalException("Empty result set for SHACL function");
        }
    }
}
 
Example 3
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 4
Source File: ITER_JSONListKeys.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public List<List<NodeValue>> exec(NodeValue json) {
    if (json.getDatatypeURI() != null
            && !json.getDatatypeURI().equals(datatypeUri)
            && !json.getDatatypeURI().equals("http://www.w3.org/2001/XMLSchema#string")) {
        LOG.debug("The URI of NodeValue1 MUST have been"
                + " <" + datatypeUri + "> or"
                + " <http://www.w3.org/2001/XMLSchema#string>."
                + " Got <" + json.getDatatypeURI() + ">"
        );
    }
    try {
        Set<String> keys = GSON.fromJson(json.asNode().getLiteralLexicalForm(), Map.class).keySet();
        List<List<NodeValue>> listNodeValues = new ArrayList<>(keys.size());
        for (String key : keys) {
            NodeValue nodeValue
                    = NodeValue.makeNode(NodeFactory.createLiteral(key));
            listNodeValues.add(Collections.singletonList(nodeValue));
        }
        LOG.trace("end JSONListKeys");
        return listNodeValues;
    } catch (Exception ex) {
        if(LOG.isDebugEnabled()) {
            Node compressed = LogUtils.compress(json.asNode());
            LOG.debug("No evaluation for " + compressed, ex);
        }
        throw new ExprEvalException("No evaluation", ex);
    }
}
 
Example 5
Source File: TemplateUtils.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
public static E_Function getFunction(SPARQLExtQuery query) {
    if (query.isSubQuery() && query.getName() != null) {
        String qs = query.toString();
        SPARQLExtQuery query2 = (SPARQLExtQuery) ParserSPARQLExt.parseSubQuery(query, qs);
        query2.setFromClauses(query.getFromClauses());
        query2.setQuerySelectType();
        query2.setQueryResultStar(true);
        query2.setName(null);
        query2.setCallParameters(null);
        if (query2.getQueryPattern() == null) {
            query2.setQueryPattern(new ElementGroup());
        }
        
        SelectExtractionVisitor selectExtractionVisitor = new SelectExtractionVisitor(query2);
        query.visit(selectExtractionVisitor);
        SPARQLExtQuery query3 = selectExtractionVisitor.getOutput();
        
        NodeValue selectQuery = null;

        if(query3 != null) {
        	selectQuery = NodeValue.makeNode(query2.toString(), null, SPARQLExt.MEDIA_TYPE_URI);
        }

        ExprList exprList = new ExprList(selectQuery);
        exprList.add(query.getName());
        exprList.addAll(query.getCallParameters());
        return new E_Function(FUN_Select_Call_Template.URI, exprList);
    } else {
        NodeValue n = NodeValue.makeNode(query.toString(), null, SPARQLExt.MEDIA_TYPE_URI);
        return new E_Function(ST.callTemplate, new ExprList(n));
    }
}
 
Example 6
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 7
Source File: ExpandPrefixedNameFunction.java    From tarql with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public NodeValue exec(NodeValue name, Context context) {
	if (name == null) return null;
	if (!name.isString()) throw new ExprEvalException("Not a string: " + name);
	PrefixMapping prefixes = context.get(ExpandPrefixFunction.PREFIX_MAPPING);
	if (prefixes == null) throw new ExprEvalException("No prefix mapping registered");
	String pname = name.asString();
	int idx = pname.indexOf(':');
	if (idx == -1) throw new ExprEvalException("Not a prefixed name: " + name);
	String prefix = pname.substring(0, idx);
	String iri = prefixes.getNsPrefixURI(prefix);
	if (iri == null) throw new ExprEvalException("Prefix not defined: " + prefix);
	return NodeValue.makeNode(NodeFactory.createURI(iri + pname.substring(idx + 1)));
}