Use facts from JSON instead of POJO

4,117 views
Skip to first unread message

Vjacheslav Yermakov

unread,
Oct 4, 2016, 11:03:02 AM10/4/16
to Drools Usage
Hello.

I have a flow of facts in JSON format from different systems. It is impossible to create POJO for every type of facts because format can be vary in time. The only option I've find is to convert JSON to Map<String, Object> and to deal with the maps from rules. But that way is impractical for the people who should create rules because they are from different technologies (.net, different CRMs, JS and so on). The question is: can I implement custom object accessor and how? I've fond very limited info on google.com.

More detailed:
Now I have to write rules like that (Map usage):

rule "Hiprio rule"
    when
        $e : Map (get("value") > 5) 
    then
        $e.put("priority", 3);
end

But it would be great if people get ability to write someting like that:

rule "Hiprio rule"
    when
        $e : Fact (value > 5) 
    then
        $e.priority = 3;
end

It will be simle and undrestandable for the people who should create rules (taking in account that property access path can be more complicated).

The only things I've found is a short idea from: https://developer.jboss.org/wiki/DroolsLanguageEnhancements/version/24#jive_content_id_Pluggable_ObjectType_Support . And mentioning of FactTemplate interface I dont understand how to use.

Could you provide me a direction where I can dig to reach that (if it possible with current Drools version).

Thank you in advance.

Ged Byrne

unread,
Oct 12, 2016, 4:32:11 PM10/12/16
to Drools Usage
Hi,

Here's a post I found useful a while ago.  I'm not sure if the code is still valid: http://orangemile.blogspot.co.uk/2008/07/drools-fact-template-example.html

The approach I favour now is to declare the fact types within my DRL files: https://docs.jboss.org/drools/release/6.0.0.Final/drools-docs/html/DroolsLanguageReferenceChapter.html#d0e5542

Otherwise you may want to create your own solution use FactTemplate.  The best way to learn how to use it is by looking at the tests:


You may also find the various marshalling options with Camel integration useful: https://docs.jboss.org/drools/release/6.2.0.CR3/drools-docs/html/ch.camel.html


Regards, 


Ged
Reply all
Reply to author
Forward
0 new messages