[rules-users] Class loader problem
Hemanth Yerra
hemanth at saha.in
Tue Nov 3 07:26:14 EST 2009
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 <swapnil.raverkar at gmail.com>
> 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 <hemanth at saha.in>
>
>
>> 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
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
--
regards
Hemanth kumar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20091103/9f41fe08/attachment.html
More information about the rules-users
mailing list