Search in sources :

Example 1 with JaxbConfigurationImpl

use of org.drools.core.builder.conf.impl.JaxbConfigurationImpl 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 ";
    List<Resource> resources = KieUtil.getResourcesFromDrls(s1);
    // XSD that defines "Sub" class
    InputStream simpleXsdStream = getClass().getResourceAsStream(simpleXsdRelativePath);
    assertNotNull("Could not find resource: " + simpleXsdRelativePath, simpleXsdStream);
    BaseResource xsdResource = new InputStreamResource(simpleXsdStream);
    xsdResource.setResourceType(ResourceType.XSD);
    xsdResource.setSourcePath(TestConstants.TEST_RESOURCES_FOLDER + simpleXsdRelativePath);
    Options xjcOptions = new Options();
    xsdResource.setConfiguration(new JaxbConfigurationImpl(xjcOptions, "test-system-id"));
    resources.add(xsdResource);
    KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, resources.toArray(new Resource[] {}));
    KieSession ks = kbase.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) BaseResource(org.drools.core.io.impl.BaseResource) InputStreamResource(org.drools.core.io.impl.InputStreamResource) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) InputStreamResource(org.drools.core.io.impl.InputStreamResource) JaxbConfigurationImpl(org.drools.core.builder.conf.impl.JaxbConfigurationImpl) Test(org.junit.Test)

Example 2 with JaxbConfigurationImpl

use of org.drools.core.builder.conf.impl.JaxbConfigurationImpl 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)

Example 3 with JaxbConfigurationImpl

use of org.drools.core.builder.conf.impl.JaxbConfigurationImpl in project drools by kiegroup.

the class DroolsJaxbHelperProviderImpl method addPackageFromXSD.

public static void addPackageFromXSD(KnowledgeBuilder kBuilder, Resource resource, ResourceConfiguration configuration) throws IOException {
    if (configuration instanceof JaxbConfigurationImpl) {
        JaxbConfigurationImpl jaxbConf = (JaxbConfigurationImpl) configuration;
        String[] classes = DroolsJaxbHelperProviderImpl.addXsdModel(resource, (KnowledgeBuilderImpl) kBuilder, jaxbConf.getXjcOpts(), jaxbConf.getSystemId());
        for (String cls : classes) {
            jaxbConf.getClasses().add(cls);
        }
    }
}
Also used : JaxbConfigurationImpl(org.drools.core.builder.conf.impl.JaxbConfigurationImpl)

Aggregations

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