org.kie.api.runtime.rule.FactHandle Java Examples
The following examples show how to use
org.kie.api.runtime.rule.FactHandle.
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: DeleteTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testModifyRetractWithFunction() throws Exception { final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_RetractModifyWithFunction.drl")); final KieSession ksession = createKnowledgeSession(kbase); final org.drools.compiler.Cheese stilton = new org.drools.compiler.Cheese("stilton", 7); final org.drools.compiler.Cheese muzzarella = new org.drools.compiler.Cheese("muzzarella", 9); final int sum = stilton.getPrice() + muzzarella.getPrice(); final FactHandle stiltonHandle = ksession.insert(stilton); final FactHandle muzzarellaHandle = ksession.insert(muzzarella); ksession.fireAllRules(); assertEquals(sum, stilton.getPrice()); assertEquals(1, ksession.getFactCount()); assertNotNull(ksession.getObject(stiltonHandle)); assertNotNull(ksession.getFactHandle(stilton)); assertNull(ksession.getObject(muzzarellaHandle)); assertNull(ksession.getFactHandle(muzzarella)); }
Example #2
Source File: DefeasibleBeliefSet.java From kogito-runtimes with Apache License 2.0 | 6 votes |
private DefeasibilityStatus checkStrict( List<? extends FactHandle> premise ) { // The rule is strict. To prove that the derivation is strict we have to check that all the premises are // either facts or strictly proved facts for ( FactHandle h : premise ) { if ( h instanceof QueryElementFactHandle ) { return DefeasibilityStatus.DEFINITELY; } EqualityKey key = ((InternalFactHandle) h).getEqualityKey(); if ( key != null && key.getStatus() == EqualityKey.JUSTIFIED ) { //DefeasibleBeliefSet bs = (DefeasibleBeliefSet) getTruthMaintenanceSystem().getJustifiedMap().get(((DefaultFactHandle) h).getId()); DefeasibleBeliefSet bs = (DefeasibleBeliefSet) key.getBeliefSet(); if ( bs.getStatus() != DefeasibilityStatus.DEFINITELY ) { // to make a fact "definitely provable", all the supporting non-factual premises must be definitely provable. return DefeasibilityStatus.DEFEASIBLY; } } // else it's a fact, so it's a good candidate for definite entailment } return DefeasibilityStatus.DEFINITELY; }
Example #3
Source File: InsertObjectCommand.java From kogito-runtimes with Apache License 2.0 | 6 votes |
public FactHandle execute(Context context) { KieSession ksession = ((RegistryContext)context).lookup( KieSession.class ); FactHandle factHandle; if ( StringUtils.isEmpty( this.entryPoint ) ) { factHandle = ksession.insert( object ); } else { factHandle = ksession.getEntryPoint( this.entryPoint ).insert( object ); } if ( outIdentifier != null ) { if ( this.returnObject ) { ((RegistryContext) context).lookup( ExecutionResultImpl.class ).setResult( this.outIdentifier, object ); } ((RegistryContext) context).lookup( ExecutionResultImpl.class ).getFactHandles().put( this.outIdentifier, factHandle ); } if ( disconnected ) { DefaultFactHandle disconnectedHandle = ((DefaultFactHandle)factHandle).clone(); disconnectedHandle.disconnect(); return disconnectedHandle; } return factHandle; }
Example #4
Source File: NullCheckOnExistentialNodeTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
private void check( String drl, int fire1, int fire2 ) { KieSession kieSession = new KieHelper().addContent( drl, ResourceType.DRL ).build().newKieSession(); A a1 = new A(); A a2 = new A(); FactHandle fhA1 = kieSession.insert( a1 ); FactHandle fhA2 = kieSession.insert( a2 ); kieSession.insert( "xxx" ); assertEquals( fire1, kieSession.fireAllRules() ); a1.setB( null ); kieSession.update( fhA1, a1 ); a2.setB( new B( null ) ); kieSession.update( fhA2, a2 ); assertEquals( fire2, kieSession.fireAllRules() ); }
Example #5
Source File: GetFactHandlesCommandTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
private void verifyThatCollectionContainsTheseFactHandle(HashSet<String> factSet, Object collection) { factSet = (HashSet<String>) factSet.clone(); if( collection instanceof Collection<?> ) { Collection<FactHandle> factHandles = (Collection<FactHandle>) collection; assertTrue(! factHandles.isEmpty()); assertTrue(factHandles.size() == factSet.size(), factSet.size() + "inserted but only " + factHandles.size() + " facts retrieved"); Object [] internalFactHandles = factHandles.toArray(); for( int i = 0; i < internalFactHandles.length; ++i ) { Object factObject = ((InternalFactHandle) internalFactHandles[i]).getObject(); assertTrue(factSet.contains(factObject)); factSet.remove(factObject); } assertTrue( factSet.isEmpty(), "Additional facts found that weren't inserted."); } else { fail("result of command was NOT a collection of FactHandles"); } }
Example #6
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 #7
Source File: SegmentMemoryPrototypeTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
private void checkKieSession(KieSession ksession) { final List<String> events = new ArrayList<String>(); 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.delete(fireFact1); ksession.fireAllRules(); assertEquals(5, events.size()); }
Example #8
Source File: EqualityAssertMapComparator.java From kogito-runtimes with Apache License 2.0 | 6 votes |
/** * Special comparator that allows FactHandles to be keys, but always checks * equals with the identity of the objects involved */ @Override public boolean areEqual(final Object o1, Object o2) { if ( o1 == o2 ) { return true; } // o1 is a FactHandle, so just check their id's are the same if ( o1 instanceof FactHandle ) { return ((InternalFactHandle)o1).getId() == ((InternalFactHandle)o2).getId() ; } // o1 is an object, so unwrap o2 for comparison final InternalFactHandle handle = ((InternalFactHandle) o2); o2 = handle.getObject(); return o1 == o2 || o2.equals( o1 ); }
Example #9
Source File: FieldDataStore.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Override public void delete(InternalFactHandle fh, RuleImpl rule, TerminalNode terminalNode, FactHandle.State fhState) { DataHandle dh = fh.getDataHandle(); if (dh != this.handle) { throw new IllegalArgumentException("The given handle is not contained in this DataStore"); } entryPointSubscribers.forEach(s -> s.delete(dh, rule, terminalNode, fhState)); subscribers.forEach(s -> s.delete(dh)); handle = null; }
Example #10
Source File: ObjectUpdatedEventImpl.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public ObjectUpdatedEventImpl(final WorkingMemory workingMemory, final PropagationContext propagationContext, final FactHandle handle, final Object oldObject, final Object object) { super( ((InternalWorkingMemory) workingMemory ).getKnowledgeRuntime(), propagationContext ); this.factHandle = handle; this.oldObject = oldObject; this.object = object; }
Example #11
Source File: JaxbUnknownAdapter.java From kogito-runtimes with Apache License 2.0 | 5 votes |
private Object convertObjectToSerializableVariant( Object obj, Map<Object, Object> seenObjectsMap ) { if( obj == null ) { return null; } if( obj instanceof QueryResultsImpl ) { obj = new FlatQueryResults((QueryResultsImpl) obj); } else if( obj instanceof FactHandle ) { obj = DisconnectedFactHandle.newFrom((FactHandle) obj); } else if( !(obj instanceof JaxbListWrapper) && (obj instanceof Collection || obj instanceof Map) ) { obj = recursiveMarshal(obj, seenObjectsMap); } return obj; }
Example #12
Source File: StatefulSessionTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testGetFactHandle() throws Exception { final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("../empty.drl")); final KieSession ksession = createKnowledgeSession(kbase); for (int i = 0; i < 20; i++) { final Object object = new Object(); ksession.insert(object); final FactHandle factHandle = ksession.getFactHandle(object); assertNotNull(factHandle); assertEquals(object, ksession.getObject(factHandle)); } ksession.dispose(); }
Example #13
Source File: DisconnectedFactHandle.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public static DisconnectedFactHandle newFrom( FactHandle handle ) { if( handle instanceof DisconnectedFactHandle ) { return (DisconnectedFactHandle) handle; } else { InternalFactHandle ifh = (InternalFactHandle) handle; return new DisconnectedFactHandle(ifh.getId(), ifh.getIdentityHashCode(), ifh.getObjectHashCode(), ifh.getRecency(), ifh.getEntryPoint() != null ? ifh.getEntryPoint().getEntryPointId() : null, ifh.getObject(), ifh.isTraitOrTraitable() ); } }
Example #14
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 #15
Source File: StatefulKnowledgeSessionImpl.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public void update(final FactHandle handle, final Object object) { update(handle, object, allSetButTraitBitMask(), Object.class, null); }
Example #16
Source File: StatefulSessionTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testGetFactHandleIdentityBehavior() throws Exception { final KieBaseConfiguration kbc = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); kbc.setOption(EqualityBehaviorOption.IDENTITY); final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase(kbc)); final KieSession ksession = createKnowledgeSession(kbase); final CheeseEqual cheese = new CheeseEqual("stilton", 10); ksession.insert(cheese); final FactHandle fh1 = ksession.getFactHandle(new Cheese("stilton", 10)); assertNull(fh1); final FactHandle fh2 = ksession.getFactHandle(cheese); assertNotNull(fh2); }
Example #17
Source File: ParallelEvaluationTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
private Callable<Void> getMultipleParallelKieSessionsWithUpdatesCallable(KieBase kBase) { return new Callable<Void>() { @Override public Void call() { KieSession ksession = kBase.newKieSession(); assertThat(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda()).as("Parallel agenda has to be enabled").isTrue(); List<Integer> list = new DebugList<Integer>(); ksession.setGlobal( "list", list ); FactHandle[] fhs = new FactHandle[10]; fhs = insertFacts(ksession, 10); ksession.fireAllRules(); assertThat(list.size()).isEqualTo(10); list.clear(); for (int i = 0; i < 10; i++) { ksession.update( fhs[i], i ); } ksession.fireAllRules(); assertThat(list.size()).isEqualTo(10); return null; } }; }
Example #18
Source File: NamedEntryPoint.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public void update(FactHandle handle, Object object, String... modifiedProperties) { Class modifiedClass = object.getClass(); TypeDeclaration typeDeclaration = kBase.getOrCreateExactTypeDeclaration( modifiedClass ); BitMask mask = typeDeclaration.isPropertyReactive() ? calculatePositiveMask( modifiedClass, asList(modifiedProperties), typeDeclaration.getAccessibleProperties() ) : AllSetBitMask.get(); update( (InternalFactHandle) handle, object, mask, modifiedClass, null); }
Example #19
Source File: RuleExecutionTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testOnDeleteMatchConsequence() throws Exception { String str = "import " + Person.class.getCanonicalName() + ";\n" + "rule R1 when\n" + " $p : Person( age > 30 )\n" + "then\n" + " $p.setStatus(\"in\");\n" + "then[$onDeleteMatch$]\n" + " $p.setStatus(\"out\");\n" + "end\n"; KieSession ksession = new KieHelper() .addContent(str, ResourceType.DRL) .build() .newKieSession(); Person mario = new Person("Mario", 40); FactHandle fact = ksession.insert(mario); ksession.fireAllRules(); assertEquals("in", mario.getStatus()); ksession.delete(fact); ksession.fireAllRules(); assertEquals("out", mario.getStatus()); }
Example #20
Source File: TruthMaintenanceTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testDeleteLogicalAssertion() { // BZ-1317026 String drl = "rule R1 when\n" + "then\n" + " insertLogical( \"test\" ); \n" + "end\n"; KieSession ksession = new KieHelper().addContent( drl, ResourceType.DRL ) .build() .newKieSession(); try { ksession.fireAllRules(); Collection<FactHandle> fhs = ksession.getFactHandles( new ClassObjectFilter( String.class ) ); assertEquals(1, fhs.size()); for (FactHandle fh : fhs) { ksession.delete( fh ); } fhs = ksession.getFactHandles( new ClassObjectFilter( String.class ) ); assertEquals(0, fhs.size()); } finally { ksession.dispose(); } }
Example #21
Source File: GetObjectCommand.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public Object execute(Context context) { KieSession ksession = ((RegistryContext) context).lookup( KieSession.class ); FactHandle factHandle = this.factHandle; if( factHandle == null ) { factHandle = this.disconnectedFactHandle; } Object object = ksession.getObject( factHandle ); if (this.outIdentifier != null) { ((RegistryContext) context).lookup( ExecutionResultImpl.class ).setResult( this.outIdentifier, object ); } return object; }
Example #22
Source File: StatefulKnowledgeSessionImpl.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public void update(FactHandle handle, Object object, String... modifiedProperties) { checkAlive(); this.defaultEntryPoint.update(handle, object, modifiedProperties); }
Example #23
Source File: ObjectDeletedEventImpl.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public ObjectDeletedEventImpl(final WorkingMemory workingMemory, final PropagationContext propagationContext, final FactHandle handle, final Object object) { super( ((InternalWorkingMemory) workingMemory ).getKnowledgeRuntime(), propagationContext ); this.factHandle = handle; this.oldbOject = object; }
Example #24
Source File: NamedEntryPoint.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public FactHandle insertAsync(Object object) { ObjectTypeConf typeConf = getObjectTypeConfigurationRegistry().getObjectTypeConf( this.entryPoint, object ); PropagationContext pctx = this.pctxFactory.createPropagationContext(this.wm.getNextPropagationIdCounter(), PropagationContext.Type.INSERTION, null, null, null, entryPoint); InternalFactHandle handle = createHandle( object, typeConf ); pctx.setFactHandle(handle); this.entryPointNode.assertObject( handle, pctx, typeConf, this.wm ); this.wm.getRuleRuntimeEventSupport().fireObjectInserted(pctx, handle, object, this.wm); return handle; }
Example #25
Source File: SerializableActivation.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public List<Object> getObjects() { List<Object> objects = new ArrayList<Object>( this.factHandles.size() ); for( FactHandle handle : this.factHandles ) { objects.add( ((InternalFactHandle)handle).getObject() ); } return Collections.unmodifiableList( objects ); }
Example #26
Source File: TraitHelper.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public void insertLogical(final Activation activation, final Object object, final Mode... modes ) { if ( !activation.isMatched() ) { // Activation is already unmatched, can't do logical insertions against it return; } // iterate to find previous equal logical insertion FactHandle handle = workingMemory.getTruthMaintenanceSystem().insert( object, modes, activation.getRule(), activation ); }
Example #27
Source File: PropertyReactivityBlockerTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test() public void testAbis_Working() { // DROOLS-644 String drl = "import " + Person.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R when\n" + " $p1 : Person( name == \"Mario\", $a1: age) \n" + " $p2 : Person( age > $a1 ) \n" + "then\n" + " list.add(\"t0\");\n" + "end\n" + "rule Z when\n" + " $p1 : Person( name == \"Mario\" ) \n" + "then\n" + " modify($p1) { setAge(35); } \n" + "end\n" ; // making the default explicit: KieSession ksession = new KieHelper(PropertySpecificOption.ALWAYS).addContent(drl, ResourceType.DRL) .build() .newKieSession(); System.out.println(drl); ReteDumper.dumpRete(ksession); List<String> list = new ArrayList<String>(); ksession.setGlobal("list", list); Person mario = new Person("Mario", 40); Person mark = new Person("Mark", 37); FactHandle fh_mario = ksession.insert(mario); ksession.insert(mark); int x = ksession.fireAllRules(); assertEquals(1, list.size()); assertEquals("t0", list.get(0)); }
Example #28
Source File: WorkingMemoryLogger.java From kogito-runtimes with Apache License 2.0 | 5 votes |
/** * Returns a String that can be used as unique identifier for an * activation. Since the activationId is the same for all assertions * that are created during a single insert, update or retract, the * key of the tuple of the activation is added too (which is a set * of fact handle ids). * * @param match The match for which a unique id should be generated * @return A unique id for the activation */ private static String getActivationId(Match match) { final StringBuilder result = new StringBuilder( match.getRule().getName() ); result.append(" ["); List< ? extends FactHandle> factHandles = match.getFactHandles(); for ( int i = 0; i < factHandles.size(); i++ ) { result.append( ((InternalFactHandle) factHandles.get(i)).getId() ); if ( i < factHandles.size() - 1 ) { result.append( ", " ); } } return result.append( "]" ).toString(); }
Example #29
Source File: AbstractConcurrentInsertionsTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
protected Callable<Boolean> getTask(final int objectCount, final KieSession ksession, final boolean disposeSession, final boolean updateFacts) { return () -> { try { for (int j = 0; j < 10; j++) { final FactHandle[] facts = new FactHandle[objectCount]; final FactHandle[] stringFacts = new FactHandle[objectCount]; for (int i = 0; i < objectCount; i++) { facts[i] = ksession.insert(new AtomicInteger(i)); stringFacts[i] = ksession.insert("test_" + i); } if (updateFacts) { for (int i = 0; i < objectCount; i++) { ksession.update(facts[i], new AtomicInteger(-i)); ksession.update(stringFacts[i], "updated_test_" + i); } } for (int i = 0; i < objectCount; i++) { ksession.delete(facts[i]); ksession.delete(stringFacts[i]); } ksession.fireAllRules(); } return true; } catch (final Exception e) { e.printStackTrace(); return false; } finally { if (disposeSession) { ksession.dispose(); } } }; }
Example #30
Source File: StatefulKnowledgeSessionImpl.java From kogito-runtimes with Apache License 2.0 | 5 votes |
private Map getActivationParameters(Tuple tuple) { Map result = new HashMap(); Declaration[] declarations = ((RuleTerminalNode) tuple.getTupleSink()).getAllDeclarations(); for (int i = 0; i < declarations.length; i++) { FactHandle handle = tuple.get(declarations[i]); if (handle instanceof InternalFactHandle) { result.put(declarations[i].getIdentifier(), declarations[i].getValue(this, ((InternalFactHandle) handle).getObject())); } } return result; }