Search in sources :

Example 1 with InputStreamResource

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));
}
Also used : DroolsError(org.drools.compiler.compiler.DroolsError) KieBase(org.kie.api.KieBase) InputStreamResource(org.drools.core.io.impl.InputStreamResource) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) DrlParser(org.drools.compiler.compiler.DrlParser) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.compiler.Cheese) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DrlDumper(org.drools.compiler.lang.DrlDumper) InputStreamResource(org.drools.core.io.impl.InputStreamResource) Test(org.junit.Test)

Example 2 with InputStreamResource

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.");
}
Also used : PackageDescr(org.drools.compiler.lang.descr.PackageDescr) File(java.io.File) FileInputStream(java.io.FileInputStream) InputStreamResource(org.drools.core.io.impl.InputStreamResource) Test(org.junit.Test)

Example 3 with InputStreamResource

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());
    }
}
Also used : Options(com.sun.tools.xjc.Options) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) InputStream(java.io.InputStream) BaseResource(org.drools.core.io.impl.BaseResource) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) Method(java.lang.reflect.Method) Field(org.drools.core.rule.ConsequenceMetaData.Field) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) InputStreamResource(org.drools.core.io.impl.InputStreamResource) JaxbConfigurationImpl(org.drools.core.builder.conf.impl.JaxbConfigurationImpl) Test(org.junit.Test)

Aggregations

InputStreamResource (org.drools.core.io.impl.InputStreamResource)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)2 KieSession (org.kie.api.runtime.KieSession)2 Options (com.sun.tools.xjc.Options)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Method (java.lang.reflect.Method)1 Arrays.asList (java.util.Arrays.asList)1 Cheese (org.drools.compiler.Cheese)1 DrlParser (org.drools.compiler.compiler.DrlParser)1 DroolsError (org.drools.compiler.compiler.DroolsError)1 DrlDumper (org.drools.compiler.lang.DrlDumper)1 JaxbConfigurationImpl (org.drools.core.builder.conf.impl.JaxbConfigurationImpl)1 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)1 BaseResource (org.drools.core.io.impl.BaseResource)1 Field (org.drools.core.rule.ConsequenceMetaData.Field)1