Search in sources :

Example 1 with InternationalAddress

use of org.drools.modelcompiler.oopathdtables.InternationalAddress in project drools by kiegroup.

the class QueryTest method testQueryWithOOPath.

@Test
public void testQueryWithOOPath() {
    String str = "import " + java.util.List.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.Person.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.Address.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.InternationalAddress.class.getCanonicalName() + ";" + "query listSafeCities\n" + "$cities : List() from accumulate (Person ( $city: /address#InternationalAddress[state == \"Safecountry\"]/city), collectList($city))\n" + "end";
    KieSession ksession = getKieSession(str);
    org.drools.modelcompiler.oopathdtables.Person person = new org.drools.modelcompiler.oopathdtables.Person();
    person.setAddress(new InternationalAddress("", 1, "Milan", "Safecountry"));
    ksession.insert(person);
    org.drools.modelcompiler.oopathdtables.Person person2 = new org.drools.modelcompiler.oopathdtables.Person();
    person2.setAddress(new InternationalAddress("", 1, "Rome", "Unsafecountry"));
    ksession.insert(person2);
    QueryResults results = ksession.getQueryResults("listSafeCities");
    List cities = (List) results.iterator().next().get("$cities");
    assertEquals(1, cities.size());
    assertEquals("Milan", cities.get(0));
}
Also used : InternationalAddress(org.drools.modelcompiler.oopathdtables.InternationalAddress) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.modelcompiler.domain.Person) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 2 with InternationalAddress

use of org.drools.modelcompiler.oopathdtables.InternationalAddress in project drools by kiegroup.

the class QueryTest method testQueryWithOOPathTransformedToFromInsideAcc.

@Test
public void testQueryWithOOPathTransformedToFromInsideAcc() {
    String str = "import " + java.util.List.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.Person.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.Address.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.InternationalAddress.class.getCanonicalName() + ";" + "query listSafeCities\n" + "$cities : List() from accumulate (" + "    $p : Person() and\n" + "    $a : InternationalAddress(state == \"Safecountry\") from $p.address and\n" + "    $city : String() from $a.city, collectList($city))\n" + "end";
    KieSession ksession = getKieSession(str);
    org.drools.modelcompiler.oopathdtables.Person person2 = new org.drools.modelcompiler.oopathdtables.Person();
    person2.setAddress(new InternationalAddress("", 1, "Rome", "Unsafecountry"));
    ksession.insert(person2);
    org.drools.modelcompiler.oopathdtables.Person person = new org.drools.modelcompiler.oopathdtables.Person();
    person.setAddress(new InternationalAddress("", 1, "Milan", "Safecountry"));
    ksession.insert(person);
    QueryResults results = ksession.getQueryResults("listSafeCities");
    List cities = (List) results.iterator().next().get("$cities");
    assertEquals(1, cities.size());
    assertEquals("Milan", cities.get(0));
}
Also used : InternationalAddress(org.drools.modelcompiler.oopathdtables.InternationalAddress) QueryResults(org.kie.api.runtime.rule.QueryResults) InternationalAddress(org.drools.modelcompiler.oopathdtables.InternationalAddress) ArrayList(java.util.ArrayList) List(java.util.List) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Example 3 with InternationalAddress

use of org.drools.modelcompiler.oopathdtables.InternationalAddress in project drools by kiegroup.

the class QueryTest method testQueryWithOOPathTransformedToFrom.

@Test
public void testQueryWithOOPathTransformedToFrom() {
    String str = "import " + java.util.List.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.Person.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.Address.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.InternationalAddress.class.getCanonicalName() + ";" + "query listSafeCities\n" + "$p  : Person()\n" + "$a  : InternationalAddress(state == \"Safecountry\") from $p.address\n" + "$cities : List() from accumulate ($city : String() from $a.city, collectList($city))\n" + "end";
    KieSession ksession = getKieSession(str);
    org.drools.modelcompiler.oopathdtables.Person person = new org.drools.modelcompiler.oopathdtables.Person();
    person.setAddress(new InternationalAddress("", 1, "Milan", "Safecountry"));
    ksession.insert(person);
    org.drools.modelcompiler.oopathdtables.Person person2 = new org.drools.modelcompiler.oopathdtables.Person();
    person2.setAddress(new InternationalAddress("", 1, "Rome", "Unsafecountry"));
    ksession.insert(person2);
    QueryResults results = ksession.getQueryResults("listSafeCities");
    List cities = (List) results.iterator().next().get("$cities");
    assertEquals(1, cities.size());
    assertEquals("Milan", cities.get(0));
}
Also used : InternationalAddress(org.drools.modelcompiler.oopathdtables.InternationalAddress) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.modelcompiler.domain.Person) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 4 with InternationalAddress

