use of org.drools.core.base.mvel.MVELSalienceExpression in project drools by kiegroup.
the class MVELSalienceBuilderTest method setUp.
@Before
public void setUp() throws Exception {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
final RuleDescr ruleDescr = new RuleDescr("rule 1");
ruleDescr.addAttribute(new AttributeDescr("salience", "(p.age + 20)/2"));
ruleDescr.setConsequence("");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
final ObjectType personObjeectType = new ClassObjectType(Person.class);
final Pattern pattern = new Pattern(0, personObjeectType);
final PatternExtractor extractor = new PatternExtractor(personObjeectType);
final Declaration declaration = new Declaration("p", extractor, pattern);
final Map<String, Declaration> map = new HashMap<String, Declaration>();
map.put("p", declaration);
declarationResolver.setDeclarations(map);
context.setDeclarationResolver(declarationResolver);
kBase = KnowledgeBaseFactory.newKnowledgeBase();
SalienceBuilder salienceBuilder = new MVELSalienceBuilder();
salienceBuilder.build(context);
((MVELSalienceExpression) context.getRule().getSalience()).compile((MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel"));
}
use of org.drools.core.base.mvel.MVELSalienceExpression 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.base.mvel.MVELSalienceExpression in project drools by kiegroup.
the class RuleTerminalNode method setDeclarations.
public void setDeclarations(Map<String, Declaration> decls) {
if (rule.getSalience() instanceof MVELSalienceExpression) {
MVELSalienceExpression expr = (MVELSalienceExpression) rule.getSalience();
Declaration[] declrs = expr.getMVELCompilationUnit().getPreviousDeclarations();
this.salienceDeclarations = new Declaration[declrs.length];
int i = 0;
for (Declaration declr : declrs) {
this.salienceDeclarations[i++] = decls.get(declr.getIdentifier());
}
Arrays.sort(this.salienceDeclarations, SortDeclarations.instance);
}
if (rule.getEnabled() instanceof MVELEnabledExpression) {
MVELEnabledExpression expr = (MVELEnabledExpression) rule.getEnabled();
Declaration[] declrs = expr.getMVELCompilationUnit().getPreviousDeclarations();
this.enabledDeclarations = new Declaration[declrs.length];
int i = 0;
for (Declaration declr : declrs) {
this.enabledDeclarations[i++] = decls.get(declr.getIdentifier());
}
Arrays.sort(this.enabledDeclarations, SortDeclarations.instance);
}
if (rule.getTimer() instanceof BaseTimer) {
this.timerDeclarations = ((BaseTimer) rule.getTimer()).getTimerDeclarations(decls);
}
}
use of org.drools.core.base.mvel.MVELSalienceExpression in project drools by kiegroup.
the class RuleTest method testIsSalienceDynamic.
@Test
public void testIsSalienceDynamic() {
final RuleImpl rule = new RuleImpl("myrule");
Salience salience = new MVELSalienceExpression();
rule.setSalience(salience);
assertTrue(rule.isSalienceDynamic());
}
Aggregations