Search in sources :

Example 46 with ClassObjectFilter

use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.

the class LogicalTraitTest method testShadowAliasClassOnTraitLogicalRetract.

@Test
public void testShadowAliasClassOnTraitLogicalRetract() {
    String drl = "package org.drools.test; \n" + "import org.drools.core.factmodel.traits.*; \n" + "import org.drools.core.factmodel.traits.Trait; \n" + "" + "global java.util.List list; \n" + "" + "declare trait X \n" + "  fld : K \n" + "  fld2 : K \n" + "end \n" + "" + "declare trait W \n" + "  fld : Q \n" + "end \n" + "declare trait V \n" + "  fld2 : T \n" + "end \n" + "" + "declare Y \n" + "@Traitable( logical = true ) \n" + "  fld : T \n" + "  fld2 : Q \n" + "end \n" + "" + "declare trait T @Trait( logical=true ) id : int end \n" + "declare trait Q @Trait( logical=true ) id : int end \n" + "declare K @Traitable() id : int end \n" + "" + "rule Don \n" + "when \n" + "  $s : String( this == \"go\" ) \n" + "then \n" + "  K k1 = new K( 1 ); \n" + "  K k2 = new K( 2 ); \n" + "  T t = don( k1, T.class ); \n" + "  Q q = don( k2, Q.class ); \n" + "  Y y = new Y( t, q ); \n" + "  insert( y ); \n" + "" + "  don( y, X.class, true ); \n" + "end \n" + "" + "rule Check \n" + "when \n" + "  String( this == \"go\" ) \n" + "  $x : X( $f1 : fld, $f2 : fld2 ) \n" + "then \n" + "  list.add( $f1.getId() );" + "  list.add( $f2.getId() );" + "end \n" + "" + "rule Check2\n" + "when \n" + "  not String( this == \"go\" ) \n" + "  $x : Y( $f1 : fld, $f2 : fld2 ) \n" + "then \n" + "  list.add( $f1.getId() );" + "  list.add( $f2.getId() );" + "end \n" + "";
    KnowledgeBuilder kbuilderImpl = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilderImpl.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kbuilderImpl.hasErrors()) {
        fail(kbuilderImpl.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilderImpl.getKnowledgePackages());
    TraitFactory.setMode(mode, kbase);
    KieSession ks = kbase.newKieSession();
    ArrayList list = new ArrayList();
    ks.setGlobal("list", list);
    FactHandle handle = ks.insert("go");
    ks.fireAllRules();
    assertEquals(Arrays.asList(1, 2), list);
    ks.retract(handle);
    ks.fireAllRules();
    for (Object o : ks.getObjects(new ClassObjectFilter(ks.getKieBase().getFactType("org.drools.test", "Y").getFactClass()))) {
        assertTrue(o instanceof TraitableBean);
    }
    try {
        ks = SerializationHelper.getSerialisedStatefulKnowledgeSession(ks, true);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    System.out.println(list);
    assertEquals(Arrays.asList(1, 2, 1, 2), list);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 47 with ClassObjectFilter

use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.

the class DroolsAbstractPMMLTest method checkFirstDataFieldOfTypeStatus.

protected void checkFirstDataFieldOfTypeStatus(FactType type, boolean valid, boolean missing, String ctx, Object... target) {
    Class<?> klass = type.getFactClass();
    Iterator iter = getKSession().getObjects(new ClassObjectFilter(klass)).iterator();
    assertTrue(iter.hasNext());
    Object obj = iter.next();
    if (ctx == null) {
        while (type.get(obj, "context") != null && iter.hasNext()) obj = iter.next();
    } else {
        while ((!ctx.equals(type.get(obj, "context"))) && iter.hasNext()) obj = iter.next();
    }
    Object tgt = type.get(obj, "value");
    if (tgt instanceof Double) {
        assert (target[0] instanceof Double);
        assertEquals((Double) target[0], (Double) tgt, 1e-6);
    } else {
        assertEquals(target[0], tgt);
    }
    assertEquals(valid, type.get(obj, "valid"));
    assertEquals(missing, type.get(obj, "missing"));
}
Also used : ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) Iterator(java.util.Iterator)

Example 48 with ClassObjectFilter

use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.

the class ConstrainedDataDictionaryTest method testProperties.

@Test
public void testProperties() throws Exception {
    getKSession().getEntryPoint("in_Vallued").insert(1);
    getKSession().getEntryPoint("in_Intervalled").insert(8.3);
    getKSession().getEntryPoint("in_Cat").insert("aa");
    getKSession().getEntryPoint("in_Sort").insert(1);
    getKSession().fireAllRules();
    Collection<EventFactHandle> fact1 = getKSession().getFactHandles(new ClassObjectFilter(getKbase().getFactType(packageName, "Vallued").getFactClass()));
    assertEquals(1, fact1.size());
    // assertEquals(true, getKbase().getFactType(packageName, "Vallued").get(fact1.iterator().next().getObject(), "continuous"));
    Collection<EventFactHandle> fact2 = getKSession().getFactHandles(new ClassObjectFilter(getKbase().getFactType(packageName, "Intervalled").getFactClass()));
    assertEquals(1, fact2.size());
    // assertEquals(true, getKbase().getFactType(packageName,"Intervalled").get(fact2.iterator().next().getObject(),"continuous"));
    Collection<EventFactHandle> fact3 = getKSession().getFactHandles(new ClassObjectFilter(getKbase().getFactType(packageName, "Cat").getFactClass()));
    assertEquals(1, fact3.size());
    // assertEquals(true, getKbase().getFactType(packageName,"Cat").get(fact3.iterator().next().getObject(),"categorical"));
    Collection<EventFactHandle> fact4 = getKSession().getFactHandles(new ClassObjectFilter(getKbase().getFactType(packageName, "Sort").getFactClass()));
    assertEquals(1, fact4.size());
    // assertEquals(true, getKbase().getFactType(packageName,"Sort").get(fact4.iterator().next().getObject(),"ordinal"));
    checkGeneratedRules();
}
Also used : ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) EventFactHandle(org.drools.core.common.EventFactHandle) DroolsAbstractPMMLTest(org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest) Test(org.junit.Test)

