Search in sources :

Example 26 with KnowledgeBaseImpl

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

the class ScenarioRunnerTest method setUp.

@Before
public void setUp() throws Exception {
    ksession = mock(KieSession.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) KieSession(org.kie.api.runtime.KieSession) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) Before(org.junit.Before)

Example 27 with KnowledgeBaseImpl

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

the class ScenarioRunnerTest method testRuleFlowGroupActivation.

@Test
public void testRuleFlowGroupActivation() throws Exception {
    Scenario scenario = new Scenario();
    scenario.getImports().addImport(new Import("foo.bar.Coolness"));
    Fixture[] given = new Fixture[] { new FactData("Coolness", "c", Arrays.<Field>asList(new FieldData("num", "42"), new FieldData("name", "mic")), false) };
    scenario.getFixtures().addAll(Arrays.asList(given));
    ExecutionTrace executionTrace = new ExecutionTrace();
    scenario.getRules().add("rule1");
    scenario.setInclusive(true);
    scenario.getFixtures().add(executionTrace);
    Expectation[] assertions = new Expectation[2];
    assertions[0] = new VerifyFact("c", ls(new VerifyField("num", "42", "==")));
    assertions[1] = new VerifyRuleFired("rule1", 1, null);
    scenario.getFixtures().addAll(Arrays.asList(assertions));
    KieSession ksession = getKieSession("rule_flow_actication.drl");
    ClassLoader classLoader = ((KnowledgeBaseImpl) ksession.getKieBase()).getRootClassLoader();
    HashSet<String> imports = new HashSet<String>();
    imports.add("foo.bar.*");
    TypeResolver resolver = new ClassTypeResolver(imports, classLoader);
    Class<?> coolnessClass = classLoader.loadClass("foo.bar.Coolness");
    assertNotNull(coolnessClass);
    ClassLoader cl_ = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(classLoader);
    // resolver will need to have generated beans in it - possibly using a composite classloader from the package,
    // including whatever CL has the generated beans...
    ScenarioRunner scenarioRunner = new ScenarioRunner(ksession);
    scenarioRunner.run(scenario);
    assertEquals(0, executionTrace.getNumberOfRulesFired().intValue());
    assertFalse(scenario.wasSuccessful());
    // Activate rule flow
    scenario.getFixtures().clear();
    given = new Fixture[] { new FactData("Coolness", "c", Arrays.<Field>asList(new FieldData("num", "42"), new FieldData("name", "mic")), false), new ActivateRuleFlowGroup("asdf") };
    scenario.getFixtures().addAll(Arrays.asList(given));
    scenario.getFixtures().add(executionTrace);
    ((InternalAgendaGroup) ksession.getAgenda().getRuleFlowGroup("asdf")).setAutoDeactivate(false);
    scenarioRunner = new ScenarioRunner(ksession);
    scenarioRunner.run(scenario);
    assertTrue(scenario.wasSuccessful());
    Thread.currentThread().setContextClassLoader(cl_);
}
Also used : Import(org.kie.soup.project.datamodel.imports.Import) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) ActivateRuleFlowGroup(org.drools.workbench.models.testscenarios.shared.ActivateRuleFlowGroup) VerifyField(org.drools.workbench.models.testscenarios.shared.VerifyField) Field(org.drools.workbench.models.testscenarios.shared.Field) FactData(org.drools.workbench.models.testscenarios.shared.FactData) ProjectClassLoader(org.drools.core.common.ProjectClassLoader) KieSession(org.kie.api.runtime.KieSession) Fixture(org.drools.workbench.models.testscenarios.shared.Fixture) VerifyFact(org.drools.workbench.models.testscenarios.shared.VerifyFact) HashSet(java.util.HashSet) InternalAgendaGroup(org.drools.core.common.InternalAgendaGroup) VerifyRuleFired(org.drools.workbench.models.testscenarios.shared.VerifyRuleFired) ExecutionTrace(org.drools.workbench.models.testscenarios.shared.ExecutionTrace) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) Scenario(org.drools.workbench.models.testscenarios.shared.Scenario) FieldData(org.drools.workbench.models.testscenarios.shared.FieldData) Expectation(org.drools.workbench.models.testscenarios.shared.Expectation) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver) Test(org.junit.Test)

Example 28 with KnowledgeBaseImpl

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

the class KieProjectRuntimeModulesTest method createMultpleJarAndFileResources.

