决策表的原生 Java 解决方案

发布于 2024-11-05 06:46:24 字数 427 浏览 3 评论 0原文

我正在与一位尊敬的同事进行有趣的讨论,并且想要一些额外的输入...

我需要在我的应用程序中实现一些基本的决策表逻辑。我希望使用 OpenL Tablets,它在 Excel 电子表格中表示决策数据。我喜欢它,它易于设置和维护,并且内存和处理占用空间较小。我可以轻松添加新表,并且我有一些包含超过 100 行和最多 10 个条件的表。这些数据非常静态,很少发生变化。

我的同事不想将第三方 API 引入其中,并且对与 Microsoft 文件格式绑定持保留态度。

我明白他的观点,但我能看到通过 Java 实现决策表的唯一方法是编写一系列丑陋的 if 或 case 语句,这对于较小的表来说很好,但当我到达较大的表时就会变得难以管理。

有谁对争论双方有什么评论吗?如果有人对可以解决我在本机 Java 中的问题的模式有任何想法,我很想听听。

非常感谢您抽出时间。

I'm haiving an interesting discussion with an esteemed colleague and would like some additional input...

I need to implement some basic decision table logic in my application. I was looking to use OpenL Tablets which represents decision data in an Excel spreadsheet. I like it, it's easy to set up and maintain and has a small memory and processing footprint. I can add new tables easily and I have some tables with over 100 rows and upto 10 conditions. This data is pretty static and rarely changes.

My colleague does not want to introduce a third party api into the mix and has reservations about being tied to a Microsoft file format.

I see his point but the only way I can see of implementing the decision table via Java is to code a series of ugly if or case statements, which is fine for the smaller tables but would become unmanageable when I get to the bigger tables.

Does anyone have any comments on both sides of the argument. If anyone has any thoughts as to a pattern that could solve my problem in native Java I'd be interested to hear it.

Many thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人│生佛魔见 2024-11-12 06:46:24

嗯,一个完全天真的尝试:

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; 
                }); 

然后你会迭代:

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

一个稍微复杂的例子,你可以用 javascript 更简洁地编写你的决策表,也许使用 Beanshell 来执行逻辑。在我给你发布一个例子之前,我必须先敲敲敲打一下。

或者,如果您发布了一个示例,那么其他人可能会想出一些简单的 Scala 例程来完成您想要的操作。

编辑:

所以我做了一些研究和思考,对于 Beanshell 你可以使用类似这样的东西:

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");

并且 somefile.bsh 可能看起来像这样:

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; 
}

与重新编译 Java 源代码相比,这会给你更大的灵活性来更改规则。

您必须向这些解决方案中的任何一个添加另一个数组才能获得 100 个源“行”

Hmmm, a completely 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 would iterate:

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

A slightly more sophisticated example you could write your decision table in javascript a lot more succinctly, and perhaps use Beanshell to execute the logic. I would have to hit a shell and bang on this a bit before I could post you an example.

Or it's possible if you posted an example someone could come up with some simple Scala routines to do what you want.

EDIT:

So I did a little research and thinking, and for Beanshell you could use something like so:

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 would give you more flexibility to change the rules than recompiling Java source.

You would have to add another array to either of these solutions to get your 100 source "rows"

柳若烟 2024-11-12 06:46:24

1) 如果您的应用程序需要高性能,那么在大型表上这种简单的方法很快就会变得很慢。例如,OpenL Tablets 在大多数情况下对决策表进行索引,为任何表大小提供恒定的性能。

2) 一旦您开始在简单的方法之上进行构建,您就会发现您需要实现不同类型的条件、不同类型的数据解析器等。

3) OpenL Tablets 允许您在任何 Java 上使用 .xls 文件 -能力平台,它只是一种存储格式,库本身是纯Java的。最后,Excel 文件是整个宇宙中最容易转换/导出/导入的文件格式之一。

4) 由于切换到最新的 Apache POI 库来解析 xls 和 xlsx 文件,OpenL Tablets 运行时依赖项大小最近有所增长。以现代标准来看,它仍然很温和:)

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

2) Once you start to build on top of the naive approach, you'll see that you need to implement different types of conditions, data parsers for different types etc.

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

4) The OpenL Tablets runtime dependency size grew recently due to switching to the latest Apache POI library for parsing xls and xlsx files. It is still modest by modern standards :)

初相遇 2024-11-12 06:46:24

关于 MS Excel,它不是唯一可以创建 Excel 文件的软件 Star/Open/LibreOffice 也可以编辑它们。而且由于决策表 LTables 所要求的那么简单,所以不应该有任何兼容性问题。

其他众所周知的工具也使用 excel 来制作决策表(请阅读 JBoss Drools)

About MS Excel, it is NOT the only software that can create Excel files Star/Open/LibreOffice can edit them too. And being as simple as decision tables LTables requires, there shouldn't be any compatibility problem.

Also other well know tools use excel for decision tables (read it JBoss Drools)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文