Search in sources :

Example 21 with KnowledgeBuilder

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

the class StrEvaluatorTest method readKnowledgeBase.

private KieBase readKnowledgeBase() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("strevaluator_test.drl")), ResourceType.DRL);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors.size() > 0) {
        for (KnowledgeBuilderError error : errors) {
            System.err.println(error);
        }
        throw new IllegalArgumentException("Could not parse knowledge." + errors.toArray());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    return kbase;
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 22 with KnowledgeBuilder

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

the class SwitchOverStringTest method testShouldFailToCompileSwitchOverStringWithLngLevel16.

@Test
public void testShouldFailToCompileSwitchOverStringWithLngLevel16() {
    System.setProperty("drools.dialect.java.compiler.lnglevel", "1.6");
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(FUNCTION_WITH_SWITCH_OVER_STRING.getBytes()), ResourceType.DRL);
    Assert.assertTrue("Compilation error(s) expected!", kbuilder.hasErrors());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Test(org.junit.Test)

Example 23 with KnowledgeBuilder

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

the class TimerAndCalendarTest method wrongTimerExpression.

private void wrongTimerExpression(String timer) {
    String str = "";
    str += "package org.simple \n";
    str += "rule xxx \n";
    str += "  timer (" + timer + ") ";
    str += "when \n";
    str += "then \n";
    str += "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)

Example 24 with KnowledgeBuilder

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

the class StatelessSessionTest method getSession2.

private StatelessKieSession getSession2(final Resource resource) throws Exception {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(resource, ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        System.out.println(kbuilder.getErrors());
    }
    assertFalse(kbuilder.hasErrors());
    final Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(pkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    final StatelessKieSession session = kbase.newStatelessKieSession();
    session.setGlobal("list", this.list);
    session.setGlobal("cheesery", this.cheesery);
    return session;
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 25 with KnowledgeBuilder

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

the class PatternTest method testCheckDuplicateVariables.

@Test
public void testCheckDuplicateVariables() throws Exception {
    // JBRULES-3035
    String str = "package com.sample\n" + "import org.drools.compiler.*\n" + "rule R1 when\n" + "   Person( $a: age, $a: name ) // this should cause a compile-time error\n" + "then\n" + "end";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertTrue(kbuilder.hasErrors());
    str = "package com.sample\n" + "rule R1 when\n" + "   accumulate( Object(), $c: count(1), $c: max(1) ) // this should cause a compile-time error\n" + "then\n" + "end";
    kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    assertTrue(kbuilder.hasErrors());
    str = "package com.sample\n" + "rule R1 when\n" + "   Number($i: intValue) from accumulate( Object(), $i: count(1) ) // this should cause a compile-time error\n" + "then\n" + "end";
    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