Ken,

Neither solution makes any assumption about data storage. Hibernate entities could be used or simple POJOs, matters not. As to your Example, I have used something similar to the following:

public class Entity {

    private String firstName;
   
    public String getFirstName() { return firstName; }
    public void setFirstname(String firstName) { this.firstName = firstName; }
}

public class FactEntity {
    private Entity entity;
    private String additionalAttribute;

    public FactEntity(Entity entity) { this.entity = entity; }
    public Entity getEntity() { return entity; }
    public void setEntity(Entity entity) { this.entity = entity; }
    public String getAdditionalAttribute() { return additionalAttribute; }
    public void setAdditionalAttribute(String additionalAttribute) { this.additionalAttribute = additionalAttribute; }
}

In your class that inserts the Entity facts into working memory, you just;

    FactEntity fe = new FactEntity(originalEntity);
    workingMemory.insert(fe);

In your rule, you can then do something like;

rule
    when
        $fe : FactEntity(additionalAttribute == "Supervisor")
    then
       System.out.println($fe.entity.firstName);
end

Earnie!

From: Ken Archer <archerovi@hotmail.com>
To: rules-users@lists.jboss.org
Sent: Thursday, July 30, 2009 9:13:35 AM
Subject: Re: [rules-users] How to Enrich Facts with Data from a Database

Earnie,

Thanks for this.  Do your two solutions to fact enrichment presume use of the Hibernate framework to interact with a database in the rules? Would an example of your first solution be the following?

when
    $w : WMFact($key : id)
    $d : DatabaseFact(parentKey == $key)
then
    $w.Field1=$d.Field1 AND $w.Field2=$d.Field2 AND $w.Field3=$d.Field3

Ken


Date: Tue, 28 Jul 2009 09:35:47 -0700
From: earniedyke@yahoo.com
To: rules-users@lists.jboss.org
Subject: Re: [rules-users] How to Enrich Facts with Data from a Database

One way is to extend the original classes and add the desired attributes, then insert the extended class into WorkingMemory. You could then use RHS to modify matched facts. Another way is to inject "related" facts. So your LHS matches a fact and you insert a new "related" fact in the RHS. Rules that must evaluate data from both facts can the do something like this:

when
    $b : BaseFact($key : id)
    $r : RelatedFact(parentKey == $key)
then
    ...

I have used both albeit on small sets of facts and both worked fine.

Earnie!


From: Ken Archer <archerovi@hotmail.com>
To: rules-users@lists.jboss.org
Sent: Tuesday, July 28, 2009 12:22:37 PM
Subject: [rules-users] How to Enrich Facts with Data from a Database

I would like to append values to facts in working memory with values from a database before rules processing.  The Fusion documentation says, “one of the most common use cases for rules is event data enrichment”, but no documentation seems to explain how this is done whether we’re talking about events or just plain old facts.  And the RHS syntax seems to be limited to working with data already in working memory.  How can I enrich facts with data from another system?  Thanks in advance.

 

Ken Archer

Telogical Systems