use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class JavaConsequenceBuilderPRAlwaysTest method setupTest.
private void setupTest(String consequence, Map<String, Object> namedConsequences) {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools");
pkg.addImport(new ImportDeclaration("org.drools.compiler.Cheese"));
KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
// Although it should be the default, for completeness we explicit this is the test cases how RHS should be rewritten as:
conf.setOption(PropertySpecificOption.ALWAYS);
KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl(pkg, conf);
ruleDescr = new RuleDescr("test consequence builder");
ruleDescr.setConsequence(consequence);
for (Entry<String, Object> entry : namedConsequences.entrySet()) {
ruleDescr.addNamedConsequences(entry.getKey(), entry.getValue());
}
RuleImpl rule = ruleDescr.toRule();
PackageRegistry pkgRegistry = kBuilder.getPackageRegistry(pkg.getName());
DialectCompiletimeRegistry reg = kBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
context = new RuleBuildContext(kBuilder, ruleDescr, reg, pkg, reg.getDialect(pkgRegistry.getDialect()));
rule.addPattern(new Pattern(0, new ClassObjectType(Cheese.class), "$cheese"));
Pattern p = new Pattern(1, new ClassObjectType(Person.class), "$persone");
Declaration declr = p.addDeclaration("age");
final InternalReadAccessor extractor = PatternBuilder.getFieldReadAccessor(context, new BindingDescr("age", "age"), p, "age", declr, true);
rule.addPattern(p);
context.getDeclarationResolver().pushOnBuildStack(rule.getLhs());
context.getDialect().getConsequenceBuilder().build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
for (String name : namedConsequences.keySet()) {
context.getDialect().getConsequenceBuilder().build(context, name);
}
context.getDialect().addRule(context);
pkgRegistry.getPackage().addRule(context.getRule());
kBuilder.compileAll();
kBuilder.reloadAll();
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class JavaConsequenceBuilderTest method setupTest.
private void setupTest(String consequence, Map<String, Object> namedConsequences) {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools");
pkg.addImport(new ImportDeclaration("org.drools.compiler.Cheese"));
KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
// this test was originally intended with PropertyReactive.ALLOWED:
conf.setOption(PropertySpecificOption.ALLOWED);
KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl(pkg, conf);
ruleDescr = new RuleDescr("test consequence builder");
ruleDescr.setConsequence(consequence);
for (Entry<String, Object> entry : namedConsequences.entrySet()) {
ruleDescr.addNamedConsequences(entry.getKey(), entry.getValue());
}
RuleImpl rule = ruleDescr.toRule();
PackageRegistry pkgRegistry = kBuilder.getPackageRegistry(pkg.getName());
DialectCompiletimeRegistry reg = kBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
context = new RuleBuildContext(kBuilder, ruleDescr, reg, pkg, reg.getDialect(pkgRegistry.getDialect()));
rule.addPattern(new Pattern(0, new ClassObjectType(Cheese.class), "$cheese"));
Pattern p = new Pattern(1, new ClassObjectType(Person.class), "$persone");
Declaration declr = p.addDeclaration("age");
final InternalReadAccessor extractor = PatternBuilder.getFieldReadAccessor(context, new BindingDescr("age", "age"), p, "age", declr, true);
rule.addPattern(p);
context.getDeclarationResolver().pushOnBuildStack(rule.getLhs());
context.getDialect().getConsequenceBuilder().build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
for (String name : namedConsequences.keySet()) {
context.getDialect().getConsequenceBuilder().build(context, name);
}
context.getDialect().addRule(context);
pkgRegistry.getPackage().addRule(context.getRule());
kBuilder.compileAll();
kBuilder.reloadAll();
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class MVELEnabledExpression method getValue.
public boolean getValue(final Tuple tuple, final Declaration[] declrs, final RuleImpl rule, final WorkingMemory workingMemory) {
VariableResolverFactory factory = unit.getFactory(null, declrs, rule, null, (LeftTuple) tuple, 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());
}
return ((Boolean) MVEL.executeExpression(this.expr, null, factory)).booleanValue();
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class MVELEvalExpression method evaluate.
public boolean evaluate(final Tuple tuple, final Declaration[] requiredDeclarations, final WorkingMemory workingMemory, final Object context) throws Exception {
VariableResolverFactory factory = (VariableResolverFactory) context;
unit.updateFactory(null, (LeftTuple) tuple, null, (InternalWorkingMemory) workingMemory, workingMemory.getGlobalResolver(), factory);
// 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());
}
final Boolean result = (Boolean) MVEL.executeExpression(this.expr, null, factory);
return result.booleanValue();
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class DisposeCommandPublicAPITest method testDisposeCommand.
@Test
public void testDisposeCommand() {
InternalKnowledgeBase kBase;
RuleImpl rule;
InternalKnowledgePackage pkg;
kBase = KnowledgeBaseFactory.newKnowledgeBase();
pkg = new KnowledgePackageImpl("org.droos.test");
pkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
JavaDialectRuntimeData data = new JavaDialectRuntimeData();
data.onAdd(pkg.getDialectRuntimeRegistry(), kBase.getRootClassLoader());
pkg.getDialectRuntimeRegistry().setDialectData("java", data);
rule = new RuleImpl("Test");
rule.setDialect("java");
rule.setConsequence(new Consequence() {
public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {
}
public String getName() {
return "default";
}
});
pkg.addRule(rule);
kBase.addPackage(pkg);
KieSession session = kBase.newKieSession();
Command dispose = KieServices.Factory.get().getCommands().newDispose();
session.insert("whatever");
session.fireAllRules();
session.execute(dispose);
try {
session.insert("whatever");
} catch (Exception e) {
Assert.assertEquals(e.getMessage(), "Illegal method call. This session was previously disposed.");
}
}
Aggregations