决策表的本机Java解决方案 [英] Native Java Solution to Decision Table

查看:162
本文介绍了决策表的本机Java解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

我的同事不希望将第三方api引入混合,并对与Microsoft文件格式绑定有所保留。

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.

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

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.

有没有人对论点的两边都有任何评论。如果有人对可以解决我在本机Java中的问题的模式有任何想法,我会有兴趣听到它。

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.

非常感谢您的时间。

推荐答案

嗯,完全是天真尝试:

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来执行逻辑。在我给你发一个例子之前,我必须打一个shell然后敲打一下。

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.

或者如果你发布了一个例子,有人可能想出一些简单的Scala例程可以做你想要的。

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

编辑:

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

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

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

这将使您更灵活地更改规则而不是重新编译Java源代码。

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

您必须在这些解决方案中添加另一个阵列才能获得100个源行

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

这篇关于决策表的本机Java解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