use of org.kie.api.runtime.Environment in project drools by kiegroup.
the class MarshallingTest method testNot.
@Test
public void testNot() throws Exception {
String header = "package org.drools.compiler.test;\n";
header += "import java.util.List;\n";
header += "import org.drools.compiler.Person\n";
header += "import org.drools.compiler.Cheese\n";
header += "global java.util.List list;\n";
String rule1 = "rule \"not rule test\"\n";
rule1 += "salience 10\n";
rule1 += "when\n";
rule1 += " Person()\n";
rule1 += " not Cheese( price >= 5 )\n";
rule1 += "then\n";
rule1 += " list.add( new Integer( 5 ) );\n";
rule1 += "end\n";
KieBase kBase = loadKnowledgeBaseFromString(header + rule1);
Environment env = EnvironmentFactory.newEnvironment();
env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, new ObjectMarshallingStrategy[] { new IdentityPlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT) });
KieSession ksession = kBase.newKieSession(null, env);
List list = new ArrayList();
ksession.setGlobal("list", list);
// add a person, no cheese
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
Person bobba = new Person("bobba fet", 50);
ksession.insert(bobba);
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(1, list.size());
// add another person, no cheese
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
Person darth = new Person("darth vadar", 200);
ksession.insert(darth);
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(2, list.size());
// add cheese
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
Cheese stilton = new Cheese("stilton", 5);
ksession.insert(stilton);
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(2, list.size());
// remove cheese
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.retract(ksession.getFactHandle(stilton));
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(4, list.size());
// put 2 cheeses back in
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.insert(stilton);
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
Cheese brie = new Cheese("brie", 18);
ksession.insert(brie);
ksession.fireAllRules();
assertEquals(4, list.size());
// now remove a cheese, should be no change
ksession.retract(ksession.getFactHandle(stilton));
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(4, list.size());
// now remove a person, should be no change
ksession.retract(ksession.getFactHandle(bobba));
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(4, list.size());
// removal remaining cheese, should increase by one, as one person left
ksession.retract(ksession.getFactHandle(brie));
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(5, list.size());
}
use of org.kie.api.runtime.Environment in project drools by kiegroup.
the class MarshallingTest method testExists.
@Test
public void testExists() throws Exception {
String header = "package org.drools.compiler.test;\n";
header += "import java.util.List;\n";
header += "import org.drools.compiler.Person\n";
header += "import org.drools.compiler.Cheese\n";
header += "global java.util.List list;\n";
String rule1 = "rule \"not rule test\"\n";
rule1 += "salience 10\n";
rule1 += "when\n";
rule1 += " Person()\n";
rule1 += " exists Cheese( price >= 5 )\n";
rule1 += "then\n";
rule1 += " list.add( new Integer( 5 ) );\n";
rule1 += "end\n";
KieBase kBase = loadKnowledgeBaseFromString(header + rule1);
Environment env = EnvironmentFactory.newEnvironment();
env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, new ObjectMarshallingStrategy[] { new IdentityPlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT) });
KieSession ksession = kBase.newKieSession(null, env);
List list = new ArrayList();
ksession.setGlobal("list", list);
// add a person, no cheese
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
Person bobba = new Person("bobba fet", 50);
ksession.insert(bobba);
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(0, list.size());
// add another person, no cheese
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
Person darth = new Person("darth vadar", 200);
ksession.insert(darth);
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(0, list.size());
// add cheese
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
Cheese stilton = new Cheese("stilton", 5);
ksession.insert(stilton);
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(2, list.size());
// remove cheese
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.retract(ksession.getFactHandle(stilton));
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(2, list.size());
// put 2 cheeses back in
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.insert(stilton);
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
Cheese brie = new Cheese("brie", 18);
ksession.insert(brie);
ksession.fireAllRules();
assertEquals(4, list.size());
// now remove a cheese, should be no change
ksession.retract(ksession.getFactHandle(stilton));
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(4, list.size());
// now remove a person, should be no change
ksession.retract(ksession.getFactHandle(bobba));
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(4, list.size());
// removal remaining cheese, no
ksession.retract(ksession.getFactHandle(brie));
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(4, list.size());
// put one cheese back in, with one person should increase by one
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.insert(stilton);
ksession.fireAllRules();
assertEquals(5, list.size());
}
use of org.kie.api.runtime.Environment in project drools by kiegroup.
the class CellTest method testCell.
@Test
public void testCell() throws Exception {
final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("evalmodify.drl"));
final Environment env = EnvironmentFactory.newEnvironment();
env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, new ObjectMarshallingStrategy[] { new IdentityPlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT) });
KieSession session = createKieSession(kbase, null, env);
final Cell cell1 = new Cell(9);
final Cell cell = new Cell(0);
session.insert(cell1);
session.insert(cell);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
session.fireAllRules();
assertEquals(9, cell.getValue());
}
use of org.kie.api.runtime.Environment in project drools by kiegroup.
the class JPAWorkItemManager method getPersistenceContext.
private PersistenceContext getPersistenceContext() {
Environment env = this.kruntime.getEnvironment();
PersistenceContext context = ((PersistenceContextManager) env.get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER)).getCommandScopedPersistenceContext();
return context;
}
use of org.kie.api.runtime.Environment in project drools by kiegroup.
the class JPAWorkItemManager method internalExecuteWorkItem.
public void internalExecuteWorkItem(WorkItem workItem) {
Environment env = this.kruntime.getEnvironment();
WorkItemInfo workItemInfo = new WorkItemInfo(workItem, env);
PersistenceContext context = getPersistenceContext();
workItemInfo = (WorkItemInfo) context.persist(workItemInfo);
((WorkItemImpl) workItem).setId(workItemInfo.getId());
if (this.workItems == null) {
this.workItems = new HashMap<Long, WorkItemInfo>();
}
workItems.put(workItem.getId(), workItemInfo);
WorkItemHandler handler = (WorkItemHandler) this.workItemHandlers.get(workItem.getName());
if (handler != null) {
handler.executeWorkItem(workItem, this);
} else {
throwWorkItemNotFoundException(workItem);
}
}
Aggregations