Skip to content

JENA-2009: Implement Visitor Pattern for SHACL Constraints#882

Merged
afs merged 5 commits intoapache:masterfrom
fkleedorfer:constraint-visitorpattern-JENA-2009
Dec 8, 2020
Merged

JENA-2009: Implement Visitor Pattern for SHACL Constraints#882
afs merged 5 commits intoapache:masterfrom
fkleedorfer:constraint-visitorpattern-JENA-2009

Conversation

@fkleedorfer
Copy link
Contributor

Notes:

  • added visit method to Constraint interface. I experimented with providing a default implementation (calling visitor.visit(Constraint)) to avoid having to add an implementation to all implementing classes. This approach does not work, which I did not expect. Learned something about Java method invocation target selection there.
  • added visit method non-abstract subclasses of Constraint. The pattern suggests they should be named accept, but jena uses visit wherever I checked. I guess it's better to stick with the existing flavor.
  • Currently no unit tests as there is no logic apart from visitor.visit(this). Please let me know if you feel differently. Using the PR in my client code, so I'm positive it works.

@afs
Copy link
Member

afs commented Dec 7, 2020

Please could you put the vitro in a consistent place in the constraints? Just before printCompact would be fine as many of them are there already.

@afs
Copy link
Member

afs commented Dec 7, 2020

I am unsure that default methods on the visitor are the way to go. An abstract class "ConstraintBase" has been the style up until Java8 and now can have a mixin/trait to do the same.

Reason: robustness.

When writing complex visitors it is good to have a way for the programmer to know they have covered all cases and be notified when a new constraint is added. Otherwise it is possible to miss a case and compile time and and it becomes a runtime problem.

Copy link
Member

@afs afs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, looks good. Some refinement for the long term in comments.

import org.apache.jena.shacl.engine.constraint.*;

public interface ConstraintVisitor {
default void visit(ReportConstraint constraint){}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one can be skipped. It's a dummy that only exists to carry a message. It can't be an executable constraint.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We must implement ReportConstraint.visit(ConstraintVisitor), though. Empty implementation for minimal friction?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'll do fine. The validation operations are throw new UnsupportedOperationException() but no-op works just as well for the visitor. (I suspect that ReportConstraint shouldn't exist at all! But from memory, passing the information it carries means adding other parameters in many places which even if purer, is verbose.)

default void visit(StrLanguageIn constraint){}
default void visit(StrMinLengthConstraint constraint){}
default void visit(JViolationConstraint constraint){}
default void visit(DatatypeConstraint constraint){}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to be with ClassConstaint. (The ideal order is as in section 4: https://www.w3.org/TR/shacl/#core-components).

The J* then go separately.

We'll need a "ConstraitExt" for extensions (not a strong point of the visitor pattern!) but that can wait for now.

Also remove empty default implementations in ConstraintVisitor
... to reflect order in in W3C SHACL 1.0 Recommendation
@fkleedorfer fkleedorfer requested a review from afs December 7, 2020 15:01
@afs afs merged commit 89e0070 into apache:master Dec 8, 2020
@fkleedorfer fkleedorfer deleted the constraint-visitorpattern-JENA-2009 branch April 8, 2022 12:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants