Search in sources :

Example 1 with FactType

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

the class DeclaredTypesTest method testFactType.

@Test
public void testFactType() throws Exception {
    String str = "package org.test;\n" + "import " + Person.class.getCanonicalName() + ";" + "declare Name\n" + "    value : String\n" + "end\n" + "rule R when\n" + "    Name($v : value == \"Mario\")\n" + "then\n" + "    insert($v);" + "end";
    KieSession ksession = getKieSession(str);
    FactType nameType = ksession.getKieBase().getFactType("org.test", "Name");
    Object name = nameType.newInstance();
    nameType.set(name, "value", "Mario");
    ksession.insert(name);
    ksession.fireAllRules();
    assertEquals("Mario", nameType.get(name, "value"));
    Collection<String> results = getObjectsIntoList(ksession, String.class);
    assertEquals(1, results.size());
    assertEquals("Mario", results.iterator().next());
}
Also used : KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 2 with FactType

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

the class KnowledgeBaseImpl method getFactType.

public FactType getFactType(String packageName, String typeName) {
    String name = packageName + "." + typeName;
    readLock();
    try {
        for (InternalKnowledgePackage pkg : this.pkgs.values()) {
            FactType type = pkg.getFactType(name);
            if (type != null) {
                return type;
            }
        }
        return null;
    } finally {
        readUnlock();
    }
}
Also used : InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) FactType(org.kie.api.definition.type.FactType)

Example 3 with FactType

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

the class TypeDeclarationTest method testCircularDeclaration.

@Test
public void testCircularDeclaration() throws Exception {
    String rule = "package org.drools.compiler.test\n" + "declare FactA\n" + "    fieldB: FactB\n" + "end\n" + "declare FactB\n" + "    fieldA: FactA\n" + "end\n" + "rule R1 when\n" + "   $fieldA : FactA( $fieldB : fieldB )\n" + "   FactB( this == $fieldB, fieldA == $fieldA )\n" + "then\n" + "end";
    KieBase kbase = new KieHelper().addContent(rule, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();
    FactType aType = kbase.getFactType("org.drools.compiler.test", "FactA");
    Object a = aType.newInstance();
    FactType bType = kbase.getFactType("org.drools.compiler.test", "FactB");
    Object b = bType.newInstance();
    aType.set(a, "fieldB", b);
    bType.set(b, "fieldA", a);
    ksession.insert(a);
    ksession.insert(b);
    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 4 with FactType

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

the class TypeDeclarationTest method testCircularDeclarationWithExtension.

@Test
public void testCircularDeclarationWithExtension() throws Exception {
    // DROOLS-640
    String drl = "package org.drools.compiler.test\n" + "declare FactA\n" + "    fieldB: FactB\n" + "end\n" + "declare FactB extends FactA end\n" + "rule R1 when\n" + "   $a : FactA( )\n" + "   $b : FactB( this == $a.fieldB )\n" + "then\n" + "end";
    KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();
    FactType aType = kbase.getFactType("org.drools.compiler.test", "FactA");
    FactType bType = kbase.getFactType("org.drools.compiler.test", "FactB");
    Object a = aType.newInstance();
    Object b = bType.newInstance();
    aType.set(a, "fieldB", b);
    ksession.insert(a);
    ksession.insert(b);
    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 5 with FactType

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

the class TypeDeclarationTest method testRecursiveDeclaration.

@Test
public void testRecursiveDeclaration() throws Exception {
    String rule = "package org.drools.compiler\n" + "declare Node\n" + "    value: String\n" + "    parent: Node\n" + "end\n" + "rule R1 when\n" + "   $parent: Node( value == \"parent\" )\n" + "   $child: Node( $value : value, parent == $parent )\n" + "then\n" + "   System.out.println( $value );\n" + "end";
    KieBase kbase = new KieHelper().addContent(rule, ResourceType.DRL).build();
    KieSession ksession = kbase.newKieSession();
    FactType nodeType = kbase.getFactType("org.drools.compiler", "Node");
    Object parent = nodeType.newInstance();
    nodeType.set(parent, "value", "parent");
    ksession.insert(parent);
    Object child = nodeType.newInstance();
    nodeType.set(child, "value", "child");
    nodeType.set(child, "parent", parent);
    ksession.insert(child);
    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) 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