Search in sources :

Example 56 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.

the class KnowledgePackageMetaDataTest method testMetaData.

@Test
public void testMetaData() {
    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());
    KiePackage pack = kBase.getPackage("org.drools.compiler.test.definitions");
    assertNotNull(pack);
    assertEquals(2, pack.getFunctionNames().size());
    assertTrue(pack.getFunctionNames().contains("fun1"));
    assertTrue(pack.getFunctionNames().contains("fun2"));
    assertEquals(2, pack.getGlobalVariables().size());
    GlobalImpl g1 = new GlobalImpl("N", "java.lang.Integer");
    GlobalImpl g2 = new GlobalImpl("list", "java.util.List");
    assertTrue(pack.getGlobalVariables().contains(g1));
    assertTrue(pack.getGlobalVariables().contains(g2));
    assertEquals(2, pack.getFactTypes().size());
    FactType type;
    for (int j = 0; j < 2; j++) {
        type = pack.getFactTypes().iterator().next();
        if (type.getName().equals("org.drools.compiler.test.definitions.Person")) {
            assertEquals(2, type.getFields().size());
        } else if (type.getName().equals("org.drools.compiler.test.definitions.Foo")) {
            assertEquals("org.drools.compiler.test.definitions.Person", type.getSuperClass());
            FactField fld = type.getField("bar");
            assertEquals(2, fld.getIndex());
            assertEquals(String.class, fld.getType());
        } else {
            fail("Unexpected fact type " + type);
        }
    }
    assertEquals(2, pack.getQueries().size());
    for (Query q : pack.getQueries()) {
        assertTrue(q.getName().equals("qry1") || q.getName().equals("qry2"));
    }
    assertEquals(4, pack.getRules().size());
    assertTrue(pack.getRules().containsAll(pack.getQueries()));
}
Also used : FactField(org.kie.api.definition.type.FactField) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Query(org.kie.api.definition.rule.Query) KiePackage(org.kie.api.definition.KiePackage) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) GlobalImpl(org.drools.core.definitions.rule.impl.GlobalImpl) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 57 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.

the class TraitTest method traitsInBatchExecution.

// BZ #748752
@Test(timeout = 10000)
public void traitsInBatchExecution() {
    String str = "package org.jboss.qa.brms.traits\n" + "import org.drools.compiler.Person;\n" + "import org.drools.core.factmodel.traits.Traitable;\n" + "" + "global java.util.List list;" + "" + "declare Person \n" + "  @Traitable \n" + "end \n" + "" + "declare trait Student\n" + "  school : String\n" + "end\n" + "\n" + "rule \"create student\" \n" + "  when\n" + "    $student : Person( age < 26 )\n" + "  then\n" + "    Student s = don( $student, Student.class );\n" + "    s.setSchool(\"Masaryk University\");\n" + "end\n" + "\n" + "rule \"print student\"\n" + "  when\n" + "    student : Person( this isA Student )\n" + "  then" + "    list.add( 1 );\n" + "    System.out.println(\"Person is a student: \" + student);\n" + "end\n" + "\n" + "rule \"print school\"\n" + "  when\n" + "    Student( $school : school )\n" + "  then\n" + "    list.add( 2 );\n" + "    System.out.println(\"Student is attending \" + $school);\n" + "end";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        throw new RuntimeException(kbuilder.getErrors().toString());
    }
    List list = new ArrayList();
    KieBase kbase = kbuilder.newKieBase();
    TraitFactory.setMode(mode, kbase);
    StatelessKieSession ksession = kbase.newStatelessKieSession();
    ksession.setGlobal("list", list);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    Person student = new Person("student", 18);
    commands.add(CommandFactory.newInsert(student));
    System.out.println("Starting execution...");
    commands.add(CommandFactory.newFireAllRules());
    ksession.execute(CommandFactory.newBatchExecution(commands));
    System.out.println("Finished...");
    assertEquals(2, list.size());
    assertTrue(list.contains(1));
    assertTrue(list.contains(2));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Command(org.kie.api.command.Command) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) List(java.util.List) ArrayList(java.util.ArrayList) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 58 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.

the class TraitTest method testShedThing.