@Test
public void createMultpleJarAndFileResources() throws IOException, ClassNotFoundException, InterruptedException {
    KieModuleModel kProjModel1 = createKieModule("jar1", true);
    KieModuleModel kProjModel2 = createKieModule("jar2", true);
    KieModuleModel kProjModel3 = createKieModule("jar3", true);
    KieModuleModel kProjModel4 = createKieModule("fol4", false);
    ReleaseId releaseId1 = KieServices.Factory.get().newReleaseId("jar1", "art1", "1.0-SNAPSHOT");
    ReleaseId releaseId2 = KieServices.Factory.get().newReleaseId("jar2", "art1", "1.0-SNAPSHOT");
    ReleaseId releaseId3 = KieServices.Factory.get().newReleaseId("jar3", "art1", "1.0-SNAPSHOT");
    ReleaseId releaseId4 = KieServices.Factory.get().newReleaseId("fol4", "art1", "1.0-SNAPSHOT");
    java.io.File file1 = fileManager.newFile("jar1-1.0-SNAPSHOT.jar");
    java.io.File file2 = fileManager.newFile("jar2-1.0-SNAPSHOT.jar");
    java.io.File file3 = fileManager.newFile("jar3-1.0-SNAPSHOT.jar");
    java.io.File fol4 = fileManager.newFile("fol4-1.0-SNAPSHOT");
    ZipKieModule mod1 = new ZipKieModule(releaseId1, kProjModel1, file1);
    ZipKieModule mod2 = new ZipKieModule(releaseId2, kProjModel2, file2);
    ZipKieModule mod3 = new ZipKieModule(releaseId3, kProjModel3, file3);
    FileKieModule mod4 = new FileKieModule(releaseId4, kProjModel4, fol4);
    mod1.addKieDependency(mod2);
    mod1.addKieDependency(mod3);
    mod1.addKieDependency(mod4);
    KieModuleKieProject kProject = new KieModuleKieProject(mod1);
    KieContainer kContainer = new KieContainerImpl(kProject, null);
    KieBase kBase = kContainer.getKieBase("jar1.KBase1");
    ClassLoader cl = ((KnowledgeBaseImpl) kBase).getRootClassLoader();
    Class cls = cl.loadClass("org.drools.compiler.cdi.test.KProjectTestClassjar1");
    assertNotNull(cls);
    cls = cl.loadClass("org.drools.compiler.cdi.test.KProjectTestClassjar2");
    assertNotNull(cls);
    cls = cl.loadClass("org.drools.compiler.cdi.test.KProjectTestClassjar3");
    assertNotNull(cls);
    testEntry(new KProjectTestClassImpl("jar1", kContainer), "jar1");
    testEntry(new KProjectTestClassImpl("jar2", kContainer), "jar2");
    testEntry(new KProjectTestClassImpl("jar3", kContainer), "jar3");
    testEntry(new KProjectTestClassImpl("fol4", kContainer), "fol4");
}
Also used : ZipKieModule(org.drools.compiler.kie.builder.impl.ZipKieModule) FileKieModule(org.drools.compiler.kie.builder.impl.FileKieModule) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) ReleaseId(org.kie.api.builder.ReleaseId) KieModuleKieProject(org.drools.compiler.kie.builder.impl.KieModuleKieProject) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 29 with KnowledgeBaseImpl

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

the class TraitTest method testWrapperEmpty.

