Search in sources :

Example 21 with KnowledgeBaseImpl

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

the class RuleTest method testRuleEnabled.

@Test
public void testRuleEnabled() {
    WorkingMemory wm = (WorkingMemory) new KnowledgeBaseImpl("x", null).newKieSession();
    final RuleImpl rule = new RuleImpl("myrule");
    rule.setEnabled(EnabledBoolean.ENABLED_FALSE);
    assertFalse(rule.isEffective(null, new RuleTerminalNode(), wm));
    final Calendar past = Calendar.getInstance();
    past.setTimeInMillis(10);
    rule.setDateEffective(past);
    assertFalse(rule.isEffective(null, new RuleTerminalNode(), wm));
    rule.setEnabled(EnabledBoolean.ENABLED_TRUE);
    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 22 with KnowledgeBaseImpl

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

the class RuleTest method testDateEffectiveExpires.

@Test
public void testDateEffectiveExpires() {
    WorkingMemory wm = (WorkingMemory) new KnowledgeBaseImpl("x", null).newKieSession();
    final RuleImpl rule = new RuleImpl("myrule");
    final Calendar past = Calendar.getInstance();
    past.setTimeInMillis(10);
    final Calendar future = Calendar.getInstance();
    future.setTimeInMillis(future.getTimeInMillis() + 100000000);
    rule.setDateEffective(past);
    rule.setDateExpires(future);
    assertTrue(rule.isEffective(null, new RuleTerminalNode(), wm));
    rule.setDateExpires(past);
    assertFalse(rule.isEffective(null, new RuleTerminalNode(), wm));
    rule.setDateExpires(future);
    rule.setDateEffective(future);
    assertFalse(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 23 with KnowledgeBaseImpl

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

the class RuleTest method testDateEffective.

@Test
public void testDateEffective() {
    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.setDateEffective(earlier);
    assertTrue(rule.isEffective(null, new RuleTerminalNode(), wm));
    final Calendar later = Calendar.getInstance();
    later.setTimeInMillis(later.getTimeInMillis() + 100000000);
    assertTrue(later.after(Calendar.getInstance()));
    rule.setDateEffective(later);
    assertFalse(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 24 with KnowledgeBaseImpl

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

the class SegmentExecution method applySegmentModel.

public void applySegmentModel(PMMLRequestData requestData, DataSource<PMMLRequestData> data, DataSource<PMML4Result> results, DataSource<SegmentExecution> segmentExecutions, KnowledgeHelper helper) throws IllegalStateException {
    if (ruleUnitClassName == null || ruleUnitClassName.trim().isEmpty()) {
        throw new IllegalStateException("Unable to apply segment model: No rule unit class name is available");
    }
    Class<? extends RuleUnit> ruleUnitClass = null;
    RuleUnitRegistry rur = ((KnowledgeBaseImpl) helper.getKieRuntime().getKieBase()).getRuleUnitRegistry();
    RuleUnit ruOld = ((WrappedStatefulKnowledgeSessionForRHS) helper.getKieRuntime()).getRuleUnitExecutor().getCurrentRuleUnit();
    RuleUnitDescr rud = rur.getNamedRuleUnit(ruleUnitClassName).orElse(null);
    if (rud != null) {
        ruleUnitClass = rud.getRuleUnitClass();
    } else {
        throw new IllegalStateException("Unable to apply segment model: Unable to find rule unit");
    }
// if (ruleUnitClass != null) {
// helper.run(ruleUnitClass);
// PMML4Result result = new PMML4Result(this);
// FactHandle requestFH = data.insert(this.requestData);
// FactHandle resultsFH = results.insert(result);
// 
// helper.run(ruleUnitClass);
// RuleUnit ruNew = ((WrappedStatefulKnowledgeSessionForRHS)helper.getKieRuntime()).getRuleUnitExecutor().getCurrentRuleUnit();
// System.out.println("** Result **");
// System.out.println(result);
// 
// // Update the state and let the Mining session know
// this.state = SegmentExecutionState.EXECUTING;
// FactHandle handle = ((InternalDataSource)segmentExecutions).getFactHandleForObject(this);
// segmentExecutions.update(handle, this);
// 
// helper.run(ruleUnitClass);
// System.out.println("Sub-Model Results");
// results.forEach(r -> {System.out.println(r);});
// }
}
Also used : RuleUnitRegistry(org.drools.core.ruleunit.RuleUnitRegistry) RuleUnitDescr(org.drools.core.ruleunit.RuleUnitDescr) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleUnit(org.kie.api.runtime.rule.RuleUnit)

Example 25 with KnowledgeBaseImpl

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

the class ScenarioRunner4JUnitTest method setUp.

@Before
public void setUp() throws Exception {
    ksession = mock(StatefulKnowledgeSessionImpl.class);
    KnowledgeBaseImpl knowledgeBase = mock(KnowledgeBaseImpl.class);
    when(ksession.getKieBase()).thenReturn(knowledgeBase);
    ProjectClassLoader classLoader = ProjectClassLoader.createProjectClassLoader();
    when(knowledgeBase.getRootClassLoader()).thenReturn(classLoader);
}
Also used : ProjectClassLoader(org.drools.core.common.ProjectClassLoader) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) Before(org.junit.Before)

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