@Test(timeout = 10000)
public void testShedThing() {
    String s1 = "package test;\n" + "import org.drools.core.factmodel.traits.*;\n" + "global java.util.List list; \n" + "" + "declare trait A id : int end\n" + "declare trait B extends A end\n" + "declare trait C extends A end\n" + "declare trait D extends A end\n" + "declare trait E extends B end\n" + "" + "declare Core @Traitable id : int = 0 end \n" + "" + "rule \"Init\" when \n" + "then \n" + "   insert( new Core() );" + "end \n" + "" + "rule \"donManyThing\"\n" + "when\n" + "    $x : Core( id == 0 )\n" + "then\n" + "    don( $x, A.class );\n" + "    don( $x, B.class );\n" + "    don( $x, C.class );\n" + "    don( $x, D.class );\n" + "    don( $x, E.class );\n" + "end\n" + "\n" + "\n" + "" + "rule \"Mod\" \n" + "salience -10 \n" + "when \n" + "  $g : String( this == \"go\" ) \n" + "  $x : Core( id == 0 ) \n" + "then \n" + "  shed( $x, Thing.class ); " + "  delete( $g ); \n\n" + "end \n" + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    // not relevant
    TraitFactory.setMode(mode, kbase);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    List list = new ArrayList();
    KieSession ksession = kbase.newKieSession();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    ksession.insert("go");
    ksession.fireAllRules();
    for (Object o : ksession.getObjects()) {
        System.out.println(o);
    }
    assertEquals(1, ksession.getObjects().size());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 59 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.

the class PositionalTest method testPositional.

@Test
public void testPositional() {
    String drl = "import " + Man.class.getCanonicalName() + ";\n" + "\n" + "global java.util.List list;" + "\n" + "rule \"To be or not to be\"\n" + "when\n" + "    $m : Man( \"john\" , 18 , $w ; )\n" + "then\n" + "    list.add($w); " + "end";
    KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    knowledgeBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    System.out.println(knowledgeBuilder.getErrors().toString());
    assertFalse(knowledgeBuilder.hasErrors());
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    kBase.addPackages(knowledgeBuilder.getKnowledgePackages());
    KieSession kSession = createKnowledgeSession(kBase);
    java.util.ArrayList list = new ArrayList();
    kSession.setGlobal("list", list);
    kSession.insert(new Man("john", 18, 84.2));
    kSession.fireAllRules();
    assertTrue(list.contains(84.2));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 60 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.

the class DescrBuilderTest method testDumperFromPkg.

@Test
public void testDumperFromPkg() {
    // DROOLS-109
    PackageDescr pkg = DescrFactory.newPackage().name("org.test").newRule().name("org.test").lhs().and().or().pattern().id("$x", false).type("Integer").constraint("this > 10").end().pattern().id("$x", false).type("Integer").constraint("this < 20").end().end().pattern().type("Integer").constraint("this == $x").constraint("this == 42").end().end().end().rhs("").end().end().getDescr();
    String drl = new DrlDumper().dump(pkg);
    System.out.println(drl);
    KnowledgeBuilderImpl knowledgeBuilder = (KnowledgeBuilderImpl) KnowledgeBuilderFactory.newKnowledgeBuilder();
    knowledgeBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    System.err.println(knowledgeBuilder.getErrors());
    assertFalse(knowledgeBuilder.getErrors().toString(), knowledgeBuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(knowledgeBuilder.getKnowledgePackages());
    KieSession knowledgeSession = kbase.newKieSession();
    KiePackage rebuiltPkg = knowledgeBuilder.getPackage("org.test");
    org.kie.api.definition.rule.Rule rule = rebuiltPkg.getRules().iterator().next();
    RuleImpl r = ((RuleImpl) rule);
    assertEquals(2, r.getLhs().getChildren().size());
    Iterator<RuleConditionElement> iter = r.getLhs().getChildren().iterator();
    RuleConditionElement arg1 = iter.next();
    assertTrue(arg1 instanceof GroupElement && ((GroupElement) arg1).getType() == GroupElement.Type.OR);
    assertEquals(2, ((GroupElement) arg1).getChildren().size());
    RuleConditionElement arg2 = iter.next();
    assertTrue(arg2 instanceof Pattern);
}
Also used : Pattern(org.drools.core.rule.Pattern) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) RuleConditionElement(org.drools.core.rule.RuleConditionElement) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DrlDumper(org.drools.compiler.lang.DrlDumper) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)79 Test (org.junit.Test)67 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)67 KieSession (org.kie.api.runtime.KieSession)33 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)29 ArrayList (java.util.ArrayList)27 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)25 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)24 CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)20 List (java.util.List)19 FactType (org.kie.api.definition.type.FactType)16 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)5 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)5 BitSet (java.util.BitSet)4 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)4 PackageDescr (org.drools.drl.ast.descr.PackageDescr)4 NamedEntryPoint (org.drools.kiesession.entrypoints.NamedEntryPoint)4 KieBase (org.kie.api.KieBase)4 StringReader (java.io.StringReader)3 LinkedList (java.util.LinkedList)3