use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class ExtendsTest method testMultipleInheritanceWithPosition1.
@Test
public void testMultipleInheritanceWithPosition1() throws Exception {
// DROOLS-249
String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + " field2 : int = 102 //@position(0)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 //@position(0)\n" + " mfield1 : int = 201 @position(2)\n" + "end";
KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kBuilder.hasErrors()) {
System.err.println(kBuilder.getErrors());
}
assertFalse(kBuilder.hasErrors());
InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase.addPackages(kBuilder.getKnowledgePackages());
FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait");
assertEquals(5, sw.getFields().size());
assertEquals("field0", sw.getFields().get(0).getName());
assertEquals("field1", sw.getFields().get(1).getName());
assertEquals("mfield1", sw.getFields().get(2).getName());
assertEquals("field2", sw.getFields().get(3).getName());
assertEquals("mfield0", sw.getFields().get(4).getName());
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class ExtendsTest method testExtendOverride.
@Test
public void testExtendOverride() {
String drl = "package test.beans;\n" + "\n" + "import java.util.List;\n" + "import java.util.ArrayList;\n" + "\n" + "global List ans;" + "\n" + "\n" + "declare ArrayList\n" + "end\n" + "" + "declare Bean extends ArrayList\n" + " fld : int = 4 \n" + " myField : String = \"xxx\" \n" + "end\n" + "\n" + "declare Bean2 extends Bean\n" + " moref : double\n" + " myField : String\n" + "end\n" + "rule \"Init\"\n" + "when\n" + "then\n" + " Bean b = new Bean2();\n" + " ans.add(b);" + " System.out.println(b);\t\n" + "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession ksession = createKnowledgeSession(kbase);
List out = new ArrayList();
ksession.setGlobal("ans", out);
ksession.fireAllRules();
Object x = out.get(0);
FactType type = kbase.getFactType("test.beans", "Bean2");
assertEquals(4, type.get(x, "fld"));
assertEquals("xxx", type.get(x, "myField"));
assertEquals(0.0, type.get(x, "moref"));
assertTrue(x instanceof ArrayList);
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class FailureOnRemovalTest method runTest.
private void runTest(boolean shareBetaNodes) throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) createKnowledgeBase(shareBetaNodes);
Collection<KiePackage> rule1 = compileRule(RULE_1);
kbase.addPackages(rule1);
// we need to add at least two rules. Test will not fail with only one rule.
Collection<KiePackage> rule2 = compileRule(RULE_2);
kbase.addPackages(rule2);
kbase.removeRule(PACKAGE, RULE_1);
KieSession ksession = createKnowledgeSession(kbase);
int fired = ksession.fireAllRules();
ksession.dispose();
assertEquals(1, fired);
Collection<KiePackage> rule3 = compileRule(RULE_3);
kbase.addPackages(rule3);
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class FireUntilHaltAccumulateTest method setUp.
@Before
public void setUp() {
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
System.err.println(kbuilder.getErrors().toString());
}
final KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(config);
kbase.addPackages(kbuilder.getKnowledgePackages());
final KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
this.statefulSession = kbase.newKieSession(sessionConfig, null);
this.stockFactory = new StockFactory(kbase);
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class FirstOrderLogicTest method testOrWithVariableResolution2.
// JBRULES-2526
@Test
public void testOrWithVariableResolution2() throws Exception {
// KieBase kbase = loadKnowledgeBase( "test_OrCEFollowedByMultipleEval.drl");
// KieSession workingMemory = createKnowledgeSession(kbase);
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_OrCEFollowedByMultipleEval2.drl", FirstOrderLogicTest.class), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
final KieSession ksession = createKnowledgeSession(kbase);
final AgendaEventListener al = mock(AgendaEventListener.class);
ksession.addEventListener(al);
ksession.insert(new FactA("a"));
ksession.insert(new FactB("b"));
ksession.insert(new FactC("c"));
ksession.fireAllRules();
verify(al, times(8)).afterMatchFired(any(AfterMatchFiredEvent.class));
}
Aggregations