Package org.kie.api.runtime.rule

Examples of org.kie.api.runtime.rule.LiveQuery


                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() );
View Full Code Here


                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() );
View Full Code Here

                      notMemory.getRightTupleMemory().size() );

        // Now execute an open query and ensure the memory is left populated
        food.clear();
        final List foodUpdated = new ArrayList();
        LiveQuery query = ksession.openLiveQuery( "look",
                                                  new Object[]{"kitchen", Variable.v},
                                                  new ViewChangedEventListener() {

                                                      public void rowUpdated(Row row) {
                                                          foodUpdated.addAll( (Collection) row.get( "food" ) );
                                                      }

                                                      public void rowDeleted(Row row) {
                                                      }

                                                      public void rowInserted(Row row) {
                                                          food.addAll( (Collection) row.get( "food" ) );
                                                      }
                                                  } );
        assertEquals( 2,
                      food.size() );
        assertContains( new String[]{"crackers", "apple"},
                        food );

        assertEquals( 2,
                      accMemory.betaMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      existsMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      fromMemory.betaMemory.getLeftTupleMemory().size() );
        assertEquals( 0,
                      notMemory.getRightTupleMemory().size() );

        food.clear();
        // Now try again, make sure it only delete's it's own tuples
        results = ksession.getQueryResults( "look",
                                            new Object[]{"kitchen", Variable.v} );
        assertEquals( 1,
                      results.size() );

        for ( org.kie.api.runtime.rule.QueryResultsRow row : results ) {
            food.addAll( (Collection) row.get( "food" ) );
            logger.debug( row.get( "food" ).toString() );
        }
        assertEquals( 2,
                      food.size() );
        assertContains( new String[]{"crackers", "apple"},
                        food );

        assertEquals( 2,
                      accMemory.betaMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      existsMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      fromMemory.betaMemory.getLeftTupleMemory().size() );
        assertEquals( 0,
                      notMemory.getRightTupleMemory().size() );
        food.clear();

        // do an update and check it's  still memory size 2
        // however this time the food should be empty, as 'crackers' now blocks the not.
        ksession.update( fh,
                         "crackers" );
        ksession.fireAllRules();

        assertEquals( 2,
                      accMemory.betaMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      existsMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      fromMemory.betaMemory.getLeftTupleMemory().size() );
        assertEquals( 1,
                      notMemory.getRightTupleMemory().size() );

        assertEquals( 0, foodUpdated.size() );

        // do an update and check it's  still memory size 2
        // this time
        ksession.update( fh,
                         "oranges" );
        ksession.fireAllRules();

        assertEquals( 2,
                      accMemory.betaMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      existsMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      fromMemory.betaMemory.getLeftTupleMemory().size() );
        assertEquals( 0,
                      notMemory.getRightTupleMemory().size() );

        assertEquals( 2,
                      food.size() );
        assertContains( new String[]{"crackers", "apple"},
                        food );

        // Close the open
        query.close();
        assertEquals( 0,
                      accMemory.betaMemory.getRightTupleMemory().size() );
        assertEquals( 0,
                      existsMemory.getRightTupleMemory().size() );
        assertEquals( 0,
View Full Code Here

        org.kie.api.runtime.rule.FactHandle c2Fh = ksession.insert( cheddar2 );
        org.kie.api.runtime.rule.FactHandle c3Fh = ksession.insert( cheddar3 );
                     
        DroolsEventList list = new DroolsEventList();
        // Open the LiveQuery
        LiveQuery query = ksession.openLiveQuery( "cheeses", new Object[] { "cheddar", "stilton" } , list );
       
        SortedList<Row> sorted = new SortedList<Row>( list, new Comparator<Row>() {

            public int compare(Row r1,
                               Row r2) {
                Cheese c1 = ( Cheese ) r1.get( "stilton" );
                Cheese c2 = ( Cheese ) r2.get( "stilton" );
                return c1.getPrice() - c2.getPrice();
            }
        });

       
        assertEquals( 3, sorted.size() );
        assertEquals( 1, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() );
        assertEquals( 2, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() );
        assertEquals( 3, ((Cheese)sorted.get( 2 ).get( "stilton" )).getPrice() );

        // alter the price to remove the last row
        stilton3.setPrice( 4 );
        ksession.updates3Fh, stilton3 );
        ksession.fireAllRules();
       
        assertEquals( 2, sorted.size() );
        assertEquals( 1, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() );
        assertEquals( 2, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() );

        // alter the price to put the last row back in
        stilton3.setPrice( 3 );
        ksession.updates3Fh, stilton3 );
        ksession.fireAllRules();

        assertEquals( 3, sorted.size() );
        assertEquals( 1, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() );
        assertEquals( 2, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() );
        assertEquals( 3, ((Cheese)sorted.get( 2 ).get( "stilton" )).getPrice() );
       
        // alter the price to remove the middle row
        stilton2.setPrice( 4 );
        ksession.updates2Fh, stilton2 );
        ksession.fireAllRules();

        assertEquals( 2, sorted.size() );
        assertEquals( 1, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() );
        assertEquals( 3, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() );
       
        // alter the price to add the previous middle rows to the end
        cheddar2.setPrice( 4 );
        ksession.updatec2Fh, cheddar2 );
        ksession.fireAllRules();

        assertEquals( 3, sorted.size() );
        assertEquals( 1, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() );
        assertEquals( 3, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() );
        assertEquals( 4, ((Cheese)sorted.get( 2 ).get( "stilton" )).getPrice() );
           
        // Check a standard retract
        ksession.retract( s1Fh );
        ksession.fireAllRules();

        assertEquals( 2, sorted.size() );
        assertEquals( 3, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() );
        assertEquals( 4, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() );

        // Close the query, we should get removed events for each row
        query.close();
       
        assertEquals( 0, sorted.size() );
      
    }
