use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class TruthMaintenanceTest method testJustificationStateOverridingBySuperClass.
@Test(timeout = 10000)
public void testJustificationStateOverridingBySuperClass() {
// DROOLS-352
String droolsSource = "package org.drools.tms.test; \n" + "" + "declare Foo end \n" + "declare Bar extends Foo end \n" + "" + "rule Justify_Sub \n" + "when \n" + "then \n" + " insertLogical( new Bar() ); \n" + " insertLogical( 42.0 ); \n" + "end \n" + "rule Justify_Sup \n" + "when \n" + " $d : Double() \n" + "then \n" + " insertLogical( new Foo() ); \n" + "end \n" + "" + "" + "rule Fuu when Foo() then end \n" + "rule Bor when Bar() then end \n";
// ///////////////////////////////////
KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kieConf.setOption(EqualityBehaviorOption.EQUALITY);
KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
KieSession session = kbase.newKieSession();
session.fireAllRules();
for (FactHandle fh : session.getFactHandles()) {
InternalFactHandle ifh = (InternalFactHandle) fh;
assertEquals(EqualityKey.JUSTIFIED, ifh.getEqualityKey().getStatus());
}
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalThenUpdateAsStatedShadowSingleOccurance.
@Test(timeout = 10000)
public void testLogicalThenUpdateAsStatedShadowSingleOccurance() {
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);
session.insert("go1");
session.fireAllRules();
TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
assertEquals(EqualityKey.JUSTIFIED, jfh1.getEqualityKey().getStatus());
InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
session.insert("go2");
session.fireAllRules();
assertEquals(EqualityKey.STATED, fh1.getEqualityKey().getStatus());
assertSame(fh1.getEqualityKey(), jfh1.getEqualityKey());
assertNotSame(fh1, jfh1);
// Make sure f1 only occurs once
assertEquals(1, list.size());
assertEquals("f1", list.get(0));
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class AbductionTest method testGenesExplanationBackTracking.
@Test
@Ignore("Not implemented yet")
public void testGenesExplanationBackTracking() {
// from wikipedia, abductive reasoning example
/*
feed(lactose):-make(permease),make(galactosidase).
make(Enzyme):-code(Gene,Enzyme),express(Gene).
express(lac(X)):-amount(glucose,low),amount(lactose,hi).
express(lac(X)):-amount(glucose,medium),amount(lactose,medium).
code(lac(y),permease).
code(lac(z),galactosidase).
temperature(low):-amount(glucose,low).
*/
String droolsSource = "package org.drools.abductive.test; \n" + "import org.kie.api.runtime.rule.Match;\n" + "" + "declare Amount " + " enz : String @key " + " level : String @key " + "end " + "query feed( String $enz ) " + " $enz := String() from 'lactose' " + " ?make( 'permease' ; ) " + " ?make( 'galactosidase' ; ) " + "end " + "query make( String $enz ) " + " ?code( $gen, $enz ; ) and ?express( $gen ; ) " + "end " + "query code( String $gen, String $enz ) " + " ( $gen := String() from 'lacY' and $enz := String() from 'permease' ) " + " or " + " ( $gen := String() from 'lacZ' and $enz := String() from 'galactosidase' ) " + "end " + "query express( String $gen ) " + " ( ?amount( 'glucose', 'low' ; ) and ?amount( 'lactose', 'hi' ; ) ) " + " or " + " ( ?amount( 'glucose', 'medium' ; ) and ?amount( 'lactose', 'medium' ; ) ) " + "end " + "query amount( String $enz, String $lev ) @Abductive( target=Amount.class ) end " + "rule Check " + "when " + " ?feed( 'lactose' ; ) " + "then " + " System.out.println( 'YES' ); " + "end " + "rule Match " + "when " + " $m : Match( rule.name != 'Match' ) " + "then" + " System.out.println( $m ); " + "end " + "";
// ///////////////////////////////////
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(DeclarativeAgendaOption.ENABLED);
KieSession session = getSessionFromString(droolsSource, kconf);
session.fireAllRules();
for (Object o : session.getObjects()) {
System.out.println(">>> " + o);
}
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class DefeasibilityTest method getSessionFromString.
protected KieSession getSessionFromString(String drlString) {
KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
try {
System.setProperty("drools.negatable", "on");
kBuilder.add(ResourceFactory.newByteArrayResource(drlString.getBytes()), ResourceType.DRL);
if (kBuilder.hasErrors()) {
System.err.println(kBuilder.getErrors());
fail();
}
} finally {
System.setProperty("drools.negatable", "off");
}
KieBaseConfiguration kieBaseConfiguration = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kieBaseConfiguration.setOption(EqualityBehaviorOption.EQUALITY);
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase(kieBaseConfiguration);
kBase.addPackages(kBuilder.getKnowledgePackages());
KieSessionConfiguration ksConf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
((SessionConfiguration) ksConf).setBeliefSystemType(BeliefSystemType.DEFEASIBLE);
KieSession kSession = kBase.newKieSession(ksConf, null);
return kSession;
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class ObjectHashMapTest method testIdentityWithResize.
@Test
public void testIdentityWithResize() {
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(EqualityBehaviorOption.IDENTITY);
KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kconf);
KieSession ksession = kbase.newKieSession();
int length = 1 * 300 * 1000;
List<FactHandle> handles = new ArrayList<FactHandle>(1000);
List<String> objects = new ArrayList<String>(1000);
for (int i = 0; i < length; i++) {
String s = getPropertyName(i);
FactHandle handle = ksession.insert(s);
objects.add(s);
handles.add(handle);
}
for (int i = 0; i < length; i++) {
String s = objects.get(i);
FactHandle handle = handles.get(i);
assertEquals(s, ksession.getObject(handle));
assertSame(handle, ksession.getFactHandle(s));
// now check with disconnected facthandle
handle = DefaultFactHandle.createFromExternalFormat(((DefaultFactHandle) handle).toExternalForm());
assertEquals(s, ksession.getObject(handle));
}
for (int i = 0; i < length; i++) {
FactHandle handle = handles.get(i);
// now retract with disconnected facthandle
handle = DefaultFactHandle.createFromExternalFormat(((DefaultFactHandle) handle).toExternalForm());
ksession.retract(handle);
assertEquals(length - i - 1, ksession.getObjects().size());
assertEquals(length - i - 1, ksession.getFactHandles().size());
}
assertEquals(0, ksession.getObjects().size());
assertEquals(0, ksession.getFactHandles().size());
}
Aggregations