Search in sources :

Example 16 with NamedEntryPoint

use of org.drools.core.common.NamedEntryPoint in project drools by kiegroup.

the class DefeasibilityTest method testDefeaterPositiveVsNegative.

@Test(timeout = 10000)
public void testDefeaterPositiveVsNegative() {
    KieSession kSession = getSession("org/drools/compiler/beliefsystem/defeasible/defeatersPosNeg.drl");
    ArrayList list = new ArrayList();
    kSession.setGlobal("list", list);
    kSession.fireAllRules();
    TruthMaintenanceSystem tms = ((NamedEntryPoint) kSession.getEntryPoint("DEFAULT")).getTruthMaintenanceSystem();
    FactType Xtype = kSession.getKieBase().getFactType("org.drools.defeasible", "X");
    ObjectHashMap keys = tms.getEqualityKeyMap();
    Iterator iter = keys.iterator();
    ObjectHashMap.ObjectEntry entry;
    while ((entry = (ObjectHashMap.ObjectEntry) iter.next()) != null) {
        EqualityKey key = (EqualityKey) entry.getValue();
        Object fact = key.getFactHandle().getObject();
        Class factClass = fact.getClass();
        if (factClass == Xtype.getFactClass()) {
            Integer val = (Integer) Xtype.get(fact, "id");
            switch(val) {
                case -1:
                    checkStatus(key, 2, DefeasibilityStatus.UNDECIDABLY);
                    break;
                case 3:
                    checkStatus(key, 1, DefeasibilityStatus.DEFEATEDLY);
                    break;
                case -35:
                    checkStatus(key, 3, DefeasibilityStatus.UNDECIDABLY);
                    break;
                case 44:
                    checkStatus(key, 2, DefeasibilityStatus.DEFEASIBLY);
                    break;
                default:
                    fail("Unrecognized fact");
            }
        } else {
            fail("Unrecognized object has been logically justified : " + factClass);
        }
    }
    assertEquals(1, list.size());
    assertTrue(list.contains(-44));
    assertTrue(!list.contains(-35));
    assertEquals(2, kSession.getFactCount());
    assertEquals(1, getNegativeObjects(kSession).size());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) EqualityKey(org.drools.core.common.EqualityKey) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ArrayList(java.util.ArrayList) Iterator(org.drools.core.util.Iterator) ObjectHashMap(org.drools.core.util.ObjectHashMap) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 17 with NamedEntryPoint

use of org.drools.core.common.NamedEntryPoint in project drools by kiegroup.

the class ReteooWorkingMemoryTest method testBasicWorkingMemoryActions.

/*
     * @see JBRULES-356
     */
@Test
@Ignore
public void testBasicWorkingMemoryActions() {
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final TruthMaintenanceSystem tms = ((NamedEntryPoint) ksession.getWorkingMemoryEntryPoint(EntryPointId.DEFAULT.getEntryPointId())).getTruthMaintenanceSystem();
    final String string = "test";
    FactHandle fd = ksession.insert(string);
    FactHandle fz = ksession.getTruthMaintenanceSystem().insert(string, null, null, new MockActivation());
    assertEquals(1, tms.getEqualityKeyMap().size());
    EqualityKey key = tms.get(string);
    assertSame(fz, key.getFactHandle());
    assertEquals(2, key.size());
    ksession.update(fd, string);
    assertEquals(1, tms.getEqualityKeyMap().size());
    key = tms.get(string);
    assertSame(fz, key.getFactHandle());
    assertEquals(2, key.size());
    ksession.retract(fd);
    assertEquals(1, tms.getEqualityKeyMap().size());
    key = tms.get(string);
    fd = ksession.insert(string);
    assertEquals(1, tms.getEqualityKeyMap().size());
    assertEquals(1, tms.getEqualityKeyMap().size());
    key = tms.get(string);
    assertSame(fd, key.getFactHandle());
    assertEquals(1, key.size());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) FactHandle(org.kie.api.runtime.rule.FactHandle) EqualityKey(org.drools.core.common.EqualityKey) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) MockActivation(org.drools.core.test.model.MockActivation) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 18 with NamedEntryPoint

use of org.drools.core.common.NamedEntryPoint in project drools by kiegroup.

the class TraitTest method testAlphaNodeSharing.