View Full Code Here

                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() );
View Full Code Here

                      notMemory.getRightTupleMemory().size() );

        // Now execute an open query and ensure the memory is left populated
        food.clear();
        final List foodUpdated = new ArrayList();
        LiveQuery query = ksession.openLiveQuery( "look",
                                                  new Object[]{"kitchen", Variable.v},
                                                  new ViewChangedEventListener() {

                                                      public void rowUpdated(Row row) {
                                                          foodUpdated.addAll( (Collection) row.get( "food" ) );
                                                      }

                                                      public void rowDeleted(Row row) {
                                                      }

                                                      public void rowInserted(Row row) {
                                                          food.addAll( (Collection) row.get( "food" ) );
                                                      }
                                                  } );
        assertEquals( 2,
                      food.size() );
        assertContains( new String[]{"crackers", "apple"},
                        food );

        assertEquals( 2,
                      accMemory.betaMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      existsMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      fromMemory.betaMemory.getLeftTupleMemory().size() );
        assertEquals( 0,
                      notMemory.getRightTupleMemory().size() );

        food.clear();
        // Now try again, make sure it only delete's it's own tuples
        results = ksession.getQueryResults( "look",
                                            new Object[]{"kitchen", Variable.v} );
        assertEquals( 1,
                      results.size() );

        for ( org.kie.api.runtime.rule.QueryResultsRow row : results ) {
            food.addAll( (Collection) row.get( "food" ) );
            logger.debug( row.get( "food" ).toString() );
        }
        assertEquals( 2,
                      food.size() );
        assertContains( new String[]{"crackers", "apple"},
                        food );

        assertEquals( 2,
                      accMemory.betaMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      existsMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      fromMemory.betaMemory.getLeftTupleMemory().size() );
        assertEquals( 0,
                      notMemory.getRightTupleMemory().size() );
        food.clear();

        // do an update and check it's  still memory size 2
        // however this time the food should be empty, as 'crackers' now blocks the not.
        ksession.update( fh,
                         "crackers" );
        ksession.fireAllRules();

        assertEquals( 2,
                      accMemory.betaMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      existsMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      fromMemory.betaMemory.getLeftTupleMemory().size() );
        assertEquals( 1,
                      notMemory.getRightTupleMemory().size() );

        assertEquals( 0, foodUpdated.size() );

        // do an update and check it's  still memory size 2
        // this time
        ksession.update( fh,
                         "oranges" );
        ksession.fireAllRules();

        assertEquals( 2,
                      accMemory.betaMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      existsMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      fromMemory.betaMemory.getLeftTupleMemory().size() );
        assertEquals( 0,
                      notMemory.getRightTupleMemory().size() );

        assertEquals( 2,
                      food.size() );
        assertContains( new String[]{"crackers", "apple"},
                        food );

        // Close the open
        query.close();
        assertEquals( 0,
                      accMemory.betaMemory.getRightTupleMemory().size() );
        assertEquals( 0,
                      existsMemory.getRightTupleMemory().size() );
        assertEquals( 0,
View Full Code Here

                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() );