Example 49 with ClassObjectFilter

use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.

the class UserDefinedFunctionsTest method testComplexFunctionsNested2.

@Test
public void testComplexFunctionsNested2() throws Exception {
    setKSession(getModelSession(source2, VERBOSE));
    setKbase(getKSession().getKieBase());
    FactType userAge1 = getKbase().getFactType(packageName, "UserAge");
    FactType userAge2 = getKbase().getFactType(packageName, "UserAgeComplex");
    FactHandle h = getKSession().getEntryPoint("in_Age").insert(10.0);
    getKSession().fireAllRules();
    checkFirstDataFieldOfTypeStatus(userAge1, true, false, null, 6270.0);
    checkFirstDataFieldOfTypeStatus(userAge2, true, false, null, 44.1);
    System.out.println("_________________________________________________________________");
    FactType age = getKbase().getFactType(packageName, "Age");
    Object aged = getKSession().getObjects(new ClassObjectFilter(age.getFactClass())).iterator().next();
    getKSession().delete(getKSession().getFactHandle(aged));
    getKSession().fireAllRules();
    assertEquals(0, getKSession().getFactCount());
    getKSession().getEntryPoint("in_Age").insert(20.0);
    getKSession().fireAllRules();
    checkFirstDataFieldOfTypeStatus(userAge1, true, false, null, 9570.0);
    checkFirstDataFieldOfTypeStatus(userAge2, true, false, null, 115.2);
    getKSession().getEntryPoint("in_Age").insert(30.0);
    getKSession().fireAllRules();
    checkFirstDataFieldOfTypeStatus(userAge1, true, false, null, 12870.0);
    checkFirstDataFieldOfTypeStatus(userAge2, true, false, null, 306.3);
    assertEquals(4, getKSession().getFactCount());
    checkGeneratedRules();
}
Also used : ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) FactHandle(org.kie.api.runtime.rule.FactHandle) FactType(org.kie.api.definition.type.FactType) DroolsAbstractPMMLTest(org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest) Test(org.junit.Test)

Example 50 with ClassObjectFilter

use of org.kie.api.runtime.ClassObjectFilter in project drools by kiegroup.

the class FlowTest method testQueryInRuleWithDeclaration.

@Test
public void testQueryInRuleWithDeclaration() {
    Variable<Person> personV = declarationOf(Person.class);
    Query2Def<Person, Integer> qdef = query("olderThan", Person.class, Integer.class);
    Query query = qdef.build(expr("exprA", qdef.getArg1(), qdef.getArg2(), (p, a) -> p.getAge() > a));
    Rule rule = rule("R").build(expr("exprB", personV, p -> p.getName().startsWith("M")), qdef.call(personV, valueOf(40)), on(personV).execute((drools, p) -> drools.insert(new Result(p.getName()))));
    Model model = new ModelImpl().addQuery(query).addRule(rule);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession ksession = kieBase.newKieSession();
    ksession.insert(new Person("Mark", 39));
    ksession.insert(new Person("Mario", 41));
    ksession.insert(new Person("Edson", 41));
    ksession.fireAllRules();
    Collection<Result> results = (Collection<Result>) ksession.getObjects(new ClassObjectFilter(Result.class));
    assertEquals(1, results.size());
    assertEquals("Mario", results.iterator().next().getValue());
}
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) Query(org.drools.model.Query) Result(org.drools.modelcompiler.domain.Result) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Aggregations

ClassObjectFilter (org.kie.api.runtime.ClassObjectFilter)51 Test (org.junit.Test)44 KieSession (org.kie.api.runtime.KieSession)24 FactType (org.kie.api.definition.type.FactType)16 Verifier (org.drools.verifier.Verifier)12 VerifierBuilder (org.drools.verifier.builder.VerifierBuilder)12 Subsumption (org.drools.verifier.report.components.Subsumption)11 FactHandle (org.kie.api.runtime.rule.FactHandle)11 KieBase (org.kie.api.KieBase)10 ArrayList (java.util.ArrayList)9 Collection (java.util.Collection)9 DroolsAbstractPMMLTest (org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest)9 KieServices (org.kie.api.KieServices)9 Iterator (java.util.Iterator)7 DroolsAbstractPMMLTest (org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)7 Map (java.util.Map)5 KieBuilder (org.kie.api.builder.KieBuilder)5 KieFileSystem (org.kie.api.builder.KieFileSystem)5 KieContainer (org.kie.api.runtime.KieContainer)5 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)5