Search in sources :

Example 41 with KnowledgeBaseImpl

use of org.drools.core.impl.KnowledgeBaseImpl in project drools by kiegroup.

the class ProtobufMarshaller method unmarshall.

public StatefulKnowledgeSession unmarshall(final InputStream stream, KieSessionConfiguration config, Environment environment) throws IOException, ClassNotFoundException {
    if (config == null) {
        config = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    }
    if (environment == null) {
        environment = KieServices.get().newEnvironment();
    }
    MarshallerReaderContext context = new MarshallerReaderContext(stream, (KnowledgeBaseImpl) kbase, RuleBaseNodes.getNodeMap((KnowledgeBaseImpl) kbase), this.strategyStore, TIMER_READERS, this.marshallingConfig.isMarshallProcessInstances(), this.marshallingConfig.isMarshallWorkItems(), environment);
    int id = ((KnowledgeBaseImpl) this.kbase).nextWorkingMemoryCounter();
    RuleBaseConfiguration conf = ((KnowledgeBaseImpl) this.kbase).getConfiguration();
    StatefulKnowledgeSessionImpl session = ProtobufInputMarshaller.readSession(context, id, environment, (SessionConfiguration) config, initializer);
    context.close();
    if (((SessionConfiguration) config).isKeepReference()) {
        ((KnowledgeBaseImpl) this.kbase).addStatefulSession(session);
    }
    return session;
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration)

Example 42 with KnowledgeBaseImpl

use of org.drools.core.impl.KnowledgeBaseImpl in project drools by kiegroup.

the class DroolsAbstractPMMLTest method createExecutor.

protected RuleUnitExecutor createExecutor(String sourceName) {
    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    ReleaseId releaseId = new ReleaseIdImpl("org.kie:pmmlTest:1.0-SNAPSHOT");
    ((KieRepositoryImpl) kr).setDefaultGAV(releaseId);
    Resource res = ResourceFactory.newClassPathResource(sourceName);
    kbase = new KieHelper().addResource(res, ResourceType.PMML).build();
    assertNotNull(kbase);
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    KieContainer kc = ((KnowledgeBaseImpl) ((InternalRuleUnitExecutor) executor).getKieSession().getKieBase()).getKieContainer();
    InternalKieModule ikm = (InternalKieModule) kr.getKieModule(releaseId);
    try (FileOutputStream fos = new FileOutputStream("/tmp/outputModule.jar")) {
        fos.write(ikm.getBytes());
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    kc.getKieBaseNames().forEach(n -> {
        System.out.println(n);
    });
    data = executor.newDataSource("request");
    resultData = executor.newDataSource("results");
    pmmlData = executor.newDataSource("pmmlData");
    return executor;
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) ReleaseIdImpl(org.drools.compiler.kproject.ReleaseIdImpl) KieRepository(org.kie.api.builder.KieRepository) Resource(org.kie.api.io.Resource) FileNotFoundException(java.io.FileNotFoundException) KieHelper(org.kie.internal.utils.KieHelper) KieServices(org.kie.api.KieServices) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) ReleaseId(org.kie.api.builder.ReleaseId) IOException(java.io.IOException) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieRepositoryImpl(org.drools.compiler.kie.builder.impl.KieRepositoryImpl) FileOutputStream(java.io.FileOutputStream) KieContainer(org.kie.api.runtime.KieContainer) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 43 with KnowledgeBaseImpl

use of org.drools.core.impl.KnowledgeBaseImpl in project drools by kiegroup.

the class RuleTest method testTimeMachine.

@Test
public void testTimeMachine() {
    SessionConfiguration conf = SessionConfiguration.newInstance();
    conf.setClockType(ClockType.PSEUDO_CLOCK);
    WorkingMemory wm = (WorkingMemory) new KnowledgeBaseImpl("x", null).newKieSession(conf, null);
    final Calendar future = Calendar.getInstance();
    ((PseudoClockScheduler) wm.getSessionClock()).setStartupTime(future.getTimeInMillis());
    final RuleImpl rule = new RuleImpl("myrule");
    rule.setEnabled(EnabledBoolean.ENABLED_TRUE);
    assertTrue(rule.isEffective(null, new RuleTerminalNode(), wm));
    future.setTimeInMillis(future.getTimeInMillis() + 100000000);
    rule.setDateEffective(future);
    assertFalse(rule.isEffective(null, new RuleTerminalNode(), wm));
    ((PseudoClockScheduler) wm.getSessionClock()).advanceTime(1000000000000L, TimeUnit.MILLISECONDS);
    assertTrue(rule.isEffective(null, new RuleTerminalNode(), wm));
}
Also used : WorkingMemory(org.drools.core.WorkingMemory) Calendar(java.util.Calendar) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) SessionConfiguration(org.drools.core.SessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 44 with KnowledgeBaseImpl

use of org.drools.core.impl.KnowledgeBaseImpl in project drools by kiegroup.

the class RuleTest method testDateExpires.

@Test
public void testDateExpires() throws Exception {
    WorkingMemory wm = (WorkingMemory) new KnowledgeBaseImpl("x", null).newKieSession();
    final RuleImpl rule = new RuleImpl("myrule");
    assertTrue(rule.isEffective(null, new RuleTerminalNode(), wm));
    final Calendar earlier = Calendar.getInstance();
    earlier.setTimeInMillis(10);
    rule.setDateExpires(earlier);
    assertFalse(rule.isEffective(null, new RuleTerminalNode(), wm));
    final Calendar later = Calendar.getInstance();
    later.setTimeInMillis(later.getTimeInMillis() + 100000000);
    rule.setDateExpires(later);
    assertTrue(rule.isEffective(null, new RuleTerminalNode(), wm));
}
Also used : WorkingMemory(org.drools.core.WorkingMemory) Calendar(java.util.Calendar) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 45 with KnowledgeBaseImpl

use of org.drools.core.impl.KnowledgeBaseImpl in project drools by kiegroup.

the class ScenarioTest 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

KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)45 Test (org.junit.Test)31 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)12 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)11 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)11 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)10 Resource (org.kie.api.io.Resource)10 LogicalTypeInconsistencyException (org.drools.core.factmodel.traits.LogicalTypeInconsistencyException)9 TraitFactory (org.drools.core.factmodel.traits.TraitFactory)9 TraitProxy (org.drools.core.factmodel.traits.TraitProxy)9 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)9 ClassPathResource (org.drools.core.io.impl.ClassPathResource)9 KieBase (org.kie.api.KieBase)9 FactType (org.kie.api.definition.type.FactType)9 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)8 KieSession (org.kie.api.runtime.KieSession)8 RuleBaseConfiguration (org.drools.core.RuleBaseConfiguration)6 WorkingMemory (org.drools.core.WorkingMemory)6 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)6 KieHelper (org.kie.internal.utils.KieHelper)6