use of org.apache.jena.shacl.ShaclException in project jena by apache.
the class EvalSparql method evalSparqlComponent.
public static Collection<Node> evalSparqlComponent(Graph data, Node node, SparqlComponent sparqlComponent) {
checkForRequiredParams(data, node, sparqlComponent);
Query query = sparqlComponent.getQuery();
if (!query.isSelectType())
throw new ShaclException("Not a SELECT query");
// Parameters
if (USE_QueryTransformOps) {
// Done with QueryTransformOps.transform
DatasetGraph dsg = DatasetGraphFactory.wrap(data);
Map<Var, Node> substitutions = parametersToSyntaxSubstitutions(data, node, sparqlComponent.getParams());
Query query2 = QueryTransformOps.transform(query, substitutions);
try (QueryExecution qExec = QueryExecutionFactory.create(query2, dsg)) {
return evalSparqlOneVar(qExec);
}
} else {
// Done with pre-binding.
Model model = ModelFactory.createModelForGraph(data);
QuerySolutionMap qsm = parametersToPreBinding(model, node, sparqlComponent.getParams());
try (QueryExecution qExec = QueryExecution.create().query(query).model(model).initialBinding(qsm).build()) {
return evalSparqlOneVar(qExec);
}
}
}
use of org.apache.jena.shacl.ShaclException in project jena by apache.
the class TargetOps method focusTargetExt.
public static Collection<Node> focusTargetExt(Graph data, Target target) {
Graph shapesGraph = Objects.requireNonNull(target.getShapesGraph());
Node targetArg = target.getObject();
if (G.hasOneSP(shapesGraph, targetArg, SHACL.select)) {
// SPARQL-based target -- sh:target [ sh:select ]
Query query = ShLib.extractSPARQLQuery(shapesGraph, targetArg);
if (!query.isSelectType())
throw new ShaclException("Not a SELECT query");
DatasetGraph dsg = DatasetGraphFactory.wrap(data);
QueryExecution qExec = QueryExecutionFactory.create(query, dsg);
return EvalSparql.evalSparqlOneVar(qExec);
}
if (G.isOfType(shapesGraph, targetArg, SHACL.Target)) {
// Now find the type.
Node type;
List<Node> types = G.typesOfNodeAsList(shapesGraph, targetArg);
if (types.size() == 1)
// It passed the G.isOfType test.
type = CollectionUtils.oneElt(types);
else {
Set<Node> allClasses = G.subClasses(shapesGraph, SHACL.Target);
// Find any(first) in allClasses
Optional<Node> x = types.stream().filter(t -> allClasses.contains(t)).findFirst();
type = x.orElseThrow();
}
try {
// This is also available via the Shapes object.
// Maybe attach to the target as it is created.
// But this is at the point of deciding focus nodes so called
// one (per target shape) not every validation of a focus node.
SparqlComponent sparqlComponent = TargetExtensions.sparqlTargetType(shapesGraph, type);
return EvalSparql.evalSparqlComponent(data, target.getObject(), sparqlComponent);
} catch (Exception ex) {
ex.printStackTrace();
}
}
throw new ShaclException("Unknown target extension");
}
use of org.apache.jena.shacl.ShaclException in project jena by apache.
the class EvalSparql method evalSparqlOneVar.
public static Collection<Node> evalSparqlOneVar(QueryExecution qExec) {
List<Var> vars = qExec.getQuery().getProjectVars();
if (vars.size() != 1)
throw new ShaclException("Except SELECT query with one output variable.");
Var var = vars.get(0);
return Iter.iter(qExec.execSelect()).map(row -> row.get(var.getVarName()).asNode()).toSet();
}
use of org.apache.jena.shacl.ShaclException in project jena by apache.
the class ShaclCompactParser method rPropertyShape.
// propertyShape: Using a new blank node ?property, produce a triple ?shape
// sh:property ?property. Produce a triple ?property sh:path ?path where
// ?path is the result of path. Use ?property as context shape for
// propertyCount and propertyOr.
protected void rPropertyShape(Path parsedPath) {
Node path = pathToNode(parsedPath);
Node b = newBlankNode("propertyShape");
beginPropertyShape(b);
triple(currentTripleAcc(), currentNodeShape(), SHACL.property, currentPropertyShape());
if (path == null)
throw new ShaclException("Internal error: no path");
triple(currentTripleAcc(), currentPropertyShape(), SHACL.path, path);
}
Aggregations