Search in sources :

Example 1 with Parameter

use of org.apache.jena.shacl.engine.Parameter 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;
}
Also used : SparqlComponent(org.apache.jena.shacl.engine.constraint.SparqlComponent) Parameter(org.apache.jena.shacl.engine.Parameter)

Example 2 with Parameter

use of org.apache.jena.shacl.engine.Parameter 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;
}
Also used : SparqlComponent(org.apache.jena.shacl.engine.constraint.SparqlComponent) Parameter(org.apache.jena.shacl.engine.Parameter)

Example 3 with Parameter

use of org.apache.jena.shacl.engine.Parameter 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;
}
Also used : SparqlComponent(org.apache.jena.shacl.engine.constraint.SparqlComponent) Parameter(org.apache.jena.shacl.engine.Parameter)

Example 4 with Parameter

use of org.apache.jena.shacl.engine.Parameter in project jena by apache.

the class Parameters method parseParameter.

public static Parameter parseParameter(Graph shapesGraph, Node sccNode, Node parameterNode) {
    Node path = G.getZeroOrOneSP(shapesGraph, parameterNode, SHACL.path);
    if (!path.isURI())
        throw new ShaclParseException("SparqlConstraintComponent: Not a URI for parameter name: " + path);
    String sparqlName = path.getLocalName();
    // Parameter constraints
    boolean isOptional = G.contains(shapesGraph, parameterNode, SHACL.optional, TRUE);
    // "all properties that are applicable to property shapes may also be used for parameters."
    List<Constraint> constraints = null;
    return new Parameter(path, sparqlName, isOptional, constraints);
}
Also used : Node(org.apache.jena.graph.Node) Parameter(org.apache.jena.shacl.engine.Parameter)

Example 5 with Parameter

use of org.apache.jena.shacl.engine.Parameter 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;
}
Also used : Node(org.apache.jena.graph.Node) SparqlComponent(org.apache.jena.shacl.engine.constraint.SparqlComponent) ConstraintComponentSPARQL(org.apache.jena.shacl.engine.constraint.ConstraintComponentSPARQL) Parameter(org.apache.jena.shacl.engine.Parameter)

Aggregations

Parameter (org.apache.jena.shacl.engine.Parameter)6 SparqlComponent (org.apache.jena.shacl.engine.constraint.SparqlComponent)4 Node (org.apache.jena.graph.Node)3 ConstraintComponentSPARQL (org.apache.jena.shacl.engine.constraint.ConstraintComponentSPARQL)1