Search in sources :

Example 1 with BaseResource

use of org.drools.core.io.impl.BaseResource in project drools by kiegroup.

the class KnowledgeBuilderImpl method add.

public void add(Resource resource, ResourceType type) {
    ResourceConfiguration resourceConfiguration = resource instanceof BaseResource ? resource.getConfiguration() : null;
    add(resource, type, resourceConfiguration);
}
Also used : BaseResource(org.drools.core.io.impl.BaseResource) ResourceConfiguration(org.kie.api.io.ResourceConfiguration)

Example 2 with BaseResource

use of org.drools.core.io.impl.BaseResource 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

BaseResource (org.drools.core.io.impl.BaseResource)2 Options (com.sun.tools.xjc.Options)1 InputStream (java.io.InputStream)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1 JaxbConfigurationImpl (org.drools.core.builder.conf.impl.JaxbConfigurationImpl)1 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)1 InputStreamResource (org.drools.core.io.impl.InputStreamResource)1 Field (org.drools.core.rule.ConsequenceMetaData.Field)1 Test (org.junit.Test)1 ResourceConfiguration (org.kie.api.io.ResourceConfiguration)1 KieSession (org.kie.api.runtime.KieSession)1 FactHandle (org.kie.api.runtime.rule.FactHandle)1 KieHelper (org.kie.internal.utils.KieHelper)1