[rules-users] Inserting facts using declared types from XML
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
Re: [rules-users] Inserting facts using declared types from XML
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.rou...@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