@Test
public void testAlphaNodeSharing() {
    String drl = "package test; " + "import " + Entity.class.getName() + " " + "declare trait Person\n" + "    name : String\n" + "end\n" + "rule Init " + "when " + "then " + "    don( new Entity(), Person.class ); " + "end\n" + "rule One when" + "    $core: Entity( this isA Person ) " + "then " + "end " + "rule Two when" + "    $core: Entity( this isA Person ) " + "then " + "end " + "\n";
    final KieBase kbase = getKieBaseFromString(drl);
    TraitFactory.setMode(mode, kbase);
    KieSession kSession = kbase.newKieSession();
    assertEquals(3, kSession.fireAllRules());
    NamedEntryPoint nep = ((NamedEntryPoint) kSession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId()));
    ObjectTypeNode otn = nep.getEntryPointNode().getObjectTypeNodes().get(new ClassObjectType(Entity.class));
    assertNotNull(otn);
    assertEquals(1, otn.getObjectSinkPropagator().getSinks().length);
}
Also used : Entity(org.drools.core.factmodel.traits.Entity) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 19 with NamedEntryPoint

use of org.drools.core.common.NamedEntryPoint in project drools by kiegroup.

the class FactHandleMarshallingTest method createEventFactHandle.

private InternalFactHandle createEventFactHandle(StatefulKnowledgeSessionImpl wm, InternalKnowledgeBase kBase) {
    // EntryPointNode
    Rete rete = kBase.getRete();
    NodeFactory nFacotry = kBase.getConfiguration().getComponentFactory().getNodeFactoryService();
    RuleBasePartitionId partionId = RuleBasePartitionId.MAIN_PARTITION;
    EntryPointNode entryPointNode = nFacotry.buildEntryPointNode(1, partionId, false, (ObjectSource) rete, EntryPointId.DEFAULT);
    WorkingMemoryEntryPoint wmEntryPoint = new NamedEntryPoint(EntryPointId.DEFAULT, entryPointNode, wm);
    EventFactHandle factHandle = new EventFactHandle(1, (Object) new Person(), 0, (new Date()).getTime(), 0, wmEntryPoint);
    return factHandle;
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) Rete(org.drools.core.reteoo.Rete) NodeFactory(org.drools.core.reteoo.builder.NodeFactory) RuleBasePartitionId(org.drools.core.common.RuleBasePartitionId) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EventFactHandle(org.drools.core.common.EventFactHandle) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) Person(org.drools.compiler.Person) Date(java.util.Date)

Example 20 with NamedEntryPoint

use of org.drools.core.common.NamedEntryPoint in project drools by kiegroup.

the class TruthMaintenanceTest method testLogicalInsertionsWithModify.

@Test(timeout = 10000)
public // @Ignore("in Java 8, the byte[] generated by serialization are not the same and requires investigation")
void testLogicalInsertionsWithModify() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionsWithUpdate.drl");
    KieSession ksession = kbase.newKieSession();
    final Person p = new Person("person");
    p.setAge(2);
    FactHandle h = ksession.insert(p);
    assertEquals(1, ksession.getObjects().size());
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, false);
    assertEquals(2, ksession.getObjects().size());
    Collection l = ksession.getObjects(new ClassObjectFilter(CheeseEqual.class));
    assertEquals(1, l.size());
    assertEquals(2, ((CheeseEqual) l.iterator().next()).getPrice());
    h = getFactHandle(h, ksession);
    ksession.retract(h);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, false);
    assertEquals(0, ksession.getObjects().size());
    TruthMaintenanceSystem tms = ((NamedEntryPoint) ksession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId())).getTruthMaintenanceSystem();
    final java.lang.reflect.Field field = tms.getClass().getDeclaredField("equalityKeyMap");
    field.setAccessible(true);
    final ObjectHashMap m = (ObjectHashMap) field.get(tms);
    field.setAccessible(false);
    assertEquals("assertMap should be empty", 0, m.size());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ObjectHashMap(org.drools.core.util.ObjectHashMap) CheeseEqual(org.drools.compiler.CheeseEqual) ClassObjectFilter(org.drools.core.ClassObjectFilter) KieBase(org.kie.api.KieBase) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Aggregations

NamedEntryPoint (org.drools.core.common.NamedEntryPoint)32 Test (org.junit.Test)21 KieSession (org.kie.api.runtime.KieSession)19 EqualityKey (org.drools.core.common.EqualityKey)17 TruthMaintenanceSystem (org.drools.core.common.TruthMaintenanceSystem)17 ObjectHashMap (org.drools.core.util.ObjectHashMap)16 InternalFactHandle (org.drools.core.common.InternalFactHandle)13 FactHandle (org.kie.api.runtime.rule.FactHandle)12 Iterator (org.drools.core.util.Iterator)10 FactType (org.kie.api.definition.type.FactType)10 ArrayList (java.util.ArrayList)9 Person (org.drools.compiler.Person)6 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)5 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)5 Ignore (org.junit.Ignore)5 List (java.util.List)4 ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)4 KieBase (org.kie.api.KieBase)4 Collection (java.util.Collection)3 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)3