Search in sources :

Example 1 with InitialFact

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();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) KieBase(org.kie.api.KieBase) InitialFact(org.drools.core.InitialFact) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with InitialFact

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;
}
Also used : PropagationContextFactory(org.drools.core.common.PropagationContextFactory) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ClassObjectTypeConf(org.drools.core.reteoo.ClassObjectTypeConf) PropagationContext(org.drools.core.spi.PropagationContext) InitialFact(org.drools.core.InitialFact) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Aggregations

InitialFact (org.drools.core.InitialFact)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)1 PropagationContextFactory (org.drools.core.common.PropagationContextFactory)1 ClassObjectTypeConf (org.drools.core.reteoo.ClassObjectTypeConf)1 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)1 PropagationContext (org.drools.core.spi.PropagationContext)1 Test (org.junit.Test)1 KieBase (org.kie.api.KieBase)1 KieSession (org.kie.api.runtime.KieSession)1