View Full Code Here

        FactHandle c2Fh = ksession.insert( cheddar2 );
        FactHandle c3Fh = ksession.insert( cheddar3 );
                     
        DroolsEventList list = new DroolsEventList();
        // Open the LiveQuery
        LiveQuery query = ksession.openLiveQuery( "cheeses", new Object[] { "cheddar", "stilton" } , list );
       
        SortedList<Row> sorted = new SortedList<Row>( list, new Comparator<Row>() {

            public int compare(Row r1,
                               Row r2) {
                Cheese c1 = ( Cheese ) r1.get( "stilton" );
                Cheese c2 = ( Cheese ) r2.get( "stilton" );
                return c1.getPrice() - c2.getPrice();
            }
        });

       
        assertEquals( 3, sorted.size() );
        assertEquals( 1, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() );
        assertEquals( 2, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() );
        assertEquals( 3, ((Cheese)sorted.get( 2 ).get( "stilton" )).getPrice() );

        // alter the price to remove the last row
        stilton3.setPrice( 4 );
        ksession.updates3Fh, stilton3 );
        ksession.fireAllRules();
       
        assertEquals( 2, sorted.size() );
        assertEquals( 1, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() );
        assertEquals( 2, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() );

        // alter the price to put the last row back in
        stilton3.setPrice( 3 );
        ksession.updates3Fh, stilton3 );
        ksession.fireAllRules();

        assertEquals( 3, sorted.size() );
        assertEquals( 1, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() );
        assertEquals( 2, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() );
        assertEquals( 3, ((Cheese)sorted.get( 2 ).get( "stilton" )).getPrice() );
       
        // alter the price to remove the middle row
        stilton2.setPrice( 4 );
        ksession.updates2Fh, stilton2 );
        ksession.fireAllRules();

        assertEquals( 2, sorted.size() );
        assertEquals( 1, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() );
        assertEquals( 3, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() );
       
        // alter the price to add the previous middle rows to the end
        cheddar2.setPrice( 4 );
        ksession.updatec2Fh, cheddar2 );
        ksession.fireAllRules();

        assertEquals( 3, sorted.size() );
        assertEquals( 1, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() );
        assertEquals( 3, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() );
        assertEquals( 4, ((Cheese)sorted.get( 2 ).get( "stilton" )).getPrice() );
           
        // Check a standard retract
        ksession.retract( s1Fh );
        ksession.fireAllRules();

        assertEquals( 2, sorted.size() );
        assertEquals( 3, ((Cheese)sorted.get( 0 ).get( "stilton" )).getPrice() );
        assertEquals( 4, ((Cheese)sorted.get( 1 ).get( "stilton" )).getPrice() );

        // Close the query, we should get removed events for each row
        query.close();
       
        assertEquals( 0, sorted.size() );
      
    }
