Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am creating a system where users will be able to subscribe to events, and get notified when the event has occured. Example of events can be phone call durations and costs, phone data traffic notations, and even stock rate changes.

Example of events:

  • customer 13532 completed a call with duration 11:45 min and cost $0.4
  • stock rate for Google decreased with 0.01%

Customers can subscribe to events using some simple rules e.g.

  • When stock rate of Google decreases more than 0.5%
  • When the cost of a call of my subscription is over $1

Now, as the set of different rules is currently predefined, I can easily create a custom implemention that applies rules to an event. But if the set of rules could be much larger, and if we also allow for custom rules (e.g. when stock rate of Google decreses more than 0.5% AND stock rate of Apple increases with 0.5%), the system should be able to adapt. I am therefore thinking of a system that can interpret rules using a simple grammer and then apply them.

After some research I found that there exists rule-based engines that can be used, but I am unsure if they fit our need as they seem more appropriate for business logic, as hinted in Does Your Project Need a Rule Engine and Jess guildelines.

Is there a open source Java framework suited for this area? If anyone can point me to the correct direction, or recommend some system, that would be great!

Edit:
Thoughts for custom solution: I am thinking of using an event-to-rule mapping, where each rule is assosicated with a set of events. Then, when an event occurs, I know what rules that can be applied. If each rule is represented by is own class, then triggering the rule is simply to execute the class. Pros: Simple and easy implementation. No need for interpretation of events to find rules Cons: Each new rule requires coding. Cannot do deduction analysis. If you see any major flaws in this approach please do tell me!

share|improve this question

closed as too broad by gnat, GlenH7, Kilian Foth, MichaelT, Robert Harvey Oct 26 '13 at 17:02

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers 2

Interestingly enough, our company just went through a similar exercise. Our application was similar but different. In our case, we are processing messages, and we wanted a way to define "rules" for how to transform the message values.

We considered 2 main approaches:

  • embedding a scripting engine (Groovy, Jython, Javascript)
  • using an existing open source rules engine (JRuleEngine)

Without going into the pros and cons of each approach, we decided that we wanted to define rules in Xml (like JRuleEngine), but that JRuleEngine was too verbose and Xml structure too awkward (for our specific use). Since we could easily support supplying access to all Java String functions and simple numeric comparisons, we felt we could write our own rules implementation fairly easily, which would give us Xml finely tuned to our specific usage.

Some considerations we used to make the decision, which I think are relevant to you are:

  • who is writing the rules? are they programmers or end-users? If programmers, you may find that embedded a scripting engine is the fastest solution. If end users, and Xml/Rules Engine approach may be easiest (if it's not too complicated).

  • how many functions and logical comparisons do you need to support? If a few, then writing your own rules engine will give a way to specify rules that is highly directed to your domain/application (as opposed to very generic rules Xml which apply to any domain). The Xml to support your custom written will be much more elegant than a generic rules engine.

  • how much time do you have to implement a solution? I think embedding a scripting engine will be fastest, followed by an existing Rules Engine (you will spend time learning Rules Engine API and Xml format), followed by slowest solution which is building your own (unless you have a very small set of features).

Best of luck!

share|improve this answer
    
Thanks for sharing your experience! After I wrote the question, I found this article by Martin Fowler. It seems you have similar conclusions which is reassuring! I am writing a PoC with strict time limit so I think I'll try the fastest one, i.e. custom. I am thinking of using an event-to-rule mapping, where each rule is assosicated with a set of events. Then, when an event occurs, I know what rules that can be applied. If each rule is represented by is own class, then triggering the rule is simply to execute the class. –  qtips Oct 25 '13 at 21:54
    
I think custom will probably suit your needs best. Just keep the scope focused, and create a good base design so that you can easily keep extending it and making it more sophisticated. By the way, the dragons who run this site have decided your question was not worth asking here. Just want you to know, I strongly disagree with the attitude of those guys - they are way off base in my opinion. –  Sam Goldberg Oct 27 '13 at 2:15

I think you may be underestimating the complexity of the "rules engine" required to manage knowledge about the sort of events you've described.

My advice would be to start with an existing framework and get some experience with that, until perhaps you reach a point where the functionality appears hopelessly inadequate for further expansion. The rewrite/build from scratch you do at that point will at least be informed by some in-the-trenches experience with one way of doing rules implementation.

In particular the events that are defined as time-relative or time-dependent are not easy to encode in rules. A Google search for "temporal reasoning" may shed some light on the scope of that task.


Added: Let's set aside the complexities of temporal reasoning and assume the event definition is understood well enough to move on to the event-to-rule mapping (as in the updated Question) of your design.

In order to get (quickly and extensibly) your well-defined events, you will likely have to resort to creating them in very fine granularity. E.g. call durations and costs defined in small units (seconds, cents, etc.) and stock price changes in small increments (hundredths of a percent). Perhaps these will be recorded as "history" in various database tables.

The event-to-rule mapping then has to pick up the slack in grouping "like" events together, so that (upon a user's subscription) a range of similar events will trigger a common action (rule).

A project I worked on a few years ago had a need for something like this (in a different context, processing data feeds). Instead of modifying procedural code to map "events" to "rules", we adopted a database table-driven approach. A table of "mapping" records was created, so changes in that table would be immediately effected in all processes which consulted it. [We needed to provide common logic for both C++ programs and database procedures.]

The key insight I want to pass along is the use of combined positive and negative conditions. A typical mapping will need to incorporate exclusions as well as inclusions, "like this" and "not like that". A rule (or action) will then be triggered by any one of the records that maps it, providing a (limited) disjunctive normal form. In practice the "cheesy" limitations of this form proved to be easy to work with.

Perhaps something analogous will give your application the generality it needs.

share|improve this answer
    
Thanks for your advice and heads up. Do you have any examples of such frameworks? Due to time requirements, I may not be able to deep-dive into existing frameworks. I will be sure to look into the complexities you have mentioned. I have edited my post to include some thoughts on a custom solution that I think will fit my needs. Please feel free to advice on that solution :) –  qtips Oct 25 '13 at 21:59
    
Thank you again for your comments! I was actually also thinking of a mapping table to allow multiple processes to access the mappings. It is reassuring that you have applied such mechanism with success! I do also see your point about positive and negative conditions, and exclusion and inclusion. –  qtips Oct 26 '13 at 22:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.