Search in sources :

Example 31 with KnowledgeBuilder

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

the class QueryTest2 method testEvalRewrite.

@Test
public void testEvalRewrite() throws Exception {
    String str = "" + "package org.drools.compiler;\n" + "global java.util.List results;\n" + "rule \"eval rewrite\"\n" + "    when\n" + "        $o1 : OrderItem( order.number == 11, $seq : seq == 1 )\n" + // "        $o2 : OrderItem( order.number == $o1.order.number, seq != $seq )\n" +
    "        $o2 : Order( items[(Integer) 1] == $o1 ) \n" + "    then\n" + "        System.out.println( $o1 + \":\" + $o2 );\n" + "end        \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    final Order order1 = new Order(11, "Bob");
    final OrderItem item11 = new OrderItem(order1, 1);
    final OrderItem item12 = new OrderItem(order1, 2);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = createKnowledgeSession(kbase);
    ksession.insert(order1);
    ksession.insert(item11);
    ksession.insert(item12);
    ksession.fireAllRules();
}
Also used : Order(org.drools.compiler.Order) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) OrderItem(org.drools.compiler.OrderItem) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 32 with KnowledgeBuilder

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

the class RuleExtensionTest method testRuleExtendsWithCompositeKBuilder.

@Test
public void testRuleExtendsWithCompositeKBuilder() {
    // DROOLS-100
    String str = "package org.drools.test;\n" + "\n" + "global java.util.List list;\n" + "\n" + "rule \"Base\"\n" + "when\n" + "  String( this == \"go\" )\n" + "then\n" + "end\n" + "";
    String str2 = "package org.drools.test;\n" + "global java.util.List list;\n" + "\n" + "rule \"ExtYes\" extends \"Base\"\n" + "when\n" + "then\n" + "  list.add( 1 );\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    CompositeKnowledgeBuilder ckb = kbuilder.batch();
    ckb.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL).add(ResourceFactory.newByteArrayResource(str2.getBytes()), ResourceType.DRL).build();
    assertFalse(kbuilder.hasErrors());
    InternalKnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase();
    kb.addPackages(kbuilder.getKnowledgePackages());
    KieSession knowledgeSession = kb.newKieSession();
    List list = new ArrayList();
    knowledgeSession.setGlobal("list", list);
    knowledgeSession.fireAllRules();
    assertEquals(0, list.size());
    knowledgeSession.insert("go");
    knowledgeSession.fireAllRules();
    assertEquals(1, list.size());
}
Also used : CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 33 with KnowledgeBuilder

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

the class AbstractKieModule method createKieBase.

public InternalKnowledgeBase createKieBase(KieBaseModelImpl kBaseModel, KieProject kieProject, ResultsImpl messages, KieBaseConfiguration conf) {
    Collection<KiePackage> pkgs = getKnowledgePackagesForKieBase(kBaseModel.getName());
    if (pkgs == null) {
        KnowledgeBuilder kbuilder = kieProject.buildKnowledgePackages(kBaseModel, messages);
        if (kbuilder.hasErrors()) {
            // Messages already populated by the buildKnowlegePackages
            return null;
        }
    }
    // if we get to here, then we know the pkgs is now cached
    pkgs = getKnowledgePackagesForKieBase(kBaseModel.getName());
    if (kBaseModel.getEventProcessingMode() == EventProcessingOption.CLOUD && (conf == null || conf.getOption(EventProcessingOption.class) == EventProcessingOption.CLOUD)) {
        for (KiePackage kpkg : pkgs) {
            if (((KnowledgePackageImpl) kpkg).needsStreamMode()) {
                throw new RuntimeException("The requested KieBase \"" + kBaseModel.getName() + "\" has been set to run in CLOUD mode but requires features only available in STREAM mode");
            }
        }
    }
    ClassLoader cl = kieProject.getClassLoader();
    if (conf == null) {
        conf = getKnowledgeBaseConfiguration(kBaseModel, cl);
    } else if (conf instanceof RuleBaseConfiguration) {
        ((RuleBaseConfiguration) conf).setClassLoader(cl);
    }
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kBaseModel.getName(), conf);
    kBase.addPackages(pkgs);
    return kBase;
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 34 with KnowledgeBuilder

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

the class AbstractKieProject method buildKnowledgePackages.

