use of org.kie.api.KieServices in project drools by kiegroup.
the class KieBuilderSetImplTest method testBuild.
@Test
public void testBuild() throws Exception {
final KieServices ks = KieServices.Factory.get();
final KieFileSystem kfs = ks.newKieFileSystem();
kfs.write("src/main/resources/rule%201.drl", ruleContent());
final KieBuilderSetImpl kieBuilderSet = new KieBuilderSetImpl(kieBuilder(ks, kfs));
kieBuilderSet.setFiles(new String[] { "src/main/resources/rule%201.drl" });
final IncrementalResults build = kieBuilderSet.build();
assertEquals(0, build.getAddedMessages().size());
assertEquals(0, build.getRemovedMessages().size());
}
use of org.kie.api.KieServices 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.KieServices in project drools by kiegroup.
the class KieBuilderImpl method init.
private PomModel init() {
KieServices ks = KieServices.Factory.get();
results = new ResultsImpl();
// if pomXML is null it will generate a default, using default ReleaseId
// if pomXml is invalid, it assign pomModel to null
PomModel pomModel = getPomModel();
// if kModuleModelXML is null or invalid it will generate a default kModule, with a default kbase name
buildKieModuleModel();
if (pomModel != null) {
// creates ReleaseId from build pom
// If the pom was generated, it will be the same as teh default ReleaseId
releaseId = pomModel.getReleaseId();
// add all the pom dependencies to this builder ... not sure this is a good idea (?)
KieRepositoryImpl repository = (KieRepositoryImpl) ks.getRepository();
for (AFReleaseId dep : pomModel.getDependencies(DependencyFilter.COMPILE_FILTER)) {
KieModule depModule = repository.getKieModule(adapt(dep), pomModel);
if (depModule != null) {
addKieDependency(depModule);
}
}
} else {
// if the pomModel is null it means that the provided pom.xml is invalid so use the default releaseId
releaseId = KieServices.Factory.get().getRepository().getDefaultReleaseId();
}
return pomModel;
}
use of org.kie.api.KieServices 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.KieServices 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();
}
Aggregations