Java Native Solution for Decision Table

I am having an interesting discussion with a respected colleague and would like to receive additional input ...

I need to implement some basic decision table logic in my application. I wanted to use OpenL Tablets, which presents the solution data in an Excel spreadsheet. I like it, it is easy to configure and maintain, and has a small memory and processing trace. I can easily add new tables, and I have several tables with over 100 rows and up to 10 conditions. This data is quite static and rarely changes.

My colleague does not want to introduce third-party api into the mix and has reservations regarding binding to a Microsoft file.

I see his point of view, but the only way I can implement the decision table through Java is through the code of a series of ugly if or case statements, which works great for smaller tables, but will become unmanageable when I get to the larger table.

Does anyone have comments on both sides of the argument. If anyone has thoughts on a template that could solve my problem in native Java, I would be interested to hear it.

Thanks so much for your time.

+6
source share
3 answers

Hmmm, an absolutely naive attempt:

public interface Condition<Type T> { public boolean process(T object); } ArrayList row = new ArrayList<Condition>(10); row.add( new Condition<YourObject>() { public boolean process(YourObject obj) { if ( obj.property > 0 ) return true; else return false; }); row.add( new Condition<YourObject>() { public boolean process(YourObject obj) { if ( obj.property2 == 100 ) return true; else return false; }); 

Then you will iterate:

 for ( Condition<YourObject> cond : row ) { if ( ! cond.process(yourobj) ) break; } 

A slightly more complex example: you can write your decision table in javascript much more succinctly and possibly use Beanshell to execute the logic. I would have to hit the shell and stumble upon this a bit before I can post you an example.

Or perhaps if you posted an example, someone might come up with some simple Scala routines to do what you want.

EDIT:

So, I did a little work and thought, and for Beanshell you can use something like this:

 import bsh.Interpreter; Interpreter i = new Interpreter(); // Construct an interpreter YourObject yourObject = new YourObject(); i.set("myObject", yourObject ); // Source an external script file i.source("somefile.bsh"); 

And somefile.bsh might look like this:

 var rules = new Array(); rules.push( function(var) { if ( var.getProperty() == 0 ) return true; else return false; }); rules.push( function(var) { if ( var.getProperty() < 1000 ) return true; else return false; }); ... more rules ... for ( var func in rules ) { if ( !func( myObject ) ) break; } 

This will give you more flexibility to change the rules than recompiling the Java source.

You will need to add another array to any of these solutions in order to get 100 source β€œstrings”

+8
source

1) If your application requires high performance, the naive approach will soon become quite slow on large tables. For example, OpenL Tablets indexes decision tables in most cases, providing consistent performance for any table size.

2) As soon as you start building on top of the naive approach, you will see that you need to implement different types of conditions, data analyzers for different types, etc.

3) OpenL Tablets allows you to use .xls files on any platform compatible with Java, it's just a storage format, the library itself is pure Java. And finally, the Excel file is one of the most easily converted / exported / imported file formats in the whole universe.

4) Recently, the size of OpenL Tablets runtime dependencies has grown due to the switch to the latest Apache POI library for analyzing xls and xlsx files. It's still modest by modern standards :)

+4
source

About MS Excel, it is NOT the only software that can create Excel files. Star / Open / LibreOffice can also edit them. And as simple as the decision tables required by LTables, there should be no compatibility issues.

Also other well-known tools use excel for decision tables (read JBoss Drools)

0
source

Source: https://habr.com/ru/post/887310/


All Articles