@Test(timeout = 10000)
public void testWrapperEmpty() {
    String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    Resource res = ResourceFactory.newClassPathResource(source);
    assertNotNull(res);
    kbuilder.add(res, ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase();
    kb.addPackages(kbuilder.getKnowledgePackages());
    TraitFactory.setMode(mode, kb);
    TraitFactory tFactory = ((KnowledgeBaseImpl) kb).getConfiguration().getComponentFactory().getTraitFactory();
    try {
        FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
        TraitableBean imp = (TraitableBean) impClass.newInstance();
        FactType studentClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
        Class trait = studentClass.getFactClass();
        TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
        Map<String, Object> virtualFields = imp._getDynamicProperties();
        Map<String, Object> wrapper = proxy.getFields();
        assertFalse(wrapper.isEmpty());
        studentClass.set(proxy, "name", "john");
        assertFalse(wrapper.isEmpty());
        studentClass.set(proxy, "name", null);
        assertFalse(wrapper.isEmpty());
        studentClass.set(proxy, "age", 32);
        assertFalse(wrapper.isEmpty());
        studentClass.set(proxy, "age", null);
        assertFalse(wrapper.isEmpty());
        // FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity");
        TraitableBean ind = new Entity();
        FactType RoleClass = kb.getFactType("org.drools.compiler.trait.test", "Role");
        Class trait2 = RoleClass.getFactClass();
        TraitProxy proxy2 = (TraitProxy) tFactory.getProxy(ind, trait2);
        Map<String, Object> wrapper2 = proxy2.getFields();
        assertTrue(wrapper2.isEmpty());
        proxy2.getFields().put("name", "john");
        assertFalse(wrapper2.isEmpty());
        proxy2.getFields().put("name", null);
        assertFalse(wrapper2.isEmpty());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) Entity(org.drools.core.factmodel.traits.Entity) Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) LogicalTypeInconsistencyException(org.drools.core.factmodel.traits.LogicalTypeInconsistencyException) TraitFactory(org.drools.core.factmodel.traits.TraitFactory) FactType(org.kie.api.definition.type.FactType) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 30 with KnowledgeBaseImpl

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

the class TraitTest method testWrapperClearAndRemove.

@Test(timeout = 10000)
public void testWrapperClearAndRemove() {
    String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    Resource res = ResourceFactory.newClassPathResource(source);
    assertNotNull(res);
    kbuilder.add(res, ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase();
    kb.addPackages(kbuilder.getKnowledgePackages());
    TraitFactory.setMode(mode, kb);
    TraitFactory tFactory = ((KnowledgeBaseImpl) kb).getConfiguration().getComponentFactory().getTraitFactory();
    try {
        FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
        TraitableBean imp = (TraitableBean) impClass.newInstance();
        impClass.set(imp, "name", "john");
        FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
        Class trait = traitClass.getFactClass();
        TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
        proxy.getFields().put("surname", "xxx");
        proxy.getFields().put("name2", "john");
        proxy.getFields().put("nfield", null);
        Set set = new HashSet();
        set.add("name");
        set.add("surname");
        set.add("age");
        set.add("school");
        set.add("name2");
        set.add("nfield");
        assertEquals(6, proxy.getFields().keySet().size());
        assertEquals(set, proxy.getFields().keySet());
        proxy.getFields().clear();
        Map<String, Object> fields = proxy.getFields();
        assertEquals(3, fields.size());
        assertTrue(fields.containsKey("age"));
        assertTrue(fields.containsKey("school"));
        assertTrue(fields.containsKey("name"));
        assertEquals(0, fields.get("age"));
        assertNull(fields.get("school"));
        assertNotNull(fields.get("name"));
        proxy.getFields().put("surname", "xxx");
        proxy.getFields().put("name2", "john");
        proxy.getFields().put("nfield", null);
        proxy.getFields().put("age", 24);
        assertEquals("john", proxy.getFields().get("name"));
        assertEquals("xxx", proxy.getFields().get("surname"));
        assertEquals("john", proxy.getFields().get("name2"));
        assertEquals(null, proxy.getFields().get("nfield"));
        assertEquals(24, proxy.getFields().get("age"));
        assertEquals(null, proxy.getFields().get("school"));
        proxy.getFields().remove("surname");
        proxy.getFields().remove("name2");
        proxy.getFields().remove("age");
        proxy.getFields().remove("school");
        proxy.getFields().remove("nfield");
        assertEquals(3, proxy.getFields().size());
        assertEquals(0, proxy.getFields().get("age"));
        assertEquals(null, proxy.getFields().get("school"));
        assertEquals("john", proxy.getFields().get("name"));
        assertEquals(null, proxy.getFields().get("nfield"));
        assertFalse(proxy.getFields().containsKey("nfield"));
        assertEquals(null, proxy.getFields().get("name2"));
        assertFalse(proxy.getFields().containsKey("name2"));
        assertEquals(null, proxy.getFields().get("surname"));
        assertFalse(proxy.getFields().containsKey("surname"));
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) Set(java.util.Set) HashSet(java.util.HashSet) BitSet(java.util.BitSet) Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) LogicalTypeInconsistencyException(org.drools.core.factmodel.traits.LogicalTypeInconsistencyException) TraitFactory(org.drools.core.factmodel.traits.TraitFactory) FactType(org.kie.api.definition.type.FactType) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) HashSet(java.util.HashSet) Test(org.junit.Test)

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