View Full Code Here

                      notMemory.getRightTupleMemory().size() );

        // Now execute an open query and ensure the memory is left populated
        food.clear();
        final List foodUpdated = new ArrayList();
        LiveQuery query = ksession.openLiveQuery( "look",
                                                  new Object[]{"kitchen", Variable.v},
                                                  new ViewChangedEventListener() {

                                                      public void rowUpdated(Row row) {
                                                          foodUpdated.addAll( (Collection) row.get( "food" ) );
                                                      }

                                                      public void rowDeleted(Row row) {
                                                      }

                                                      public void rowInserted(Row row) {
                                                          food.addAll( (Collection) row.get( "food" ) );
                                                      }
                                                  } );
        assertEquals( 2,
                      food.size() );
        assertContains( new String[]{"crackers", "apple"},
                        food );

        assertEquals( 2,
                      accMemory.betaMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      existsMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      fromMemory.betaMemory.getLeftTupleMemory().size() );
        assertEquals( 0,
                      notMemory.getRightTupleMemory().size() );

        food.clear();
        // Now try again, make sure it only delete's it's own tuples
        results = ksession.getQueryResults( "look",
                                            new Object[]{"kitchen", Variable.v} );
        assertEquals( 1,
                      results.size() );

        for ( org.kie.api.runtime.rule.QueryResultsRow row : results ) {
            food.addAll( (Collection) row.get( "food" ) );
            logger.debug( row.get( "food" ).toString() );
        }
        assertEquals( 2,
                      food.size() );
        assertContains( new String[]{"crackers", "apple"},
                        food );

        assertEquals( 2,
                      accMemory.betaMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      existsMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      fromMemory.betaMemory.getLeftTupleMemory().size() );
        assertEquals( 0,
                      notMemory.getRightTupleMemory().size() );
        food.clear();

        // do an update and check it's  still memory size 2
        // however this time the food should be empty, as 'crackers' now blocks the not.
        ksession.update( fh,
                         "crackers" );
        ksession.fireAllRules();

        assertEquals( 2,
                      accMemory.betaMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      existsMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      fromMemory.betaMemory.getLeftTupleMemory().size() );
        assertEquals( 1,
                      notMemory.getRightTupleMemory().size() );

        assertEquals( 0, foodUpdated.size() );

        // do an update and check it's  still memory size 2
        // this time
        ksession.update( fh,
                         "oranges" );
        ksession.fireAllRules();

        assertEquals( 2,
                      accMemory.betaMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      existsMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      fromMemory.betaMemory.getLeftTupleMemory().size() );
        assertEquals( 0,
                      notMemory.getRightTupleMemory().size() );

        assertEquals( 2,
                      food.size() );
        assertContains( new String[]{"crackers", "apple"},
                        food );

        // Close the open
        query.close();
        assertEquals( 0,
                      accMemory.betaMemory.getRightTupleMemory().size() );
        assertEquals( 0,
                      existsMemory.getRightTupleMemory().size() );
        assertEquals( 0,
View Full Code Here

                      notMemory.getRightTupleMemory().size() );

        // Now execute an open query and ensure the memory is left populated
        food.clear();
        final List foodUpdated = new ArrayList();
        LiveQuery query = ksession.openLiveQuery( "look",
                                                  new Object[]{"kitchen", Variable.v},
                                                  new ViewChangedEventListener() {

                                                      public void rowUpdated(Row row) {
                                                          foodUpdated.addAll( (Collection) row.get( "food" ) );
                                                      }

                                                      public void rowDeleted(Row row) {
                                                      }

                                                      public void rowInserted(Row row) {
                                                          food.addAll( (Collection) row.get( "food" ) );
                                                      }
                                                  } );
        assertEquals( 2,
                      food.size() );
        assertContains( new String[]{"crackers", "apple"},
                        food );

        assertEquals( 2,
                      accMemory.betaMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      existsMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      fromMemory.betaMemory.getLeftTupleMemory().size() );
        assertEquals( 0,
                      notMemory.getRightTupleMemory().size() );

        food.clear();
        // Now try again, make sure it only delete's it's own tuples
        results = ksession.getQueryResults( "look",
                                            new Object[]{"kitchen", Variable.v} );
        assertEquals( 1,
                      results.size() );

        for ( org.kie.api.runtime.rule.QueryResultsRow row : results ) {
            food.addAll( (Collection) row.get( "food" ) );
            logger.debug( row.get( "food" ).toString() );
        }
        assertEquals( 2,
                      food.size() );
        assertContains( new String[]{"crackers", "apple"},
                        food );

        assertEquals( 2,
                      accMemory.betaMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      existsMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      fromMemory.betaMemory.getLeftTupleMemory().size() );
        assertEquals( 0,
                      notMemory.getRightTupleMemory().size() );
        food.clear();

        // do an update and check it's  still memory size 2
        // however this time the food should be empty, as 'crackers' now blocks the not.
        ksession.update( fh,
                         "crackers" );
        ksession.fireAllRules();

        assertEquals( 2,
                      accMemory.betaMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      existsMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      fromMemory.betaMemory.getLeftTupleMemory().size() );
        assertEquals( 1,
                      notMemory.getRightTupleMemory().size() );

        assertEquals( 0, foodUpdated.size() );

        // do an update and check it's  still memory size 2
        // this time
        ksession.update( fh,
                         "oranges" );
        ksession.fireAllRules();

        assertEquals( 2,
                      accMemory.betaMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      existsMemory.getRightTupleMemory().size() );
        assertEquals( 2,
                      fromMemory.betaMemory.getLeftTupleMemory().size() );
        assertEquals( 0,
                      notMemory.getRightTupleMemory().size() );

        assertEquals( 2,
                      food.size() );
        assertContains( new String[]{"crackers", "apple"},
                        food );

        // Close the open
        query.close();
        assertEquals( 0,
                      accMemory.betaMemory.getRightTupleMemory().size() );
        assertEquals( 0,
                      existsMemory.getRightTupleMemory().size() );
        assertEquals( 0,
View Full Code Here

TOP

Related Classes of org.kie.api.runtime.rule.LiveQuery

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.