Search in sources :

Example 26 with FactType

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

the class ExtendsTest method testMultipleInheritanceWithPosition1.

@Test
public void testMultipleInheritanceWithPosition1() throws Exception {
    // DROOLS-249
    String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + " field2 : int = 102 //@position(0)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 //@position(0)\n" + " mfield1 : int = 201 @position(2)\n" + "end";
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kBuilder.hasErrors()) {
        System.err.println(kBuilder.getErrors());
    }
    assertFalse(kBuilder.hasErrors());
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
    knowledgeBase.addPackages(kBuilder.getKnowledgePackages());
    FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait");
    assertEquals(5, sw.getFields().size());
    assertEquals("field0", sw.getFields().get(0).getName());
    assertEquals("field1", sw.getFields().get(1).getName());
    assertEquals("mfield1", sw.getFields().get(2).getName());
    assertEquals("field2", sw.getFields().get(3).getName());
    assertEquals("mfield0", sw.getFields().get(4).getName());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 27 with FactType

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

the class ExtendsTest method testFieldInit.

@Test
public void testFieldInit() throws Exception {
    KieSession ksession = genSession("test_ExtFieldInit.drl");
    FactType test = ksession.getKieBase().getFactType("org.drools.compiler", "MyBean3");
    Object x = test.newInstance();
    assertNotNull(x);
    assertEquals(12, test.get(x, "fieldint"));
    assertEquals("xyz", test.get(x, "fieldstr"));
    assertEquals(3.23, test.get(x, "fielddbl"));
    assertEquals(0, test.get(x, "field0"));
    assertEquals(0.0f, test.get(x, "field1"));
    assertEquals(1.2f, test.get(x, "fieldflt"));
    short sht = 2;
    assertEquals(sht, test.get(x, "fieldsht"));
    assertEquals(0, test.get(x, "field2"));
    byte byt = 1;
    assertEquals(byt, test.get(x, "fieldbyt"));
    assertEquals(true, test.get(x, "fieldbln"));
    assertEquals('x', test.get(x, "fieldchr"));
    assertEquals(9999L, test.get(x, "fieldlng"));
    System.out.println(x);
}
Also used : KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 28 with FactType

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

the class ExtendsTest method testExtendOverride.

@Test
public void testExtendOverride() {
    String drl = "package test.beans;\n" + "\n" + "import java.util.List;\n" + "import java.util.ArrayList;\n" + "\n" + "global List ans;" + "\n" + "\n" + "declare ArrayList\n" + "end\n" + "" + "declare Bean extends ArrayList\n" + "  fld : int = 4 \n" + "  myField : String = \"xxx\" \n" + "end\n" + "\n" + "declare Bean2 extends Bean\n" + "  moref : double\n" + "  myField : String\n" + "end\n" + "rule \"Init\"\n" + "when\n" + "then\n" + "  Bean b = new Bean2();\n" + "  ans.add(b);" + "  System.out.println(b);\t\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = createKnowledgeSession(kbase);
    List out = new ArrayList();
    ksession.setGlobal("ans", out);
    ksession.fireAllRules();
    Object x = out.get(0);
    FactType type = kbase.getFactType("test.beans", "Bean2");
    assertEquals(4, type.get(x, "fld"));
    assertEquals("xxx", type.get(x, "myField"));
    assertEquals(0.0, type.get(x, "moref"));
    assertTrue(x instanceof ArrayList);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 29 with FactType

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

the class GeneratedBeansTest method testGeneratedBeansSerializable.

@Test
public void testGeneratedBeansSerializable() throws Exception {
    final KieBase kbase = loadKnowledgeBase("test_GeneratedBeansSerializable.drl");
    final FactType cheeseFact = kbase.getFactType("org.drools.generatedbeans", "Cheese");
    assertTrue("Generated beans must be serializable", Serializable.class.isAssignableFrom(cheeseFact.getFactClass()));
    final Object cheese = cheeseFact.newInstance();
    cheeseFact.set(cheese, "type", "stilton");
    final Object cheese2 = cheeseFact.newInstance();
    cheeseFact.set(cheese2, "type", "brie");
    final KieSession ksession = createKnowledgeSession(kbase);
    final List<Number> results = new ArrayList<>();
    ksession.setGlobal("results", results);
    ksession.insert(cheese);
    ksession.insert(cheese2);
    ksession.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(2, results.get(0).intValue());
}
Also used : Serializable(java.io.Serializable) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 30 with FactType

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

the class GeneratedBeansTest method testGeneratedBeans2.

@Test
public void testGeneratedBeans2() throws Exception {
    final KieBase kbase = loadKnowledgeBase("test_GeneratedBeans2.drl");
    final FactType cheeseFact = kbase.getFactType("org.drools.generatedbeans", "Cheese");
    final Object cheese = cheeseFact.newInstance();
    cheeseFact.set(cheese, "type", "stilton");
    assertEquals("stilton", cheeseFact.get(cheese, "type"));
    final Object cheese2 = cheeseFact.newInstance();
    cheeseFact.set(cheese2, "type", "stilton");
    assertEquals(cheese, cheese2);
    final FactType personType = kbase.getFactType("org.drools.generatedbeans", "Person");
    final Object ps = personType.newInstance();
    personType.set(ps, "name", "mark");
    personType.set(ps, "last", "proctor");
    personType.set(ps, "age", 42);
    final Object ps2 = personType.newInstance();
    personType.set(ps2, "name", "mark");
    personType.set(ps2, "last", "proctor");
    personType.set(ps2, "age", 30);
    assertEquals(ps, ps2);
    personType.set(ps2, "last", "little");
    assertFalse(ps.equals(ps2));
    final KieSession wm = createKnowledgeSession(kbase);
    final Object cg = cheeseFact.newInstance();
    wm.setGlobal("cg", cg);
    final List result = new ArrayList();
    wm.setGlobal("list", result);
    wm.insert(cheese);
    wm.fireAllRules();
    assertEquals(1, result.size());
    assertEquals(5, result.get(0));
    final FactType personFact = kbase.getFactType("org.drools.generatedbeans", "Person");
    final Object person = personFact.newInstance();
    personFact.getField("likes").set(person, cheese);
    personFact.getField("age").set(person, 7);
    wm.insert(person);
    wm.fireAllRules();
    assertEquals(2, result.size());
    assertEquals(person, result.get(1));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) FactType(org.kie.api.definition.type.FactType) 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