Search in sources :

Example 1 with ObjectEntry

use of org.drools.core.util.ObjectHashMap.ObjectEntry in project drools by kiegroup.

the class JTMSTest method testChangeInNegativePrime.

@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public void testChangeInNegativePrime() {
    String s = "package org.drools.core.beliefsystem.jtms;\n" + "\n" + "import org.kie.internal.event.rule.ActivationUnMatchListener;\n" + "import java.util.List \n" + "import org.drools.core.common.AgendaItem;" + "import org.drools.compiler.Person;" + "global java.util.List list;\n" + "\n" + "declare entry-point 'neg' end \n" + "" + "rule \"go1\"\n" + "when\n" + "    String( this == 'go1' )\n" + "then\n" + "    Person p = new Person( 'darth' ); \n" + "    p.setNotInEqualTestObject(1); \n" + "    insertLogical( p, 'neg' );\n" + "end\n" + "rule \"go2\"\n" + "when\n" + "    String( this == 'go2' )\n" + "then\n" + "    Person p = new Person( 'darth' ); \n" + "    p.setNotInEqualTestObject(2); \n" + "    insertLogical( p, 'neg' );\n" + "end\n" + "rule \"go3\"\n" + "when\n" + "    String( this == 'go3' )\n" + "then\n" + "    Person p = new Person( 'darth' ); \n" + "    p.setNotInEqualTestObject(3); \n" + "    insertLogical( p, 'neg' );\n" + "end\n" + "\n";
    KieSession kSession = getSessionFromString(s);
    List list = new ArrayList();
    kSession.setGlobal("list", list);
    // We want to make sure go1 is prime, and then that it switches to go2
    FactHandle fhGo1 = kSession.insert("go1");
    kSession.fireAllRules();
    FactHandle fhGo2 = kSession.insert("go2");
    kSession.fireAllRules();
    FactHandle fhGo3 = kSession.insert("go3");
    kSession.fireAllRules();
    NamedEntryPoint ep = (NamedEntryPoint) ((StatefulKnowledgeSessionImpl) kSession).getEntryPoint("DEFAULT");
    // just go1, go2, go3
    assertEquals(3, ep.getObjects().size());
    // Person(darth)
    assertEquals(1, getNegativeObjects(kSession).size());
    int count = 0;
    for (Object object : getNegativeObjects(kSession)) {
        if (object instanceof Person) {
            assertEquals(new Integer(1), ((Person) object).getNotInEqualTestObject());
            count++;
        }
    }
    assertEquals(1, count);
    ObjectHashMap equalityMap = ep.getTruthMaintenanceSystem().getEqualityKeyMap();
    // Only Person type is logical
    assertEquals(1, equalityMap.size());
    org.drools.core.util.Iterator it = equalityMap.iterator();
    EqualityKey key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    }
    assertEquals(3, key.getBeliefSet().size());
    assertEquals(new Integer(1), ((Person) ((JTMSBeliefSetImpl) key.getBeliefSet()).getFactHandle().getObject()).getNotInEqualTestObject());
    kSession.retract(fhGo1);
    kSession.fireAllRules();
    it = equalityMap.iterator();
    key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    }
    assertEquals(2, key.getBeliefSet().size());
    assertEquals(new Integer(3), ((Person) ((JTMSBeliefSetImpl) key.getBeliefSet()).getFactHandle().getObject()).getNotInEqualTestObject());
    kSession.retract(fhGo3);
    kSession.fireAllRules();
    it = equalityMap.iterator();
    key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    }
    assertEquals(1, key.getBeliefSet().size());
    assertEquals(new Integer(2), ((Person) ((JTMSBeliefSetImpl) key.getBeliefSet()).getFactHandle().getObject()).getNotInEqualTestObject());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ArrayList(java.util.ArrayList) ObjectHashMap(org.drools.core.util.ObjectHashMap) ObjectEntry(org.drools.core.util.ObjectHashMap.ObjectEntry) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EqualityKey(org.drools.core.common.EqualityKey) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.compiler.Person) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with ObjectEntry

use of org.drools.core.util.ObjectHashMap.ObjectEntry in project drools by kiegroup.

the class ObjectHashSet method remove.

public boolean remove(final Object value) {
    final int hashCode = this.comparator.hashCodeOf(value);
    final int index = indexOf(hashCode, this.table.length);
    ObjectEntry previous = (ObjectEntry) this.table[index];
    ObjectEntry current = previous;
    while (current != null) {
        final ObjectEntry next = (ObjectEntry) current.getNext();
        if (hashCode == current.cachedHashCode && this.comparator.equal(value, current.value)) {
            if (previous == current) {
                this.table[index] = next;
            } else {
                previous.setNext(next);
            }
            current.setNext(null);
            this.size--;
            return true;
        }
        previous = current;
        current = next;
    }
    return false;
}
Also used : ObjectEntry(org.drools.core.util.ObjectHashMap.ObjectEntry)

