Search in sources :

Example 46 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class TraitTest method testWrapperSize.

@Test(timeout = 10000)
public void testWrapperSize() {
    String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    Resource res = ResourceFactory.newClassPathResource(source);
    assertNotNull(res);
    kbuilder.add(res, ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase();
    kb.addPackages(kbuilder.getKnowledgePackages());
    TraitFactory.setMode(mode, kb);
    TraitFactory tFactory = ((KnowledgeBaseImpl) kb).getConfiguration().getComponentFactory().getTraitFactory();
    try {
        FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
        TraitableBean imp = (TraitableBean) impClass.newInstance();
        FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
        Class trait = traitClass.getFactClass();
        TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
        Map<String, Object> virtualFields = imp._getDynamicProperties();
        Map<String, Object> wrapper = proxy.getFields();
        assertEquals(3, wrapper.size());
        assertEquals(1, virtualFields.size());
        impClass.set(imp, "name", "john");
        assertEquals(3, wrapper.size());
        assertEquals(1, virtualFields.size());
        proxy.getFields().put("school", "skol");
        assertEquals(3, wrapper.size());
        assertEquals(1, virtualFields.size());
        proxy.getFields().put("surname", "xxx");
        assertEquals(4, wrapper.size());
        assertEquals(2, virtualFields.size());
        // FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity");
        // TraitableBean ind = (TraitableBean) indClass.newInstance();
        TraitableBean ind = new Entity();
        TraitProxy proxy2 = (TraitProxy) tFactory.getProxy(ind, trait);
        Map virtualFields2 = ind._getDynamicProperties();
        Map wrapper2 = proxy2.getFields();
        assertEquals(3, wrapper2.size());
        assertEquals(3, virtualFields2.size());
        traitClass.set(proxy2, "name", "john");
        assertEquals(3, wrapper2.size());
        assertEquals(3, virtualFields2.size());
        proxy2.getFields().put("school", "skol");
        assertEquals(3, wrapper2.size());
        assertEquals(3, virtualFields2.size());
        proxy2.getFields().put("surname", "xxx");
        assertEquals(4, wrapper2.size());
        assertEquals(4, virtualFields2.size());
        FactType traitClass2 = kb.getFactType("org.drools.compiler.trait.test", "Role");
        Class trait2 = traitClass2.getFactClass();
        // TraitableBean ind2 = (TraitableBean) indClass.newInstance();
        TraitableBean ind2 = new Entity();
        TraitProxy proxy99 = (TraitProxy) tFactory.getProxy(ind2, trait2);
        proxy99.getFields().put("surname", "xxx");
        proxy99.getFields().put("name", "xyz");
        proxy99.getFields().put("school", "skol");
        assertEquals(3, proxy99.getFields().size());
        TraitProxy proxy100 = (TraitProxy) tFactory.getProxy(ind2, trait);
        assertEquals(4, proxy100.getFields().size());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) Entity(org.drools.core.factmodel.traits.Entity) Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) LogicalTypeInconsistencyException(org.drools.core.factmodel.traits.LogicalTypeInconsistencyException) TraitFactory(org.drools.core.factmodel.traits.TraitFactory) FactType(org.kie.api.definition.type.FactType) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Map(java.util.Map) HashMap(java.util.HashMap) TraitTypeMap(org.drools.core.factmodel.traits.TraitTypeMap) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 47 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class TypeDeclarationTest method testMultipleAnnotationDeclarations.

@Test
public void testMultipleAnnotationDeclarations() {
    String str1 = "";
    str1 += "package org.kie1 " + "" + "declare Foo \n" + "    name : String " + "    age : int " + "end ";
    String str2 = "";
    str2 += "package org.kie2 " + "" + "declare org.kie1.Foo " + "    @role(event) " + "end ";
    String str3 = "";
    str3 += "package org.kie3 " + "" + "declare org.kie1.Foo " + "    @propertyReactive " + "end ";
    String str4 = "" + "package org.kie4; " + "import org.kie1.Foo; " + "" + "rule Check " + "when " + " $f : Foo( name == 'bar' ) " + "then " + " modify( $f ) { setAge( 99 ); } " + "end ";
    KieHelper helper = new KieHelper();
    helper.addContent(str1, ResourceType.DRL);
    helper.addContent(str2, ResourceType.DRL);
    helper.addContent(str3, ResourceType.DRL);
    helper.addContent(str4, ResourceType.DRL);
    List<Message> msg = helper.verify().getMessages(Message.Level.ERROR);
    System.out.println(msg);
    assertEquals(0, msg.size());
    KieBase kieBase = helper.build();
    FactType type = kieBase.getFactType("org.kie1", "Foo");
    assertEquals(2, type.getFields().size());
    Object foo = null;
    try {
        foo = type.newInstance();
        type.set(foo, "name", "bar");
        assertEquals("bar", type.get(foo, "name"));
    } catch (InstantiationException e) {
        fail(e.getMessage());
    } catch (IllegalAccessException e) {
        fail(e.getMessage());
    }
    KieSession session = kieBase.newKieSession();
    FactHandle handle = session.insert(foo);
    int n = session.fireAllRules(5);
    assertTrue(handle instanceof EventFactHandle);
    assertEquals(1, n);
    assertEquals(99, type.get(foo, "age"));
}
Also used : Message(org.kie.api.builder.Message) FactHandle(org.kie.api.runtime.rule.FactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) KieHelper(org.kie.internal.utils.KieHelper) FactType(org.kie.api.definition.type.FactType) KieBase(org.kie.api.KieBase) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 48 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class TypeDeclarationTest method testPreventReflectionAPIsOnJavaClasses.

@Test(expected = UnsupportedOperationException.class)
public void testPreventReflectionAPIsOnJavaClasses() {
    String drl = "package org.test; " + // existing java class
    "declare org.drools.compiler.Person " + "  @role(event) " + "end \n" + "";
    KieBuilder kieBuilder = build(drl);
    assertFalse(kieBuilder.getResults().hasMessages(Message.Level.ERROR));
    KieBase kieBase = KieServices.Factory.get().newKieContainer(kieBuilder.getKieModule().getReleaseId()).getKieBase();
    FactType type = kieBase.getFactType("org.drools.compiler", "Person");
}
Also used : KieBase(org.kie.api.KieBase) KieBuilder(org.kie.api.builder.KieBuilder) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 49 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class AdapterTest method testCustomInputAdapter.

@Test
public void testCustomInputAdapter() {
    String source = PMML4Helper.pmmlDefaultPackageName().replace(".", "/") + "/" + "mock_cold_adapter.xml";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write(ResourceFactory.newClassPathResource(source).setResourceType(ResourceType.PMML));
    Results res = ks.newKieBuilder(kfs).buildAll().getResults();
    if (res.hasMessages(Message.Level.ERROR)) {
        System.out.println(res.getMessages(Message.Level.ERROR));
    }
    assertEquals(0, res.getMessages(Message.Level.ERROR).size());
    KieBase kieBase = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).getKieBase();
    FactType ft = kieBase.getFactType("test", "MyAdapter");
    assertTrue(ft != null);
    assertTrue(ft.getFactClass().isInterface());
    FactType fto = kieBase.getFactType("test", "MyOutAdapter");
    assertTrue(fto != null);
    assertTrue(fto.getFactClass().isInterface());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Results(org.kie.api.builder.Results) KieBase(org.kie.api.KieBase) KieServices(org.kie.api.KieServices) FactType(org.kie.api.definition.type.FactType) DroolsAbstractPMMLTest(org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest) Test(org.junit.Test)

Example 50 with FactType

use of org.kie.api.definition.type.FactType in project drools by kiegroup.

the class ConstrainedDataDictionaryTest method testContinuousDomainIntervals.

@Test
public void testContinuousDomainIntervals() throws Exception {
    FactType ivals = getKbase().getFactType(packageName, "Intervalled");
    assertNotNull(ivals);
    Object data1 = ivals.newInstance();
    ivals.set(data1, "value", -0.4);
    Object data2 = ivals.newInstance();
    ivals.set(data2, "value", 0.3);
    Object data3 = ivals.newInstance();
    ivals.set(data3, "value", 1.6);
    Object data4 = ivals.newInstance();
    ivals.set(data4, "value", 2.0);
    Object data5 = ivals.newInstance();
    ivals.set(data5, "value", 3.0);
    Object data6 = ivals.newInstance();
    ivals.set(data6, "value", 8.2);
    Object data7 = ivals.newInstance();
    ivals.set(data7, "value", 12.4);
    Object data8 = ivals.newInstance();
    ivals.set(data8, "value", 999.9);
    getKSession().insert(data1);
    getKSession().insert(data2);
    getKSession().insert(data3);
    getKSession().insert(data4);
    getKSession().insert(data5);
    getKSession().insert(data6);
    getKSession().insert(data7);
    getKSession().insert(data8);
    getKSession().fireAllRules();
    assertEquals(false, ivals.get(data1, "valid"));
    assertEquals(true, ivals.get(data2, "valid"));
    assertEquals(false, ivals.get(data3, "valid"));
    assertEquals(true, ivals.get(data4, "valid"));
    assertEquals(false, ivals.get(data5, "valid"));
    assertEquals(false, ivals.get(data6, "valid"));
    assertEquals(true, ivals.get(data7, "valid"));
    assertEquals(false, ivals.get(data8, "valid"));
    checkGeneratedRules();
}
Also used : FactType(org.kie.api.definition.type.FactType) DroolsAbstractPMMLTest(org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest) Test(org.junit.Test)

Aggregations

FactType (org.kie.api.definition.type.FactType)208 Test (org.junit.Test)193 KieSession (org.kie.api.runtime.KieSession)114 KieBase (org.kie.api.KieBase)63 DroolsAbstractPMMLTest (org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest)45 DroolsAbstractPMMLTest (org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)31 ArrayList (java.util.ArrayList)28 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)27 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)27 KieServices (org.kie.api.KieServices)22 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)18 Resource (org.kie.api.io.Resource)17 KieBuilder (org.kie.api.builder.KieBuilder)16 ClassObjectFilter (org.kie.api.runtime.ClassObjectFilter)16 KieFileSystem (org.kie.api.builder.KieFileSystem)15 KieContainer (org.kie.api.runtime.KieContainer)15 List (java.util.List)10 Results (org.kie.api.builder.Results)10 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)9 LogicalTypeInconsistencyException (org.drools.core.factmodel.traits.LogicalTypeInconsistencyException)9