|
|
|
Hemanth kumar
|
(This post was updated on )
hi,
Im working on a sample test project. In that im dynamically creating a class ( fact) and compiling it. here is my test project -------------------------------------------------------------------------------------- mainJavaclass public void runRule() { try{ System.out.println("Initializing Fact...."); URL[] urls = new URL[]{ new URL("file://"+path) }; ClassLoader loader = new URLClassLoader(urls); Class clazz = loader.loadClass("test.Message"); //Class.forName("test.Message", false, ucl); Object factObj = clazz.newInstance(); String ruleFile = "test/Sample.drl"; Method method = clazz.getMethod("setMessage", new Class[]{String.class}); Method method1 = clazz.getMethod("getMessage"); //System.out.println("facts loaded\n"); method.invoke(factObj, new Object[]{"Hello"}); System.out.println("initializing packageBuilder"); PackageBuilderConfiguration config = new PackageBuilderConfiguration(); config.setClassLoader(loader); KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(config); System.out.println("finding Rule"); kbuilder.add(ResourceFactory.newClassPathResource("test/Sample.drl"),ResourceType.DRL); if (kbuilder.hasErrors()) throw new RuntimeException("Unable to compile rules. " + kbuilder.getErrors().toString()); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(new RuleBaseConfiguration(loader)); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); StatelessKnowledgeSession session = kbase.newStatelessKnowledgeSession(); System.out.println("running rule....... \n"); session.execute(factObj); System.out.println("\nend...\n"); System.out.println(method1.invoke(factObj).toString()); } catch (Throwable t) { t.printStackTrace(); } } ---------------------------------------------------------------------- Fact package test; public class Message{ private String message; public String getMessage(){ return this.message; } public void setMessage(String message) { this.message = message; } } ------------------------------------------------------------------------ sample rule package test import test.Message; rule "Your First Rule" dialect "mvel" when m:Message(message != "Good Bye" ) then System.out.println("First Rule fired "+m.message ); modify(m){ message = "Good Bye"}; end ----------------------------------------------------------------- console.PNG what happens is when i was running the project inside eclipse IDE it works fine but when i hosted in tomcat and calling from outside ide the rule is not getting fired. I think the dyanamically loaded class is not recognised by the rule engine. suggest me any ideas or post an working example Hemanth |
|||||||||||||||
|
Swapnil Raverkar
|
Are you inserting the instance of the dynamically loaded class into the KnowledgeBase?
i.e. session.insert(dynamicInstance); If not the rule won't get fired. Cheers, Swapnil
2009/11/3 Hemanth kumar <[hidden email]>
_______________________________________________ rules-users mailing list [hidden email] https://lists.jboss.org/mailman/listinfo/rules-users |
|
Hemanth kumar
|
swapnil thanx for the reply,
this is my modified code try{ System.out.println("Initializing Fact...."); URL[] urls = new URL[]{ new URL("file://"+path) }; ClassLoader loader = new URLClassLoader(urls); Class clazz = loader.loadClass("test.Message"); //Class.forName("test.Message", false, ucl); Object factObj = clazz.newInstance(); String ruleFile = "test/Sample.drl"; Method method = clazz.getMethod("setMessage", new Class[]{String.class}); Method method1 = clazz.getMethod("getMessage"); //System.out.println("facts loaded\n"); method.invoke(factObj, new Object[]{"Hello"}); System.out.println("initializing packageBuilder"); PackageBuilderConfiguration config = new PackageBuilderConfiguration(); config.setClassLoader(loader); KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(config); System.out.println("finding Rule"); kbuilder.add(ResourceFactory.newClassPathResource("test/Sample.drl"),ResourceType.DRL); if (kbuilder.hasErrors()) throw new RuntimeException("Unable to compile rules. " + kbuilder.getErrors().toString()); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(new RuleBaseConfiguration(loader)); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); StatelessKnowledgeSession session = kbase.newStatelessKnowledgeSession(); System.out.println("running rule....... \n"); session.execute(factObj); System.out.println("\nend...\n"); System.out.println(method1.invoke(factObj).toString()); } catch (Throwable t) { t.printStackTrace(); } thanx and regards Hemanth 2009/11/3 Swapnil Raverkar <[hidden email]> Are you inserting the instance of the dynamically loaded class into the KnowledgeBase? -- regards Hemanth kumar _______________________________________________ rules-users mailing list [hidden email] https://lists.jboss.org/mailman/listinfo/rules-users |
||||||||||||||||
|
Sachint
|
In reply to this post
by Hemanth kumar
Hi Hemant,
In eclipse we have configured drools runtime environment which contains the required jars, i assume you have provided them to tomcat. Regards, Sachin
|
|||||||||||||||
|
Hemanth kumar
|
In reply to this post
by Swapnil Raverkar
swapnil thanx for the reply,
this is my modified code try{ System.out.println("Initializing Fact...."); URL[] urls = new URL[]{ new URL("file://"+path) }; ClassLoader loader = new URLClassLoader(urls); Class clazz = loader.loadClass("test.Message"); //Class.forName("test.Message", false, ucl); Object factObj = clazz.newInstance(); String ruleFile = "test/Sample.drl"; Method method = clazz.getMethod("setMessage", new Class[]{String.class}); Method method1 = clazz.getMethod("getMessage"); //System.out.println("facts loaded\n"); method.invoke(factObj, new Object[]{"Hello"}); System.out.println("initializing packageBuilder"); PackageBuilderConfiguration config = new PackageBuilderConfiguration(); config.setClassLoader(loader); KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(config); System.out.println("finding Rule"); kbuilder.add(ResourceFactory.newClassPathResource("test/Sample.drl"),ResourceType.DRL); if (kbuilder.hasErrors()) throw new RuntimeException("Unable to compile rules. " + kbuilder.getErrors().toString()); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(new RuleBaseConfiguration(loader)); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); StatelessKnowledgeSession session = kbase.newStatelessKnowledgeSession(); System.out.println("running rule....... \n"); session.execute(factObj); System.out.println("\nend...\n"); System.out.println(method1.invoke(factObj).toString()); } catch (Throwable t) { t.printStackTrace(); } ------------------------------------------------------------ Added the following JARS in WEB-INF\lib antlr-runtime-3.1.1.jar core-3.4.2.v_883_R34x.jar drools-api-5.0.1.jar drools-core-5.0.1.jar drools-decisiontables-5.0.1.jar mvel2-2.0.10.jar drools-compiler-5.0.1.jar xstream-1.3.1.jar thanx and regards Hemanth |
|||||||||||||||
|
Swapnil Raverkar
|
URL[] urls = new URL[]{ new URL("file://"+path) Are you using relative or absolute paths for classLoaders? Did you check that the paths are valid when you deploy your application in Tomcat container? Cheers, Swapnil 2009/11/4 Hemanth kumar <[hidden email]>
_______________________________________________ rules-users mailing list [hidden email] https://lists.jboss.org/mailman/listinfo/rules-users |
||||||||||||||||
|
Hemanth kumar
|
Hi swapnil thanx for the reply
I think there is no error in url path.for a week i was not active in my project. Im sending my sample project Here is my sample application --------------------------------------------------------------------- main java class ruleTest.txt sample rule Sample.drl tomcat console console1.PNG Added following jars in tomcat/lib ( actual path : C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\JStudio\WEB-INF\lib) antlr-runtime-3.1.1.jar core-3.4.2.v_883_R34x.jar drools-api-5.0.1.jar drools-compiler-5.0.1.jar drools-core-5.0.1.jar drools-jsr94-5.0.1.jar drools-decisiontables-5.0.1.jar mvel2-2.0.14.jar xstream-1.3.1.jar suggest me any ideas |
|||||||||||||||
|
leo_gomes
|
Hi Hemanth,
If you're looking for dynamic creation of Facts, you should have a look here: http://downloads.jboss.com/drools/docs/5.0.1.26597.FINAL/drools-expert/html_single/index.html#d0e3350 HTH, Leo. On Mon, Nov 9, 2009 at 8:37 AM, Hemanth kumar <[hidden email]> wrote: > > Hi swapnil thanx for the reply > I think there is no error in url path.for a week i was not active in my > project. > Im sending my sample project > > Here is my sample application > --------------------------------------------------------------------- > main java class > http://old.nabble.com/file/p26262304/ruleTest.txt ruleTest.txt > > sample rule > > http://old.nabble.com/file/p26262304/Sample.drl Sample.drl > > tomcat console > > http://old.nabble.com/file/p26262304/console1.PNG console1.PNG > > > Added following jars in tomcat/lib > ( actual path : C:\Program Files\Apache Software Foundation\Tomcat > 6.0\webapps\JStudio\WEB-INF\lib) > > antlr-runtime-3.1.1.jar > core-3.4.2.v_883_R34x.jar > drools-api-5.0.1.jar > drools-compiler-5.0.1.jar > drools-core-5.0.1.jar > drools-jsr94-5.0.1.jar > drools-decisiontables-5.0.1.jar > mvel2-2.0.14.jar > xstream-1.3.1.jar > > suggest me any ideas > -- > View this message in context: http://old.nabble.com/Class-loader-problem-tp26160051p26262304.html > Sent from the drools - user mailing list archive at Nabble.com. > > _______________________________________________ > rules-users mailing list > [hidden email] > https://lists.jboss.org/mailman/listinfo/rules-users > rules-users mailing list [hidden email] https://lists.jboss.org/mailman/listinfo/rules-users |
||||||||||||||||
|
isterin
|
In reply to this post
by Hemanth kumar
Hemanth, I had a similar issue, here is a blog post...
http://www.ilyasterin.com/blog/2009/10/java-multiple-class-loaders-issue.html Hope this helps. On Tue, Nov 3, 2009 at 5:27 AM, Hemanth kumar <[hidden email]> wrote: > > hi, > Im working on a sample test project. > In that im dynamically creating a class ( fact) and compiling it. > > here is my test project > -------------------------------------------------------------------------------------- > mainJavaclass > > > public void runRule() > { > try { > System.out.println("\nRunning rule\n"); > > // go ! > > URL[] urls = new URL[]{ new > URL("file://"+path) }; > > URLClassLoader ucl = new > URLClassLoader(urls); > Class<?> clazz = > ucl.loadClass("test.Message"); > Object classObj = clazz.newInstance(); > > > Method method = > clazz.getDeclaredMethod("setMessage", new Class[]{String.class}); > > //System.out.println("facts loaded\n"); > > method.invoke(classObj, new > Object[]{"Hello"}); > > > log.info("==== Calling Rule Runner ======"); > > Collection facts = new ArrayList(); > facts.add(classObj); > > > // Load and fire our rules files against the > data > new > RuleRunner().runStatelessRules(RULES_FILES, null, facts, null, null, > logger); > > } > catch (Throwable t) { > t.printStackTrace(); > } > > } > > ---------------------------------------------------------------------- > Fact > > package test; > public class Message{ > private String message; > public String getMessage(){ > return this.message; > } > > public void setMessage(String message) { > this.message = message; > } > > } > > > ------------------------------------------------------------------------ > sample rule > > package test > import test.Message; > > rule "Your First Rule" > dialect "mvel" > when > m:Message(message != "Good Bye" ) > then > System.out.println("First Rule fired "+m.message ); > modify(m){ message = "Good Bye"}; > end > ----------------------------------------------------------------- > http://old.nabble.com/file/p26160051/console.PNG console.PNG > > what happens is when i was running the project inside eclipse IDE it works > fine but when i hosted in tomcat and calling from outside ide the rule is > not getting fired. > I think the dyanamically loaded class is not recognised by the rule engine. > > suggest me any ideas or post an working example > > Hemanth > -- > View this message in context: http://old.nabble.com/Class-loader-problem-tp26160051p26160051.html > Sent from the drools - user mailing list archive at Nabble.com. > > _______________________________________________ > rules-users mailing list > [hidden email] > https://lists.jboss.org/mailman/listinfo/rules-users > _______________________________________________ rules-users mailing list [hidden email] https://lists.jboss.org/mailman/listinfo/rules-users |
||||||||||||||||
| Free Embeddable Forum Powered by Nabble | Help |