Example 3 with ObjectEntry

use of org.drools.core.util.ObjectHashMap.ObjectEntry in project drools by kiegroup.

the class ObjectHashSet method add.

public boolean add(final Object value, final boolean checkExists) {
    final int hashCode = this.comparator.hashCodeOf(value);
    final int index = indexOf(hashCode, this.table.length);
    // scan the linked entries to see if it exists
    if (checkExists) {
        ObjectEntry current = (ObjectEntry) this.table[index];
        while (current != null) {
            if (hashCode == current.cachedHashCode && this.comparator.equal(value, current.value)) {
                final Object oldValue = current.value;
                current.value = value;
                return true;
            }
            current = (ObjectEntry) current.getNext();
        }
    }
    // We aren't checking the key exists, or it didn't find the key
    final ObjectEntry entry = new ObjectEntry(value, hashCode);
    entry.next = this.table[index];
    this.table[index] = entry;
    if (this.size++ >= this.threshold) {
        resize(2 * this.table.length);
    }
    return false;
}
Also used : ObjectEntry(org.drools.core.util.ObjectHashMap.ObjectEntry)

Example 4 with ObjectEntry

use of org.drools.core.util.ObjectHashMap.ObjectEntry in project drools by kiegroup.

the class JTMSTest method testChangeInPositivePrime.

@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public void testChangeInPositivePrime() {
    String s = "package org.drools.core.beliefsystem.jtms;\n" + "\n" + "import org.kie.internal.event.rule.ActivationUnMatchListener;\n" + "import java.util.List \n" + "import org.drools.core.common.AgendaItem;" + "import org.drools.compiler.Person;" + "global java.util.List list;\n" + "\n" + "rule \"go1\"\n" + "when\n" + "    String( this == 'go1' )\n" + "then\n" + "    Person p = new Person( 'darth' ); \n" + "    p.setNotInEqualTestObject(1); \n" + "    insertLogical( p );\n" + "end\n" + "rule \"go2\"\n" + "when\n" + "    String( this == 'go2' )\n" + "then\n" + "    Person p = new Person( 'darth' ); \n" + "    p.setNotInEqualTestObject(2); \n" + "    insertLogical( p );\n" + "end\n" + "rule \"go3\"\n" + "when\n" + "    String( this == 'go3' )\n" + "then\n" + "    Person p = new Person( 'darth' ); \n" + "    p.setNotInEqualTestObject(3); \n" + "    insertLogical( p );\n" + "end\n" + "\n";
    KieSession kSession = getSessionFromString(s);
    List list = new ArrayList();
    kSession.setGlobal("list", list);
    // We want to make sure go1 is prime, and then that it switches to go2
    FactHandle fhGo1 = kSession.insert("go1");
    kSession.fireAllRules();
    FactHandle fhGo2 = kSession.insert("go2");
    kSession.fireAllRules();
    FactHandle fhGo3 = kSession.insert("go3");
    kSession.fireAllRules();
    NamedEntryPoint ep = (NamedEntryPoint) ((StatefulKnowledgeSessionImpl) kSession).getEntryPoint("DEFAULT");
    // just go1, go2, go3, Person(darth)
    assertEquals(4, ep.getObjects().size());
    int count = 0;
    for (Object object : ep.getObjects()) {
        if (object instanceof Person) {
            assertEquals(new Integer(1), ((Person) object).getNotInEqualTestObject());
            count++;
        }
    }
    assertEquals(1, count);
    ObjectHashMap equalityMap = ep.getTruthMaintenanceSystem().getEqualityKeyMap();
    // Only Person type is logical
    assertEquals(1, equalityMap.size());
    org.drools.core.util.Iterator it = equalityMap.iterator();
    EqualityKey key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    }
    assertEquals(3, key.getBeliefSet().size());
    assertEquals(new Integer(1), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
    kSession.retract(fhGo1);
    kSession.fireAllRules();
    it = equalityMap.iterator();
    key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    }
    assertEquals(2, key.getBeliefSet().size());
    assertEquals(new Integer(3), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
    kSession.retract(fhGo3);
    kSession.fireAllRules();
    it = equalityMap.iterator();
    key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals(new Person("darth"))) {
        key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    }
    assertEquals(1, key.getBeliefSet().size());
    assertEquals(new Integer(2), ((Person) key.getBeliefSet().getFactHandle().getObject()).getNotInEqualTestObject());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ArrayList(java.util.ArrayList) ObjectHashMap(org.drools.core.util.ObjectHashMap) ObjectEntry(org.drools.core.util.ObjectHashMap.ObjectEntry) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EqualityKey(org.drools.core.common.EqualityKey) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.compiler.Person) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with ObjectEntry

