Skip to content

Commit

Permalink
apacheGH-2851: Print shape characteristics
Browse files Browse the repository at this point in the history
  • Loading branch information
afs committed Jan 1, 2025
1 parent 9e94299 commit 8a262bf
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ private void padInternal() {

/** Get row/line (counts from 1) */
public int getRow() { return row; }

/** Get the absolute column.
* This is the location where the next character on the line will be printed.
* The IndentedWriter may not yet have padded to this place.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private static void writeOneShapeCompact(IndentedWriter out, NodeFormatter nodeF

/** Write in compact syntax or skip, noting the fact in a comment */
private static void writeOneShapeCompactOrSkip(IndentedWriter out, NodeFormatter nodeFmt, ShapeOutputVisitor visitor, Shape sh) {
// Write a shape is we can, else comment.
// Write a shape if we can, else comment.
try {
try ( IndentedLineBuffer out2 = new IndentedLineBuffer() ) {
// Need new visitor to hold the IndentedLineBuffer
Expand Down Expand Up @@ -179,7 +179,7 @@ public static void output(IndentedWriter out, NodeFormatter nodeFmt, ShapeOutput
*/
public static void output(IndentedWriter out, NodeFormatter nodeFmt, Shape sh) {
// If this were critical for performance, having a "serialization context"
// with out, nodeFmt and prefixes" would be better. But this is the only place the
// with "out", "nodeFmt" and "prefixes" would be better.
PrefixMapping prefixMappingWithStd = SHACLC.withStandardPrefixes(sh.getShapeGraph().getPrefixMapping());
ShapeOutputVisitor visitor = new ShapeOutputVisitor(prefixMappingWithStd, nodeFmt, out);
sh.visit(visitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@

package org.apache.jena.shacl.compact.writer;

import static org.apache.jena.shacl.compact.writer.CompactOut.*;
import static org.apache.jena.shacl.compact.writer.CompactOut.compact;
import static org.apache.jena.shacl.compact.writer.CompactOut.compactUnquotedString;

import org.apache.jena.atlas.io.IndentedWriter;
import org.apache.jena.graph.Node;
import org.apache.jena.riot.out.NodeFormatter;
import org.apache.jena.shacl.engine.ShaclPaths;
import org.apache.jena.shacl.engine.Target;
import org.apache.jena.shacl.engine.constraint.MaxCount;
import org.apache.jena.shacl.engine.constraint.MinCount;
import org.apache.jena.shacl.parser.*;
Expand All @@ -47,25 +50,29 @@ private ShapeOutputVisitor(NodeFormatter nodeFmt, IndentedWriter out, org.apache
this.prologue = prologue;
}

/** New ShapeOutputVisitor, using the same setup but with a different {@link IndentedWriter} */
/** New ShapeOutputVisitor, using the same setup */
public ShapeOutputVisitor fork(IndentedWriter out) {
return new ShapeOutputVisitor(this.nodeFmt, out, this.prologue);
}

@Override
public void visit(NodeShape nodeShape) {
printShape(nodeShape);
printTargets(nodeShape);
boolean printingStarted = printTargets(nodeShape);
// nodeParams, not constraints : 'deactivated' | 'severity' | 'message' | (name) | (description)
printShapeParams(nodeShape, true, printingStarted);
nodeShape.getConstraints().forEach(c->nodeConstraint(c));
nodeShape.getPropertyShapes().forEach(this::outputPropertyShape);
}

@Override
public void visit(PropertyShape propertyShape) {
printShape(propertyShape);
boolean outputStarted = false;
// Any nodeParam constraint?
Path path = propertyShape.getPath();
ShaclPaths.write(out, path, prologue);
outputStarted = true;

printShapeParams(propertyShape, false, outputStarted);

int minCount = -1 ;
int maxCount = -1;
Expand Down Expand Up @@ -108,32 +115,52 @@ public void visit(PropertyShape propertyShape) {
propertyShape.getPropertyShapes().forEach(this::outputPropertyShape);
}

private void printShape(Shape shape) {
if ( shape.deactivated() ) {
compactUnquotedString(out, "deactivated", "true");

private void paramPrinter(boolean forNodeShape, boolean outputStarted, Runnable action) {
if ( outputStarted )
out.println(" ");
action.run();
if ( forNodeShape )
out.println(" .");
}

private boolean printShapeParams(Shape shape, boolean forNodeShape, boolean outputStarted) {
if ( shape.deactivated() ) {
paramPrinter(forNodeShape, outputStarted,
()->compactUnquotedString(out, "deactivated", "true") );
outputStarted = true;
}
if ( shape.getSeverity() != null && ! SHACL.Violation.equals(shape.getSeverity().level())) {
compact(out, nodeFmt, "severity",shape.getSeverity().level());
out.println(" .");
paramPrinter(forNodeShape, outputStarted,
()->compact(out, nodeFmt, "severity", shape.getSeverity().level()) );
outputStarted = true;
}

if ( shape.getMessages() != null ) {
shape.getMessages().forEach(msg->{
boolean space = outputStarted;
for ( Node msg : shape.getMessages() ) {
if ( space )
out.print(" ");
compact(out, nodeFmt, "message", msg);
out.println(" .");
});
if ( forNodeShape )
out.println(" .");
space = true;
}
outputStarted = space;
}
return outputStarted;
}

private void printTargets(Shape shape) {
shape.getTargets().forEach(target->{
private boolean printTargets(Shape shape) {
boolean havePrinted = false;
for ( Target target : shape.getTargets() ) {
switch ( target.getTargetType() ) {
case implicitClass :
// Different syntax. Already printed.
return;
continue;
case targetClass :
// Different syntax. Already printed.
return;
continue;
case targetNode :
break;
case targetObjectsOf :
Expand All @@ -149,7 +176,9 @@ private void printTargets(Shape shape) {
out.print(" = ");
nodeFmt.format(out, target.getObject());
out.println(" .");
});
havePrinted = true;
}
return havePrinted;
}

private void outputPropertyShape(PropertyShape ps) {
Expand Down
3 changes: 3 additions & 0 deletions jena-shacl/src/test/files/local/shaclc-syntax/nodeParams.shc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ shapeClass ex:TestNodeParams {
nodeKind=sh:IRI .
datatype=xsd:double .
pattern="^.*$" .
message="MESSAGE" .
severity= sh:Warning .
deactivated = true .
languageIn=["en" "fr"] .
minLength=3 .
maxLength=5 .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ shapeClass ex:TestPropertyParams {
:q2 lessThanOrEquals=:q99 .
:q3 ! lessThan=:q98 .
:q4 disjoint=:q97 .
:zz message="MESSAGE" severity= sh:Warning deactivated = true .
}

0 comments on commit 8a262bf

Please sign in to comment.