Java Code Examples for org.kie.api.runtime.KieSession#retract()
The following examples show how to use
org.kie.api.runtime.KieSession#retract() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Query3Test.java From kogito-runtimes with Apache License 2.0 | 6 votes |
private void doIt(Object o1, Object o2, String query, int expected, boolean doUpdate, boolean doRetract) { KieSession knowledgeSession = knowledgeBase.newKieSession(); try { knowledgeSession.insert( o1 ); FactHandle handle2 = knowledgeSession.insert( o2 ); if ( doUpdate ) { knowledgeSession.update( handle2, o2 ); } else if ( doRetract ) { knowledgeSession.retract( handle2 ); handle2 = knowledgeSession.insert( o2 ); } QueryResults queryResults = knowledgeSession.getQueryResults( query ); assertEquals( expected, queryResults.size() ); } finally { knowledgeSession.dispose(); } }
Example 2
Source File: DeclarativeAgendaTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testApplyBlockerSecond() { KieSession ksession = getStatefulKnowledgeSession(); List list = new ArrayList(); ksession.setGlobal( "list", list ); FactHandle go1 = ksession.insert( "go1" ); FactHandle go2 = ksession.insert( "go2" ); ksession.fireAllRules(); assertEquals( 1, list.size() ); assertTrue( list.contains( "rule1:go2" ) ); list.clear(); ksession.retract( go2 ); ksession.fireAllRules(); assertEquals( 1, list.size() ); assertTrue( list.contains( "rule1:go1" ) ); }
Example 3
Source File: DeclarativeAgendaTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testApplyBlockerFirst() { KieSession ksession = getStatefulKnowledgeSession(); List list = new ArrayList(); ksession.setGlobal( "list", list ); FactHandle go2 = ksession.insert( "go2" ); //((InternalWorkingMemory) ksession).flushPropagations(); FactHandle go1 = ksession.insert( "go1" ); ksession.fireAllRules(); assertEquals( 1, list.size() ); assertTrue( list.contains( "rule1:go2" ) ); list.clear(); ksession.retract( go2 ); ksession.fireAllRules(); assertEquals( 1, list.size() ); assertTrue( list.contains( "rule1:go1" ) ); }
Example 4
Source File: DynamicRulesChangesTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public List<String> call() throws Exception { final List<String> events = new ArrayList<String>(); try { KieSession ksession = kbase.newKieSession(); ksession.setGlobal("events", events); // phase 1 Room room1 = new Room("Room 1"); ksession.insert(room1); FactHandle fireFact1 = ksession.insert(new Fire(room1)); ksession.fireAllRules(); assertEquals(1, events.size()); // phase 2 Sprinkler sprinkler1 = new Sprinkler(room1); ksession.insert(sprinkler1); ksession.fireAllRules(); assertEquals(2, events.size()); // phase 3 ksession.retract(fireFact1); ksession.fireAllRules(); } catch (Exception e) { System.err.println("Exception in thread " + Thread.currentThread().getName() + ": " + e.getLocalizedMessage()); throw e; } return events; }
Example 5
Source File: FirstOrderLogicTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testNot() throws Exception { KieBase kbase = loadKnowledgeBase("not_rule_test.drl"); KieSession wm = createKnowledgeSession(kbase); final List list = new ArrayList(); wm.setGlobal( "list", list ); final Cheese stilton = new Cheese( "stilton", 5 ); final FactHandle stiltonHandle = (FactHandle) wm.insert( stilton ); final Cheese cheddar = new Cheese( "cheddar", 7 ); final FactHandle cheddarHandle = (FactHandle) wm.insert( cheddar ); wm.fireAllRules(); assertEquals( 0, list.size() ); wm.retract( stiltonHandle ); wm.fireAllRules(); assertEquals( 4, list.size() ); assertTrue( list.contains( new Integer( 5 ) ) ); assertTrue( list.contains( new Integer( 6 ) ) ); assertTrue( list.contains( new Integer( 7 ) ) ); assertTrue( list.contains( new Integer( 8 ) ) ); }
Example 6
Source File: FirstOrderLogicTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testNotWithBindings() throws Exception { KieBase kbase = loadKnowledgeBase("not_with_bindings_rule_test.drl"); KieSession wm = createKnowledgeSession(kbase); final List list = new ArrayList(); wm.setGlobal( "list", list ); final Cheese stilton = new Cheese( "stilton", 5 ); final FactHandle stiltonHandle = (FactHandle) wm.insert( stilton ); final Cheese cheddar = new Cheese( "cheddar", 7 ); final FactHandle cheddarHandle = (FactHandle) wm.insert( cheddar ); final PersonInterface paul = new Person( "paul", "stilton", 12 ); wm.insert( paul ); wm.fireAllRules(); assertEquals( 0, list.size() ); wm.retract( stiltonHandle ); wm.fireAllRules(); assertEquals( 1, list.size() ); }
Example 7
Source File: DeclarativeAgendaTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testApplyBlockerSecondWithUpdate() { KieSession ksession = getStatefulKnowledgeSession(); List list = new ArrayList(); ksession.setGlobal( "list", list ); FactHandle go1 = ksession.insert( "go1" ); FactHandle go2 = ksession.insert( "go2" ); ksession.fireAllRules(); assertEquals( 1, list.size() ); assertTrue( list.contains( "rule1:go2" ) ); list.clear(); ksession.update( go2, "go2" ); ksession.fireAllRules(); assertEquals( 1, list.size() ); assertTrue( list.contains( "rule1:go2" ) ); list.clear(); ksession.retract( go2 ); ksession.fireAllRules(); assertEquals( 1, list.size() ); assertTrue( list.contains( "rule1:go1" ) ); }
Example 8
Source File: DeclarativeAgendaTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testApplyBlockerFirstWithFireAllRulesInbetween() { KieSession ksession = getStatefulKnowledgeSession(); List list = new ArrayList(); ksession.setGlobal( "list", list ); FactHandle go2 = ksession.insert( "go2" ); ksession.fireAllRules(); assertEquals( 0, list.size() ); FactHandle go1 = ksession.insert( "go1" ); ksession.fireAllRules(); assertEquals( 1, list.size() ); assertTrue( list.contains( "rule1:go2" ) ); list.clear(); ksession.retract( go2 ); ksession.fireAllRules(); assertEquals( 1, list.size() ); assertTrue( list.contains( "rule1:go1" ) ); }
Example 9
Source File: DeclarativeAgendaTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testBasicBlockOnAnnotation() { String str = ""; str += "package org.domain.test \n"; str += "import " + Match.class.getName() + "\n"; str += "global java.util.List list \n"; str += "dialect 'mvel' \n"; str += "rule rule1 @department(sales) \n"; str += "when \n"; str += " $s : String( this == 'go1' ) \n"; str += "then \n"; str += " list.add( kcontext.rule.name + ':' + $s ); \n"; str += "end \n"; str += "rule rule2 @department(sales) \n"; str += "when \n"; str += " $s : String( this == 'go1' ) \n"; str += "then \n"; str += " list.add( kcontext.rule.name + ':' + $s ); \n"; str += "end \n"; str += "rule rule3 @department(sales) \n"; str += "when \n"; str += " $s : String( this == 'go1' ) \n"; str += "then \n"; str += " list.add( kcontext.rule.name + ':' + $s ); \n"; str += "end \n"; str += "rule blockerAllSalesRules @activationListener('direct') \n"; str += "when \n"; str += " $s : String( this == 'go2' ) \n"; str += " $i : Match( department == 'sales' ) \n"; str += "then \n"; str += " list.add( $i.rule.name + ':' + $s ); \n"; str += " kcontext.blockMatch( $i ); \n"; str += "end \n"; KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); kconf.setOption( DeclarativeAgendaOption.ENABLED ); KieBase kbase = loadKnowledgeBaseFromString( kconf, str ); KieSession ksession = createKnowledgeSession(kbase); List list = new ArrayList(); ksession.setGlobal( "list", list ); ksession.insert( "go1" ); FactHandle go2 = ksession.insert( "go2" ); ksession.fireAllRules(); assertEquals( 3, list.size() ); assertTrue( list.contains( "rule1:go2" ) ); assertTrue( list.contains( "rule2:go2" ) ); assertTrue( list.contains( "rule3:go2" ) ); list.clear(); ksession.retract( go2 ); ksession.fireAllRules(); assertEquals( 3, list.size() ); assertTrue( list.contains( "rule1:go1" ) ); assertTrue( list.contains( "rule2:go1" ) ); assertTrue( list.contains( "rule3:go1" ) ); ksession.dispose(); }
Example 10
Source File: SessionInspectorTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testGetSessionInfo() { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newClassPathResource( "org/drools/compiler/integrationtests/test_SubNetworks.drl" ), ResourceType.DRL ); kbuilder.add( ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/test_AccumulateWithFromChaining.drl"), ResourceType.DRL ); kbuilder.add( ResourceFactory.newClassPathResource( "org/drools/compiler/integrationtests/test_CollectResultsBetaConstraint.drl" ), ResourceType.DRL ); kbuilder.add( ResourceFactory.newClassPathResource( "org/drools/compiler/integrationtests/test_QueryMemoryLeak.drl" ), ResourceType.DRL ); assertFalse(kbuilder.hasErrors(), kbuilder.getErrors().toString()); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages( kbuilder.getKnowledgePackages() ); KieSession ksession = createKnowledgeSession(kbase); ksession.setGlobal( "results", new ArrayList<Object>() ); ksession.insert( new Dimension( 100, 50 ) ); ksession.insert( new Dimension( 130, 80 ) ); ksession.insert( new Dimension( 50, 40 ) ); ksession.insert( new Dimension( 50, 40 ) ); Cheesery cheesery = new Cheesery(); cheesery.addCheese( new Cheese( "brie", 10 )); cheesery.addCheese( new Cheese( "brie", 10 )); cheesery.addCheese( new Cheese( "brie", 10 )); cheesery.addCheese( new Cheese( "brie", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); ksession.insert( cheesery ); ksession.insert( new Person( "Bob", "muzzarella") ); ksession.insert( new Person( "Mark", "brie") ); ksession.insert( new Cheese( "brie", 10 ) ); ksession.insert( new Cheese( "brie", 10 ) ); ksession.insert( new Cheese( "brie", 10 ) ); ksession.insert( new Cheese( "brie", 10 ) ); ksession.insert( new Cheese( "muzzarella", 10 ) ); ksession.insert( new Cheese( "muzzarella", 10 ) ); ksession.insert( new Cheese( "muzzarella", 10 ) ); ksession.insert( new Cheese( "muzzarella", 10 ) ); ksession.insert( new Cheese( "Stilton", 10 ) ); ksession.insert( new Cheese( "Stilton", 10 ) ); ksession.insert( new Cheese( "Stilton", 10 ) ); ksession.insert( new Double( 10 ) ); ksession.insert( new Double( 11 ) ); ksession.insert( new Double( 12 ) ); ksession.insert( new Double( 13 ) ); ksession.insert( new Double( 14 ) ); ksession.insert( new Integer( 15 ) ); ksession.insert( new Integer( 16 ) ); ksession.insert( new Integer( 17 ) ); ksession.insert( new Integer( 18 ) ); FactHandle handle = ksession.insert( new Worker( ) ); ksession.retract( handle ); SessionInspector inspector = new SessionInspector( ksession ); StatefulKnowledgeSessionInfo info = inspector.getSessionInfo(); String report = SessionReporter.generateReport( "simple", info, null ); assertNotNull( report ); }
Example 11
Source File: DeclarativeAgendaTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testMultipleBlockers() { String str = ""; str += "package org.domain.test \n"; str += "import " + Match.class.getName() + "\n"; str += "global java.util.List list \n"; str += "dialect 'mvel' \n"; str += "rule rule0 @department(sales) \n"; str += "when \n"; str += " $s : String( this == 'go0' ) \n"; str += "then \n"; str += " list.add( kcontext.rule.name + ':' + $s ); \n"; str += "end \n"; str += "rule blockerAllSalesRules1 @activationListener('direct') \n"; str += "when \n"; str += " $s : String( this == 'go1' ) \n"; str += " $i : Match( department == 'sales' ) \n"; str += "then \n"; str += " list.add( $i.rule.name + ':' + $s ); \n"; str += " kcontext.blockMatch( $i ); \n"; str += "end \n"; str += "rule blockerAllSalesRules2 @activationListener('direct') \n"; str += "when \n"; str += " $s : String( this == 'go2' ) \n"; str += " $i : Match( department == 'sales' ) \n"; str += "then \n"; str += " list.add( $i.rule.name + ':' + $s ); \n"; str += " kcontext.blockMatch( $i ); \n"; str += "end \n"; str += "rule blockerAllSalesRules3 @activationListener('direct') \n"; str += "when \n"; str += " $s : String( this == 'go3' ) \n"; str += " $i : Match( department == 'sales' ) \n"; str += "then \n"; str += " list.add( $i.rule.name + ':' + $s ); \n"; str += " kcontext.blockMatch( $i ); \n"; str += "end \n"; KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); kconf.setOption( DeclarativeAgendaOption.ENABLED ); KieBase kbase = loadKnowledgeBaseFromString( kconf, str ); KieSession ksession = createKnowledgeSession(kbase); List list = new ArrayList(); ksession.setGlobal( "list", list ); FactHandle go0 = ksession.insert( "go0" ); FactHandle go1 = ksession.insert( "go1" ); FactHandle go2 = ksession.insert( "go2" ); FactHandle go3 = ksession.insert( "go3" ); ksession.fireAllRules(); assertEquals( 3, list.size() ); assertTrue( list.contains( "rule0:go1" ) ); assertTrue( list.contains( "rule0:go2" ) ); assertTrue( list.contains( "rule0:go3" ) ); list.clear(); ksession.retract( go3 ); ksession.fireAllRules(); assertEquals( 0, list.size() ); ksession.retract( go2 ); ksession.fireAllRules(); assertEquals( 0, list.size() ); ksession.retract( go1 ); ksession.fireAllRules(); assertEquals( 1, list.size() ); assertTrue( list.contains( "rule0:go0" ) ); ksession.dispose(); }
Example 12
Source File: DeclarativeAgendaTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testApplyBlockerSecondAfterUpdate() { KieSession ksession = getStatefulKnowledgeSession(); List list = new ArrayList(); ksession.setGlobal( "list", list ); FactHandle go1 = ksession.insert( "go1" ); ksession.fireAllRules(); assertEquals( 1, list.size() ); assertTrue( list.contains( "rule1:go1" ) ); list.clear(); FactHandle go2 = ksession.insert( "go2" ); ksession.fireAllRules(); assertEquals( 1, list.size() ); assertTrue( list.contains( "rule1:go2" ) ); list.clear(); ksession.update( go1, "go1" ); ksession.fireAllRules(); assertEquals( 1, list.size() ); assertTrue( list.contains( "rule1:go2" ) ); list.clear(); ksession.retract( go2 ); ksession.fireAllRules(); assertEquals( 1, list.size() ); assertTrue( list.contains( "rule1:go1" ) ); }
Example 13
Source File: SessionInspectorTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testGetSessionInfoWithCustomTemplate() { if ( System.getProperty("java.vendor").toUpperCase().contains("IBM") ) { return; //Does not work in the IBM JDK due to a bug in MVEL } KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newClassPathResource( "org/drools/compiler/integrationtests/test_SubNetworks.drl" ), ResourceType.DRL ); kbuilder.add( ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/test_AccumulateWithFromChaining.drl"), ResourceType.DRL ); kbuilder.add( ResourceFactory.newClassPathResource( "org/drools/compiler/integrationtests/test_CollectResultsBetaConstraint.drl" ), ResourceType.DRL ); kbuilder.add( ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/test_QueryMemoryLeak.drl"), ResourceType.DRL ); assertFalse(kbuilder.hasErrors(), kbuilder.getErrors().toString()); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages( kbuilder.getKnowledgePackages() ); KieSession ksession = createKnowledgeSession(kbase); ksession.setGlobal( "results", new ArrayList<Object>() ); ksession.insert( new Dimension( 100, 50 ) ); ksession.insert( new Dimension( 130, 80 ) ); ksession.insert( new Dimension( 50, 40 ) ); ksession.insert( new Dimension( 50, 40 ) ); Cheesery cheesery = new Cheesery(); cheesery.addCheese( new Cheese( "brie", 10 )); cheesery.addCheese( new Cheese( "brie", 10 )); cheesery.addCheese( new Cheese( "brie", 10 )); cheesery.addCheese( new Cheese( "brie", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "muzzarella", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); cheesery.addCheese( new Cheese( "stilton", 10 )); ksession.insert( cheesery ); ksession.insert( new Person( "Bob", "muzzarella") ); ksession.insert( new Person( "Mark", "brie") ); ksession.insert( new Cheese( "brie", 10 ) ); ksession.insert( new Cheese( "brie", 10 ) ); ksession.insert( new Cheese( "brie", 10 ) ); ksession.insert( new Cheese( "brie", 10 ) ); ksession.insert( new Cheese( "muzzarella", 10 ) ); ksession.insert( new Cheese( "muzzarella", 10 ) ); ksession.insert( new Cheese( "muzzarella", 10 ) ); ksession.insert( new Cheese( "muzzarella", 10 ) ); ksession.insert( new Cheese( "Stilton", 10 ) ); ksession.insert( new Cheese( "Stilton", 10 ) ); ksession.insert( new Cheese( "Stilton", 10 ) ); ksession.insert( new Double( 10 ) ); ksession.insert( new Double( 11 ) ); ksession.insert( new Double( 12 ) ); ksession.insert( new Double( 13 ) ); ksession.insert( new Double( 14 ) ); ksession.insert( new Integer( 15 ) ); ksession.insert( new Integer( 16 ) ); ksession.insert( new Integer( 17 ) ); ksession.insert( new Integer( 18 ) ); FactHandle handle = ksession.insert( new Worker( ) ); ksession.retract( handle ); SessionInspector inspector = new SessionInspector( ksession ); StatefulKnowledgeSessionInfo info = inspector.getSessionInfo(); SessionReporter.addNamedTemplate( "topten", getClass().getResourceAsStream( "customreports.mvel" ) ); String report = SessionReporter.generateReport( "topten", info, null ); assertNotNull( report ); }
Example 14
Source File: FirstOrderLogicTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testForallSinglePattern() throws Exception { KieBase kbase = loadKnowledgeBase( "test_ForallSinglePattern.drl"); KieSession workingMemory = createKnowledgeSession(kbase); final List list = new ArrayList(); workingMemory.setGlobal( "results", list ); int fired = 0; // no cheeses, so should fire workingMemory.fireAllRules(); assertEquals( ++fired, list.size() ); // only stilton, so should not fire again FactHandle stilton1 = (FactHandle) workingMemory.insert( new Cheese( "stilton", 10 ) ); workingMemory.fireAllRules(); assertEquals( fired, list.size() ); // only stilton, so should not fire again FactHandle stilton2 = (FactHandle) workingMemory.insert( new Cheese( "stilton", 11 ) ); workingMemory.fireAllRules(); assertEquals( fired, list.size() ); // still only stilton, so should not fire workingMemory.retract( stilton1 ); workingMemory.fireAllRules(); assertEquals( fired, list.size() ); // there is a brie, so should not fire FactHandle brie = (FactHandle) workingMemory.insert( new Cheese( "brie", 10 ) ); workingMemory.fireAllRules(); assertEquals( fired, list.size() ); // no brie anymore, so should fire workingMemory.retract( brie ); workingMemory.fireAllRules(); assertEquals( ++fired, list.size() ); // no more cheese, but since it already fired, should not fire again workingMemory.retract( stilton2 ); workingMemory.fireAllRules(); assertEquals( fired, list.size() ); }
Example 15
Source File: FirstOrderLogicTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testCollectModifyAlphaRestriction() throws Exception { KieBase kbase = loadKnowledgeBase( "test_CollectAlphaRestriction.drl"); KieSession wm = createKnowledgeSession(kbase); final List results = new ArrayList(); wm.setGlobal( "results", results ); final Cheese[] cheese = new Cheese[]{new Cheese( "stilton", 10 ), new Cheese( "stilton", 2 ), new Cheese( "stilton", 5 ), new Cheese( "brie", 15 ), new Cheese( "brie", 16 ), new Cheese( "provolone", 8 )}; final FactHandle[] cheeseHandles = new FactHandle[cheese.length]; for ( int i = 0; i < cheese.length; i++ ) { cheeseHandles[i] = (FactHandle) wm.insert( cheese[i] ); } // ---------------- 1st scenario int fireCount = 0; wm.fireAllRules(); assertEquals( ++fireCount, results.size() ); assertEquals( 3, ((Collection) results.get( fireCount - 1 )).size() ); assertEquals( ArrayList.class.getName(), results.get( fireCount - 1 ).getClass().getName() ); // ---------------- 2nd scenario final int index = 1; cheese[index].setType( "brie" ); wm.update( cheeseHandles[index], cheese[index] ); wm.fireAllRules(); assertEquals( ++fireCount, results.size() ); assertEquals( 2, ((Collection) results.get( fireCount - 1 )).size() ); assertEquals( ArrayList.class.getName(), results.get( fireCount - 1 ).getClass().getName() ); // ---------------- 3rd scenario wm.retract( cheeseHandles[2] ); wm.fireAllRules(); assertEquals( ++fireCount, results.size() ); assertEquals( 1, ((Collection) results.get( fireCount - 1 )).size() ); assertEquals( ArrayList.class.getName(), results.get( fireCount - 1 ).getClass().getName() ); }
Example 16
Source File: FirstOrderLogicTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testRemoveIdentitiesSubNetwork() throws Exception { KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); conf.setOption(RemoveIdentitiesOption.YES); KieBase kbase = loadKnowledgeBase(conf, "test_removeIdentitiesSubNetwork.drl"); KieSession workingMemory = createKnowledgeSession(kbase); final List list = new ArrayList(); workingMemory.setGlobal( "results", list ); final Person bob = new Person( "bob", "stilton" ); workingMemory.insert( bob ); final Person mark = new Person( "mark", "stilton" ); workingMemory.insert( mark ); final Cheese stilton1 = new Cheese( "stilton", 6 ); final FactHandle stilton1Handle = (FactHandle) workingMemory.insert( stilton1 ); final Cheese stilton2 = new Cheese( "stilton", 7 ); final FactHandle stilton2Handle = (FactHandle) workingMemory.insert( stilton2 ); workingMemory.fireAllRules(); assertEquals( 0, list.size() ); workingMemory.retract( stilton1Handle ); workingMemory.fireAllRules(); assertEquals( 1, list.size() ); assertEquals( mark, list.get( 0 ) ); workingMemory.retract( stilton2Handle ); workingMemory.fireAllRules(); assertEquals( 2, list.size() ); assertEquals( bob, list.get( 1 ) ); }
Example 17
Source File: FirstOrderLogicTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testCollectModify() throws Exception { KieBase kbase = loadKnowledgeBase("test_Collect.drl"); KieSession wm = createKnowledgeSession(kbase); List results = new ArrayList(); wm.setGlobal( "results", results ); final Cheese[] cheese = new Cheese[]{new Cheese( "stilton", 10 ), new Cheese( "stilton", 2 ), new Cheese( "stilton", 5 ), new Cheese( "brie", 15 ), new Cheese( "brie", 16 ), new Cheese( "provolone", 8 )}; final Person bob = new Person( "Bob", "stilton" ); final FactHandle[] cheeseHandles = new FactHandle[cheese.length]; for ( int i = 0; i < cheese.length; i++ ) { cheeseHandles[i] = (FactHandle) wm.insert( cheese[i] ); } final FactHandle bobHandle = (FactHandle) wm.insert( bob ); // ---------------- 1st scenario int fireCount = 0; wm.fireAllRules(); assertEquals( ++fireCount, results.size() ); assertEquals( 3, ((Collection) results.get( fireCount - 1 )).size() ); assertEquals( ArrayList.class.getName(), results.get( fireCount - 1 ).getClass().getName() ); // ---------------- 2nd scenario final int index = 1; cheese[index].setPrice( 9 ); wm.update( cheeseHandles[index], cheese[index] ); wm.fireAllRules(); assertEquals( ++fireCount, results.size() ); assertEquals( 3, ((Collection) results.get( fireCount - 1 )).size() ); assertEquals( ArrayList.class.getName(), results.get( fireCount - 1 ).getClass().getName() ); // ---------------- 3rd scenario bob.setLikes( "brie" ); wm.update( bobHandle, bob ); wm.fireAllRules(); assertEquals( fireCount, results.size() ); // ---------------- 4th scenario wm.retract( cheeseHandles[3] ); wm.fireAllRules(); // should not have fired as per constraint assertEquals( fireCount, results.size() ); }
Example 18
Source File: JTMSTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test @Disabled("Currently cannot support updates") public void testConflictTMS() { KieSession kSession = getSessionFromFile( "posNegTms.drl" ); ArrayList list = new ArrayList(); kSession.setGlobal( "list", list ); FactHandle a = kSession.insert( "a" ); FactHandle b = kSession.insert( "b" ); FactHandle c = kSession.insert( "c" ); FactHandle d = kSession.insert( "d" ); try { kSession.fireAllRules(); assertEquals( 4, kSession.getFactCount() ); assertEquals( 0, list.size() ); kSession.retract( a ); kSession.fireAllRules(); assertEquals( 3, kSession.getFactCount() ); assertEquals( 0, list.size() ); kSession.retract( b ); kSession.fireAllRules(); assertEquals( 2, kSession.getFactCount() ); assertEquals(1, getNegativeObjects(kSession).size()); assertEquals( 1, list.size() ); a = kSession.insert( "a" ); kSession.fireAllRules(); assertEquals( 3, kSession.getFactCount()); assertEquals( 0, getNegativeObjects(kSession).size() ); assertEquals( 1, list.size() ); kSession.retract( c ); kSession.fireAllRules(); assertEquals( 2, kSession.getFactCount() ); assertEquals( 0, getNegativeObjects(kSession).size() ); assertEquals( 1, list.size() ); kSession.retract( d ); kSession.fireAllRules(); assertEquals( 2, kSession.getFactCount() ); assertEquals( 0, getNegativeObjects(kSession).size() ); assertEquals( 2, list.size() ); kSession.retract( a ); kSession.fireAllRules(); assertEquals( 0, kSession.getFactCount() ); assertEquals( 0, getNegativeObjects(kSession).size() ); assertEquals( 2, list.size() ); c = kSession.insert( "c" ); kSession.fireAllRules(); assertEquals( 1, kSession.getFactCount() ); assertEquals( 1, getNegativeObjects(kSession).size() ); assertEquals( 3, list.size() ); } catch ( Exception e ) { e.printStackTrace(); fail( "No exception should have been thrown" ); } }
Example 19
Source File: JTMSTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test @Disabled("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" ); assertEquals( 3, ep.getObjects().size() ); //just go1, go2, go3 assertEquals( 1, getNegativeObjects(kSession).size() ); // Person(darth) 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(); assertEquals( 1, equalityMap.size() ); // Only Person type is logical 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() ); }
Example 20
Source File: JTMSTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test @Disabled("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" ); assertEquals( 4, ep.getObjects().size() ); //just go1, go2, go3, Person(darth) 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(); assertEquals( 1, equalityMap.size() ); // Only Person type is logical 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() ); }