Search in sources :

Example 1 with Address

use of org.drools.compiler.Address in project drools by kiegroup.

the class StatelessStressTest method testLotsOfStateless.

@Test
@Ignore
public void testLotsOfStateless() throws Exception {
    final KieBase kbase = loadKnowledgeBase("thread_class_test.drl");
    int numThreads = 100;
    Thread[] ts = new Thread[numThreads];
    for (int i = 0; i < numThreads; i++) {
        Runnable run = new Runnable() {

            public void run() {
                long start = 0;
                long previous = 0;
                while (true) {
                    start = System.currentTimeMillis();
                    StatelessKieSession sess = kbase.newStatelessKieSession();
                    try {
                        sess = SerializationHelper.serializeObject(sess);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    Person p = new Person();
                    p.setName("Michael");
                    Address add1 = new Address();
                    add1.setStreet("High");
                    Address add2 = new Address();
                    add2.setStreet("Low");
                    List l = new ArrayList();
                    l.add(add1);
                    l.add(add2);
                    p.setAddresses(l);
                    sess.execute(p);
                    long current = System.currentTimeMillis() - start;
                    if (previous != 0) {
                        float f = current / previous;
                        if (f > 1) {
                            System.err.println("SLOWDOWN");
                        }
                    }
                    previous = current;
                }
            }
        };
        Thread t = new Thread(run);
        t.start();
        ts[i] = t;
    }
    for (int i = 0; i < ts.length; i++) {
        ts[i].join();
    }
}
Also used : Address(org.drools.compiler.Address) ArrayList(java.util.ArrayList) KieBase(org.kie.api.KieBase) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) List(java.util.List) ArrayList(java.util.ArrayList) Person(org.drools.compiler.Person) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with Address

use of org.drools.compiler.Address in project drools by kiegroup.

the class MarshallingTest method testSerializeAdd3.

/*
     *  Works Fine if both the scenarios mentioned above are skipped.
     */
@Test
public void testSerializeAdd3() throws Exception {
    // Create a rulebase, a session, and test it
    Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Dynamic1_0.drl");
    kpkgs = SerializationHelper.serializeObject(kpkgs);
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
    kBase.addPackages(kpkgs);
    kBase = SerializationHelper.serializeObject(kBase);
    List results = new ArrayList();
    KieSession session = kBase.newKieSession();
    session.setGlobal("results", results);
    InternalFactHandle stilton1 = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
    InternalFactHandle brie1 = (InternalFactHandle) session.insert(new Cheese("brie", 10));
    session.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(stilton1.getObject(), results.get(0));
    // serialize session and rulebase
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    // dynamic add pkgs
    kpkgs = loadKnowledgePackages("../test_Dynamic3_0.drl");
    kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    InternalFactHandle stilton2 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
    InternalFactHandle brie2 = (InternalFactHandle) session.insert(new Cheese("brie", 20));
    InternalFactHandle bob1 = (InternalFactHandle) session.insert(new Person("bob", 20));
    InternalFactHandle bob2 = (InternalFactHandle) session.insert(new Person("bob", 30));
    session.fireAllRules();
    assertEquals(4, results.size());
    assertEquals(stilton2.getObject(), results.get(1));
    assertEquals(bob2.getObject(), results.get(2));
    assertEquals(bob1.getObject(), results.get(3));
    // serialize session and rulebase
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    // dynamic add pkgs
    kpkgs = loadKnowledgePackages("../test_Dynamic1_2.drl");
    kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    InternalFactHandle stilton3 = (InternalFactHandle) session.insert(new Cheese("stilton", 40));
    InternalFactHandle brie3 = (InternalFactHandle) session.insert(new Cheese("brie", 40));
    InternalFactHandle bob3 = (InternalFactHandle) session.insert(new Person("bob", 40));
    InternalFactHandle bob4 = (InternalFactHandle) session.insert(new Person("bob", 40));
    InternalFactHandle addr1 = (InternalFactHandle) session.insert(new Address("bangalore"));
    InternalFactHandle addr2 = (InternalFactHandle) session.insert(new Address("India"));
    session.fireAllRules();
    assertEquals(9, results.size());
    assertEquals(stilton3.getObject(), results.get(4));
    assertEquals(bob4.getObject(), results.get(5));
    assertEquals(bob3.getObject(), results.get(6));
    assertEquals(addr2.getObject(), results.get(7));
    assertEquals(addr1.getObject(), results.get(8));
    // serialize session and rulebase
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    InternalFactHandle stilton4 = (InternalFactHandle) session.insert(new Cheese("stilton", 50));
    InternalFactHandle brie4 = (InternalFactHandle) session.insert(new Cheese("brie", 50));
    InternalFactHandle bob5 = (InternalFactHandle) session.insert(new Person("bob", 50));
    InternalFactHandle bob6 = (InternalFactHandle) session.insert(new Person("bob", 50));
    InternalFactHandle addr3 = (InternalFactHandle) session.insert(new Address("Tripura"));
    InternalFactHandle addr4 = (InternalFactHandle) session.insert(new Address("Agartala"));
    session.fireAllRules();
    assertEquals(14, results.size());
    assertEquals(stilton4.getObject(), results.get(9));
    assertEquals(bob6.getObject(), results.get(10));
    assertEquals(bob5.getObject(), results.get(11));
    assertEquals(addr4.getObject(), results.get(12));
    assertEquals(addr3.getObject(), results.get(13));
    // serialize session and rulebase
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.dispose();
}
Also used : Address(org.drools.compiler.Address) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 3 with Address

