Search in sources :

Example 1 with Environment

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());
}
Also used : IdentityPlaceholderResolverStrategy(org.drools.core.marshalling.impl.IdentityPlaceholderResolverStrategy) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 2 with Environment

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());
}
Also used : IdentityPlaceholderResolverStrategy(org.drools.core.marshalling.impl.IdentityPlaceholderResolverStrategy) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 3 with Environment

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());
}
Also used : IdentityPlaceholderResolverStrategy(org.drools.core.marshalling.impl.IdentityPlaceholderResolverStrategy) KieBase(org.kie.api.KieBase) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) Cell(org.drools.compiler.Cell) Test(org.junit.Test)

Example 4 with Environment

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;
}
Also used : Environment(org.kie.api.runtime.Environment) PersistenceContext(org.drools.persistence.api.PersistenceContext) PersistenceContextManager(org.drools.persistence.api.PersistenceContextManager)

Example 5 with Environment

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);
    }
}
Also used : WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) Environment(org.kie.api.runtime.Environment) PersistenceContext(org.drools.persistence.api.PersistenceContext) WorkItemInfo(org.drools.persistence.info.WorkItemInfo)

Aggregations

Environment (org.kie.api.runtime.Environment)31 Test (org.junit.Test)15 KieBase (org.kie.api.KieBase)9 KieSession (org.kie.api.runtime.KieSession)9 ArrayList (java.util.ArrayList)6 DroolsPersistenceUtil.createEnvironment (org.drools.persistence.util.DroolsPersistenceUtil.createEnvironment)6 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)6 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)5 IdentityPlaceholderResolverStrategy (org.drools.core.marshalling.impl.IdentityPlaceholderResolverStrategy)4 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)4 Cheese (org.drools.compiler.Cheese)3 PersistenceContext (org.drools.persistence.api.PersistenceContext)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Collection (java.util.Collection)2 List (java.util.List)2 EntityManager (javax.persistence.EntityManager)2 UserTransaction (javax.transaction.UserTransaction)2 Person (org.drools.compiler.Person)2 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)2