use of org.drools.modelcompiler.oopathdtables.InternationalAddress in project drools by kiegroup.

the class FlowTest method testQueryOOPathAccumulateTransformed.

@Test
public void testQueryOOPathAccumulateTransformed() {
    final org.drools.model.QueryDef queryDef_listSafeCities = query("listSafeCities");
    final Variable<org.drools.modelcompiler.oopathdtables.Person> var_$p = declarationOf(org.drools.modelcompiler.oopathdtables.Person.class, "$p");
    final Variable<org.drools.modelcompiler.oopathdtables.InternationalAddress> var_$a = declarationOf(org.drools.modelcompiler.oopathdtables.InternationalAddress.class, "$a", from(var_$p, (_this) -> _this.getAddress()));
    final Variable<java.util.List> var_$cities = declarationOf(java.util.List.class, "$cities");
    final Variable<String> var_$city = declarationOf(String.class, "$city", from(var_$a, (_this) -> _this.getCity()));
    org.drools.model.Query listSafeCities_build = queryDef_listSafeCities.build(input(var_$p), expr("$expr$2$", var_$a, (_this) -> _this.getState().equals("Safecountry")).indexedBy(String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getState(), "Safecountry").reactOn("state"), accumulate(expr(var_$city, s -> true), accFunction(org.drools.core.base.accumulators.CollectListAccumulateFunction.class, var_$city).as(var_$cities)));
    Model model = new ModelImpl().addQuery(listSafeCities_build);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession ksession = kieBase.newKieSession();
    org.drools.modelcompiler.oopathdtables.Person person = new org.drools.modelcompiler.oopathdtables.Person();
    person.setAddress(new InternationalAddress("", 1, "Milan", "Safecountry"));
    ksession.insert(person);
    org.drools.modelcompiler.oopathdtables.Person person2 = new org.drools.modelcompiler.oopathdtables.Person();
    person2.setAddress(new InternationalAddress("", 1, "Rome", "Unsafecountry"));
    ksession.insert(person2);
    QueryResults results = ksession.getQueryResults("listSafeCities");
    assertEquals("Milan", ((List) results.iterator().next().get("$cities")).iterator().next());
}
Also used : Arrays(java.util.Arrays) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) FlowDSL.eval(org.drools.model.FlowDSL.eval) ObjectOutput(java.io.ObjectOutput) Man(org.drools.modelcompiler.domain.Man) Global(org.drools.model.Global) FlowDSL.not(org.drools.model.FlowDSL.not) Toy(org.drools.modelcompiler.domain.Toy) FlowDSL.and(org.drools.model.FlowDSL.and) FlowDSL.accFunction(org.drools.model.FlowDSL.accFunction) Relationship(org.drools.modelcompiler.domain.Relationship) BaseModelTest.getObjectsIntoList(org.drools.modelcompiler.BaseModelTest.getObjectsIntoList) Assert.assertThat(org.junit.Assert.assertThat) Query2Def(org.drools.model.Query2Def) Child(org.drools.modelcompiler.domain.Child) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) FlowDSL.from(org.drools.model.FlowDSL.from) QueryResults(org.kie.api.runtime.rule.QueryResults) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) FlowDSL.reactiveFrom(org.drools.model.FlowDSL.reactiveFrom) Assertions(org.assertj.core.api.Assertions) KieSession(org.kie.api.runtime.KieSession) FlowDSL.declarationOf(org.drools.model.FlowDSL.declarationOf) TargetPolicy(org.drools.modelcompiler.domain.TargetPolicy) FlowDSL.or(org.drools.model.FlowDSL.or) EventProcessingOption(org.kie.api.conf.EventProcessingOption) Collection(java.util.Collection) FlowDSL.on(org.drools.model.FlowDSL.on) Customer(org.drools.modelcompiler.domain.Customer) Serializable(java.io.Serializable) ConstraintType(org.drools.model.Index.ConstraintType) List(java.util.List) Query(org.drools.model.Query) FlowDSL.executeScript(org.drools.model.FlowDSL.executeScript) InternationalAddress(org.drools.modelcompiler.oopathdtables.InternationalAddress) Assert.assertFalse(org.junit.Assert.assertFalse) KnowledgeBaseFactory(org.drools.core.impl.KnowledgeBaseFactory) FlowDSL.bind(org.drools.model.FlowDSL.bind) FlowDSL.valueOf(org.drools.model.FlowDSL.valueOf) Person(org.drools.modelcompiler.domain.Person) ObjectInput(java.io.ObjectInput) InOperator(org.drools.model.operators.InOperator) FlowDSL.when(org.drools.model.FlowDSL.when) ModelImpl(org.drools.model.impl.ModelImpl) FlowDSL.globalOf(org.drools.model.FlowDSL.globalOf) StockTick(org.drools.modelcompiler.domain.StockTick) Employee.createEmployee(org.drools.modelcompiler.domain.Employee.createEmployee) ClockType(org.drools.core.ClockType) FlowDSL.accumulate(org.drools.model.FlowDSL.accumulate) ArrayList(java.util.ArrayList) Result(org.drools.modelcompiler.domain.Result) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Adult(org.drools.modelcompiler.domain.Adult) Employee(org.drools.modelcompiler.domain.Employee) FlowDSL.execute(org.drools.model.FlowDSL.execute) Woman(org.drools.modelcompiler.domain.Woman) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) FlowDSL.rule(org.drools.model.FlowDSL.rule) FlowDSL.expr(org.drools.model.FlowDSL.expr) Variable(org.drools.model.Variable) FlowDSL.query(org.drools.model.FlowDSL.query) Address(org.drools.modelcompiler.domain.Address) FlowDSL.window(org.drools.model.FlowDSL.window) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) FlowDSL.input(org.drools.model.FlowDSL.input) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) TimeUnit(java.util.concurrent.TimeUnit) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) Assert.assertNull(org.junit.Assert.assertNull) FlowDSL.forall(org.drools.model.FlowDSL.forall) Rule(org.drools.model.Rule) Query1Def(org.drools.model.Query1Def) Assert.assertEquals(org.junit.Assert.assertEquals) InternationalAddress(org.drools.modelcompiler.oopathdtables.InternationalAddress) KieBase(org.kie.api.KieBase) BaseModelTest.getObjectsIntoList(org.drools.modelcompiler.BaseModelTest.getObjectsIntoList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ModelImpl(org.drools.model.impl.ModelImpl) QueryResults(org.kie.api.runtime.rule.QueryResults) Query(org.drools.model.Query) Model(org.drools.model.Model) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Example 5 with InternationalAddress

use of org.drools.modelcompiler.oopathdtables.InternationalAddress in project drools by kiegroup.

the class AccumulateTest method testAccumulateFromWithConstraint.

@Test
public void testAccumulateFromWithConstraint() {
    String str = "import " + java.util.List.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.Person.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.Address.class.getCanonicalName() + ";" + "import " + org.drools.modelcompiler.oopathdtables.InternationalAddress.class.getCanonicalName() + ";" + "rule listSafeCities when\n" + "  $a : InternationalAddress()\n" + "  $cities : List(size > 0) from accumulate ($city : String() from $a.city, collectList($city))\n" + "then\n" + "   insert($cities.get(0));\n" + "end";
    KieSession ksession = getKieSession(str);
    ksession.insert(new InternationalAddress("", 1, "Milan", "Safecountry"));
    ksession.fireAllRules();
    List<String> results = getObjectsIntoList(ksession, String.class);
    assertEquals(1, results.size());
    assertThat(results, hasItem("Milan"));
}
Also used : InternationalAddress(org.drools.modelcompiler.oopathdtables.InternationalAddress) InternationalAddress(org.drools.modelcompiler.oopathdtables.InternationalAddress) ArrayList(java.util.ArrayList) List(java.util.List) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)5 List (java.util.List)5 InternationalAddress (org.drools.modelcompiler.oopathdtables.InternationalAddress)5 Test (org.junit.Test)5 KieSession (org.kie.api.runtime.KieSession)5 Person (org.drools.modelcompiler.domain.Person)3 QueryResults (org.kie.api.runtime.rule.QueryResults)3 IOException (java.io.IOException)1 ObjectInput (java.io.ObjectInput)1 ObjectOutput (java.io.ObjectOutput)1 Serializable (java.io.Serializable)1 Arrays (java.util.Arrays)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 Map (java.util.Map)1 TimeUnit (java.util.concurrent.TimeUnit)1 Assertions (org.assertj.core.api.Assertions)1 ClockType (org.drools.core.ClockType)1 KnowledgeBaseFactory (org.drools.core.impl.KnowledgeBaseFactory)1 FlowDSL.accFunction (org.drools.model.FlowDSL.accFunction)1