Search in sources :

Example 81 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class Misc2Test method testMergeMVELDialect.

@Test
public void testMergeMVELDialect() {
    // DROOLS-1751
    String drl1 = "package com.sample\n" + "import org.drools.compiler.*;\n" + "rule rule1 \n" + "    when\n" + "        (PersonHolder($addresses : person.addresses))\n" + "            &&\n" + "        (Address (street == \"AAA\") from $addresses)\n" + "    then\n" + "end";
    KnowledgeBuilder kbuilder1 = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder1.add(ResourceFactory.newByteArrayResource(drl1.getBytes()), ResourceType.DRL);
    Collection<KiePackage> knowledgePackages1 = kbuilder1.getKnowledgePackages();
    String drl2 = "package com.sample\n" + "import org.drools.compiler.*;\n" + "rule rule2 \n" + "    when\n" + "        PersonHolder()\n" + "    then\n" + "end";
    KnowledgeBuilder kbuilder2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder2.add(ResourceFactory.newByteArrayResource(drl2.getBytes()), ResourceType.DRL);
    Collection<KiePackage> knowledgePackages2 = kbuilder2.getKnowledgePackages();
    InternalKnowledgeBase kbase1 = KnowledgeBaseFactory.newKnowledgeBase();
    Collection<KiePackage> combinedPackages1 = new ArrayList<KiePackage>();
    combinedPackages1.addAll(knowledgePackages1);
    combinedPackages1.addAll(knowledgePackages2);
    // Add once to make inUse=true
    kbase1.addPackages(combinedPackages1);
    InternalKnowledgeBase kbase2 = KnowledgeBaseFactory.newKnowledgeBase();
    Collection<KiePackage> combinedPackages2 = new ArrayList<KiePackage>();
    combinedPackages2.addAll(knowledgePackages1);
    combinedPackages2.addAll(knowledgePackages2);
    // this will cause package deepClone
    kbase2.addPackages(combinedPackages2);
    KieSession ksession = kbase2.newKieSession();
    PersonHolder personHolder = new PersonHolder();
    Person person = new Person("John");
    Address address = new Address("AAA", "BBB", "111");
    person.addAddress(address);
    personHolder.setPerson(person);
    ksession.insert(personHolder);
    int fired = ksession.fireAllRules();
    assertEquals(2, fired);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Address(org.drools.compiler.Address) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) PersonHolder(org.drools.compiler.PersonHolder) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 82 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class Misc2Test method testUnsupportedPolymorphicDeclaration.

@Test
public void testUnsupportedPolymorphicDeclaration() {
    // DROOLS-162
    String drl = "package org.drools.test; \n" + "" + "declare Foo end \n" + "declare Bar end \n" + "" + "" + "rule X when\n" + " $x : Foo() " + " or " + " $x : Bar() \n" + "then\n" + " System.out.println( $x ); \n" + "end\n" + "" + "rule Init\n" + "when\n" + "then\n" + " insert( new Foo() ); \n" + " insert( new Bar() ); \n" + "end";
    KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kb.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    assertTrue(kb.hasErrors());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) Test(org.junit.Test)

Example 83 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class Misc2Test method testPackageVisibility.

@Test
public void testPackageVisibility() {
    // DROOLS-61
    String str = "package org.drools.integrationtests;\n" + "rule \"getX\"\n" + "when\n" + "    $x: PackageProtected( )\n" + "then\n" + "    System.out.println( $x );\n" + "end";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertTrue(kbuilder.hasErrors());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Test(org.junit.Test)

Example 84 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class Misc2Test method testReportErrorOnWrongDateEffective.

@Test
public void testReportErrorOnWrongDateEffective() {
    // BZ-1013545
    String drl = // ensure no Locale can parse the Date
    "rule X date-effective \"9-asbrdfh-1974\" when\n" + "    $s : String() " + "then\n" + "end\n";
    KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kb.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    assertTrue(kb.hasErrors());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) Test(org.junit.Test)

Example 85 with KnowledgeBuilder

use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.

the class Misc2Test method testDeclarationsScopeUsingOR4.

@Test
public void testDeclarationsScopeUsingOR4() {
    // DROOLS-44
    String str = "declare A\n" + "    a1 : String\n" + "end\n" + "\n" + "declare B\n" + "    b1 : String\n" + "end\n" + "\n" + "rule R when \n" + "    A ( $a1 : a1 != null )\n" + "    (or\n" + "        (and\n" + "            B( $b1 : b1 != null )\n" + "            eval( $a1.compareTo( $b1 ) < 0 )\n" + "        )\n" + "        (and\n" + "            B( b1 == null )\n" + "            eval( $a1.compareTo( $b1 ) < 0 )\n" + "        )\n" + "    )\n" + "then\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertTrue(kbuilder.hasErrors());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Test(org.junit.Test)

Aggregations

KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)376 Test (org.junit.Test)302 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)231 KieSession (org.kie.api.runtime.KieSession)149 ArrayList (java.util.ArrayList)112 List (java.util.List)65 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)52 KiePackage (org.kie.api.definition.KiePackage)39 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)38 FactHandle (org.kie.api.runtime.rule.FactHandle)36 FactType (org.kie.api.definition.type.FactType)27 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)24 Person (org.drools.compiler.Person)23 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)23 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)22 InternalFactHandle (org.drools.core.common.InternalFactHandle)19 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)17 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)17 StringReader (java.io.StringReader)15 Resource (org.kie.api.io.Resource)15