use of org.kie.api.builder.model.KieModuleModel in project drools by kiegroup.
the class KieModuleModelTest method testMarshallingUnmarshalling.
@Test
public void testMarshallingUnmarshalling() {
KieServices ks = KieServices.Factory.get();
KieModuleModel kproj = ks.newKieModuleModel().setConfigurationProperty(LanguageLevelOption.PROPERTY_NAME, LanguageLevelOption.DRL6_STRICT.toString()).setConfigurationProperty(PropertySpecificOption.PROPERTY_NAME, PropertySpecificOption.ALWAYS.toString());
KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1").setEqualsBehavior(EqualityBehaviorOption.EQUALITY).setEventProcessingMode(EventProcessingOption.STREAM).setDeclarativeAgenda(DeclarativeAgendaOption.ENABLED).addInclude("OtherKBase").addPackage("org.kie.pkg1").addPackage("org.kie.pkg2");
KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("KSession1").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("realtime")).setBeliefSystem(BeliefSystemTypeOption.get("jtms")).setFileLogger("drools.log", 10, true).setDefault(true);
ksession1.newListenerModel("org.domain.FirstInterface", ListenerModel.Kind.AGENDA_EVENT_LISTENER);
ksession1.newListenerModel("org.domain.SecondInterface", ListenerModel.Kind.RULE_RUNTIME_EVENT_LISTENER).newQualifierModel("MyQualfier2");
ksession1.newListenerModel("org.domain.ThirdInterface", ListenerModel.Kind.PROCESS_EVENT_LISTENER).newQualifierModel("MyQualfier3").setValue("v1");
ksession1.newListenerModel("org.domain.FourthInterface", ListenerModel.Kind.AGENDA_EVENT_LISTENER).newQualifierModel("MyQualfier4").addArgument("name1", "xxxx").addArgument("name2", "yyyy");
ksession1.newWorkItemHandlerModel("name", "org.domain.FifthInterface").newQualifierModel("MyQualfier5").addArgument("name1", "aaa").addArgument("name2", "bbb");
// KieBaseModel kieBaseModel2 = kproj.newKieBaseModel("KBase2")
// .setEqualsBehavior( EqualityBehaviorOption.EQUALITY )
// .setEventProcessingMode( EventProcessingOption.STREAM );
String xml = kproj.toXML();
// System.out.println( xml );
KieModuleModel kprojXml = fromXML(xml);
assertEquals(LanguageLevelOption.DRL6_STRICT.toString(), kprojXml.getConfigurationProperty(LanguageLevelOption.PROPERTY_NAME));
assertEquals(PropertySpecificOption.ALWAYS.toString(), kprojXml.getConfigurationProperty(PropertySpecificOption.PROPERTY_NAME));
KieBaseModel kieBaseModelXML = kprojXml.getKieBaseModels().get("KBase1");
assertSame(kprojXml, ((KieBaseModelImpl) kieBaseModelXML).getKModule());
assertEquals(EqualityBehaviorOption.EQUALITY, kieBaseModelXML.getEqualsBehavior());
assertEquals(EventProcessingOption.STREAM, kieBaseModelXML.getEventProcessingMode());
assertEquals(DeclarativeAgendaOption.ENABLED, kieBaseModelXML.getDeclarativeAgenda());
assertFalse(kieBaseModelXML.isDefault());
assertEquals("org.kie.pkg1", kieBaseModelXML.getPackages().get(0));
assertEquals("org.kie.pkg2", kieBaseModelXML.getPackages().get(1));
assertEquals("OtherKBase", ((KieBaseModelImpl) kieBaseModelXML).getIncludes().iterator().next());
KieSessionModel kieSessionModelXML = kieBaseModelXML.getKieSessionModels().get("KSession1");
assertSame(kieBaseModelXML, ((KieSessionModelImpl) kieSessionModelXML).getKieBaseModel());
assertEquals(KieSessionType.STATEFUL, kieSessionModelXML.getType());
assertEquals(ClockTypeOption.get("realtime"), kieSessionModelXML.getClockType());
assertEquals(BeliefSystemTypeOption.get("jtms"), kieSessionModelXML.getBeliefSystem());
FileLoggerModel fileLogger = kieSessionModelXML.getFileLogger();
assertEquals("drools.log", fileLogger.getFile());
assertEquals(10, fileLogger.getInterval());
assertEquals(true, fileLogger.isThreaded());
assertTrue(kieSessionModelXML.isDefault());
List<ListenerModel> listeners = kieSessionModelXML.getListenerModels();
ListenerModel listener2 = listeners.get(0);
assertEquals("org.domain.SecondInterface", listener2.getType());
assertEquals(ListenerModel.Kind.RULE_RUNTIME_EVENT_LISTENER, listener2.getKind());
// QualifierModel qualifier2 = listener2.getQualifierModel();
// assertEquals("MyQualfier2", qualifier2.getType());
ListenerModel listener1 = listeners.get(1);
assertEquals("org.domain.FirstInterface", listener1.getType());
assertEquals(ListenerModel.Kind.AGENDA_EVENT_LISTENER, listener1.getKind());
// assertNull(listener1.getQualifierModel());
ListenerModel listener4 = listeners.get(2);
assertEquals("org.domain.FourthInterface", listener4.getType());
assertEquals(ListenerModel.Kind.AGENDA_EVENT_LISTENER, listener4.getKind());
// QualifierModel qualifier4 = listener4.getQualifierModel();
// assertEquals("MyQualfier4", qualifier4.getType());
// assertEquals("xxxx", qualifier4.getArguments().get("name1"));
// assertEquals("yyyy", qualifier4.getArguments().get("name2"));
ListenerModel listener3 = listeners.get(3);
assertEquals("org.domain.ThirdInterface", listener3.getType());
assertEquals(ListenerModel.Kind.PROCESS_EVENT_LISTENER, listener3.getKind());
// QualifierModel qualifier3 = listener3.getQualifierModel();
// assertEquals("MyQualfier3", qualifier3.getType());
// assertEquals("v1", qualifier3.getValue());
WorkItemHandlerModel wihm = kieSessionModelXML.getWorkItemHandlerModels().get(0);
assertEquals("org.domain.FifthInterface", wihm.getType());
// QualifierModel qualifier5 = wihm.getQualifierModel();
// assertEquals("MyQualfier5", qualifier5.getType());
// assertEquals("aaa", qualifier5.getArguments().get("name1"));
// assertEquals("bbb", qualifier5.getArguments().get("name2"));
}
use of org.kie.api.builder.model.KieModuleModel in project drools by kiegroup.
the class KieMetaInfoBuilder method generateKieModuleMetaInfo.
public KieModuleMetaInfo generateKieModuleMetaInfo(ResourceStore trgMfs) {
// TODO: I think this method is wrong because it is only inspecting packages that are included
// in at least one kbase, but I believe it should inspect all packages, even if not included in
// any kbase, as they could be included in the future
Map<String, TypeMetaInfo> typeInfos = new HashMap<String, TypeMetaInfo>();
Map<String, Set<String>> rulesPerPackage = new HashMap<String, Set<String>>();
KieModuleModel kieModuleModel = kModule.getKieModuleModel();
for (String kieBaseName : kieModuleModel.getKieBaseModels().keySet()) {
KnowledgeBuilderImpl kBuilder = (KnowledgeBuilderImpl) kModule.getKnowledgeBuilderForKieBase(kieBaseName);
Map<String, PackageRegistry> pkgRegistryMap = kBuilder.getPackageRegistry();
KieModuleCache.KModuleCache.Builder _kmoduleCacheBuilder = createCacheBuilder();
KieModuleCache.CompilationData.Builder _compData = createCompilationData();
for (KiePackage kPkg : kBuilder.getKnowledgePackages()) {
PackageRegistry pkgRegistry = pkgRegistryMap.get(kPkg.getName());
JavaDialectRuntimeData runtimeData = (JavaDialectRuntimeData) pkgRegistry.getDialectRuntimeRegistry().getDialectData("java");
List<String> types = new ArrayList<String>();
for (FactType factType : kPkg.getFactTypes()) {
Class<?> typeClass = ((ClassDefinition) factType).getDefinedClass();
TypeDeclaration typeDeclaration = pkgRegistry.getPackage().getTypeDeclaration(typeClass);
if (typeDeclaration != null) {
typeInfos.put(typeClass.getName(), new TypeMetaInfo(typeDeclaration));
}
String className = factType.getName();
String internalName = className.replace('.', '/') + ".class";
if (trgMfs != null) {
byte[] bytes = runtimeData.getBytecode(internalName);
if (bytes != null) {
trgMfs.write(internalName, bytes, true);
}
}
types.add(internalName);
}
Set<String> rules = rulesPerPackage.get(kPkg.getName());
if (rules == null) {
rules = new HashSet<String>();
}
for (Rule rule : kPkg.getRules()) {
if (!rules.contains(rule.getName())) {
rules.add(rule.getName());
}
}
if (!rules.isEmpty()) {
rulesPerPackage.put(kPkg.getName(), rules);
}
addToCompilationData(_compData, runtimeData, types);
}
_kmoduleCacheBuilder.addCompilationData(_compData.build());
if (trgMfs != null) {
writeCompilationDataToTrg(_kmoduleCacheBuilder.build(), kieBaseName, trgMfs);
}
}
return new KieModuleMetaInfo(typeInfos, rulesPerPackage);
}
use of org.kie.api.builder.model.KieModuleModel in project drools by kiegroup.
the class AbstractKnowledgeTest method createKieModule.
public KieModuleModel createKieModule(String namespace, boolean createJar, String version) throws IOException, ClassNotFoundException, InterruptedException {
KieModuleModel kproj = new KieModuleModelImpl();
KieBaseModel kieBaseModel1 = kproj.newKieBaseModel(namespace + ".KBase1").setEqualsBehavior(EqualityBehaviorOption.EQUALITY).setEventProcessingMode(EventProcessingOption.STREAM).addPackage(namespace + ".KBase1").setDefault(true);
kieBaseModel1.newKieSessionModel(namespace + ".KSession1").setType(KieSessionType.STATELESS).setClockType(ClockTypeOption.get("realtime")).setDefault(true);
kieBaseModel1.newKieSessionModel(namespace + ".KSession2").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo"));
kieBaseModel1.newKieSessionModel(namespace + ".KSessionDefault").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo")).setDefault(true);
KieBaseModel kieBaseModel2 = kproj.newKieBaseModel(namespace + ".KBase2").setEqualsBehavior(EqualityBehaviorOption.IDENTITY).addPackage(namespace + ".KBase2").setEventProcessingMode(EventProcessingOption.CLOUD);
kieBaseModel2.newKieSessionModel(namespace + ".KSession3").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo"));
KieBaseModel kieBaseModel3 = kproj.newKieBaseModel(namespace + ".KBase3").addInclude(kieBaseModel1.getName()).addInclude(kieBaseModel2.getName()).setEqualsBehavior(EqualityBehaviorOption.IDENTITY).setEventProcessingMode(EventProcessingOption.CLOUD);
kieBaseModel3.newKieSessionModel(namespace + ".KSession4").setType(KieSessionType.STATELESS).setClockType(ClockTypeOption.get("pseudo"));
KieServices ks = KieServices.Factory.get();
KieFileSystemImpl kfs = (KieFileSystemImpl) ks.newKieFileSystem();
kfs.write("src/main/resources/META-INF/beans.xml", generateBeansXML());
kfs.writeKModuleXML(((KieModuleModelImpl) kproj).toXML());
ReleaseId releaseId = ks.newReleaseId(namespace, "art1", version);
kfs.generateAndWritePomXML(releaseId);
String kBase1R1 = getRule(namespace + ".test1", "rule1", version);
String kBase1R2 = getRule(namespace + ".test1", "rule2", version);
String kbase2R1 = getRule(namespace + ".test2", "rule1", version);
String kbase2R2 = getRule(namespace + ".test2", "rule2", version);
String fldKB1 = "src/main/resources/" + kieBaseModel1.getName().replace('.', '/');
String fldKB2 = "src/main/resources/" + kieBaseModel2.getName().replace('.', '/');
kfs.write(fldKB1 + "/rule1.drl", kBase1R1.getBytes());
kfs.write(fldKB1 + "/rule2.drl", kBase1R2.getBytes());
kfs.write(fldKB2 + "/rule1.drl", kbase2R1.getBytes());
kfs.write(fldKB2 + "/rule2.drl", kbase2R2.getBytes());
kfs.write("src/main/java/org/drools/compiler/cdi/test/KProjectTestClass" + namespace + ".java", generateKProjectTestClass(kproj, namespace));
KieBuilder kBuilder = ks.newKieBuilder(kfs);
kBuilder.buildAll();
if (kBuilder.getResults().hasMessages(Level.ERROR)) {
fail("should not have errors" + kBuilder.getResults());
}
MemoryKieModule kieModule = (MemoryKieModule) kBuilder.getKieModule();
MemoryFileSystem trgMfs = kieModule.getMemoryFileSystem();
if (createJar) {
trgMfs.writeAsJar(fileManager.getRootDirectory(), namespace + "-" + version);
} else {
java.io.File file = fileManager.newFile(namespace + "-" + version);
trgMfs.writeAsFs(file);
}
return kproj;
}
use of org.kie.api.builder.model.KieModuleModel in project drools by kiegroup.
the class SimulateTestBase method createKJarWithMultipleResources.
public static ReleaseId createKJarWithMultipleResources(String id, String[] resources, ResourceType[] types) {
KieServices ks = KieServices.Factory.get();
KieModuleModel kproj = ks.newKieModuleModel();
KieFileSystem kfs = ks.newKieFileSystem();
for (int i = 0; i < resources.length; i++) {
String res = resources[i];
String type = types[i].getDefaultExtension();
kfs.write("src/main/resources/" + id.replaceAll("\\.", "/") + "/org/test/res" + i + "." + type, res);
}
KieBaseModel kBase1 = kproj.newKieBaseModel(id).setEqualsBehavior(EqualityBehaviorOption.EQUALITY).setEventProcessingMode(EventProcessingOption.STREAM);
KieSessionModel ksession1 = kBase1.newKieSessionModel(id + ".KSession1").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo"));
kfs.writeKModuleXML(kproj.toXML());
KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
assertTrue(kieBuilder.getResults().getMessages().isEmpty());
KieModule kieModule = kieBuilder.getKieModule();
return kieModule.getReleaseId();
}
use of org.kie.api.builder.model.KieModuleModel in project drools by kiegroup.
the class NegativePatternsTest method prepareKieSession.
@Before
public void prepareKieSession() {
String drl = "package org.drools.compiler.integrationtests\n" + "\n" + "import org.drools.compiler.integrationtests.NegativePatternsTest.TestEvent\n" + "\n" + "declare TestEvent\n" + " @role( event )\n" + " @expires( 22ms )\n" + "end\n" + "\n" + "rule \"SingleAbsence\"\n" + " duration( 18ms )\n" + " when\n" + " not ( TestEvent ( ) from entry-point EventStream )\n" + " then\n" + " // consequence\n" + "end\n" + "\n" + "rule \"SingleConstrained\"\n" + " duration( 18ms )\n" + " when\n" + " not ( TestEvent ( name == \"EventA\" ) from entry-point EventStream )\n" + " then\n" + " // consequence\n" + "end\n" + "\n" + "rule \"MultipleEvents\"\n" + " duration( 18ms )\n" + " when\n" + " TestEvent ( name == \"EventA\" ) over window:time( 22ms ) from entry-point EventStream\n" + " not ( TestEvent ( name == \"EventB\" ) over window:time( 22ms ) from entry-point EventStream )\n" + " then\n" + " // consequence\n" + "end\n" + "\n" + "rule \"MultipleEntryPoints\"\n" + " duration( 18ms )\n" + " when\n" + " not (\n" + " TestEvent( name == \"EventA\" ) from entry-point EventStream\n" + " )\n" + " not (\n" + " TestEvent( name == \"EventB\" ) from entry-point OtherStream\n" + " )\n" + " then\n" + " // consequence\n" + "end\n";
KieServices ks = KieServices.Factory.get();
KieModuleModel kieModule = ks.newKieModuleModel();
KieBaseModel defaultBase = kieModule.newKieBaseModel("defaultKBase").setDefault(true).addPackage("*").setEventProcessingMode(EventProcessingOption.STREAM);
defaultBase.newKieSessionModel(DEFAULT_SESSION_NAME).setClockType(ClockTypeOption.get("pseudo")).setDefault(true);
KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
kfs.writeKModuleXML(kieModule.toXML());
KieModule builtModule = ks.newKieBuilder(kfs).buildAll().getKieModule();
ks.getRepository().addKieModule(builtModule);
ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession(DEFAULT_SESSION_NAME);
firedRulesListener = new TrackingAgendaEventListener();
ksession.addEventListener(firedRulesListener);
}
Aggregations