Java Code Examples for org.kie.api.runtime.KieSession#getFactHandles()
The following examples show how to use
org.kie.api.runtime.KieSession#getFactHandles() .
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: FromExternalFactHandleCommand.java From kogito-runtimes with Apache License 2.0 | 6 votes |
public FactHandle execute(Context context) { KieSession ksession = ((RegistryContext) context).lookup( KieSession.class ); Collection<FactHandle> factHandles = ksession.getFactHandles(); int fhId = Integer.parseInt(factHandleExternalForm.split(":")[1]); for (FactHandle factHandle : factHandles) { if (factHandle instanceof InternalFactHandle && ((InternalFactHandle) factHandle).getId() == fhId) { InternalFactHandle fhClone = ((InternalFactHandle) factHandle).clone(); if (disconnected) { fhClone.disconnect(); } return fhClone; } } return null; }
Example 2
Source File: BusPassService.java From buspass-ws with Apache License 2.0 | 6 votes |
/** * Search the {@link KieSession} for bus passes. */ private BusPass findBusPass(KieSession kieSession) { // Find all BusPass facts and 1st generation child classes of BusPass. ObjectFilter busPassFilter = new ObjectFilter() { @Override public boolean accept(Object object) { if (BusPass.class.equals(object.getClass())) return true; if (BusPass.class.equals(object.getClass().getSuperclass())) return true; return false; } }; // printFactsMessage(kieSession); List<BusPass> facts = new ArrayList<BusPass>(); for (FactHandle handle : kieSession.getFactHandles(busPassFilter)) { facts.add((BusPass) kieSession.getObject(handle)); } if (facts.size() == 0) { return null; } // Assumes that the rules will always be generating a single bus pass. return facts.get(0); }
Example 3
Source File: FactFinder.java From qzr with Apache License 2.0 | 6 votes |
/** * An assertion that a fact of the expected class with specified properties * is in working memory. * * @param session * A {@link KnowledgeSession} in which we are looking for the * fact. * @param factClass * The simple name of the class of the fact we're looking for. * @param expectedProperties * A sequence of expected property name/value pairs. * @throws IllegalAccessException * @throws InvocationTargetException * @throws NoSuchMethodException */ @SuppressWarnings("unchecked") public List<T> findFacts(final KieSession session, final BeanPropertyFilter... expectedProperties) { ObjectFilter filter = new ObjectFilter() { @Override public boolean accept(Object object) { return object.getClass().equals(classToFind) && beanMatcher.matches(object, expectedProperties); } }; Collection<FactHandle> factHandles = session.getFactHandles(filter); List<T> facts = new ArrayList<T>(); for (FactHandle handle : factHandles) { facts.add((T) session.getObject(handle)); } return facts; }
Example 4
Source File: FactFinder.java From qzr with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") public List<T> findFacts(final KieSession session) { ObjectFilter filter = new ObjectFilter() { @Override public boolean accept(Object object) { return object.getClass().equals(classToFind); } }; Collection<FactHandle> factHandles = session.getFactHandles(filter); List<T> facts = new ArrayList<T>(); for (FactHandle handle : factHandles) { facts.add((T) session.getObject(handle)); } return facts; }
Example 5
Source File: TruthMaintenanceTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
public InternalFactHandle getFactHandle(FactHandle factHandle, KieSession ksession) { Map<Long, FactHandle> handles = new HashMap<>(); for ( FactHandle fh : ksession.getFactHandles() ) { handles.put( ((InternalFactHandle) fh).getId(), fh ); } return (InternalFactHandle) handles.get( ((InternalFactHandle) factHandle).getId() ); }
Example 6
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 7
Source File: TruthMaintenanceTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testDeleteLogicalAssertionFromRule() { // BZ-1317026 String drl = "global java.util.List list;\n" + "rule R1 when\n" + "then\n" + " insertLogical( \"test\" ); \n" + "end\n" + "rule R2 when\n" + " $s : String()\n" + "then\n" + " list.add( $s ); \n" + " delete( $s ); \n" + "end\n"; KieSession ksession = new KieHelper().addContent( drl, ResourceType.DRL ) .build() .newKieSession(); try { List<String> list = new ArrayList<String>(); ksession.setGlobal( "list", list ); ksession.fireAllRules(); assertEquals(1, list.size()); assertEquals("test", list.get(0)); Collection<FactHandle> fhs = ksession.getFactHandles( new ClassObjectFilter( String.class ) ); assertEquals(0, fhs.size()); } finally { ksession.dispose(); } }
Example 8
Source File: TruthMaintenanceTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testStatedDeleteLogicalAssertionFromRule() { // BZ-1317026 String drl = "global java.util.List list;\n" + "rule R1 when\n" + "then\n" + " insertLogical( \"test\" ); \n" + "end\n" + "rule R2 when\n" + " $s : String()\n" + "then\n" + " list.add( $s ); \n" + " delete( $s, org.kie.api.runtime.rule.FactHandle.State.STATED ); \n" + "end\n"; KieSession ksession = new KieHelper().addContent( drl, ResourceType.DRL ) .build() .newKieSession(); try { List<String> list = new ArrayList<String>(); ksession.setGlobal( "list", list ); ksession.fireAllRules(); assertEquals(1, list.size()); assertEquals("test", list.get(0)); Collection<FactHandle> fhs = ksession.getFactHandles( new ClassObjectFilter( String.class ) ); assertEquals(1, fhs.size()); } finally { ksession.dispose(); } }
Example 9
Source File: BusPassService.java From buspass-ws with Apache License 2.0 | 5 votes |
/** * Print out details of all facts in working memory. * Handy for debugging. */ @SuppressWarnings("unused") private void printFactsMessage(KieSession kieSession) { Collection<FactHandle> allHandles = kieSession.getFactHandles(); String msg = "\nAll facts:\n"; for (FactHandle handle : allHandles) { msg += " " + kieSession.getObject(handle) + "\n"; } System.out.println(msg); }
Example 10
Source File: FactFinder.java From qzr with Apache License 2.0 | 5 votes |
public void deleteFacts(final KieSession session, final BeanPropertyFilter... expectedProperties) { ObjectFilter filter = new ObjectFilter() { @Override public boolean accept(Object object) { return object.getClass().equals(classToFind) && beanMatcher.matches(object, expectedProperties); } }; Collection<FactHandle> factHandles = session.getFactHandles(filter); for (FactHandle handle : factHandles) { session.delete(handle); } }
Example 11
Source File: TruthMaintenanceTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testJustificationStateOverridingBySuperClass() { //DROOLS-352 String droolsSource = "package org.drools.tms.test; \n" + "" + "declare Foo end \n" + "declare Bar extends Foo end \n" + "" + "rule Justify_Sub \n" + "when \n" + "then \n" + " insertLogical( new Bar() ); \n" + " insertLogical( 42.0 ); \n" + "end \n" + "rule Justify_Sup \n" + "when \n" + " $d : Double() \n" + "then \n" + " insertLogical( new Foo() ); \n" + "end \n" + "" + "" + "rule Fuu when Foo() then end \n" + "rule Bor when Bar() then end \n"; ///////////////////////////////////// KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); kieConf.setOption( EqualityBehaviorOption.EQUALITY ); KieBase kbase = loadKnowledgeBaseFromString( kieConf, droolsSource ); KieSession session = kbase.newKieSession(); try { session.fireAllRules(); for ( FactHandle fh : session.getFactHandles() ) { InternalFactHandle ifh = (InternalFactHandle) fh; assertEquals(EqualityKey.JUSTIFIED, ifh.getEqualityKey().getStatus()); } } finally { session.dispose(); } }
Example 12
Source File: ProcessActionTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testActionContextJava() { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); Reader source = new StringReader( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + " type=\"RuleFlow\" name=\"flow\" id=\"org.drools.actions\" package-name=\"org.drools\" version=\"1\" >\n" + "\n" + " <header>\n" + " <imports>\n" + " <import name=\"org.jbpm.integrationtests.test.Message\" />\n" + " </imports>\n" + " <globals>\n" + " <global identifier=\"list\" type=\"java.util.List\" />\n" + " </globals>\n" + " <variables>\n" + " <variable name=\"variable\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>SomeText</value>\n" + " </variable>\n" + " </variables>\n" + " </header>\n" + "\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <actionNode id=\"2\" name=\"MyActionNode\" >\n" + " <action type=\"expression\" dialect=\"java\" >System.out.println(\"Triggered\");\n" + "String myVariable = (String) kcontext.getVariable(\"variable\");\n" + "list.add(myVariable);\n" + "String nodeName = kcontext.getNodeInstance().getNodeName();\n" + "list.add(nodeName);\n" + "insert( new Message() );\n" + "</action>\n" + " </actionNode>\n" + " <end id=\"3\" name=\"End\" />\n" + " </nodes>\n" + "\n" + " <connections>\n" + " <connection from=\"1\" to=\"2\" />\n" + " <connection from=\"2\" to=\"3\" />\n" + " </connections>\n" + "\n" + "</process>"); kbuilder.add(new ReaderResource(source), ResourceType.DRF); KieBase kbase = kbuilder.newKieBase(); KieSession ksession = kbase.newKieSession(); List<String> list = new ArrayList<String>(); ksession.setGlobal("list", list); ProcessInstance processInstance = ksession.startProcess("org.drools.actions"); assertEquals(2, list.size()); assertEquals("SomeText", list.get(0)); assertEquals("MyActionNode", list.get(1)); Collection<FactHandle> factHandles = ksession.getFactHandles(new ObjectFilter() { public boolean accept(Object object) { return object instanceof Message; } }); assertFalse(factHandles.isEmpty()); assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState()); }
Example 13
Source File: ProcessActionTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testActionContextMVEL() { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); Reader source = new StringReader( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + " type=\"RuleFlow\" name=\"flow\" id=\"org.drools.actions\" package-name=\"org.drools\" version=\"1\" >\n" + "\n" + " <header>\n" + " <imports>\n" + " <import name=\"org.jbpm.integrationtests.test.Message\" />\n" + " </imports>\n" + " <globals>\n" + " <global identifier=\"list\" type=\"java.util.List\" />\n" + " </globals>\n" + " <variables>\n" + " <variable name=\"variable\" >\n" + " <type name=\"org.jbpm.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>SomeText</value>\n" + " </variable>\n" + " </variables>\n" + " </header>\n" + "\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <actionNode id=\"2\" name=\"MyActionNode\" >\n" + " <action type=\"expression\" dialect=\"mvel\" >System.out.println(\"Triggered\");\n" + "System.out.println(kcontext.getKnowledgeRuntime());\n" + "String myVariable = (String) kcontext.getVariable(\"variable\");\n" + "list.add(myVariable);\n" + "String nodeName = kcontext.getNodeInstance().getNodeName();\n" + "list.add(nodeName);\n" + "insert( new Message() );\n" + "</action>\n" + " </actionNode>\n" + " <end id=\"3\" name=\"End\" />\n" + " </nodes>\n" + "\n" + " <connections>\n" + " <connection from=\"1\" to=\"2\" />\n" + " <connection from=\"2\" to=\"3\" />\n" + " </connections>\n" + "\n" + "</process>"); kbuilder.add(new ReaderResource(source), ResourceType.DRF); if ( kbuilder.hasErrors() ) { fail( kbuilder.getErrors().toString() ); } KieBase kbase = kbuilder.newKieBase(); KieSession ksession = kbase.newKieSession(); List<String> list = new ArrayList<String>(); ksession.setGlobal("list", list); ProcessInstance processInstance = ksession.startProcess("org.drools.actions"); assertEquals(2, list.size()); assertEquals("SomeText", list.get(0)); assertEquals("MyActionNode", list.get(1)); Collection<FactHandle> factHandles = ksession.getFactHandles(new ObjectFilter() { public boolean accept(Object object) { return object instanceof Message; } }); assertFalse(factHandles.isEmpty()); assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState()); }
Example 14
Source File: ProcessActionTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testActionContextJavaBackwardCheck() { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); Reader source = new StringReader( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + " xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + " type=\"RuleFlow\" name=\"flow\" id=\"org.drools.actions\" package-name=\"org.drools\" version=\"1\" >\n" + "\n" + " <header>\n" + " <imports>\n" + " <import name=\"org.jbpm.integrationtests.test.Message\" />\n" + " </imports>\n" + " <globals>\n" + " <global identifier=\"list\" type=\"java.util.List\" />\n" + " </globals>\n" + " <variables>\n" + " <variable name=\"variable\" >\n" + " <type name=\"org.drools.core.process.core.datatype.impl.type.StringDataType\" />\n" + " <value>SomeText</value>\n" + " </variable>\n" + " </variables>\n" + " </header>\n" + "\n" + " <nodes>\n" + " <start id=\"1\" name=\"Start\" />\n" + " <actionNode id=\"2\" name=\"MyActionNode\" >\n" + " <action type=\"expression\" dialect=\"java\" >System.out.println(\"Triggered\");\n" + "String myVariable = (String) kcontext.getVariable(\"variable\");\n" + "list.add(myVariable);\n" + "String nodeName = kcontext.getNodeInstance().getNodeName();\n" + "list.add(nodeName);\n" + "insert( new Message() );\n" + "</action>\n" + " </actionNode>\n" + " <end id=\"3\" name=\"End\" />\n" + " </nodes>\n" + "\n" + " <connections>\n" + " <connection from=\"1\" to=\"2\" />\n" + " <connection from=\"2\" to=\"3\" />\n" + " </connections>\n" + "\n" + "</process>"); kbuilder.add(new ReaderResource(source), ResourceType.DRF); KieBase kbase = kbuilder.newKieBase(); KieSession ksession = kbase.newKieSession(); List<String> list = new ArrayList<String>(); ksession.setGlobal("list", list); ProcessInstance processInstance = ksession.startProcess("org.drools.actions"); assertEquals(2, list.size()); assertEquals("SomeText", list.get(0)); assertEquals("MyActionNode", list.get(1)); Collection<FactHandle> factHandles = ksession.getFactHandles(new ObjectFilter() { public boolean accept(Object object) { return object instanceof Message; } }); assertFalse(factHandles.isEmpty()); assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState()); }
Example 15
Source File: DroolsUtil.java From qzr with Apache License 2.0 | 2 votes |
/** * Find all handles to facts in working memory matching an * {@link ObjectFilter}. For example, to find all facts of a class called * "MyObject": * * <pre> * getFactHandles(new ObjectFilter() { * public boolean accept(Object object) { * return object.getClass().getSimpleName() * .equals(MyObject.class.getSimpleName()); * } * }); * </pre> * * @param filter * The {@link ObjectFilter}. * @return A collection of facts matching the filter. */ public static Collection<FactHandle> getFactHandles(KieSession ksession, ObjectFilter filter) { return ksession.getFactHandles(filter); }