@Test public void updateFactOnRuleFireTest() { final Cheese camembert = new Cheese("Camembert", 19); final Cheese cheddar = new Cheese("Cheddar", 45); ksession.insert(camembert); ksession.insert(cheddar); verifyFactsWithQuery(Cheese.class, "cheeseTypes", camembert, cheddar); final List<Cheese> expensiveCheese = new ArrayList<>(); ksession.setGlobal("expensiveCheese", expensiveCheese); final int firedRules = ksession.fireAllRules(); assertThat(firedRules).isEqualTo(2); verifyList(expensiveCheese, camembert, cheddar); verifyFactsWithQuery(Cheese.class, "cheeseTypes", camembert, cheddar); assertThat(camembert.getPrice()).isEqualTo(21); assertThat(cheddar.getPrice()).isEqualTo(45); }
@Test @Ignore public void testAgendaEventListener() throws Exception { InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); pkg.addRule( rule ); kbase.addPackages( Collections.singleton(pkg) ); KieSession ksession = kbase.newKieSession(); final List agendaList = new ArrayList(); final AgendaEventListener agendaEventListener = new AgendaEventListener() { ksession.addEventListener( agendaEventListener ); assertEquals( 1, ksession.getAgendaEventListeners().size() ); final Cheese cheddar = new Cheese( "cheddar", 15 ); FactHandle cheddarHandle = ksession.insert( cheddar ); InternalAgenda agenda = (InternalAgenda) ksession.getAgenda(); cheddar.setPrice( 14 ); ksession.update(cheddarHandle, cheddar); cheddar.setPrice( 14 ); ksession.update( cheddarHandle, cheddar );
@Test public void testRemoveRuleRuntimeEventListener() { KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); KieSession ksession = kbase.newKieSession(); ksession.addEventListener( eventListener ); ksession.removeEventListener( eventListener ); final Cheese stilton = new Cheese( "stilton", 15 ); final Cheese cheddar = new Cheese( "cheddar", 17 ); final FactHandle stiltonHandle = ksession.insert( stilton ); assertTrue( wmList.isEmpty() );
@Test public void deleteUpdatedFactDifferentClassTest() { FactHandle fact = ksession.insert(new Person("George", 18)); Assertions.assertThat(ksession.getObjects()).hasSize(1); Assertions.assertThat(ksession.getObjects().iterator().next()).isInstanceOf(Person.class); ksession.update(fact, new Cheese("Cheddar", 50)); Assertions.assertThat(ksession.getObjects()).hasSize(1); Assertions.assertThat(ksession.getObjects().iterator().next()).isInstanceOf(Cheese.class); ksession.delete(fact); Assertions.assertThat(ksession.getObjects()).isEmpty(); }
@Test public void testObjectIterator() { InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(); KieSession ksession = kBase.newKieSession(); ksession.insert( new Person( "bob", 35) ); ksession.insert( new Cheese( "stilton", 35) ); ksession.insert( new Cheese( "brie", 35) ); ksession.insert( new Person( "steve", 55) ); ksession.insert( new Person( "tom", 100) ); int i = 0; for ( FactHandle fh : ksession.getFactHandles()) { if ( i++ > 5 ) { fail( "should not iterate for than 3 times" ); } } i = 0; for ( FactHandle fh : ksession.getFactHandles()) { if ( i++ > 5 ) { fail( "should not iterate for than 3 times" ); } } }
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(); StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl)kBase.newKieSession(); FieldFactory.getInstance().getFieldValue( 5 ), extractor ); final Cheese cheddar = new Cheese( "cheddar", 5 ); final InternalFactHandle cheddarHandle = (InternalFactHandle) ksession.insert( cheddar ); ksession ) ); final Cheese stilton = new Cheese( "stilton", 10 ); final InternalFactHandle stiltonHandle = (InternalFactHandle) ksession.insert( stilton );
@Test public void testLiteralConstraintAssertObjectWithoutMemory() throws Exception { InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(); BuildContext buildContext = new BuildContext( kBase ); buildContext.setRule(new RuleImpl("test")); StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl)kBase.newKieSession(); PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory(); final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null); alphaNode.addObjectSink( sink ); final Cheese cheddar = new Cheese( "cheddar", 5 ); final DefaultFactHandle f0 = (DefaultFactHandle) ksession.insert( cheddar ); ksession.getObject( (DefaultFactHandle) list[0] ) ); final Cheese stilton = new Cheese( "stilton", 6 ); final DefaultFactHandle f1 = new DefaultFactHandle( 1, stilton ); ksession.getObject( (DefaultFactHandle) list[0] ) );
@Test public void testReturnValueConstraintAssertObject() throws Exception { InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(); BuildContext buildContext = new BuildContext( kBase ); buildContext.setRule(new RuleImpl("test")); StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl)kBase.newKieSession(); PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory(); final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null); alphaNode.addObjectSink( sink ); final Cheese cheddar = new Cheese( "cheddar", 5 ); final DefaultFactHandle f0 = (DefaultFactHandle) ksession.insert( cheddar ); final Object[] list = (Object[]) sink.getAsserted().get( 0 ); assertSame( cheddar, ksession.getObject( (DefaultFactHandle) list[0] ) ); final Cheese stilton = new Cheese( "stilton", 6 ); f0.setObject( stilton );
"org.drools.core.test.model.Cheese"); RuleBaseConfiguration conf = new RuleBaseConfiguration(properties); InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(conf); buildContext = new BuildContext(kBase); final StatefulKnowledgeSessionImpl ksession = new StatefulKnowledgeSessionImpl(1L, kBase); final Rete rete = kBase.getRete(); final EntryPointNode entryPoint = new EntryPointNode(0, rete, final Cheese cheese = new Cheese("brie", 15); final DefaultFactHandle h1 = new DefaultFactHandle(1, cheese); ksession); ksession.fireAllRules();
@Test public void testUpdateSinkWithoutMemory() { InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(); BuildContext buildContext = new BuildContext( kBase ); buildContext.setRule(new RuleImpl("test")); StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl)kBase.newKieSession(); PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory(); final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null); final Cheese cheese = new Cheese( "cheddar", 0 ); final DefaultFactHandle handle1 = new DefaultFactHandle( 1, cheese ); final Cheese stilton = new Cheese( "stilton", 10 ); final DefaultFactHandle handle2 = new DefaultFactHandle( 2, stilton );
@Test public void testIsShadowed() { StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl)kBase.newKieSession(); // Create a Rete network with ObjectTypeNodes for List, Collection and ArrayList final Rete rete = kBase.getRete(); final ObjectTypeNode objectTypeNode = new ObjectTypeNode(1, this.entryPoint, new ClassObjectType(Cheese.class), buildContext); objectTypeNode.attach(buildContext); final MockObjectSink sink1 = new MockObjectSink(); objectTypeNode.addObjectSink(sink1); // There are no String ObjectTypeNodes, make sure its not propagated final Cheese cheese = new Cheese("brie", 15); final DefaultFactHandle h1 = new DefaultFactHandle(1, cheese); rete.assertObject(h1, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession); ksession.fireAllRules(); final Object[] results = (Object[]) sink1.getAsserted().get(0); }
IdGenerator idGenerator = kBase.getReteooBuilder().getIdGenerator(); final Rete rete = kBase.getRete(); final ObjectTypeNode objectTypeNode = new ObjectTypeNode( idGenerator.getNextId(), this.entryPoint, final Agenda agenda = ksession.getAgenda(); rule1.setConsequence( consequence ); true ); final PropagationContext context1 = pctxFactory.createPropagationContext(0, PropagationContext.INSERTION, null, null, new DefaultFactHandle()); ksession ); final Cheese cheese = new Cheese( "brie", 10 ); final FactHandle cheeseHandle = ksession.insert( cheese, null, false, (Activation) tuple1.getContextObject() ); cheese.setType( "cheddar" ); cheese.setPrice( 20 ); TruthMaintenanceSystem tms = ((NamedEntryPoint)ksession.getWorkingMemoryEntryPoint( EntryPointId.DEFAULT.getEntryPointId() ) ).getTruthMaintenanceSystem();
new Declaration("id", extractor, null) , MvelConstraint.INDEX_EVALUATOR ); final SingleIndex singleIndex = new SingleIndex( new FieldIndex[]{fieldIndex}, 1 ); Tuple tuple = new RightTupleImpl( new DefaultFactHandle( 1, new Cheese("stilton", 10) ) ); final TupleList index = new AbstractHashTable.SingleIndexTupleList( singleIndex, tuple, "stilton".hashCode(), false ); index.hashCode() ); final Cheese stilton1 = new Cheese( "stilton", 35 ); final InternalFactHandle h1 = new DefaultFactHandle( 1, stilton1 );
"type" ); final FieldIndex fieldIndex = new FieldIndex( extractor, new Declaration("id", extractor, null) , MvelConstraint.INDEX_EVALUATOR ); final SingleIndex singleIndex = new SingleIndex( new FieldIndex[]{fieldIndex}, 1 ); Tuple tuple = new RightTupleImpl( new DefaultFactHandle( 1, new Cheese("stilton", 10) ) ); final TupleList index = new AbstractHashTable.SingleIndexTupleList( singleIndex, tuple, "stilton".hashCode(), false ); final Cheese stilton1 = new Cheese( "stilton", 35 ); final InternalFactHandle h1 = new DefaultFactHandle( 1, stilton1 ); final Cheese stilton2 = new Cheese( "stilton", 59 ); final InternalFactHandle h2 = new DefaultFactHandle( 2, stilton1.setType( "cheddar" );
final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.INSERTION, null, null, null); final StatefulKnowledgeSessionImpl workingMemory = new StatefulKnowledgeSessionImpl( 1L, (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase() ); new ClassObjectType( Person.class ) ); final Declaration declaration = new Declaration( "age", ageExtractor, pattern ); final Cheese cheese1 = new Cheese( "cheddar", 18 ); final Cheese cheese2 = new Cheese( "brie", 12 ); list.add( cheese1 ); cheese1.setPrice( 30 ); final Person person2 = new Person( "xxx2", 30 ); final FactHandle person2Handle = workingMemory.insert( person2 ); final LeftTuple tuple2 = new LeftTupleImpl( (DefaultFactHandle) person2Handle, from, tuple.getObject(1) ); cheese2.setPrice( 30 ); final Person person3 = new Person( "xxx2", 30 );
@Test public void testActivationUnMatchListener() { StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl)kBase.newStatefulKnowledgeSession(); final InternalAgenda agenda = (InternalAgenda) ksession.getAgenda(); buildContext); Cheese cheese = new Cheese(); cheese.setPrice(50); final RuleTerminalNodeLeftTuple tuple = new RuleTerminalNodeLeftTuple(new DefaultFactHandle(1, cheese), node1, true); rule1, null, new DefaultFactHandle()); assertEquals(50, cheese.getPrice()); assertEquals(100, cheese.getPrice()); assertEquals(50, cheese.getPrice());
@Test public void testRestract() { final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.INSERTION, null, null, null); final StatefulKnowledgeSessionImpl workingMemory = new StatefulKnowledgeSessionImpl( 1L, (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase() ); final ClassFieldReader extractor = store.getReader( Cheese.class, "type" ); final Cheese cheese1 = new Cheese( "stilton", 5 ); final Cheese cheese2 = new Cheese( "stilton", 15 ); list.add( cheese1 ); final FactHandle person1Handle = workingMemory.insert( person1 ); final LeftTuple tuple = new LeftTupleImpl( (DefaultFactHandle) person1Handle, from, asserted.size() ); final FromMemory memory = (FromMemory) workingMemory.getNodeMemory( from ); assertEquals( 1, memory.getBetaMemory().getLeftTupleMemory().size() );
public Sandwich() { super(); // Compiler adds it if it is not explicitly added by programmer // All the instance variable initialization is moved here by the compiler. b = new Bread(); c = new Cheese(); l = new Lettuce(); System.out.println("Sandwich()"); }
@Test public void testAlphaNode() { final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.INSERTION, null, null, null); final StatefulKnowledgeSessionImpl workingMemory = new StatefulKnowledgeSessionImpl(1L, kBase); final Cheese cheese1 = new Cheese("cheddar", 20); final Cheese cheese2 = new Cheese("brie", 20); list.add(cheese1); final FactHandle person1Handle = workingMemory.insert( person1 ); final LeftTuple tuple1 = new LeftTupleImpl( (DefaultFactHandle) person1Handle, from, cheese1.setType( "stilton" ); final Person person2 = new Person( "xxx2", 30 ); final FactHandle person2Handle = workingMemory.insert( person2 ); final LeftTuple tuple2 = new LeftTupleImpl( (DefaultFactHandle) person2Handle, from, tuple.getObject(1) ); cheese2.setType( "stilton" ); final Person person3 = new Person( "xxx2", 30 );