Search in sources :

Example 6 with ViewChangedEventListener

use of org.kie.api.runtime.rule.ViewChangedEventListener in project drools by kiegroup.

the class LiveQueriesBadResultTest method testCallingLiveQueryWithoutParametersButItHasParams.

@Ignore("TODO - check correct exception in this test when DROOLS-2186 is fixed.")
@Test
public void testCallingLiveQueryWithoutParametersButItHasParams() {
    final ViewChangedEventListener listener = new ViewChangedEventListener() {

        @Override
        public void rowUpdated(Row row) {
            updated.add(row.get("person"));
        }

        @Override
        public void rowInserted(Row row) {
            inserted.add(row.get("person"));
        }

        @Override
        public void rowDeleted(Row row) {
            deleted.add(row.get("person"));
        }
    };
    final KieBase kieBase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "query.drl");
    KieSession ksession = kieBase.newKieSession();
    ksession.insert(new Person("Petr"));
    ksession.openLiveQuery("queryWithParams", new Object[] {}, listener);
}
Also used : ViewChangedEventListener(org.kie.api.runtime.rule.ViewChangedEventListener) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Row(org.kie.api.runtime.rule.Row) Person(org.drools.testcoverage.common.model.Person) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with ViewChangedEventListener

use of org.kie.api.runtime.rule.ViewChangedEventListener in project drools by kiegroup.

the class QueryTest method testOpenQuery.

