use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class TruthMaintenanceTest method testStatedShadowLogicalThenLogicalOnly.
@Test(timeout = 10000)
public void testStatedShadowLogicalThenLogicalOnly() {
String droolsSource = "package org.drools.tms.test; \n" + "global java.util.List list; \n" + "rule Justify \n" + "when \n" + " String( this == 'go1' ) " + "then \n" + " insertLogical( 'f1' ); \n" + "end \n" + "rule StillHere \n" + "when \n" + " String( this == 'go2' ) " + " s : String( this == 'f1' ) " + "then \n" + " list.add( s ); \n" + "end \n" + "";
KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kieConf.setOption(EqualityBehaviorOption.IDENTITY);
KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
KieSession session = kbase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
FactHandle g1 = session.insert("go1");
session.fireAllRules();
// This removes the stated position, but it should still be logical now and exist
session.delete(fh1, FactHandle.State.STATED);
session.insert("go2");
session.fireAllRules();
// fh1 is invalid and no longer in the WM. However it's now reverted to it's Justified version and will still be there
assertFalse(fh1.isValid());
// Make sure f1 is still there, but logical only now
assertEquals(1, list.size());
assertEquals("f1", list.get(0));
InternalFactHandle jfh1 = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem().get("f1").getLogicalFactHandle();
assertEquals(EqualityKey.JUSTIFIED, jfh1.getEqualityKey().getStatus());
assertSame(jfh1, session.getFactHandle("f1"));
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class SequentialTest method testSequentialWithNoLoop.
@Test(timeout = 10000L)
public void testSequentialWithNoLoop() throws Exception {
// BZ-1228098
String str = "package org.drools.compiler.test\n" + "import \n" + Message.class.getCanonicalName() + ";" + "rule R1 no-loop when\n" + " $s : String( )" + " $m : Message( )\n" + "then\n" + " modify($m) { setMessage($s) };\n" + "end\n";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write("src/main/resources/r0.drl", str);
KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
Results results = kieBuilder.getResults();
if (results.hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
throw new RuntimeException(results.getMessages().toString());
}
KieContainer kieContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
KieBaseConfiguration kieBaseConf = ks.newKieBaseConfiguration();
kieBaseConf.setOption(SequentialOption.YES);
StatelessKieSession sequentialKsession = kieContainer.newKieBase(kieBaseConf).newStatelessKieSession();
List result = (List) sequentialKsession.execute(CommandFactory.newInsertElements(Arrays.asList("test", new Message())));
assertEquals(2, result.size());
StatelessKieSession ksession = kieContainer.getKieBase().newStatelessKieSession();
result = (List) ksession.execute(CommandFactory.newInsertElements(Arrays.asList("test", new Message())));
assertEquals(2, result.size());
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class StatefulSessionTest method testGetFactHandleEqualityBehavior.
@Test
public void testGetFactHandleEqualityBehavior() throws Exception {
final KieBaseConfiguration kbc = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbc.setOption(EqualityBehaviorOption.EQUALITY);
final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase(kbc));
final KieSession ksession = createKnowledgeSession(kbase);
final CheeseEqual cheese = new CheeseEqual("stilton", 10);
ksession.insert(cheese);
final FactHandle fh = ksession.getFactHandle(new CheeseEqual("stilton", 10));
assertNotNull(fh);
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class MarshallingTest method testMarshallWithNot.
@Test
public void testMarshallWithNot() throws Exception {
String str = "import " + getClass().getCanonicalName() + ".*\n" + "rule one\n" + "when\n" + " A()\n" + " not(B())\n" + "then\n" + "System.out.println(\"a\");\n" + "end\n" + "\n" + "rule two\n" + "when\n" + " A()\n" + "then\n" + "System.out.println(\"b\");\n" + "end\n";
KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
KieBase kBase = loadKnowledgeBaseFromString(config, str);
KieSession ksession = kBase.newKieSession();
ksession.insert(new A());
MarshallerFactory.newMarshaller(kBase).marshall(new ByteArrayOutputStream(), ksession);
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class MarshallingTest method testMarshallEntryPointsWithNot.
@Test
public void testMarshallEntryPointsWithNot() throws Exception {
String str = "package org.domain.test \n" + "import " + getClass().getCanonicalName() + ".*\n" + "global java.util.List list\n" + "declare A\n" + " @role( event )\n" + " @expires( 10m )\n" + "end\n" + "declare B\n" + "" + " @role( event )\n" + " @expires( 10m )\n" + "end\n" + "" + "rule a1\n" + "when\n" + " $a : A() from entry-point 'a-ep'\n" + " not B( this after[0s, 10s] $a) from entry-point 'a-ep'\n" + "then\n" + "list.add( $a );" + "end\n";
KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
KieBase kBase = loadKnowledgeBaseFromString(config, str);
KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksconf.setOption(ClockTypeOption.get("pseudo"));
ksconf.setOption(TimerJobFactoryOption.get("trackable"));
KieSession ksession = kBase.newKieSession(ksconf, null);
List list = new ArrayList();
ksession.setGlobal("list", list);
EntryPoint aep = ksession.getEntryPoint("a-ep");
aep.insert(new A());
ksession = marsallStatefulKnowledgeSession(ksession);
PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
timeService.advanceTime(3, TimeUnit.SECONDS);
ksession = marsallStatefulKnowledgeSession(ksession);
ksession.fireAllRules();
ksession = marsallStatefulKnowledgeSession(ksession);
assertEquals(0, list.size());
}
Aggregations