Rules formulation question
by Armaghan Mahmud
I'm new to the mailing list so I'll start off by saying hello to everyone.
I've recently started using DROOLS and I seem to have hit a dead end. I'm
trying to pass in an enum and a list into the working memory one at a time
but I would like my rule to first see if it the enum matches a hard coded
enum value. Additionally, I would also like the rule to see if the enum
value that is being passed to the working memory happens to fall in the list
or not. First of all, is it possible to do that in one rule? I've been
trying to do that using "&&" and "contains" keywords but I'm not getting
anywhere. I don't have the code in front of me or I would've pasted it but I
hope you can understand my point. Any help that I can get in this matter
will be greatly appreciated.
Thanks,
Armaghan
15 years, 7 months
can't get samples to work from Drools 5 drools-doc-expert
by Darrin Mison
Hi, I'm very new to Drools and I'm trying to get code running based on
the first sample code from the QuickStart chapter of the drools-doc-
expert.
It compiles and runs (tried on commandline & in JBoss Dev Studio
2.0.0.GA) but does not work, ie it always says "rejected" instead of
"accepted".
I've had to fill out a bit of code to get it to compile so I may be
doing something completely wrong. I've included the code (rule, model,
main) below.
Any help would be really appreciated.
Darrin
#Sample.drl
package com.company.license
rule "Is of valid age"
when
$a : Applicant( age > 18);
then
$a.setValid( true );
System.out.println("true");
end
//Applicant.java
package com.company.license;
public class Applicant
{
private String name;
private int Age;
private boolean valid;
// getter and setter methods here
public String getName() { return name; }
public int getAge() { return Age; }
public boolean getValid(){ return valid; }
public void setName(String s){ this.name = s; }
public void setAge(int i){ this.Age = i; }
public void setValid(boolean v){ this.valid = v; }
}
//ruletest.java
package com.company.license;
import org.drools.builder.*;
import org.drools.io.*;
import org.drools.runtime.*;
import org.drools.*;
public class ruletest
{
public ruletest(){}
public static void main(String args[])
{
ruletest rt = new ruletest();
rt.go();
}
public void go()
{
Applicant dude = new Applicant();
dude.setName("Rufus");
dude.setAge(32);
System.out.println(dude.getName());
System.out.println("test: "+dude.getValid());
KnowledgeBuilder kbuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder();
Resource ruleResource =
ResourceFactory.newClassPathResource( "Sample.drl", getClass() );
kbuilder.add( ruleResource, ResourceType.DRL );
if ( kbuilder.hasErrors() )
System.err.println( kbuilder.getErrors().toString() );
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
StatelessKnowledgeSession ksession =
kbase.newStatelessKnowledgeSession();
System.out.println( dude.getValid() ); //should display false
ksession.execute( dude );
if (dude.getValid())
System.out.println("Application approved"); //should display due
to hardcoded as of 32
else
System.out.println("Application rejected");
}
}
15 years, 7 months
Hibernate proxy objects equality tests failing in Drools 5
by CK
Hi,
I'm testing out some simple codes using Hibernate and Drools 5 and I'm
hitting an issue with hibernate proxy objects.
With the newer version of hibernate, Javassist is used to proxy some
hibernate objects. By doing this, hibernate objects essentially
cannot use == comparisons, which is fine.
However, this is causing issue in equality checks within Drools 5 LHS
evaluations of hibernate persistent objects.
Let's say for example I have the following Hibernate objects:
class Parent {
Child child;
}
class Child {
String name;
}
normally, if you output Parent.child, it should be Child.class. But
under Hibernate, the child property in Parent may be proxied and
enhanced with Javassist, thus making Parent.child.class to be
something like "Child_$$_javassist_75".
Inside Drools, in the LHS:
when
$child : Child()
$parent : Parent(child == $child)
then
...
end
Assume that I have insert separate Child instances here that were
retrieved from an Hibernate query directly. Assume that I have also
inserted Parent instances that were retrieved from a separate query
without eager fetching. Given this particular case, the above rule
would not match even though it technically should. Under normal java
code, using equality, the parent's child is equal to the child.
According to MVEL language, which Drools LHS may not necessarily be
using, equality between two objects are compared using values. Not
sure exactly what that means.
Either way, this seems to be a rather unfortunate problem for using
Drools to work with Hibernate objects. Older hibernate versions
didn't cause this problem.
It seemed like jBPM had a similar problem according to the following
doc:
http://www.jboss.org/community/docs/DOC-11169
Excerpt:
Object identity and object equality
MAKE SURE TO USE ALWAYS .equals AND NEVER == comparisons in the Java
code.
Typically, in the PVM Java objects, we want to use the default object
identity as the object equality. But hibernate persistence breaks
this (even within the scope of one hibernate session) because of the
proxies. So we have to apply a trick in order to make the comparison
between a persistent object and its proxy return true. See jBPM 3
class EqualsUtil for that trick.
So my question is whether Drools uses equality or identity when it
comes to comparing values in LHS constraints. Is there a way to get
around this issue?
-Chris
15 years, 7 months
Guvnor stuck when opening web based decision table - any hints?
by Ingomar Otter
When opening a web-based decision table my 5.0.0CR1 guvnor gets stuck
at "Loading Asset...".
There is no error in the logfile.
That specific server/browser combination used to work until I did
'some' change to the table. ;-)
Any idea how I can track this down?
Cheers,
Ingomar
--
Ingomar Otter
CTO
mailto:ingomar.otter@valtech.de
Mobile: +49 172 2367867
Valtech GmbH
Bahnstraße 16
40212 Düsseldorf
Germany
Phone: +49 211 179237-0
Fax: +49 211 179237-19
http://www.valtech.de
Geschäftsführer: Ingo Kriescher
Amtsgericht Düsseldorf HRB48672
15 years, 7 months
Doubts with a own example
by Kevin Alonso
#created on: 17-abr-2009
package src.main.rules
dialect "mvel"
#list any import classes here.
import hangman.Hangman.RandomWord;
import hangman.Hangman.GuessedWord;
import hangman.Hangman.GameRules;
import hangman.Hangman.Solution;
import java.io.InputStreamReader;
import java.io.BufferedReader;
#declare any global variables here
rule "Letter or Solve"
ruleflow-group "Decision"
no-loop
when
rules : GameRules(g : allowedGuesses , s : allowedSolves)
GuessedWord( guess : guessedWord , l : letters )
then
//Erase, if it exists, the previous value of decision
//modify ( rules ) { decision = null }
System.out.println("\n WORD:");
System.out.println(guess + "\n");
System.out.println( "Said Letters: "+ l +"\n");
System.out.println( "*You still have "+ g +" tries to guess a letter and "+ s + " tries to solve the word.\n");
System.out.println( "Press \"X\" to exit, \"S\" to solve the word or \"L\" to guess a new Letter");
br = new BufferedReader( new InputStreamReader( System.in ) );
i = br.readLine().toUpperCase();
modify ( rules ) { decision = i }
end
rule "Letter"
ruleflow-group "Letter"
no-loop
when
rules : GameRules( allowedGuesses > 0 )
guess : GuessedWord()
then
modify ( rules ) { allowedGuesses -= 1 }
System.out.println( "Write a Letter:");
br = new BufferedReader( new InputStreamReader( System.in ) );
l = br.readLine().toUpperCase();
System.out.println( l );
System.out.println( guess.guessedWord);
guess.sayOneLetter(l);
System.out.println( guess.guessedWord);
System.out.println( guess.letters);
end
rule "Enter Solution"
ruleflow-group "Solve"
no-loop
when
rules : GameRules( allowedSolves > 0 )
sol : Solution()
RandomWord(word : randomWord)
then
modify ( rules ) { allowedSolves -= 1 }
System.out.println( "Write the solution:");
br = new BufferedReader( new InputStreamReader( System.in ) );
i = br.readLine().toUpperCase();
System.out.println( "You have written"+i );
System.out.println( "SSHHH DonŽt say it to anyone:"+word );
System.out.println( word.compareTo(i));
modify ( sol ) { solution = i }
System.out.println( sol.solution);
end
package hangman;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;
/**
* This is a sample file to launch a process.
*/
public class Hangman {
public static final void main(String[] args) {
// try {
// // load up the knowledge base
// KnowledgeBase kbase = readKnowledgeBase();
// StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
// KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "Hangman");
//
// //Insert classes into a session to allow the management of those
// RandomWord rW = new RandomWord();
// ksession.insert( rW );
// ksession.insert( new GuessedWord() );
// ksession.insert( new GameRules(4) );
//
// // start a new process instance
// ksession.startProcess("Hangman");
// logger.close();
// } catch (Throwable t) {
// t.printStackTrace();
// }
//Add Rules and Flow to Knowledge Builder
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add( ResourceFactory.newClassPathResource( "hangman.drl",
Hangman.class ),//Ponía ShppongExample.class
ResourceType.DRL );
kbuilder.add( ResourceFactory.newClassPathResource( "hangman.rf",
Hangman.class ),
ResourceType.DRF );
//Create Knowledge Base and add Knowledge from KBuilder
final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
// Create a stateful session
final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
//Generate a log file of the session
KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "log/hang");
//Insert classes into a session to allow the management of those
RandomWord rW = new RandomWord();
ksession.insert( rW );
ksession.insert( new GuessedWord(rW) );
ksession.insert( new Solution() );
ksession.insert( new GameRules(4) );
//Start the the process
ksession.startProcess( "Hangman" );
//Fire all rules
ksession.fireAllRules();
//Close the logger
logger.close();
ksession.dispose();
}
private static KnowledgeBase readKnowledgeBase() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("hangman.rf"), ResourceType.DRF);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error: errors) {
System.err.println(error);
}
throw new IllegalArgumentException("Could not parse knowledge.");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
return kbase;
}
public static class RandomWord {
private String randomWord;
public RandomWord() {
int index = new Random().nextInt( 9 );
Map<Integer,String> m = new HashMap<Integer, String>();
m.put(0,"MULTIMEDIA");
m.put(1,"INFORMATION");
m.put(2,"RETRIEVAL");
m.put(3,"DROOLS");
m.put(4,"MORTGAGE");
m.put(5,"INSURANCE");
m.put(6,"TUPPERWARE");
m.put(7,"HANGMAN");
m.put(8,"MOVIES");
m.put(9,"CAR");
this.randomWord = m.get(index);
}
public String getrandomWord() {
return this.randomWord;
}
}
public static class GuessedWord {
private RandomWord randomWord;
private String guessedWord = "";
private String letters = "";
public GuessedWord(RandomWord rW){
randomWord = rW;
this.guessedWord = "";
int count = this.randomWord.getrandomWord().length();
while(count != 0){
this.guessedWord = this.guessedWord + "_ ";
count = count -1;
}
}
public boolean solution(String solution){
solution = solution.toUpperCase();
if(solution.compareTo(randomWord.getrandomWord()) == 0){
return true;
}
else{
return false;
}
}
public void sayOneLetter(String c){
c = c.toUpperCase();
StringBuffer buffer = new StringBuffer(this.randomWord.getrandomWord());
StringBuffer buffer2 = new StringBuffer(this.guessedWord);
this.letters = this.letters + c + " ";
int index = 0;
while((index = buffer.indexOf(c, index)) != -1){
buffer2 = buffer2.replace(index*2, index*2+2, c+" ");
index = index + 1;
}
this.guessedWord = buffer2.toString();
}
public String getGuessedWord() {
return this.guessedWord;
}
public String getLetters() {
return this.letters;
}
public String getrandomWord() {
return this.randomWord.getrandomWord();
}
}
public static class GameRules {
private int allowedGuesses;
// private int allowedSolves;
private int value;
private boolean exit = false;
public String decision;
public GameRules(int allowedGuesses) {
this.allowedGuesses = allowedGuesses;
// this.allowedSolves = 2;
this.value = 2;
}
public int getAllowedGuesses() {
return this.allowedGuesses;
}
public void setAllowedGuesses(int in) {
this.allowedGuesses = in;
}
public int getAllowedSolves() {
// return this.allowedSolves;
return this.value;
}
public void setAllowedSolves(int in) {
// this.allowedSolves = in;
this.value = in;
}
public String getDecision() {
return this.decision;
}
public void setDecision(String in) {
this.decision = in;
}
public void setExit(boolean exit) {
this.exit = exit;
}
public boolean isExit() {
return exit;
}
}
public static class Solution {
private String solution;
public Solution() {
this.solution = "";
}
public String getSolution() {
return this.solution;
}
public void setSolution(String in) {
this.solution = in;
}
}
}
15 years, 7 months
Tomcat 6.0 + BRMS
by Sergio Lombrico
Hi. Has anyone manged to get the BRMS running with Tomcat 6.0 ?
I have followed the instructions:
"If you are using Tomcat 5,6 or any other application server different than
JBoss AS 4.x
please follow this
instructions<http://wiki.jboss.org/wiki/Wiki.jsp?page=JBRMSjsfdependencies>to
get BRMS working in your environmen"
but cannot manage to get the BRMS to start.
Has anyone had success with this ?
Sergio
15 years, 7 months
drools 5.0 quotes
by Mark Proctor
We are just updating the website for the 5.0 launch. I'm looking for
anyone that works for a well known firm who would be willing to give us
a quote (just one or two lines), particularly around Drools Flow or
Fusion. This is not a testimoney or a reference. It's just a quote on
what you think, the promise of the technology, or how you hope it'll
help your business - something along those lines. If you can help,
please contact me directly. Ideally we'd like two or three quotes to go
out with the release announcement.
Thanks
Mark
15 years, 7 months