Design Question (hashCode/equals/fact maintenance)
by Fenderbosch, Eric
I'm just looking for a bit of verification that this is a reasonable
solution. This just feels like a hack and there's probably a better way
that I'm just not seeing. Alternate ideas are welcome and appreciated.
Thanks in advance.
The objective is to find the best workers for a job:
Jobs have a location.
Workers have a location.
Scores are calculated based on miles and minutes of a route from the
worker to a job.
When a job or worker location changes, then the route should be
recalculated so that scores can be recalculated. It is possible for a
location to get re-inserted that might be the functionally the same as a
previous location for a job/worker, but a different instance. In this
case, the route should not be recalculated and the old scores should
remain.
assert behavior = equality
logical override = discard
maintain tms = true
remove identies = true
shadow proxies are enabled
public class JobLatitudeLongitude {
private String jobId;
private double latitude;
private double longitude;
private long timestamp = System.currentTimeMillis();
// getters & setters omitted for brevity
public int hashCode() {
return jobId.hashCode();
}
public boolean equals(Object obj) {
// not null or type safe, just simple version for
brevity
JobLatitudeLongitude other = (JobLatitudeLongitude) obj;
return this.jobId.equals(other.jobId) && this.timestamp
== other.timestamp;
// would "return false;" work ???
}
}
rule "retract redundant job latitude/longitude facts"
// if more than one lat/long with same position for a job, keep the
oldest
salience 600
when
older : JobLatitudeLongitude()
newer : JobLatitudeLongitude(jobId == older.jobId,
latitude == older.latitude, longitude == older.longitude, timestamp >=
older.timestamp)
then
retract(newer);
end
rule "retract old job latitude/longitude facts"
// if more than one lat/long with different position for a job, keep the
newest
salience 600
when
older : JobLatitudeLongitude()
newer : JobLatitudeLongitude(jobId == older.jobId,
latitude != older.latitude, longitude != older.longitude, timestamp >=
older.timestamp)
then
retract(older);
end
rule "add Route fact"
salience 450
when
jobLatitudeLongitude : JobLatitudeLongitude()
workerLatitudeLongitude : WorkerLatitudeLongitude()
then
// calculateRoute is an imported function
Route route = calculateRoute(workerLatitudeLongitude,
jobLatitudeLongitude);
insertLogical(route);
end
17 years, 11 months
Map using from constraint
by Paul Ryan
Hi all,
Is there a way using the 'from' constraint to get the values of a map(the implementations of java.lang.Map eg HashMap, TreeMap, etc) into a constraint parameter, and if so can someone please give an example. Below is the way I've been trying, without success, to get a value based on a key.
import my.package.MyFact
import java.util.Map.Entry
rule "Find with map"
when
$fact : MyFact($paramMap : paramMap)
$myValue : Entry(key == "mykey1") from $paramMap
Then
// Sudo code...
<use $myValue.getValue()>
end
Paul Ryan
Software Architect
Product Engineering
InfoTrust Group, Inc.
[cid:image001.jpg@01C8E75E.F8448000]
500 Discovery Parkway, Suite 200
Superior, CO 80027
Office (303) 627-6528
Fax (303) 666-6711
Email pryan(a)infotrustgroup.com<mailto:pryan@infotrustgroup.com>
WEB www.infotrustgroup.com<http://www.infotrustgroup.com/>
This e-mail and all information included herein do not constitute a legal agreement accorded by INFOTRUST GROUP and its affiliates and subsidiaries. All legal agreements must be formulated in writing by a legal representative of INFOTRUST GROUP. This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this e-mail by mistake, please inform us and destroy this e-mail and any documents it might contain. Please note that any views or opinions presented in this email are solely those of the author and do not necessarily represent those of the company. Finally, the recipient should check this email and any attachments for the presence of viruses. The company accepts no liability for any damage caused by any virus transmitted by this email. Thank you for your cooperation.
17 years, 11 months
Re: variable 'or' constraints with a DSL
by Matt Geis
Hi Reid,
There is a way to do this, but it doesn't involve the "-" (AND) hyphen shortcut. You would write a regular DSL, and the RHS of your rule would be one line.
It's important to remember that DSL entry order is very important in your file. The domain-specific code you write can (either intentionally or unintentionally) be expanded many times before it arrives at the DRL language that is fed into the rules engine. In your case, the RHS of your rule will be tested once for a match against every "condition" entry in your DSL file.
What this means is that you want to first write your rule in standard drools syntax, then work backward to see how expansion of DSL entries slowly moves your DSL rule closer to, and ultimately into, a DRL rule.
You should be able to write something close to
There is a person with {color} hair or with {color} eyes or that is {height} inches tall or that is {age} years old
You *might* have to use the variable typing feature new to Drools 5.0, but you may not.
A couple approaches that may help you out are...
1. Write a unit test, and pass your DSL code through the DSLExpander and see what DRL code it generates (which may or may not be valid, but at least you'll see what the DSL engine is doing to your DSL rule).
2. Download the source and use a step-through debugger with a breakpoint so you can examine the consequences of every match attempted against the RHS of the rule, and the consequent results of the replacement if a match is found.
3. If you don't want to run a debugger, you could write some AOP code to emit the results of the RHS with each step of the expansion, but this approach would be more work than just running a debugger.
If you'd like to write a self-contained unit test with your DRL rule (the one that looks like the one I provided above, but verified to work), I can take a look at it and see what I'd do DSL-wise.
Matt
17 years, 11 months
Drools in multithreaded env
by Jin
Env setup:
1. There is only one instance of working memory.
2. There are at lease two threads constantly insert/update/delete facts from the
same workings memory based on some incoming messages.
3. I use temporal rules (duration clause) as well. When it is fired, it will be
on the third thread.
Question:
I can synchronize the first two threads by acquiring lock on the working memory.
But how do I synchronize the temporal rules thread especially on the left hand
side of the rules?
Thanks,
Jin
17 years, 11 months
variable 'or' constraints with a DSL
by Reid Kaufmann
I really like how you can specify a DSL like this (for example):
[condition]There is a person=$p : Person()
[condition]- with {color} hair=hairColor matches {color}
[condition]- with {color} eyes=eyeColor matches {color}
[condition]- that is {height} inches tall =height == {height}
[condition]- that is {age} years old=yearsOld == {age}
If I'm reading the documentation correctly, the rules can then be
specified with any subset of the constraints (lines beginning with
"-"). These constraints are then essentially ANDed together since they
are comma separated when converted to DRL. Since they are called
constraints, this makes sense. My question is this: Is there a similar
way to allow a variable number of "constraints" that are all ORed ("||")
together?
I've been reading the documentation and searching mail archives and so
far haven't found anything helpful. Let me know if there is a way to do
this or any clever workarounds, since I don't want to delineate every
possible combination of attributes when OR is needed.
reid
17 years, 11 months
Function call in LHS
by BINET JEAN-BAPTISTE
Hello,
I am new to Drools. I have downloaded and installed Drools 4.0.7.
After having read the documentation, I saw that it is possible to create
function in the .drl file.
I succeed in creating a function, calling it in the LHS with eval()
instruction or in the RHS.
But, is it possible to call a function in the LHS out of an eval() ? For
example, I would like to use
a pattern such as :
$tmp : MyClass( name == MyFunction() )
Thanks in advance.
Regards.
JB
17 years, 11 months
RE: [rules-dev] Multi threading usage best practice
by Anstis, Michael (M.)
Cross posted for information.
Ths subject is more for the user list than the dev' one.
Cheers,
________________________________
From: rules-dev-bounces(a)lists.jboss.org
[mailto:rules-dev-bounces@lists.jboss.org] On Behalf Of 9Lives 9Lives
Sent: 14 July 2008 14:33
To: Rules Dev List
Subject: RE: [rules-dev] Multi threading usage best practice
TnX Mike 4 the quick response.
I'm afraid that u r right regarding the through-put ;-(
Using the "synchronized" method will probably solve my problem
but will damage the solution.
If u have any other thoughts on the matter i would love 2 hear
them.
Regards
Dotan
________________________________
Subject: RE: [rules-dev] Multi threading usage best practice
Date: Mon, 14 Jul 2008 14:24:02 +0100
From: manstis1(a)ford.com
To: rules-dev(a)lists.jboss.org
Hi,
Would synchronising on working memory effectively serialise the
effects of fireAllRules()?
...
synchronised(wm) {
wm.fireAllRules();
}
...
I don't know whether this would kill your through-put either.
Cheers,
Mike
________________________________
From: rules-dev-bounces(a)lists.jboss.org
[mailto:rules-dev-bounces@lists.jboss.org] On Behalf Of 9Lives 9Lives
Sent: 14 July 2008 13:41
To: rules-dev(a)lists.jboss.org
Subject: [rules-dev] Multi threading usage best practice
Hello
I'm using Drools 4.0.7 inside a mail relay application 2
determine the operations that need 2 b executed on each passing message.
To do this i'm using the following scenario:
1.
I have a ruleBase.newStatefulSession().
2.
I have a fixed set of facts.
3.
I have a fixed set of rules.
4.
Each mailer (a thread that is handling a single
message) is inserting the message to the working memory, calls the
"fireAllRules" method and retracts the message.
5.
Rules that r executed change custom attributes
in the message.
Problem:
I noticed that sometimes a rule can b executed on the
same message more then once.
Assumption:
My guess is that because i'm working is a multi
threading environment but using a stateful session, what happens is:
1. Thread A is inserting Message A.
2. Thread B is inserting Message B
3. Thread A is calling fireAllRules
4. Rule X is executed on messages A + B.
5. Thread B is calling fireAllRules
6. Rule X is executed on messages A + B
7. Thread A is retracting Message A
8. Thread B is retracting message B
Question:
My goal is 2 make sure a rule is executed only once on a
single message.
Any ideas on how 2 avoid the situation described above?
TnX
Dotan
________________________________
Invite your mail contacts to join your friends list with
Windows Live Spaces. It's easy! Try it!
<http://spaces.live.com/spacesapi.aspx?wx_action=create&wx_url=/friends.
aspx&mkt=en-us>
________________________________
Discover the new Windows Vista Learn more!
<http://search.msn.com/results.aspx?q=windows+vista&mkt=en-US&form=QBRE>
17 years, 11 months
Decision table
by Vanina Beraudo
Hello,
I need use a excel Decision table, in the CONDITION I invoque a
method, not a getter, and this method have a parameter. I dont know
how is the correct way to call a method with a parameter in CONDITION
part.
I do this,
CONDITION
i:Tax
taxValue("$1") < $2
10, 20
Could somebody give some idea how can I call a method with parameters?
Thanks,
--
Lic. Vanina Beraudo
17 years, 11 months
Re:[rules-users] Decision table
by tizianalauciello@libero.it
Hi,
I think that you would associate a variable to the method, like Java notation.
I hope this can help you.
Bye
Tiziana
---------- Initial Header -----------
>From : rules-users-bounces(a)lists.jboss.org
To : rules-users(a)lists.jboss.org
Cc :
Date : Thu, 10 Jul 2008 11:44:07 -0300
Subject : [rules-users] Decision table
> Hello,
>
> I need use a excel Decision table, in the CONDITION I invoque a
> method, not a getter, and this method have a parameter. I dont know
> how is the correct way to call a method with a parameter in CONDITION
> part.
>
> I do this,
>
> CONDITION
> i:Tax
> taxValue("$1") < $2
>
> 10, 20
>
> Could somebody give some idea how can I call a method with parameters?
>
> Thanks,
>
>
>
>
>
> --
> Lic. Vanina Beraudo
> _______________________________________________
> rules-users mailing list
> rules-users(a)lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
17 years, 11 months