use of org.drools.core.rule.MVELDialectRuntimeData in project drools by kiegroup.
the class MVELObjectExpressionBuilder method build.
public static MVELObjectExpression build(String expression, 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(), expression, 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, RuleTerminalNode.SortDeclarations.instance);
MVELCompilationUnit unit = dialect.getMVELCompilationUnit(expression, analysis, previousDeclarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.EXPRESSION);
MVELObjectExpression expr = new MVELObjectExpression(unit, dialect.getId());
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
data.addCompileable(context.getRule(), expr);
expr.compile(data);
return expr;
} catch (final Exception e) {
DialectUtil.copyErrorLocation(e, context.getRuleDescr());
context.addError(new DescrBuildError(context.getParentDescr(), context.getRuleDescr(), null, "Unable to build expression : " + e.getMessage() + "'" + expression + "'"));
return null;
} finally {
context.setTypesafe(typesafe);
}
}
use of org.drools.core.rule.MVELDialectRuntimeData in project drools by kiegroup.
the class MVELClassFieldExtractorTest method testIsNullValue.
@Test
public void testIsNullValue() {
try {
assertFalse(this.extractor.isNullValue(null, this.person[0]));
MVELObjectClassFieldReader nullExtractor = (MVELObjectClassFieldReader) store.getMVELReader(Person.class.getPackage().getName(), Person.class.getName(), "addresses['business'].phone", true, String.class);
MVELDialectRuntimeData data = new MVELDialectRuntimeData();
data.addImport(Person.class.getSimpleName(), Person.class);
data.onAdd(null, ProjectClassLoader.createProjectClassLoader());
nullExtractor.compile(data);
//
// InternalReadAccessor nullExtractor = store.getReader( Person.class,
// "addresses['business'].phone",
// getClass().getClassLoader() );
assertTrue(nullExtractor.isNullValue(null, this.person[0]));
} catch (final Exception e) {
fail("Should not throw an exception");
}
}
use of org.drools.core.rule.MVELDialectRuntimeData in project drools by kiegroup.
the class ScenarioTest method createContext.
public BuildContext createContext() {
RuleBaseConfiguration conf = new RuleBaseConfiguration();
KnowledgeBaseImpl rbase = new KnowledgeBaseImpl("ID", conf);
BuildContext buildContext = new BuildContext(rbase);
RuleImpl rule = new RuleImpl("rule1").setPackage("org.pkg1");
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.pkg1");
pkg.getDialectRuntimeRegistry().setDialectData("mvel", new MVELDialectRuntimeData());
pkg.addRule(rule);
buildContext.setRule(rule);
return buildContext;
}
use of org.drools.core.rule.MVELDialectRuntimeData in project drools by kiegroup.
the class MVELAccumulator method init.
/* (non-Javadoc)
* @see org.kie.spi.Accumulator#init(java.lang.Object, org.kie.spi.Tuple, org.kie.rule.Declaration[], org.kie.WorkingMemory)
*/
public void init(Object workingMemoryContext, Object context, Tuple tuple, Declaration[] declarations, WorkingMemory workingMemory) throws Exception {
Object[] localVars = new Object[initUnit.getOtherIdentifiers().length];
MVELAccumulatorFactoryContext factoryContext = (MVELAccumulatorFactoryContext) workingMemoryContext;
VariableResolverFactory factory = factoryContext.getInitFactory();
initUnit.updateFactory(null, tuple, localVars, (InternalWorkingMemory) workingMemory, workingMemory.getGlobalResolver(), factory);
InternalKnowledgePackage pkg = workingMemory.getKnowledgeBase().getPackage("MAIN");
if (pkg != null) {
MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData("mvel");
factory.setNextFactory(data.getFunctionFactory());
}
MVELSafeHelper.getEvaluator().executeExpression(this.init, null, factory);
DroolsVarFactory df = (DroolsVarFactory) factory.getNextFactory();
if (localVars.length > 0) {
for (int i = 0; i < df.getOtherVarsLength(); i++) {
localVars[i] = factory.getIndexedVariableResolver(df.getOtherVarsPos() + i).getValue();
}
}
((MVELAccumulatorContext) context).setVariables(localVars);
}
use of org.drools.core.rule.MVELDialectRuntimeData in project drools by kiegroup.
the class MVELConsequence method evaluate.
public void evaluate(final KnowledgeHelper knowledgeHelper, final WorkingMemory workingMemory) throws Exception {
VariableResolverFactory factory = unit.getFactory(knowledgeHelper, ((AgendaItem) knowledgeHelper.getMatch()).getTerminalNode().getRequiredDeclarations(), knowledgeHelper.getRule(), knowledgeHelper.getTuple(), null, (InternalWorkingMemory) workingMemory, workingMemory.getGlobalResolver());
// do we have any functions for this namespace?
InternalKnowledgePackage pkg = workingMemory.getKnowledgeBase().getPackage("MAIN");
if (pkg != null) {
MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData(this.id);
factory.setNextFactory(data.getFunctionFactory());
}
CompiledExpression compexpr = (CompiledExpression) this.expr;
if (MVELDebugHandler.isDebugMode()) {
if (MVELDebugHandler.verbose) {
logger.info(DebugTools.decompile(compexpr));
}
MVEL.executeDebugger(compexpr, knowledgeHelper, factory);
} else {
MVEL.executeExpression(compexpr, knowledgeHelper, factory);
}
}
Aggregations