use of org.apache.jena.shacl.engine.Target in project jena by apache.
the class ShapesParser method parseProcess.
/* The parsing process.
* <p>
* Applications should call functions in {@link Shapes} rather than call the parser directly.
*/
public static ParserResult parseProcess(Graph shapesGraph, Collection<Node> declaredNodes) {
Targets targets = ShapesParser.targets(shapesGraph);
ConstraintComponents sparqlConstraintComponents = ConstraintComponents.parseSparqlConstraintComponents(shapesGraph);
TargetExtensions targetExtensions = TargetExtensions.parseSPARQLTargetType(shapesGraph);
// Parse from the targets.
Map<Node, Shape> shapesMap = ShapesParser.parseShapes(shapesGraph, targets, sparqlConstraintComponents, targetExtensions);
// Root shapes, after parsing from the targets.
Collection<Shape> rootShapes = shapesMap.values().stream().filter(sh -> sh.hasTarget()).collect(Collectors.toUnmodifiableList());
// Parse other shapes - i.e. addressable node and property shapes without
// target if they were not reached when parsing form the targets.
// This skips declared+targets because the shapesMap is in common.
Collection<Shape> declShapes = new ArrayList<>();
declaredNodes.forEach(shapeNode -> {
if (!shapesMap.containsKey(shapeNode)) {
Shape shape = ShapesParser.parseShape(shapesMap, shapesGraph, shapeNode);
declShapes.add(shape);
}
});
// rootShapes and declShapes are disjoint.
if (true) {
rootShapes.forEach(sh -> {
if (declShapes.contains(sh))
System.err.println("Shape in both: " + sh);
if (!sh.hasTarget())
System.err.println("Root shape with no target: " + sh);
});
declShapes.forEach(sh -> {
if (sh.hasTarget())
System.err.println("Declared shape with target: " + sh);
});
}
// Extract base and imports.
Pair<Node, List<Node>> pair = Imports.baseAndImports(shapesGraph);
Node shapesBase = pair.getLeft();
List<Node> imports = pair.getRight();
ParserResult x = new ParserResult(shapesBase, imports, shapesMap, targets, rootShapes, declShapes, sparqlConstraintComponents, targetExtensions);
return x;
}
use of org.apache.jena.shacl.engine.Target in project jena by apache.
the class ShapesParser method accTarget.
private static void accTarget(Collection<Target> acc, Graph shapesGraph, Node shape, TargetType targetType) {
ExtendedIterator<Triple> iter = shapesGraph.find(shape, targetType.predicate, null);
Graph graph;
switch(targetType) {
// Java 14 has "->"
case targetExtension:
graph = shapesGraph;
break;
// These do not need access to the shapes graph so don't force holding on the the graph reference.
case implicitClass:
case targetClass:
case targetNode:
case targetObjectsOf:
case targetSubjectsOf:
default:
graph = null;
break;
}
try {
iter.mapWith(triple -> Target.create(triple.getSubject(), targetType, triple.getObject(), graph)).forEachRemaining(target -> acc.add(target));
} finally {
iter.close();
}
}
use of org.apache.jena.shacl.engine.Target in project jena by apache.
the class ShapesParser method parseShape$.
private static Shape parseShape$(Set<Node> traversed, Map<Node, Shape> parsed, Graph shapesGraph, Node shapeNode) {
if (DEBUG)
OUT.printf("Parse shape : %s\n", displayStr(shapeNode));
boolean isDeactivated = contains(shapesGraph, shapeNode, SHACL.deactivated, NodeConst.nodeTrue);
Collection<Target> targets = targets(shapesGraph, shapeNode);
List<Constraint> constraints = Constraints.parseConstraints(shapesGraph, shapeNode, parsed, traversed);
Severity severity = severity(shapesGraph, shapeNode);
List<Node> messages = listSP(shapesGraph, shapeNode, SHACL.message);
if (DEBUG)
OUT.incIndent();
// sh:Property PropertyShapes from this shape.
// sh:node is treated as a constraint.
List<PropertyShape> propertyShapes = findPropertyShapes(traversed, parsed, shapesGraph, shapeNode);
if (DEBUG)
OUT.decIndent();
boolean isPropertyShape = contains(shapesGraph, shapeNode, SHACL.path, Node.ANY);
if (!isPropertyShape) {
if (DEBUG)
OUT.printf("Node shape %s\n", displayStr(shapeNode));
return new NodeShape(shapesGraph, shapeNode, isDeactivated, severity, messages, targets, constraints, propertyShapes);
}
if (DEBUG)
OUT.incIndent();
Node pathNode = getOneSP(shapesGraph, shapeNode, SHACL.path);
Path path = parsePath(shapesGraph, pathNode);
if (DEBUG)
OUT.printf("Property shape: path = %s\n", pathToString(shapesGraph, path));
// 2.3.2 Non-Validating Property Shape Characteristics
// 2.3.2.1 sh:name and sh:description
// 2.3.2.2 sh:order
// 2.3.2.3 sh:group
// 2.3.2.4 sh:defaultValue
// sh:order and sh:defaultValue - unique.
List<Node> names = listSP(shapesGraph, shapeNode, SHACL.name);
List<Node> descriptions = listSP(shapesGraph, shapeNode, SHACL.description);
List<Node> groups = listSP(shapesGraph, shapeNode, SHACL.group);
Node defaultValue = G.getZeroOrOneSP(shapesGraph, shapeNode, SHACL.defaultValue);
// The values of sh:order are decimals. "maybe used on any type of subject" but section is "property shapes".
// We allow more than decimals.
Node order = G.getZeroOrOneSP(shapesGraph, shapeNode, SHACL.order);
if (order != null && !isDecimalCompatible(order))
throw new ShaclParseException("Not an xsd:decimal for sh:order");
if (DEBUG) {
OUT.printf("Property shape %s\n", displayStr(shapeNode));
OUT.decIndent();
}
return new PropertyShape(shapesGraph, shapeNode, isDeactivated, severity, messages, targets, path, constraints, propertyShapes);
}
use of org.apache.jena.shacl.engine.Target in project jena by apache.
the class CompactWriter method output.
public static void output(IndentedWriter out, NodeFormatter nodeFmt, ShapeOutputVisitor visitor, Shape sh) {
List<Target> targetImplicitClasses = sh.getTargets().stream().filter(t -> t.getTargetType() == TargetType.implicitClass).collect(Collectors.toList());
List<Target> targetClasses = sh.getTargets().stream().filter(t -> t.getTargetType() == TargetType.targetClass).collect(Collectors.toList());
if (targetImplicitClasses.isEmpty()) {
out.print("shape ");
nodeFmt.format(out, sh.getShapeNode());
if (!targetClasses.isEmpty()) {
out.print(" ->");
targetClasses.forEach(t -> {
out.print(" ");
nodeFmt.format(out, t.getObject());
});
}
} else {
if (targetImplicitClasses.size() > 1)
CompactWriter.notShaclc("Multiple implicit classes");
if (!targetClasses.isEmpty())
CompactWriter.notShaclc("Implicit classes and targetClass");
Target target = targetImplicitClasses.get(0);
out.print("shapeClass ");
nodeFmt.format(out, target.getObject());
}
out.println(" {");
out.incIndent();
sh.visit(visitor);
out.decIndent();
out.ensureStartOfLine();
out.println("}");
}
Aggregations