use of org.drools.core.rule.MVELDialectRuntimeData in project drools by kiegroup.
the class MVELSalienceBuilder method build.
public void build(RuleBuildContext context) {
boolean typesafe = context.isTypesafe();
// pushing consequence LHS into the stack for variable resolution
context.getDeclarationResolver().pushOnBuildStack(context.getRule().getLhs());
try {
// This builder is re-usable in other dialects, so specify by name
MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
MVELAnalysisResult analysis = (MVELAnalysisResult) dialect.analyzeExpression(context, context.getRuleDescr(), context.getRuleDescr().getSalience(), new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
context.setTypesafe(analysis.isTypesafe());
final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
int i = usedIdentifiers.getDeclrClasses().keySet().size();
Declaration[] previousDeclarations = new Declaration[i];
i = 0;
for (String id : usedIdentifiers.getDeclrClasses().keySet()) {
previousDeclarations[i++] = decls.get(id);
}
Arrays.sort(previousDeclarations, SortDeclarations.instance);
MVELCompilationUnit unit = dialect.getMVELCompilationUnit(context.getRuleDescr().getSalience(), analysis, previousDeclarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.EXPRESSION);
MVELSalienceExpression expr = new MVELSalienceExpression(unit, dialect.getId());
context.getRule().setSalience(KiePolicyHelper.isPolicyEnabled() ? new SafeSalience(expr) : expr);
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
data.addCompileable(context.getRule(), expr);
expr.compile(data, context.getRule());
} catch (final Exception e) {
copyErrorLocation(e, context.getRuleDescr());
context.addError(new DescrBuildError(context.getParentDescr(), context.getRuleDescr(), null, "Unable to build expression for 'salience' : " + e.getMessage() + "'" + context.getRuleDescr().getSalience() + "'"));
} finally {
context.setTypesafe(typesafe);
}
}
use of org.drools.core.rule.MVELDialectRuntimeData in project drools by kiegroup.
the class MVELClassFieldExtractorTest method setUp.
@Before
public void setUp() throws Exception {
store.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
store.setEagerWire(true);
extractor = (MVELObjectClassFieldReader) store.getMVELReader(Person.class.getPackage().getName(), Person.class.getName(), "addresses['home'].street", true, String.class);
MVELDialectRuntimeData data = new MVELDialectRuntimeData();
data.addImport(Person.class.getSimpleName(), Person.class);
data.onAdd(null, ProjectClassLoader.createProjectClassLoader());
extractor.compile(data);
person[0] = new Person("bob", 30);
business[0] = new Address("Business Street", "999", null);
home[0] = new Address("Home Street", "555", "55555555");
person[0].getAddresses().put("business", business[0]);
person[0].getAddresses().put("home", home[0]);
person[1] = new Person("mark", 35);
business[1] = new Address("Another Business Street", "999", null);
home[1] = new Address("Another Home Street", "555", "55555555");
person[1].getAddresses().put("business", business[1]);
person[1].getAddresses().put("home", home[1]);
}
use of org.drools.core.rule.MVELDialectRuntimeData in project drools by kiegroup.
the class MvelConstraint method createMvelConditionEvaluator.
protected ConditionEvaluator createMvelConditionEvaluator(InternalWorkingMemory workingMemory) {
if (compilationUnit != null) {
MVELDialectRuntimeData data = getMVELDialectRuntimeData(workingMemory);
ExecutableStatement statement = (ExecutableStatement) compilationUnit.getCompiledExpression(data, evaluationContext);
ParserConfiguration configuration = statement instanceof CompiledExpression ? ((CompiledExpression) statement).getParserConfiguration() : data.getParserConfiguration();
return new MvelConditionEvaluator(compilationUnit, configuration, statement, declarations, operators, getAccessedClass());
} else {
return new MvelConditionEvaluator(getParserConfiguration(workingMemory), expression, declarations, operators, getAccessedClass());
}
}
use of org.drools.core.rule.MVELDialectRuntimeData in project jbpm by kiegroup.
the class MVELDecisionBuilderTest method testSimpleAction.
@Test
public void testSimpleAction() throws Exception {
final InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
ActionDescr actionDescr = new ActionDescr();
actionDescr.setText("list.add( 'hello world' )");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
PackageRegistry pkgReg = pkgBuilder.getPackageRegistry(pkg.getName());
MVELDialect mvelDialect = (MVELDialect) pkgReg.getDialectCompiletimeRegistry().getDialect("mvel");
PackageBuildContext context = new PackageBuildContext();
context.init(pkgBuilder, pkg, null, pkgReg.getDialectCompiletimeRegistry(), mvelDialect, null);
pkgBuilder.addPackageFromDrl(new StringReader("package pkg1;\nglobal java.util.List list;\n"));
ActionNode actionNode = new ActionNode();
DroolsAction action = new DroolsConsequenceAction("java", null);
actionNode.setAction(action);
final MVELActionBuilder builder = new MVELActionBuilder();
builder.build(context, action, actionDescr, actionNode);
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(Arrays.asList(pkgBuilder.getPackages()));
final KieSession wm = kbase.newKieSession();
List<String> list = new ArrayList<String>();
wm.setGlobal("list", list);
MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkgBuilder.getPackage("pkg1").getDialectRuntimeRegistry().getDialectData("mvel");
ProcessContext processContext = new ProcessContext(((InternalWorkingMemory) wm).getKnowledgeRuntime());
((MVELAction) actionNode.getAction().getMetaData("Action")).compile(data);
((Action) actionNode.getAction().getMetaData("Action")).execute(processContext);
assertEquals("hello world", list.get(0));
}
use of org.drools.core.rule.MVELDialectRuntimeData in project jbpm by kiegroup.
the class MVELReturnValueConstraintEvaluatorBuilderTest method testSimpleReturnValueConstraintEvaluator.
@Test
public void testSimpleReturnValueConstraintEvaluator() throws Exception {
final InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
ReturnValueDescr descr = new ReturnValueDescr();
descr.setText("return value");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
PackageBuildContext context = new PackageBuildContext();
context.init(pkgBuilder, pkg, null, dialectRegistry, mvelDialect, null);
pkgBuilder.addPackageFromDrl(new StringReader("package pkg1;\nglobal Boolean value;"));
ReturnValueConstraintEvaluator node = new ReturnValueConstraintEvaluator();
final MVELReturnValueEvaluatorBuilder builder = new MVELReturnValueEvaluatorBuilder();
builder.build(context, node, descr, null);
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(Arrays.asList(pkgBuilder.getPackages()));
final KieSession ksession = kbase.newKieSession();
ksession.setGlobal("value", true);
RuleFlowProcessInstance processInstance = new RuleFlowProcessInstance();
processInstance.setKnowledgeRuntime((InternalKnowledgeRuntime) ksession);
SplitInstance splitInstance = new SplitInstance();
splitInstance.setProcessInstance(processInstance);
MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkgBuilder.getPackage("pkg1").getDialectRuntimeRegistry().getDialectData("mvel");
((MVELReturnValueEvaluator) node.getReturnValueEvaluator()).compile(data);
assertTrue(node.evaluate(splitInstance, null, null));
ksession.setGlobal("value", false);
assertFalse(node.evaluate(splitInstance, null, null));
}
Aggregations