public KnowledgeBuilder buildKnowledgePackages(KieBaseModelImpl kBaseModel, ResultsImpl messages) {
    InternalKieModule kModule = getKieModuleForKBase(kBaseModel.getName());
    KnowledgeBuilder kbuilder = createKnowledgeBuilder(kBaseModel, kModule);
    CompositeKnowledgeBuilder ckbuilder = kbuilder.batch();
    Set<Asset> assets = new HashSet<>();
    boolean allIncludesAreValid = true;
    for (String include : getTransitiveIncludes(kBaseModel)) {
        if (StringUtils.isEmpty(include)) {
            continue;
        }
        InternalKieModule includeModule = getKieModuleForKBase(include);
        if (includeModule == null) {
            String text = "Unable to build KieBase, could not find include: " + include;
            log.error(text);
            messages.addMessage(Message.Level.ERROR, KieModuleModelImpl.KMODULE_SRC_PATH, text).setKieBaseName(kBaseModel.getName());
            allIncludesAreValid = false;
            continue;
        }
        addFiles(assets, getKieBaseModel(include), includeModule);
    }
    if (!allIncludesAreValid) {
        return null;
    }
    addFiles(assets, kBaseModel, kModule);
    if (assets.isEmpty()) {
        if (kModule instanceof FileKieModule) {
            log.warn("No files found for KieBase " + kBaseModel.getName() + ", searching folder " + kModule.getFile());
        } else {
            log.warn("No files found for KieBase " + kBaseModel.getName());
        }
    } else {
        for (Asset asset : assets) {
            asset.kmodule.addResourceToCompiler(ckbuilder, kBaseModel, asset.name);
        }
    }
    ckbuilder.build();
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            messages.addMessage(error).setKieBaseName(kBaseModel.getName());
        }
        log.error("Unable to build KieBaseModel:" + kBaseModel.getName() + "\n" + kbuilder.getErrors().toString());
    }
    if (kbuilder.hasResults(ResultSeverity.WARNING)) {
        for (KnowledgeBuilderResult warn : kbuilder.getResults(ResultSeverity.WARNING)) {
            messages.addMessage(warn).setKieBaseName(kBaseModel.getName());
        }
        log.warn("Warning : " + kBaseModel.getName() + "\n" + kbuilder.getResults(ResultSeverity.WARNING).toString());
    }
    // cache KnowledgeBuilder and results
    kModule.cacheKnowledgeBuilderForKieBase(kBaseModel.getName(), kbuilder);
    kModule.cacheResultsForKieBase(kBaseModel.getName(), messages);
    return kbuilder;
}
Also used : CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) HashSet(java.util.HashSet) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult)

Example 35 with KnowledgeBuilder

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

the class KieBaseUpdater method run.

@Override
public void run() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(ctx.kBase, ctx.newKM.getBuilderConfiguration(ctx.newKieBaseModel));
    KnowledgeBuilderImpl pkgbuilder = (KnowledgeBuilderImpl) kbuilder;
    CompositeKnowledgeBuilder ckbuilder = kbuilder.batch();
    boolean shouldRebuild = applyResourceChanges(pkgbuilder, ckbuilder);
    // remove resources first
    for (ResourceChangeSet rcs : ctx.cs.getChanges().values()) {
        if (rcs.getChangeType() == ChangeType.REMOVED) {
            String resourceName = rcs.getResourceName();
            if (!resourceName.endsWith(".properties") && isFileInKBase(ctx.currentKM, ctx.currentKieBaseModel, resourceName)) {
                pkgbuilder.removeObjectsGeneratedFromResource(ctx.currentKM.getResource(resourceName));
            }
        }
    }
    // remove all ObjectTypeNodes for the modified classes
    if (ctx.modifyingUsedClass) {
        for (Class<?> cls : ctx.modifiedClasses) {
            clearInstancesOfModifiedClass(cls);
        }
    }
    if (shouldRebuild) {
        // readd unchanged dsl files to the kbuilder
        for (String dslFile : ctx.unchangedResources) {
            if (isFileInKBase(ctx.newKM, ctx.newKieBaseModel, dslFile)) {
                ctx.newKM.addResourceToCompiler(ckbuilder, ctx.newKieBaseModel, dslFile);
            }
        }
        rebuildAll(pkgbuilder, ckbuilder);
    }
    ctx.kBase.setResolvedReleaseId(ctx.newReleaseId);
    for (InternalWorkingMemory wm : ctx.kBase.getWorkingMemories()) {
        wm.notifyWaitOnRest();
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet)

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