use of org.drools.core.util.ObjectHashMap.ObjectEntry in project drools by kiegroup.

the class JTMSTest method testRetractHandleWhenOnlyNeg.

@Test(timeout = 10000)
@Ignore("Currently cannot support updates")
public void testRetractHandleWhenOnlyNeg() {
    String s = "package org.drools.core.beliefsystem.jtms;\n" + "\n" + "import java.util.List \n" + "import org.drools.core.common.AgendaItem;" + "global java.util.List list;\n" + "\n" + "rule \"go1_1\"\n" + "when\n" + "    String( this == 'go1' )\n" + "then\n" + "    insertLogical( new String( 'neg' ), 'neg' );\n" + "end\n" + "rule \"go1_2\"\n" + "when\n" + "    String( this == 'go1' )\n" + "then\n" + "    insertLogical( new String( 'neg' ), 'neg' );\n" + "end\n" + "rule \"go1_3\"\n" + "when\n" + "    String( this == 'go1' )\n" + "then\n" + "    insertLogical( new String( 'neg' ), 'neg' );\n" + "end\n" + "\n" + "rule \"Negative\"\n" + "when\n" + "    $n : String(  _.neg, this != 'go1' || == 'go2' ) \n" + "then\n" + "    final String s = '-' + $n; \n" + "    final List l = list; \n" + "    l.add( s ); \n" + "end\n";
    KieSession kSession = getSessionFromString(s);
    List list = new ArrayList();
    kSession.setGlobal("list", list);
    ((RuleEventManager) kSession).addEventListener(new RuleEventListener() {

        @Override
        public void onDeleteMatch(Match match) {
            String rule = match.getRule().getName();
            if (rule.equals("Negative")) {
                list.remove("-" + match.getDeclarationValue("$n"));
            }
        }
    });
    FactHandle fhGo1 = kSession.insert("go1");
    kSession.fireAllRules();
    assertTrue(list.contains("-neg"));
    // just go1
    assertEquals(1, kSession.getEntryPoint("DEFAULT").getObjects().size());
    assertEquals(1, getNegativeObjects(kSession).size());
    NamedEntryPoint ep = (NamedEntryPoint) ((StatefulKnowledgeSessionImpl) kSession).getEntryPoint("DEFAULT");
    ObjectHashMap equalityMap = ep.getTruthMaintenanceSystem().getEqualityKeyMap();
    // go1, neg are two different strings.
    assertEquals(2, equalityMap.size());
    org.drools.core.util.Iterator it = equalityMap.iterator();
    EqualityKey key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    while (!key.getFactHandle().getObject().equals("neg")) {
        key = (EqualityKey) ((ObjectEntry) it.next()).getValue();
    }
    assertEquals(3, key.getBeliefSet().size());
    ep.getTruthMaintenanceSystem().delete(key.getLogicalFactHandle());
    assertEquals(0, key.getBeliefSet().size());
    // just go1
    assertEquals(1, kSession.getEntryPoint("DEFAULT").getObjects().size());
    assertEquals(0, getNegativeObjects(kSession).size());
    assertEquals(0, key.getBeliefSet().size());
    assertEquals(1, ep.getTruthMaintenanceSystem().getEqualityKeyMap().size());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ArrayList(java.util.ArrayList) RuleEventListener(org.kie.internal.event.rule.RuleEventListener) ObjectHashMap(org.drools.core.util.ObjectHashMap) ObjectEntry(org.drools.core.util.ObjectHashMap.ObjectEntry) Match(org.kie.api.runtime.rule.Match) RuleEventManager(org.kie.internal.event.rule.RuleEventManager) EqualityKey(org.drools.core.common.EqualityKey) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

ObjectEntry (org.drools.core.util.ObjectHashMap.ObjectEntry)11 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 EqualityKey (org.drools.core.common.EqualityKey)3 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)3 ObjectHashMap (org.drools.core.util.ObjectHashMap)3 Ignore (org.junit.Ignore)3 KieSession (org.kie.api.runtime.KieSession)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 Person (org.drools.compiler.Person)2 Iterator (org.drools.core.util.Iterator)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 IndexableConstraint (org.drools.core.rule.IndexableConstraint)1 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)1 Match (org.kie.api.runtime.rule.Match)1 RuleEventListener (org.kie.internal.event.rule.RuleEventListener)1 RuleEventManager (org.kie.internal.event.rule.RuleEventManager)1