use of org.drools.core.impl.KnowledgeBaseImpl in project drools by kiegroup.
the class TraitTest method testInternalComponents1.
@Test(timeout = 10000)
public void testInternalComponents1() {
String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
Resource res = ResourceFactory.newClassPathResource(source);
assertNotNull(res);
kbuilder.add(res, ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase();
kb.addPackages(kbuilder.getKnowledgePackages());
TraitFactory.setMode(mode, kb);
TraitFactory tFactory = ((KnowledgeBaseImpl) kb).getConfiguration().getComponentFactory().getTraitFactory();
try {
FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
TraitableBean imp = (TraitableBean) impClass.newInstance();
FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
Class trait = traitClass.getFactClass();
TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
Object proxyFields = proxy.getFields();
Object coreTraits = imp._getTraitMap();
Object coreProperties = imp._getDynamicProperties();
assertTrue(proxy.getObject() instanceof TraitableBean);
assertNotNull(proxyFields);
assertNotNull(coreTraits);
assertNotNull(coreProperties);
if (mode == VirtualPropertyMode.MAP) {
assertTrue(proxyFields instanceof MapWrapper);
assertTrue(coreTraits instanceof TraitTypeMap);
assertTrue(coreProperties instanceof HashMap);
} else {
assertEquals("org.drools.compiler.trait.test.Student.org.drools.compiler.trait.test.Imp_ProxyWrapper", proxyFields.getClass().getName());
assertTrue(proxyFields instanceof TripleBasedStruct);
assertTrue(coreTraits instanceof TraitTypeMap);
assertTrue(coreProperties instanceof TripleBasedBean);
}
StudentProxy2 sp2 = new StudentProxy2(new Imp2(), null);
System.out.println(sp2.toString());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.drools.core.impl.KnowledgeBaseImpl in project drools by kiegroup.
the class TraitTest method testTraitWrapGetAndSet.
@Test(timeout = 10000)
public void testTraitWrapGetAndSet() {
String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
Resource res = ResourceFactory.newClassPathResource(source);
assertNotNull(res);
kbuilder.add(res, ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase();
TraitFactory.setMode(mode, kb);
kb.addPackages(kbuilder.getKnowledgePackages());
TraitFactory tFactory = ((KnowledgeBaseImpl) kb).getConfiguration().getComponentFactory().getTraitFactory();
try {
FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
TraitableBean imp = (TraitableBean) impClass.newInstance();
Class trait = kb.getFactType("org.drools.compiler.trait.test", "Student").getFactClass();
TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
Map<String, Object> virtualFields = imp._getDynamicProperties();
Map<String, Object> wrapper = proxy.getFields();
wrapper.put("name", "john");
wrapper.put("virtualField", "xyz");
wrapper.entrySet();
assertEquals(4, wrapper.size());
assertEquals(2, virtualFields.size());
assertEquals("john", wrapper.get("name"));
assertEquals("xyz", wrapper.get("virtualField"));
assertEquals("john", impClass.get(imp, "name"));
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.drools.core.impl.KnowledgeBaseImpl in project drools by kiegroup.
the class DynamicEvalTest method testDynamicAdd.
@Test
public void testDynamicAdd() {
String test = "\nrule id3" + "\nwhen" + // this eval works
"\neval(0 < 1)" + "\nthen" + "\ninsertLogical( \"done\" );" + "\nend";
loadPackages(ResourceFactory.newByteArrayResource(test.getBytes()), ResourceType.DRL);
((KnowledgeBaseImpl) session.getKieBase()).addPackages(kbuilder.getKnowledgePackages());
session.addEventListener(new DebugRuleRuntimeEventListener());
// 1
int fired = session.fireAllRules();
System.out.println(fired);
effects = session.getObjects();
assertTrue("fired", effects.contains("done"));
// so the above works, let's try it again
String test2 = "\nrule id4" + "\nwhen" + // this eval doesn't
"\neval(0 == 0 )" + "\nthen" + "\ninsertLogical( \"done2\" );" + "\nend";
loadPackages(ResourceFactory.newByteArrayResource(test2.getBytes()), ResourceType.DRL);
((KnowledgeBaseImpl) session.getKieBase()).addPackages(kbuilder.getKnowledgePackages());
// 0
fired = session.fireAllRules();
System.out.println(fired);
effects = session.getObjects();
// fails
assertTrue("fired", effects.contains("done2"));
}
use of org.drools.core.impl.KnowledgeBaseImpl in project drools by kiegroup.
the class DynamicEvalTest method testDynamicAdd2.
@Test
public void testDynamicAdd2() {
String test = "rule id3\n" + "when\n" + "eval(0 == 0)\n" + // this eval works
"String( this == \"go\" )\n" + "then\n" + "insertLogical( \"done\" );\n" + "end\n" + "rule id5\n" + "when\n" + "eval(0 == 0)\n" + // this eval works
"Integer( this == 7 )\n" + "then\n" + "insertLogical( \"done3\" );\n" + "end\n";
loadPackages(ResourceFactory.newByteArrayResource(test.getBytes()), ResourceType.DRL);
((KnowledgeBaseImpl) session.getKieBase()).addPackages(kbuilder.getKnowledgePackages());
session.addEventListener(new DebugRuleRuntimeEventListener());
session.insert("go");
session.insert(5);
session.insert(7);
// 1
int fired = session.fireAllRules();
System.out.println(fired);
effects = session.getObjects();
assertTrue("fired", effects.contains("done"));
// so the above works, let's try it again
String test2 = "\nrule id4" + "\nwhen" + // this eval doesn't
"\neval(0 == 0 )" + "\nInteger( this == 5 )" + "\nthen" + "\ninsertLogical( \"done2\" );" + "\nend";
loadPackages(ResourceFactory.newByteArrayResource(test2.getBytes()), ResourceType.DRL);
((KnowledgeBaseImpl) session.getKieBase()).addPackages(kbuilder.getKnowledgePackages());
// 0
fired = session.fireAllRules();
System.out.println(fired);
effects = session.getObjects();
// fails
assertTrue("fired", effects.contains("done2"));
for (Object o : session.getObjects()) {
System.out.println(o);
}
}
use of org.drools.core.impl.KnowledgeBaseImpl in project drools by kiegroup.
the class DefaultTemplateRuleBaseTest method testSimpleTemplate.
@SuppressWarnings("unchecked")
@Test
public void testSimpleTemplate() throws Exception {
TemplateContainer tc = new TemplateContainer() {
private Column[] columns = new Column[] { new LongColumn("column1"), new LongColumn("column2"), new StringColumn("column3") };
public Column[] getColumns() {
return columns;
}
public String getHeader() {
return null;
}
public Map<String, RuleTemplate> getTemplates() {
Map<String, RuleTemplate> templates = new HashMap<String, RuleTemplate>();
RuleTemplate ruleTemplate = new RuleTemplate("template1", this);
ruleTemplate.addColumn("column1 == 10");
ruleTemplate.addColumn("column2 < 5 || > 20");
ruleTemplate.addColumn("column3 == \"xyz\"");
templates.put("template1", ruleTemplate);
return templates;
}
public Column getColumn(String name) {
return columns[Integer.parseInt(name.substring(6)) - 1];
}
};
DefaultTemplateRuleBase ruleBase = new DefaultTemplateRuleBase(tc);
InternalKnowledgePackage[] packages = ((KnowledgeBaseImpl) ruleBase.newStatefulSession().getKieBase()).getPackages();
assertEquals(1, packages.length);
Map<String, String> globals = packages[0].getGlobals();
assertEquals(DefaultGenerator.class.getName(), globals.get("generator"));
Collection<org.kie.api.definition.rule.Rule> rules = packages[0].getRules();
assertEquals(1, rules.size());
assertEquals("template1", rules.iterator().next().getName());
GroupElement lhs = ((RuleImpl) rules.iterator().next()).getLhs();
// when
// r : Row()
// column1 : Column(name == "column1")
// exists LongCell(row == r, column == column1, value == 10)
// column2 : Column(name == "column2")
// exists LongCell(row == r, column == column2, value < 5 | > 20)
// column3 : Column(name == "column3")
// exists StringCell(row == r, column == column3, value == "xyz")
assertEquals(7, lhs.getChildren().size());
org.drools.core.rule.Pattern pattern = (org.drools.core.rule.Pattern) lhs.getChildren().get(1);
assertEquals(1, pattern.getConstraints().size());
Constraint constraint = pattern.getConstraints().get(0);
GroupElement exists = (GroupElement) lhs.getChildren().get(2);
pattern = (org.drools.core.rule.Pattern) exists.getChildren().get(0);
assertEquals(3, pattern.getConstraints().size());
IndexableConstraint vconstraint = (IndexableConstraint) pattern.getConstraints().get(1);
assertEquals(Column.class, vconstraint.getFieldIndex().getExtractor().getExtractToClass());
assertEquals("column1", vconstraint.getRequiredDeclarations()[0].getIdentifier());
pattern = (org.drools.core.rule.Pattern) lhs.getChildren().get(3);
assertEquals(1, pattern.getConstraints().size());
constraint = pattern.getConstraints().get(0);
exists = (GroupElement) lhs.getChildren().get(4);
pattern = (org.drools.core.rule.Pattern) exists.getChildren().get(0);
assertEquals(3, pattern.getConstraints().size());
vconstraint = (IndexableConstraint) pattern.getConstraints().get(1);
assertEquals(Column.class, vconstraint.getFieldIndex().getExtractor().getExtractToClass());
assertEquals("column2", vconstraint.getRequiredDeclarations()[0].getIdentifier());
pattern = (org.drools.core.rule.Pattern) lhs.getChildren().get(5);
assertEquals(1, pattern.getConstraints().size());
constraint = pattern.getConstraints().get(0);
exists = (GroupElement) lhs.getChildren().get(6);
pattern = (org.drools.core.rule.Pattern) exists.getChildren().get(0);
assertEquals(3, pattern.getConstraints().size());
vconstraint = (IndexableConstraint) pattern.getConstraints().get(1);
assertEquals(Column.class, vconstraint.getFieldIndex().getExtractor().getExtractToClass());
assertEquals("column3", vconstraint.getRequiredDeclarations()[0].getIdentifier());
}
Aggregations