27
27
import javax .script .ScriptException ;
28
28
29
29
import org .apache .jena .rdf .model .RDFNode ;
30
+ import org .slf4j .Logger ;
31
+ import org .slf4j .LoggerFactory ;
30
32
31
33
import io .openmanufacturing .sds .aspectmodel .shacl .JsLibrary ;
32
34
import io .openmanufacturing .sds .aspectmodel .shacl .constraint .js .JsFactory ;
46
48
* The JavaScript object can contain a key "message" which, if present, overrides the sh:message defined in the shape.
47
49
*/
48
50
public class JsConstraint implements Constraint {
51
+ private static final Logger LOG = LoggerFactory .getLogger ( JsConstraint .class );
52
+ private static boolean evaluateJavaScript = true ;
49
53
private final ScriptEngine engine ;
50
54
private final String message ;
51
55
private final JsLibrary jsLibrary ;
@@ -56,6 +60,11 @@ public JsConstraint( final String message, final JsLibrary jsLibrary, final Stri
56
60
this .jsLibrary = jsLibrary ;
57
61
this .jsFunctionName = jsFunctionName ;
58
62
63
+ if ( !evaluateJavaScript ) {
64
+ engine = null ;
65
+ return ;
66
+ }
67
+
59
68
// The guest application (i.e., the JavaScript) can only be compiled at runtime if either the host application runs via GraalVM
60
69
// or, on a regular JVMCI-enabled JDK, the Graal Compiler is set up (additional JIT compilers are added). Otherwise, the script runs
61
70
// 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
75
84
}
76
85
}
77
86
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
+
78
96
@ Override
79
97
public List <Violation > apply ( final RDFNode rdfNode , final EvaluationContext context ) {
98
+ if ( !evaluateJavaScript ) {
99
+ return List .of ();
100
+ }
101
+
80
102
final Bindings bindings = engine .getBindings ( ScriptContext .ENGINE_SCOPE );
81
103
bindings .put ( "$data" , new JsGraph ( rdfNode .getModel ().getGraph () ) );
82
104
bindings .put ( "$shapes" , new JsGraph ( context .validator ().getShapesModel ().getGraph () ) );
0 commit comments