use of org.drools.core.io.impl.InputStreamResource in project drools by kiegroup.
the class DRLDumperTest method testDumpers.
@Test
public void testDumpers() throws Exception {
final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
final Resource resource = new InputStreamResource(getClass().getResourceAsStream("test_Dumpers.drl"));
final PackageDescr pkg = parser.parse(resource);
if (parser.hasErrors()) {
for (final DroolsError error : parser.getErrors()) {
logger.warn(error.toString());
}
fail(parser.getErrors().toString());
}
KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase(pkg));
KieSession ksession = kbase.newKieSession();
List list = new ArrayList();
ksession.setGlobal("list", list);
final Cheese brie = new Cheese("brie", 12);
ksession.insert(brie);
ksession.fireAllRules();
assertEquals(3, list.size());
assertEquals("3 1", list.get(0));
assertEquals("MAIN", list.get(1));
assertEquals("1 1", list.get(2));
final DrlDumper drlDumper = new DrlDumper();
final String drlResult = drlDumper.dump(pkg);
System.out.println(drlResult);
kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(drlResult));
ksession = kbase.newKieSession();
list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert(brie);
ksession.fireAllRules();
assertEquals(3, list.size());
assertEquals("3 1", list.get(0));
assertEquals("MAIN", list.get(1));
assertEquals("1 1", list.get(2));
}
use of org.drools.core.io.impl.InputStreamResource in project drools by kiegroup.
the class DescrResourceSetTest method drlFilesTest.
@Test
public void drlFilesTest() throws Exception {
Set<File> drlFiles = getDrlFiles();
for (File drl : drlFiles) {
final DrlParser parser = new DrlParser(((KnowledgeBuilderConfigurationImpl) conf).getLanguageLevel());
InputStreamResource resource = new InputStreamResource(new FileInputStream(drl));
PackageDescr pkgDescr = parser.parse(resource);
if (parser.hasErrors()) {
continue;
}
visitor.visit(pkgDescr);
}
logger.debug(drlFiles.size() + " drl tested.");
}
use of org.drools.core.io.impl.InputStreamResource in project drools by kiegroup.
the class DroolsJaxbHelperTest method testXsdModelInRule.
@Test
public void testXsdModelInRule() {
// DRL rule that references object created in XSD
final String s1 = "package test; " + "global java.util.List list; " + "rule Init when " + "then " + " insert( new Sub() ); " + " insert( new Message() ); " + " insert( new Test() ); " + " insert( new Left() ); " + "end\n" + "rule CheckSub when " + " $s : Sub() " + "then " + " list.add( \"Sub\" ); " + "end\n" + "rule CheckMsg when " + " $s : Message() " + "then " + " list.add( \"Message\" ); " + "end\n ";
KieHelper kh = new KieHelper();
kh.addContent(s1, ResourceType.DRL);
// XSD that defines "Sub" class
InputStream simpleXsdStream = getClass().getResourceAsStream(simpleXsdRelativePath);
assertNotNull("Could not find resource: " + simpleXsdRelativePath, simpleXsdStream);
BaseResource xsdResource = new InputStreamResource(simpleXsdStream);
Options xjcOptions = new Options();
xsdResource.setConfiguration(new JaxbConfigurationImpl(xjcOptions, "test-system-id"));
kh.addResource(xsdResource, ResourceType.XSD);
// Verify that build succeeded
assertEquals(0, kh.verify().getMessages(Message.Level.ERROR).size());
assertEquals(0, kh.verify().getMessages(Message.Level.WARNING).size());
// Run rules
KieSession ks = kh.build().newKieSession();
List list = new ArrayList();
ks.setGlobal("list", list);
ks.fireAllRules();
// Verify results
assertEquals(2, list.size());
assertTrue(list.containsAll(asList("Sub", "Message")));
Collection<FactHandle> fhs = ks.getFactHandles();
Iterator<FactHandle> iter = fhs.iterator();
DefaultFactHandle subFh = null, msgFh = null, leftFh = null, testFh = null;
while (iter.hasNext()) {
DefaultFactHandle dfh = (DefaultFactHandle) iter.next();
if (dfh.getObjectClassName().equals("test.Sub")) {
subFh = dfh;
} else if (dfh.getObjectClassName().equals("test.Message")) {
msgFh = dfh;
} else if (dfh.getObjectClassName().equals("test.Left")) {
leftFh = dfh;
} else if (dfh.getObjectClassName().equals("test.Test")) {
testFh = dfh;
} else {
fail("Unexpected FH class: " + dfh.getObjectClassName());
}
}
assertNotNull("No FactHandle for Sub found!", subFh);
assertNotNull("No FactHandle for Message found!", msgFh);
Object xsdObj = subFh.getObject();
Class xsdClass = xsdObj.getClass();
try {
Method m2 = xsdClass.getMethod("getFld");
assertNotNull(m2);
assertEquals(String.class, m2.getReturnType());
assertEquals(0, xsdClass.getFields().length);
java.lang.reflect.Field[] declaredFields = xsdClass.getDeclaredFields();
assertEquals(1, declaredFields.length);
assertEquals("fld", declaredFields[0].getName());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
// "Message" has attribute 'mixed="true"' which means only one field "content"
xsdObj = msgFh.getObject();
xsdClass = xsdObj.getClass();
try {
Method m2 = xsdClass.getMethod("getContent");
assertNotNull(m2);
assertEquals(List.class, m2.getReturnType());
assertEquals(0, xsdClass.getFields().length);
java.lang.reflect.Field[] declaredFields = xsdClass.getDeclaredFields();
assertEquals(1, declaredFields.length);
assertEquals("content", declaredFields[0].getName());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Aggregations