use of org.drools.core.InitialFact in project drools by kiegroup.
the class BackwardChainingTest method testInsertionOrderTwo.
@Test(timeout = 10000)
public void testInsertionOrderTwo() {
final StringBuilder drlBuilder = new StringBuilder("" + "package org.drools.compiler.test \n" + "import java.util.List \n" + "global List list \n" + "declare Thing \n" + " thing : String @key \n" + "end \n" + "declare Edible extends Thing \n" + "end \n" + "declare Location extends Thing \n" + " location : String @key \n" + "end \n" + "declare Here \n" + " place : String \n" + "end \n" + "rule kickOff \n" + "when \n" + " Integer( $i: intValue ) \n" + "then \n" + " switch( $i ){ \n");
String[] facts = new String[] { "new Edible( 'peach' )", "new Location( 'peach', 'table' )", "new Here( 'table' )" };
int f = 0;
for (final String fact : facts) {
for (final String fact1 : facts) {
for (final String fact2 : facts) {
// use a Set to make sure we only include 3 unique values
final Set<String> set = new HashSet<>();
set.add(fact);
set.add(fact1);
set.add(fact2);
if (set.size() == 3) {
drlBuilder.append(" case ").append(f++).append(": \n").append(" insert( ").append(fact).append(" ); \n").append(" insert( ").append(fact1).append(" ); \n").append(" insert( ").append(fact2).append(" ); \n").append(" break; \n");
}
}
}
}
facts = new String[] { "new Edible( 'peach' )", "new Location( 'table', 'office' )", "new Location( 'peach', 'table' )", "new Here( 'office' )" };
int h = f;
for (final String fact : facts) {
for (final String fact1 : facts) {
for (final String fact3 : facts) {
for (final String fact2 : facts) {
// use a Set to make sure we only include 3 unique values
final Set<String> set = new HashSet<>();
set.add(fact);
set.add(fact1);
set.add(fact3);
set.add(fact2);
if (set.size() == 4) {
drlBuilder.append(" case ").append(h++).append(": \n").append(" insert( ").append(fact).append(" ); \n").append(" insert( ").append(fact1).append(" ); \n").append(" insert( ").append(fact3).append(" ); \n").append(" insert( ").append(fact2).append(" ); \n").append(" break; \n");
}
}
}
}
}
drlBuilder.append(" } \n" + "end \n" + "\n" + "query whereFood( String x, String y ) \n" + " ( Location(x, y;) and Edible(x;) ) \n " + " or \n" + " ( Location(z, y;) and whereFood(x, z;) ) \n" + "end " + "query look(String place, List things, List food) \n" + " Here(place;) \n" + " things := List() from accumulate( Location(thing, place;), \n" + " collectList( thing ) ) \n" + " food := List() from accumulate( whereFood(thing, place;), \n" + " collectList( thing ) ) \n" + "end \n" + "rule reactiveLook \n" + "when \n" + " Here( $place : place) \n" + " look($place, $things, $food;) \n" + "then \n" + " list.addAll( $things ); \n" + " list.addAll( $food ); \n" + "end \n" + "");
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("backward-chaining-test", kieBaseTestConfiguration, drlBuilder.toString());
for (int i = 0; i < f; i++) {
final KieSession ksession = kbase.newKieSession();
try {
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
list.clear();
InternalFactHandle fh = (InternalFactHandle) ksession.insert(i);
ksession.fireAllRules();
assertEquals(2, list.size());
assertEquals("peach", list.get(0));
assertEquals("peach", list.get(1));
list.clear();
final InternalFactHandle[] handles = ksession.getFactHandles().toArray(new InternalFactHandle[0]);
for (int j = 0; j < handles.length; j++) {
if (handles[j].getObject() instanceof InitialFact || handles[j].getObject() instanceof Integer) {
continue;
}
handles[j] = getFactHandle(handles[j], ksession);
final Object o = handles[j].getObject();
// first retract + assert
ksession.delete(handles[j]);
handles[j] = (InternalFactHandle) ksession.insert(o);
ksession.fireAllRules();
assertEquals(2, list.size());
assertEquals("peach", list.get(0));
assertEquals("peach", list.get(1));
list.clear();
// now try update
// session was serialised so need to get factHandle
handles[j] = getFactHandle(handles[j], ksession);
ksession.update(handles[j], handles[j].getObject());
ksession.fireAllRules();
assertEquals(2, list.size());
assertEquals("peach", list.get(0));
assertEquals("peach", list.get(1));
list.clear();
}
fh = getFactHandle(fh, ksession);
ksession.delete(fh);
} finally {
ksession.dispose();
}
}
for (int i = f; i < h; i++) {
final KieSession ksession = kbase.newKieSession();
try {
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
list.clear();
InternalFactHandle fh = (InternalFactHandle) ksession.insert(i);
ksession.fireAllRules();
assertEquals(2, list.size());
assertEquals("table", list.get(0));
assertEquals("peach", list.get(1));
list.clear();
final InternalFactHandle[] handles = ksession.getFactHandles().toArray(new InternalFactHandle[0]);
for (int j = 0; j < handles.length; j++) {
if (handles[j].getObject() instanceof InitialFact || handles[j].getObject() instanceof Integer) {
continue;
}
handles[j] = getFactHandle(handles[j], ksession);
final Object o = handles[j].getObject();
ksession.delete(handles[j]);
handles[j] = (InternalFactHandle) ksession.insert(o);
ksession.fireAllRules();
assertEquals(2, list.size());
assertEquals("table", list.get(0));
assertEquals("peach", list.get(1));
list.clear();
// now try update
handles[j] = getFactHandle(handles[j], ksession);
ksession.update(handles[j], handles[j].getObject());
ksession.fireAllRules();
assertEquals(2, list.size());
assertEquals("table", list.get(0));
assertEquals("peach", list.get(1));
list.clear();
}
fh = getFactHandle(fh, ksession);
ksession.delete(fh);
} finally {
ksession.dispose();
}
}
}
use of org.drools.core.InitialFact in project drools by kiegroup.
the class StatefulKnowledgeSessionImpl method initInitialFact.
public InternalFactHandle initInitialFact(InternalKnowledgeBase kBase, InternalWorkingMemoryEntryPoint entryPoint, EntryPointId epId, MarshallerReaderContext context) {
InitialFact initialFact = InitialFactImpl.getInstance();
InternalFactHandle handle = new DefaultFactHandle(0, initialFact, 0, entryPoint);
ClassObjectTypeConf otc = (ClassObjectTypeConf) entryPoint.getObjectTypeConfigurationRegistry().getObjectTypeConf(epId, initialFact);
ObjectTypeNode otn = otc.getConcreteObjectTypeNode();
if (otn != null) {
PropagationContextFactory ctxFact = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
PropagationContext pctx = ctxFact.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, handle, epId, context);
otn.assertInitialFact(handle, pctx, this);
}
return handle;
}
Aggregations