Search in sources :

Example 1 with RuleBaseConfiguration

use of org.drools.core.RuleBaseConfiguration in project opennms by OpenNMS.

the class DroolsNorthbounder method initializeDroolsEngine.

/**
 * Initialize drools engine.
 *
 * @throws Exception the exception
 */
private void initializeDroolsEngine() throws Exception {
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kFileSystem = ks.newKieFileSystem();
    for (String ruleFile : m_engine.getRuleFiles()) {
        LOG.debug("Loading rules file: {}", ruleFile);
        kFileSystem.write("src/main/resources/" + ruleFile, ks.getResources().newFileSystemResource(new File(ruleFile)));
    }
    KieBuilder kbuilder = ks.newKieBuilder(kFileSystem);
    kbuilder.buildAll();
    if (kbuilder.getResults().hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
        LOG.warn("Unable to initialize Drools engine: {}", kbuilder.getResults().getMessages(Level.ERROR));
        throw new IllegalStateException("Unable to initialize Drools engine: " + kbuilder.getResults().getMessages(Level.ERROR));
    }
    KieContainer kContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    AssertBehaviour behaviour = AssertBehaviour.determineAssertBehaviour(m_engine.getAssertBehaviour());
    RuleBaseConfiguration ruleBaseConfig = new RuleBaseConfiguration();
    ruleBaseConfig.setAssertBehaviour(behaviour);
    ruleBaseConfig.setEventProcessingMode(EventProcessingOption.STREAM);
    m_kieBase = kContainer.newKieBase(ruleBaseConfig);
    m_kieSession = m_kieBase.newKieSession();
    m_kieSession.setGlobal("engine", this);
    unmarshallStateFromDisk(true);
    ApplicationContext ctx = m_context;
    if (m_engine.getAppContext() != null) {
        ctx = new FileSystemXmlApplicationContext(new String[] { m_engine.getAppContext() }, m_context);
    }
    for (Global global : m_engine.getGlobals()) {
        m_kieSession.setGlobal(global.getName(), global.constructValue(ctx));
    }
    new Thread(() -> {
        Logging.putPrefix(getName());
        LOG.debug("Starting task thread for {}", getName());
        m_kieSession.fireUntilHalt();
        LOG.debug("Stopping task thread for {}", getName());
    }, "FireTask-" + getName()).start();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) FileSystemXmlApplicationContext(org.springframework.context.support.FileSystemXmlApplicationContext) FileSystemXmlApplicationContext(org.springframework.context.support.FileSystemXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) AssertBehaviour(org.drools.core.RuleBaseConfiguration.AssertBehaviour) KieBuilder(org.kie.api.builder.KieBuilder) File(java.io.File) KieContainer(org.kie.api.runtime.KieContainer)

Example 2 with RuleBaseConfiguration

use of org.drools.core.RuleBaseConfiguration in project drools by kiegroup.

the class MultithreadTest method testJittingShortComparison.

@Test(timeout = 20000)
public void testJittingShortComparison() {
    // DROOLS-1633
    final String drl = "import " + BeanA.class.getCanonicalName() + "\n;" + "global java.util.List list;" + "rule R when\n" + "  $a1: BeanA($sv1 : shortValue)\n" + "  $b2: BeanA(shortValue != $sv1)\n" + "then\n" + "  list.add(\"FIRED\");\n" + "end";
    final List<String> list = Collections.synchronizedList(new ArrayList());
    final RuleBaseConfiguration rbc = (RuleBaseConfiguration) KieServices.Factory.get().newKieBaseConfiguration();
    rbc.setJittingThreshold(0);
    final KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build(rbc);
    final int threadNr = 1000;
    final Thread[] threads = new Thread[threadNr];
    for (int i = 0; i < threadNr; i++) {
        threads[i] = new Thread(new SessionRunner(kbase, list));
    }
    for (int i = 0; i < threadNr; i++) {
        threads[i].start();
    }
    for (int i = 0; i < threadNr; i++) {
        try {
            threads[i].join();
        } catch (final InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    Assertions.assertThat(list).hasSize(0);
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) EntryPoint(org.kie.api.runtime.rule.EntryPoint) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) KieBase(org.kie.api.KieBase) Test(org.junit.Test)

Example 3 with RuleBaseConfiguration

use of org.drools.core.RuleBaseConfiguration 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 4 with RuleBaseConfiguration

use of org.drools.core.RuleBaseConfiguration in project drools by kiegroup.

the class PhreakJoinNodeTest method createContext.

public BuildContext createContext() {
    RuleBaseConfiguration conf = new RuleBaseConfiguration();
    KnowledgeBaseImpl rbase = new KnowledgeBaseImpl("ID", conf);
    BuildContext buildContext = new BuildContext(rbase);
    RuleImpl rule = new RuleImpl("rule1").setPackage("org.pkg1");
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.pkg1");
    pkg.getDialectRuntimeRegistry().setDialectData("mvel", new MVELDialectRuntimeData());
    pkg.addRule(rule);
    buildContext.setRule(rule);
    return buildContext;
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) BuildContext(org.drools.core.reteoo.builder.BuildContext) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 5 with RuleBaseConfiguration

use of org.drools.core.RuleBaseConfiguration in project drools by kiegroup.

the class PhreakNotNodeTest method createContext.

public BuildContext createContext() {
    RuleBaseConfiguration conf = new RuleBaseConfiguration();
    KnowledgeBaseImpl rbase = new KnowledgeBaseImpl("ID", conf);
    BuildContext buildContext = new BuildContext(rbase);
    RuleImpl rule = new RuleImpl("rule1").setPackage("org.pkg1");
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.pkg1");
    pkg.getDialectRuntimeRegistry().setDialectData("mvel", new MVELDialectRuntimeData());
    pkg.addRule(rule);
    buildContext.setRule(rule);
    return buildContext;
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) BuildContext(org.drools.core.reteoo.builder.BuildContext) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

RuleBaseConfiguration (org.drools.core.RuleBaseConfiguration)43 Test (org.junit.Test)19 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)11 BuildContext (org.drools.core.reteoo.builder.BuildContext)10 ArrayList (java.util.ArrayList)8 Properties (java.util.Properties)8 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)8 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)8 BetaNodeFieldConstraint (org.drools.core.spi.BetaNodeFieldConstraint)8 KieSession (org.kie.api.runtime.KieSession)8 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)6 BetaMemory (org.drools.core.reteoo.BetaMemory)6 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)6 TupleList (org.drools.core.util.index.TupleList)6 List (java.util.List)5 BetaConstraints (org.drools.core.common.BetaConstraints)5 InternalFactHandle (org.drools.core.common.InternalFactHandle)5 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)5 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)5 SingleBetaConstraints (org.drools.core.common.SingleBetaConstraints)4