[rules-users] Inserting facts using declared types from XML

Francois Rouaix francois.rouaix at gmail.com
Tue May 29 14:10:57 EDT 2012


Folks,
I'm trying to use Drools in a mostly Java-less environment (rules are
written in .drl files, facts will be inserted from XML files). I'm running
into an issue where the XStream unmarshaller is unable to convert my XML to
a BatchExecutionCommand because it cannot find the class for a type
declared in the .drl file.
Is there any configuration required to make sure the XStream marshaller
knows about the dynamically generated classes from the .drl?
I'd appreciated any clues. I cannot add POJO classes for my types due to
the nature of the app.
Thanks.


DRL file
-----------
package com.example
declare Answers
....
end
/* some rules after this */
...

XML file
-----------
<batch-execution>
  <insert>
   <com.example.Answers>
       ....


Runner code
public static void Run(String xmlFactsFile, String drlFilename)
{
// Compile the drl file
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add( ResourceFactory.newFileResource(drlFilename),
ResourceType.DRL );
if ( kbuilder.hasErrors() ) {
System.err.println( kbuilder.getErrors().toString() );
throw new RuntimeException("Cannot compile \"" + drlFilename + "\"");
}
 // kbase setup from the compiled rules
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
 StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
File facts = new File(xmlFactsFile);
BatchExecutionCommand command = (BatchExecutionCommand)
BatchExecutionHelper.newXStreamMarshaller().fromXML(facts);
ExecutionResults bresults = ksession.execute(command);
System.out.println(BatchExecutionHelper.newXStreamMarshaller().toXML(bresults));
}
}

Error at runtime
----------------------
Exception in thread "main"
com.thoughtworks.xstream.converters.ConversionException:
com.example.Answers : com.example.Answers
---- Debugging information ----
message             : com.example.Answers
cause-exception     :
com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message       : com.example.Answers
class               : org.drools.command.runtime.rule.InsertObjectCommand
required-type       : org.drools.command.runtime.rule.InsertObjectCommand
converter-type      :
org.drools.runtime.help.impl.XStreamXML$InsertConverter
path                : /batch-execution/insert/com.example.Answers
line number         : 3
class[1]            : org.drools.command.runtime.BatchExecutionCommandImpl
converter-type[1]   :
com.thoughtworks.xstream.converters.reflection.ReflectionConverter
version             : null
-------------------------------
        at
com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
        at
com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
        at
com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
        at
com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
        at
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:318)
        at
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:230)
        at
com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
        at
com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
        at
com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
        at
com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
        at
com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
        at
com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
        at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1035)
        at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1019)
        at com.thoughtworks.xstream.XStream.fromXML(XStream.java:930)
        at
com.intentsoft.RulesEngine.RulesExecution.Run(RulesExecution.java:34)
        at com.intentsoft.RulesEngine.Main.main(Main.java:10)
Caused by: com.thoughtworks.xstream.mapper.CannotResolveClassException:
com.example.Answers
        at
com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:56)
        at
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
        at
com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55)
        at
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
        at
com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88)
        at
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
        at
com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:79)
        at
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
        at
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
        at
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
        at
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
        at
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
        at
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
        at
com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:74)
        at
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
        at
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
        at
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
        at
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
        at
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
        at
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
        at
com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
        at
com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:45)
        at
com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29)
        at
com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:70)
        at
org.drools.runtime.help.impl.XStreamXML$InsertConverter.unmarshal(XStreamXML.java:135)
        at
com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
        ... 16 more
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20120529/720ede56/attachment.html 


More information about the rules-users mailing list