Search in sources :

Example 21 with FactType

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

the class KieContainerTest method insertMessageFromTypeDeclaration.

private void insertMessageFromTypeDeclaration(KieSession ksession) throws InstantiationException, IllegalAccessException {
    FactType messageType = ksession.getKieBase().getFactType("org.drools.test", "Message");
    Object message = messageType.newInstance();
    messageType.set(message, "message", "Hello World");
    ksession.insert(message);
}
Also used : FactType(org.kie.api.definition.type.FactType)

Example 22 with FactType

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

the class ExtendsTest method testExpressionFieldInit.

@Test
public void testExpressionFieldInit() throws Exception {
    KieSession ksession = genSession("test_ExtFieldInit.drl");
    FactType test = ksession.getKieBase().getFactType("org.drools.compiler", "MyBoxExpressionBean");
    Object x = test.newInstance();
    assertNotNull(x);
    assertEquals("foobar", test.get(x, "f0"));
    assertEquals(-32, test.get(x, "fieldint"));
    assertEquals(4.0, test.get(x, "fielddbl"));
    assertEquals(0.5f, test.get(x, "fieldflt"));
    short sht = 6;
    assertEquals(sht, test.get(x, "fieldsht"));
    byte byt = 2;
    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);
    FactType test2 = ksession.getKieBase().getFactType("org.drools.compiler", "MySimpleExpressionBean");
    x = test2.newInstance();
    assertNotNull(x);
    assertEquals("foobar", test2.get(x, "f0"));
    assertEquals(-32, test2.get(x, "fieldint"));
    assertEquals(4.0, test2.get(x, "fielddbl"));
    assertEquals(0.5f, test2.get(x, "fieldflt"));
    sht = 6;
    assertEquals(sht, test2.get(x, "fieldsht"));
    byt = 2;
    assertEquals(byt, test2.get(x, "fieldbyt"));
    assertEquals(true, test2.get(x, "fieldbln"));
    assertEquals('x', test2.get(x, "fieldchr"));
    assertEquals(9999L, test2.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 23 with FactType

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

the class ExtendsTest method testMultipleInheritanceWithPosition6.

@Test
public void testMultipleInheritanceWithPosition6() 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 SecPosTrait\n" + "@propertyReactive\n" + " field3 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait, SecPosTrait\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(6, sw.getFields().size());
    assertEquals("field0", sw.getFields().get(0).getName());
    assertEquals("field1", sw.getFields().get(1).getName());
    assertEquals("field2", sw.getFields().get(2).getName());
    assertEquals("field3", sw.getFields().get(3).getName());
    assertEquals("mfield0", sw.getFields().get(4).getName());
    assertEquals("mfield1", sw.getFields().get(5).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 24 with FactType

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

the class ExtendsTest method testExtendsDump.

@Test
public void testExtendsDump() {
    PackageDescrBuilder pkgd = DescrFactory.newPackage();
    pkgd.name("org.test").newDeclare().type().name("Foo").newField("id").type("int").end().end().newDeclare().type().name("Bar").superType("Foo").newField("val").type("int").initialValue("42").end().end();
    String drl = new DrlDumper().dump(pkgd.getDescr());
    KieBase kb = loadKnowledgeBaseFromString(drl);
    FactType bar = kb.getFactType("org.test", "Bar");
    try {
        Object x = bar.newInstance();
        assertEquals(42, bar.get(x, "val"));
        bar.set(x, "id", 1);
        assertEquals(1, bar.get(x, "id"));
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : PackageDescrBuilder(org.drools.compiler.lang.api.PackageDescrBuilder) KieBase(org.kie.api.KieBase) DrlDumper(org.drools.compiler.lang.DrlDumper) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 25 with FactType

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

the class ExtendsTest method testMultipleInheritanceWithPosition2.

@Test
public void testMultipleInheritanceWithPosition2() 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("mfield0", sw.getFields().get(0).getName());
    assertEquals("field0", sw.getFields().get(1).getName());
    assertEquals("mfield1", sw.getFields().get(2).getName());
    assertEquals("field1", sw.getFields().get(3).getName());
    assertEquals("field2", 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)

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