*
* @param rule the rule to get the context for
* @return the evaluation context
*/
public static synchronized IEvaluationContext getContext(Rule rule) {
RuleModel ruleModel = (RuleModel) rule.eContainer();
// check if a context already exists on the resource
for(Adapter adapter : ruleModel.eAdapters()) {
if(adapter instanceof RuleContextAdapter) {
return ((RuleContextAdapter) adapter).getContext();
}
}
// no evaluation context found, so create a new one
IEvaluationContext evaluationContext = contextProvider.get();
for(XExpression expr : ruleModel.getVariables()) {
if (expr instanceof XVariableDeclaration) {
XVariableDeclaration var = (XVariableDeclaration) expr;
try {
Object initialValue = var.getRight()==null ? null : scriptEngine.newScriptFromXExpression(var.getRight()).execute();
evaluationContext.newValue(QualifiedName.create(var.getName()), initialValue);
} catch (ScriptExecutionException e) {
logger.warn("Variable '{}' on rule file '{}' cannot be initialized with value '{}': {}",
new String[] { var.getName(), ruleModel.eResource().getURI().path(), var.getRight().toString(), e.getMessage() });
}
}
}
ruleModel.eAdapters().add(new RuleContextAdapter(evaluationContext));
return evaluationContext;
}