use of org.drools.compiler.kie.builder.impl.KieFileSystemImpl 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.drools.compiler.kie.builder.impl.KieFileSystemImpl in project drools by kiegroup.
the class KieBuilderTest method testNoPomXml.
@Test
public void testNoPomXml() throws ClassNotFoundException, InterruptedException, IOException {
String namespace = "org.kie.test";
KieModuleModel kProj = createKieProject(namespace);
ReleaseId releaseId = KieServices.Factory.get().getRepository().getDefaultReleaseId();
KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
generateKProjectXML(kfs, namespace, kProj);
generateMessageClass(kfs, namespace);
generateRule(kfs, namespace);
MemoryFileSystem mfs = ((KieFileSystemImpl) kfs).asMemoryFileSystem();
createAndTestKieContainer(releaseId, createKieBuilder(kfs), namespace);
}
use of org.drools.compiler.kie.builder.impl.KieFileSystemImpl in project drools by kiegroup.
the class KieBuilderTest method testNoProjectXml.
@Test
public void testNoProjectXml() throws ClassNotFoundException, InterruptedException, IOException {
String namespace = "org.kie.test";
ReleaseId releaseId = KieServices.Factory.get().newReleaseId(namespace, "memory", "1.0-SNAPSHOT");
KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
generatePomXML(kfs, releaseId);
generateMessageClass(kfs, namespace);
generateRule(kfs, namespace);
MemoryFileSystem mfs = ((KieFileSystemImpl) kfs).asMemoryFileSystem();
createAndTestKieContainer(releaseId, createKieBuilder(kfs), null);
}
use of org.drools.compiler.kie.builder.impl.KieFileSystemImpl in project drools by kiegroup.
the class KieBuilderTest method testNoPomAndProjectXml.
@Test
public void testNoPomAndProjectXml() throws ClassNotFoundException, InterruptedException, IOException {
String namespace = "org.kie.test";
ReleaseId releaseId = KieServices.Factory.get().getRepository().getDefaultReleaseId();
KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
generateMessageClass(kfs, namespace);
generateRule(kfs, namespace);
MemoryFileSystem mfs = ((KieFileSystemImpl) kfs).asMemoryFileSystem();
createAndTestKieContainer(releaseId, createKieBuilder(kfs), null);
}
use of org.drools.compiler.kie.builder.impl.KieFileSystemImpl in project drools by kiegroup.
the class KieRepositoryScannerTest method testKScannerWithRange_withListener_errors.
@Test
public void testKScannerWithRange_withListener_errors() throws Exception {
KieServices ks = KieServices.Factory.get();
KieMavenRepository repository = getKieMavenRepository();
String artifactId = "scanner-range-test-withlistener-errors";
ReleaseId releaseId1 = ks.newReleaseId("org.kie", artifactId, "1.0.1");
ReleaseId releaseId2 = ks.newReleaseId("org.kie", artifactId, "1.0.2");
// Because KieScanner also check the local repo, ensure the artifact are not deployed on the local maven repo
repository.removeLocalArtifact(releaseId1);
repository.removeLocalArtifact(releaseId2);
ReleaseId releaseRange = ks.newReleaseId("org.kie", artifactId, "[1.0.0,)");
InternalKieModule kJar1 = createKieJar(ks, releaseId1, "rule1", "rule2");
KieContainer kieContainer = ks.newKieContainer(releaseRange);
List<KieScannerEvent> events = new ArrayList<>();
InternalKieScanner scanner = (KieRepositoryScannerImpl) ks.newKieScanner(kieContainer);
KieScannerEventListener listener = new KieScannerEventListener() {
public void onKieScannerStatusChangeEvent(KieScannerStatusChangeEvent statusChange) {
LOG.info("KieScanner event: {}", statusChange);
events.add(statusChange);
}
public void onKieScannerUpdateResultsEvent(KieScannerUpdateResultsEvent updtaeResults) {
LOG.info("KieScanner event: {}", updtaeResults);
events.add(updtaeResults);
}
};
scanner.addListener(listener);
scanner.scanNow();
// Because 1.0.2 does not exist, will perform a scan but will NOT update.
assertThat(events, CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.SCANNING)));
assertThat(events, CoreMatchers.not(CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.UPDATING))));
events.clear();
repository.installArtifact(releaseId1, kJar1, createKPom(fileManager, releaseId1));
KieSession ksession = kieContainer.newKieSession("KSession1");
checkKSession(ksession, "rule1", "rule2");
// Create a wrong KJAR for releaseId2
KieFileSystem kfs = ks.newKieFileSystem();
String pomContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n" + " <modelVersion>4.0.0</modelVersion>\n" + "\n" + " <groupId>" + releaseId2.getGroupId() + "</groupId>\n" + " <artifactId>" + releaseId2.getArtifactId() + "</artifactId>\n" + " <version>" + releaseId2.getVersion() + "</version></project>\n";
kfs.writePomXML(pomContent);
kfs.write("rulebroken" + ".drl", "rule X when asd asd then end");
kfs.write("META-INF/kmodule.xml", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + " <!-- This is a minimal kmodule.xml. Ref Drools documentation, see http://docs.jboss.org/drools/release/6.1.0.Final/drools-docs/html_single/index.html#d0e1112 -->\n" + "</kmodule>\n");
File tempJar = File.createTempFile("org.kie" + artifactId + "1.0.2", "tmp");
LOG.info("Writing to temp file: {}", tempJar);
// please notice that writeAsJar would return a new file with .jar postfixed
tempJar = ((KieFileSystemImpl) kfs).asMemoryFileSystem().writeAsJar(tempJar.getParentFile(), tempJar.getName());
File tempPom = File.createTempFile("org.kie" + artifactId + "1.0.2", "tmp.xml");
LOG.info("Writing to temp file: {}", tempPom);
FileUtils.writeStringToFile(tempPom, pomContent, "UTF-8");
repository.installArtifact(releaseId2, tempJar, tempPom);
assertEquals(releaseId1, scanner.getCurrentReleaseId());
assertEquals(InternalKieScanner.Status.STOPPED, scanner.getStatus());
// scan the maven repo to get the new kjar version and deploy it on the kcontainer
scanner.scanNow();
// Keeping at previous release
assertEquals(releaseId1, scanner.getCurrentReleaseId());
assertEquals(InternalKieScanner.Status.STOPPED, scanner.getStatus());
// there should be no update performed.
assertThat(events, CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.SCANNING)));
assertThat(events, CoreMatchers.hasItem(new KieScannerStatusChangeEventImpl(KieScanner.Status.UPDATING)));
assertTrue(events.get(2) instanceof KieScannerUpdateResultsEventImpl);
assertTrue(((KieScannerUpdateResultsEventImpl) events.get(2)).getResults().hasMessages(Message.Level.ERROR));
events.clear();
ks.getRepository().removeKieModule(releaseId1);
ks.getRepository().removeKieModule(releaseId2);
scanner.removeListener(listener);
assertEquals(0, scanner.getListeners().size());
repository.removeLocalArtifact(releaseId1);
repository.removeLocalArtifact(releaseId2);
}
Aggregations