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);
}
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_);
}
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");
}
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());
}
}
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());
}
}
Aggregations