use of org.drools.compiler.Address in project drools by kiegroup.

the class NullSafeDereferencingTest method testNullSafeNestedAccessors.

@Test
public void testNullSafeNestedAccessors() {
    String str = "package org.drools.test; " + "import " + Person.class.getName() + "; " + "global java.util.List list; " + "rule R1 when " + " $street : String() " + " Person( address!.( street == $street, $zip : zipCode ) ) " + "then " + " list.add( $zip ); " + "end";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession ksession = kbase.newKieSession();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert(new Person("Mario", 38));
    Person mark = new Person("Mark", 37);
    mark.setAddress(new Address("Main Street", "", "123456"));
    ksession.insert(mark);
    ksession.insert("Main Street");
    assertEquals(1, ksession.fireAllRules());
    ksession.dispose();
    assertEquals(Arrays.asList("123456"), list);
}
Also used : Address(org.drools.compiler.Address) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 4 with Address

use of org.drools.compiler.Address in project drools by kiegroup.

the class NullSafeDereferencingTest method testNullSafeNullComparisonReverse.

@Test
public void testNullSafeNullComparisonReverse() {
    // DROOLS-82
    String str = "import org.drools.compiler.*;\n" + "rule R1 when\n" + "   Person( \"Main Street\".equalsIgnoreCase(address!.street) )\n" + "then\n" + "end";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession ksession = kbase.newKieSession();
    ksession.insert(new Person("Mario", 38));
    Person mark = new Person("Mark", 37);
    mark.setAddress(new Address("Main Street"));
    ksession.insert(mark);
    Person edson = new Person("Edson", 34);
    edson.setAddress(new Address(null));
    ksession.insert(edson);
    assertEquals(1, ksession.fireAllRules());
    ksession.dispose();
}
Also used : Address(org.drools.compiler.Address) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 5 with Address

use of org.drools.compiler.Address in project drools by kiegroup.

the class NullSafeDereferencingTest method testMixedNullSafes.

@Test
public void testMixedNullSafes() {
    String str = "import org.drools.compiler.*;\n" + "rule R1 when\n" + " $p : Person( " + " address!.street!.length > 0 && ( address!.street!.length < 15 || > 20 && < 30 ) " + " && address!.zipCode!.length > 0 && address.zipCode == \"12345\" " + " ) \n" + "then\n" + " System.out.println( $p ); \n" + "end";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession ksession = kbase.newKieSession();
    ksession.insert(new Person("Mario", 38));
    Person mark = new Person("Mark", 37);
    mark.setAddress(new Address("Main Street", null, "12345"));
    ksession.insert(mark);
    Person edson = new Person("Edson", 34);
    edson.setAddress(new Address(null));
    ksession.insert(edson);
    Person alex = new Person("Alex", 34);
    alex.setAddress(new Address("The Main Verrry Long Street"));
    ksession.insert(alex);
    Person frank = new Person("Frank", 24);
    frank.setAddress(new Address("Long Street number 21", null, "12345"));
    ksession.insert(frank);
    assertEquals(2, ksession.fireAllRules());
    ksession.dispose();
}
Also used : Address(org.drools.compiler.Address) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Aggregations

Address (org.drools.compiler.Address)26 Test (org.junit.Test)25 Person (org.drools.compiler.Person)24 KieSession (org.kie.api.runtime.KieSession)24 KieBase (org.kie.api.KieBase)17 ArrayList (java.util.ArrayList)14 List (java.util.List)11 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)7 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)7 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)7 MVELObjectClassFieldReader (org.drools.core.base.extractors.MVELObjectClassFieldReader)4 AlphaNode (org.drools.core.reteoo.AlphaNode)4 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)4 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)4 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)4 Cheese (org.drools.compiler.Cheese)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 KiePackage (org.kie.api.definition.KiePackage)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1