use of org.apache.jena.shacl.engine.constraint.SparqlComponent in project jena by apache.
the class ConstraintComponents method parseSparqlConstraintComponents.
/**
* Stage 1 : find all Constraint Components
*/
public static ConstraintComponents parseSparqlConstraintComponents(Graph shapesGraph) {
ConstraintComponents x = new ConstraintComponents();
// SHACL.SPARQLConstraintComponent is not a subclass of SHACL.ConstraintComponent
// SHACL.SPARQLConstraintComponent is an instance of SHACL.ConstraintComponent.
// Need to process by actual property present.
G.allNodesOfTypeRDFS(shapesGraph, SHACL.ConstraintComponent).forEach(sccNode -> {
SparqlComponent c = sparqlConstraintComponent(shapesGraph, sccNode);
if (c != null) {
for (Parameter p : c.getParams()) {
x.paramPathToComponents.put(p.getParameterPath(), c);
x.parameters.add(p);
}
}
});
return x;
}
use of org.apache.jena.shacl.engine.constraint.SparqlComponent in project jena by apache.
the class ConstraintComponents method sparqlConstraintComponent.
/**
* This handles all ConstraintComponents; only SPARQL ones ar currently supported.
*/
private static SparqlComponent sparqlConstraintComponent(Graph shapesGraph, Node constraintComponentNode) {
if (SHACL.JSConstraintComponent.equals(constraintComponentNode))
return null;
List<Parameter> params = Parameters.parseParameters(shapesGraph, constraintComponentNode);
// 6.2.3 Validators
//
// For every supported shape type (i.e., property shape or node shape) the constraint component
// declares a suitable validator. For a given constraint, a validator is selected from the
// constraint component using the following rules, in order:
//
// 1. For node shapes, use one of the values of sh:nodeValidator, if present.
// 2. For property shapes, use one of the values of sh:propertyValidator, if present.
// 3. Otherwise, use one of the values of sh:validator.
//
// If no suitable validator can be found, a SHACL-SPARQL processor ignores the constraint.
//
// SHACL-SPARQL includes two types of validators, based on SPARQL SELECT (for sh:nodeValidator
// and sh:propertyValidator) or SPARQL ASK queries (for sh:validator).
SparqlComponent x = possibleValidator(shapesGraph, constraintComponentNode, SHACL.nodeValidator, params);
if (x == null)
x = possibleValidator(shapesGraph, constraintComponentNode, SHACL.propertyValidator, params);
if (x == null)
x = possibleValidator(shapesGraph, constraintComponentNode, SHACL.validator, params);
return x;
}
use of org.apache.jena.shacl.engine.constraint.SparqlComponent in project jena by apache.
the class ConstraintComponents method possibleSparqlValidator.
private static SparqlComponent possibleSparqlValidator(Graph shapesGraph, Node valNode, List<Parameter> params, /* for reporting */
Node constraintComponentNode) {
// Check for SHACL-JS
Node xJSFunctionName = G.getZeroOrOneSP(shapesGraph, valNode, SHACL.jsFunctionName);
if (xJSFunctionName != null)
Log.warn(ConstraintComponents.class, "Found javascript validator - ignored (JavaScript not currently supported)");
// One of sh:select or sh:ask.
Node xSelect = G.getZeroOrOneSP(shapesGraph, valNode, SHACL.select);
Node xAsk = G.getZeroOrOneSP(shapesGraph, valNode, SHACL.ask);
if (xSelect == null && xAsk == null)
return null;
if (xSelect != null && xAsk != null)
throw new ShaclParseException("SparqlConstraintComponent: Multiple SPARQL queries: " + displayStr(constraintComponentNode));
String prefixes = ShLib.prefixes(shapesGraph, valNode);
String queryString = firstNonNull(xSelect, xAsk).getLiteralLexicalForm().trim();
String message = asString(G.getZeroOrOneSP(shapesGraph, valNode, SHACL.message));
if (!prefixes.isEmpty())
queryString = prefixes + "\n" + queryString;
boolean isSelect = (xSelect != null);
SparqlComponent cs = SparqlComponent.constraintComponent(constraintComponentNode, queryString, params, message);
if (cs.getQuery().isSelectType() != isSelect)
throw new ShaclParseException("Query type does not match property");
return cs;
}
use of org.apache.jena.shacl.engine.constraint.SparqlComponent in project jena by apache.
the class TargetExtensions method parseSPARQLTargetType.
// SPARQL-based target types.
public static TargetExtensions parseSPARQLTargetType(Graph shapesGraph) {
TargetExtensions x = new TargetExtensions();
G.allNodesOfTypeRDFS(shapesGraph, SHACL.SPARQLTargetType).forEach(sttNode -> {
SparqlComponent c = sparqlTargetType(shapesGraph, sttNode);
if (c != null) {
for (Parameter p : c.getParams()) {
x.paramPathToComponents.put(p.getParameterPath(), c);
x.parameters.add(p);
}
}
});
return x;
}
use of org.apache.jena.shacl.engine.constraint.SparqlComponent 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");
}
Aggregations