use of org.kie.api.builder.Message in project drools by kiegroup.
the class TypeDeclarationTest method testMultipleAnnotationDeclarations.
@Test
public void testMultipleAnnotationDeclarations() {
String str1 = "";
str1 += "package org.kie1 " + "" + "declare Foo \n" + " name : String " + " age : int " + "end ";
String str2 = "";
str2 += "package org.kie2 " + "" + "declare org.kie1.Foo " + " @role(event) " + "end ";
String str3 = "";
str3 += "package org.kie3 " + "" + "declare org.kie1.Foo " + " @propertyReactive " + "end ";
String str4 = "" + "package org.kie4; " + "import org.kie1.Foo; " + "" + "rule Check " + "when " + " $f : Foo( name == 'bar' ) " + "then " + " modify( $f ) { setAge( 99 ); } " + "end ";
KieHelper helper = new KieHelper();
helper.addContent(str1, ResourceType.DRL);
helper.addContent(str2, ResourceType.DRL);
helper.addContent(str3, ResourceType.DRL);
helper.addContent(str4, ResourceType.DRL);
List<Message> msg = helper.verify().getMessages(Message.Level.ERROR);
System.out.println(msg);
assertEquals(0, msg.size());
KieBase kieBase = helper.build();
FactType type = kieBase.getFactType("org.kie1", "Foo");
assertEquals(2, type.getFields().size());
Object foo = null;
try {
foo = type.newInstance();
type.set(foo, "name", "bar");
assertEquals("bar", type.get(foo, "name"));
} catch (InstantiationException e) {
fail(e.getMessage());
} catch (IllegalAccessException e) {
fail(e.getMessage());
}
KieSession session = kieBase.newKieSession();
FactHandle handle = session.insert(foo);
int n = session.fireAllRules(5);
assertTrue(handle instanceof EventFactHandle);
assertEquals(1, n);
assertEquals(99, type.get(foo, "age"));
}
use of org.kie.api.builder.Message in project drools by kiegroup.
the class IncrementalCompilationTest method testIncrementalCompilationDuplicateXLSResources.
@Test
public void testIncrementalCompilationDuplicateXLSResources() throws Exception {
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
InputStream in1 = null;
InputStream in2 = null;
try {
// Add XLS decision table
in1 = this.getClass().getResourceAsStream("incrementalBuild.dtable.xls");
kfs.write("src/main/resources/incrementalBuild1.dtable.xls", KieServices.Factory.get().getResources().newInputStreamResource(in1));
// Expect no errors
KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
assertEquals(0, kieBuilder.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR).size());
// Add the same XLS decision table again as a different resource
in2 = this.getClass().getResourceAsStream("incrementalBuild.dtable.xls");
kfs.write("src/main/resources/incrementalBuild2.dtable.xls", KieServices.Factory.get().getResources().newInputStreamResource(in2));
IncrementalResults addResults = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/incrementalBuild2.dtable.xls").build();
// Expect duplicate rule errors
assertEquals(1, addResults.getAddedMessages().size());
assertEquals(0, addResults.getRemovedMessages().size());
// Check errors on a full build
List<Message> messages = ks.newKieBuilder(kfs).buildAll().getResults().getMessages();
assertFalse(messages.isEmpty());
} finally {
if (in1 != null) {
in1.close();
}
if (in2 != null) {
in2.close();
}
}
}
use of org.kie.api.builder.Message in project drools by kiegroup.
the class BaseModelTest method createKieBuilder.
protected KieBuilder createKieBuilder(KieServices ks, KieModuleModel model, ReleaseId releaseId, boolean failIfBuildError, KieFile... stringRules) {
ks.getRepository().removeKieModule(releaseId);
KieFileSystem kfs = ks.newKieFileSystem();
if (model != null) {
kfs.writeKModuleXML(model.toXML());
}
kfs.writePomXML(KJARUtils.getPom(releaseId));
for (int i = 0; i < stringRules.length; i++) {
kfs.write(stringRules[i].path, stringRules[i].content);
}
KieBuilder kieBuilder;
if (testRunType == RUN_TYPE.FLOW_DSL) {
kieBuilder = ks.newKieBuilder(kfs).buildAll(ExecutableModelFlowProject.class);
} else if (testRunType == RUN_TYPE.PATTERN_DSL) {
kieBuilder = ks.newKieBuilder(kfs).buildAll(ExecutableModelProject.class);
} else {
kieBuilder = ks.newKieBuilder(kfs).buildAll();
}
if (failIfBuildError) {
List<Message> messages = kieBuilder.getResults().getMessages();
if (!messages.isEmpty()) {
fail(messages.toString());
}
}
return kieBuilder;
}
use of org.kie.api.builder.Message in project drools by kiegroup.
the class BuildFromKJarTest method createKieModule.
private InternalKieModule createKieModule(KieServices ks, ReleaseId releaseId) {
KieFileSystem kfs = ks.newKieFileSystem();
kfs.writeKModuleXML(getDefaultKieModuleModel(ks).toXML());
kfs.writePomXML(KJARUtils.getPom(releaseId));
String javaSrc = Person.class.getCanonicalName().replace('.', '/') + ".java";
Resource javaResource = ks.getResources().newFileSystemResource("src/test/java/" + javaSrc);
kfs.write("src/main/java/" + javaSrc, javaResource);
kfs.write("src/main/resources/rule.drl", getRule());
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
List<Message> messages = ((KieBuilderImpl) kieBuilder).buildAll(ExecutableModelFlowProject.class).getResults().getMessages();
if (!messages.isEmpty()) {
fail(messages.toString());
}
return (InternalKieModule) kieBuilder.getKieModule();
}
use of org.kie.api.builder.Message in project drools by kiegroup.
the class KieUtil method getKieBuilderFromFileSystemWithResources.
private static KieBuilder getKieBuilderFromFileSystemWithResources(final KieBaseTestConfiguration kieBaseTestConfiguration, final KieFileSystem kfs, final boolean failIfBuildError, final Resource... resources) {
for (Resource res : resources) {
kfs.write(res);
}
KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);
final Class<? extends KieBuilder.ProjectType> projectClass;
switch(kieBaseTestConfiguration.runType()) {
case STANDARD_FROM_DRL:
projectClass = DrlProject.class;
break;
case FLOW_DSL:
projectClass = ExecutableModelFlowProject.class;
break;
case PATTERN_DSL:
projectClass = ExecutableModelProject.class;
break;
default:
throw new RuntimeException("Unknown type: " + kieBaseTestConfiguration.runType());
}
kbuilder.buildAll(projectClass);
// Messages from KieBuilder with increasing severity
List<Message> msgs = kbuilder.getResults().getMessages(Message.Level.INFO);
if (msgs.size() > 0) {
LOGGER.info("KieBuilder information: {}", msgs.toString());
}
msgs = kbuilder.getResults().getMessages(Message.Level.WARNING);
if (msgs.size() > 0) {
LOGGER.warn("KieBuilder warnings: {}", msgs.toString());
}
msgs = kbuilder.getResults().getMessages(Message.Level.ERROR);
if (msgs.size() > 0) {
LOGGER.error("KieBuilder errors: {}", msgs.toString());
}
if (failIfBuildError) {
Assertions.assertThat(msgs).as(msgs.toString()).isEmpty();
}
return kbuilder;
}
Aggregations