XStream is a simple library to serialize objects to XML and back again.

Sometimes it can get tedious to call all those XStream aliases/register converter methods or you might simply like the new trend on configuring POJOs: Java annotations.

While using annotations if you ever got this exception the there might be a change that you forgot to call process annotation

    XStream stream = new XStream(new DomDriver());
    stream.processAnnotations(YourClassName.class);

Calling stream.processAnnotations(YourClassName.class); will clear down this exception.



On Tue, May 29, 2012 at 11:40 PM, Francois Rouaix <francois.rouaix@gmail.com> wrote:
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


_______________________________________________
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users