Skip to content

Commit d2dce4d

Browse files
authored
Merge pull request #275 from bci-oss/274-allow-disabling-javascript-evaluation
Make JavaScript constraint evaluation configurable
2 parents f83bb42 + 8600768 commit d2dce4d

File tree

1 file changed

+22
-0
lines changed
  • core/sds-aspect-model-validator/src/main/java/io/openmanufacturing/sds/aspectmodel/shacl/constraint

1 file changed

+22
-0
lines changed

core/sds-aspect-model-validator/src/main/java/io/openmanufacturing/sds/aspectmodel/shacl/constraint/JsConstraint.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import javax.script.ScriptException;
2828

2929
import org.apache.jena.rdf.model.RDFNode;
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
3032

3133
import io.openmanufacturing.sds.aspectmodel.shacl.JsLibrary;
3234
import io.openmanufacturing.sds.aspectmodel.shacl.constraint.js.JsFactory;
@@ -46,6 +48,8 @@
4648
* The JavaScript object can contain a key "message" which, if present, overrides the sh:message defined in the shape.
4749
*/
4850
public class JsConstraint implements Constraint {
51+
private static final Logger LOG = LoggerFactory.getLogger( JsConstraint.class );
52+
private static boolean evaluateJavaScript = true;
4953
private final ScriptEngine engine;
5054
private final String message;
5155
private final JsLibrary jsLibrary;
@@ -56,6 +60,11 @@ public JsConstraint( final String message, final JsLibrary jsLibrary, final Stri
5660
this.jsLibrary = jsLibrary;
5761
this.jsFunctionName = jsFunctionName;
5862

63+
if ( !evaluateJavaScript ) {
64+
engine = null;
65+
return;
66+
}
67+
5968
// The guest application (i.e., the JavaScript) can only be compiled at runtime if either the host application runs via GraalVM
6069
// or, on a regular JVMCI-enabled JDK, the Graal Compiler is set up (additional JIT compilers are added). Otherwise, the script runs
6170
// in interpreted mode, which will be slower and print a corresponding warning. Since in the SHACL validation only very little JavaScript
@@ -75,8 +84,21 @@ public JsConstraint( final String message, final JsLibrary jsLibrary, final Stri
7584
}
7685
}
7786

87+
/**
88+
* Globally enables or disables evaluation of JavaScript constraints.
89+
* @param doEvaluate configure whether to evaluate JavaScript or not
90+
*/
91+
public static void setEvaluateJavaScript( final boolean doEvaluate ) {
92+
LOG.debug( String.format( "Globally %sabled JavaScript constraint evaluation", doEvaluate ? "en" : "dis" ) );
93+
JsConstraint.evaluateJavaScript = doEvaluate;
94+
}
95+
7896
@Override
7997
public List<Violation> apply( final RDFNode rdfNode, final EvaluationContext context ) {
98+
if ( !evaluateJavaScript ) {
99+
return List.of();
100+
}
101+
80102
final Bindings bindings = engine.getBindings( ScriptContext.ENGINE_SCOPE );
81103
bindings.put( "$data", new JsGraph( rdfNode.getModel().getGraph() ) );
82104
bindings.put( "$shapes", new JsGraph( context.validator().getShapesModel().getGraph() ) );

0 commit comments

Comments
 (0)