@Test
public void testOpenQuery() throws Exception {
    String str = "";
    str += "package org.drools.compiler.test  \n";
    str += "import org.drools.compiler.Cheese \n";
    str += "query cheeses(String $type1, String $type2) \n";
    str += "    stilton : Cheese(type == $type1, $sprice : price) \n";
    str += "    cheddar : Cheese(type == $type2, $cprice : price == stilton.price) \n";
    str += "end\n";
    KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(str));
    KieSession ksession = createKieSession(kbase);
    Cheese stilton1 = new Cheese("stilton", 1);
    Cheese cheddar1 = new Cheese("cheddar", 1);
    Cheese stilton2 = new Cheese("stilton", 2);
    Cheese cheddar2 = new Cheese("cheddar", 2);
    Cheese stilton3 = new Cheese("stilton", 3);
    Cheese cheddar3 = new Cheese("cheddar", 3);
    FactHandle s1Fh = ksession.insert(stilton1);
    ksession.insert(stilton2);
    ksession.insert(stilton3);
    ksession.insert(cheddar1);
    ksession.insert(cheddar2);
    FactHandle c3Fh = ksession.insert(cheddar3);
    final List<Object[]> updated = new ArrayList<Object[]>();
    final List<Object[]> removed = new ArrayList<Object[]>();
    final List<Object[]> added = new ArrayList<Object[]>();
    ViewChangedEventListener listener = new ViewChangedEventListener() {

        public void rowUpdated(Row row) {
            Object[] array = new Object[6];
            array[0] = row.get("stilton");
            array[1] = row.get("cheddar");
            array[2] = row.get("$sprice");
            array[3] = row.get("$cprice");
            array[4] = row.get("$type1");
            array[5] = row.get("$type2");
            updated.add(array);
        }

        public void rowDeleted(Row row) {
            Object[] array = new Object[6];
            array[0] = row.get("stilton");
            array[1] = row.get("cheddar");
            array[2] = row.get("$sprice");
            array[3] = row.get("$cprice");
            array[4] = row.get("$type1");
            array[5] = row.get("$type2");
            removed.add(array);
        }

        public void rowInserted(Row row) {
            Object[] array = new Object[6];
            array[0] = row.get("stilton");
            array[1] = row.get("cheddar");
            array[2] = row.get("$sprice");
            array[3] = row.get("$cprice");
            array[4] = row.get("$type1");
            array[5] = row.get("$type2");
            added.add(array);
        }
    };
    // Open the LiveQuery
    LiveQuery query = ksession.openLiveQuery("cheeses", new Object[] { "stilton", "cheddar" }, listener);
    ksession.fireAllRules();
    // Assert that on opening we have three rows added
    assertEquals(3, added.size());
    assertEquals(0, removed.size());
    assertEquals(0, updated.size());
    // Assert that the identifiers where retrievable
    assertSame(stilton1, added.get(2)[0]);
    assertSame(cheddar1, added.get(2)[1]);
    assertEquals(1, added.get(2)[2]);
    assertEquals(1, added.get(2)[3]);
    assertEquals("stilton", added.get(2)[4]);
    assertEquals("cheddar", added.get(2)[5]);
    // And that we have correct values from those rows
    assertEquals(3, added.get(0)[3]);
    assertEquals(2, added.get(1)[3]);
    assertEquals(1, added.get(2)[3]);
    // Do an update that causes a match to become untrue, thus triggering a removed
    cheddar3.setPrice(4);
    ksession.update(c3Fh, cheddar3);
    ksession.fireAllRules();
    assertEquals(3, added.size());
    assertEquals(1, removed.size());
    assertEquals(0, updated.size());
    assertEquals(4, removed.get(0)[3]);
    // Now make that partial true again, and thus another added
    cheddar3.setPrice(3);
    ksession.update(c3Fh, cheddar3);
    ksession.fireAllRules();
    assertEquals(4, added.size());
    assertEquals(1, removed.size());
    assertEquals(0, updated.size());
    assertEquals(3, added.get(3)[3]);
    // check a standard update
    cheddar3.setOldPrice(0);
    ksession.update(c3Fh, cheddar3);
    ksession.fireAllRules();
    assertEquals(4, added.size());
    assertEquals(1, removed.size());
    assertEquals(1, updated.size());
    assertEquals(3, updated.get(0)[3]);
    // Check a standard retract
    ksession.retract(s1Fh);
    ksession.fireAllRules();
    assertEquals(4, added.size());
    assertEquals(2, removed.size());
    assertEquals(1, updated.size());
    assertEquals(1, removed.get(1)[3]);
    // Close the query, we should get removed events for each row
    query.close();
    ksession.fireAllRules();
    assertEquals(4, added.size());
    assertEquals(4, removed.size());
    assertEquals(1, updated.size());
    assertEquals(2, removed.get(3)[3]);
    assertEquals(3, removed.get(2)[3]);
    // Check that updates no longer have any impact.
    ksession.update(c3Fh, cheddar3);
    assertEquals(4, added.size());
    assertEquals(4, removed.size());
    assertEquals(1, updated.size());
}
Also used : ViewChangedEventListener(org.kie.api.runtime.rule.ViewChangedEventListener) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) DomainObject(org.drools.compiler.DomainObject) InsertedObject(org.drools.compiler.InsertedObject) FlatQueryResultRow(org.drools.core.runtime.rule.impl.FlatQueryResultRow) Row(org.kie.api.runtime.rule.Row) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) LiveQuery(org.kie.api.runtime.rule.LiveQuery) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)7 KieBase (org.kie.api.KieBase)7 Row (org.kie.api.runtime.rule.Row)7 ViewChangedEventListener (org.kie.api.runtime.rule.ViewChangedEventListener)7 KieSession (org.kie.api.runtime.KieSession)5 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 BetaMemory (org.drools.core.reteoo.BetaMemory)3 NotNode (org.drools.core.reteoo.NotNode)3 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)3 Person (org.drools.testcoverage.common.model.Person)3 Person (org.drools.compiler.Person)2 DoubleNonIndexSkipBetaConstraints (org.drools.core.common.DoubleNonIndexSkipBetaConstraints)2 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)2 AlphaNode (org.drools.core.reteoo.AlphaNode)2 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)2 IndexableConstraint (org.drools.core.rule.IndexableConstraint)2 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)2 Ignore (org.junit.Ignore)2