use of org.apache.jena.shacl.engine.constraint.SparqlComponent in project jena by apache.
the class ConstraintComponents method processShape.
/**
* Stage 2 :
* Build all the Constraints for Validators for this shape.
* Must have all the required parameters (else ignore).
*/
public static List<Constraint> processShape(Graph shapesGraph, ConstraintComponents components, Shape shape) {
List<Constraint> constraints = new ArrayList<>();
// Only add set of requires once, not once per parameter.
Set<Set<Node>> seen = new HashSet<>();
for (Parameter param : components.parameters) {
if (param.isOptional())
continue;
// The parameter's property node
Node paramPath = param.getParameterPath();
// Does the shape use the parameter?
boolean b = G.contains(shapesGraph, shape.getShapeNode(), paramPath, null);
if (!b)
continue;
// Alternative approach
// // Find all shapes uses the parameter.
// List<Node> shapes = G1.find(shapesGraph, null, paramPath, null).mapWith(Triple::getSubject).toList();
Node shNode = shape.getShapeNode();
// All components with this parameter.
Collection<SparqlComponent> sccs = components.paramPathToComponents.get(paramPath);
// Which shapes conform (have all required parameters)?
// And we have not seen before for this shape?
sccs.forEach(scc -> {
List<Node> required = scc.getRequiredParameters();
// Check not seen.
Set<Node> x = Set.copyOf(required);
if (seen.contains(x)) {
return;
}
seen.add(x);
if (Parameters.doesShapeHaveAllParameters(shapesGraph, shape.getShapeNode(), required)) {
// shape -> parameters
// Is this a cross product and can it be avoided?
Multimap<Parameter, Node> parameterValues = constraintParameterValues(shapesGraph, shNode, scc);
if (parameterValues.keySet().size() > 1) {
Map<Parameter, Node> parameterMap = new HashMap<>();
parameterValues.asMap().forEach((p, values) -> {
if (values.size() > 1)
throw new ShaclParseException("Multiple values for parameter " + p + " in constraint with multiple parameters");
});
}
ConstraintComponentSPARQL constraintComponentSPARQL = new ConstraintComponentSPARQL(scc, parameterValues);
constraints.add(constraintComponentSPARQL);
} else {
// Incomplete.
// System.out.println("shape: no: "+shNode);
}
});
}
return constraints;
}
Aggregations