0

I'm trying to find a data-structure to help me model this scenario...

I have a set of expressions that have two types: 1. Expressions that have a calculations that should be executed and return a true or false outcome 2. Expressions that print out information and have no return value The expressions have a sequential order that depends on the outcome of their calculation.

The expressions are stored in the database and at runtime will be loaded into a data structure which will need to retain the order of the expressions and the decisions that should be followed depending on an expression's outcome.

Let me illustrate this with a simple example where there are 4 expressions and the first two are rules that when carried out have a true or false return value but the final 2 are just informational so after being executed the flow would continue to the next expression.

Expression 1: "5+5=10" If True then go to Expression 2 If False then go to Expression 4 
Expression 2: "6+1=7" If True then go to Expression 4 If False then go to Expression 3
Expression 3: "print hello" 
Expression 4: "print goodbye"

My immediate thought is that the data structure could be some sort of linked list that would need to have not only a next() method to point to the next expression but also true() and false() methods to point to the appropriate nodes in the case that the expression has a return value.

Is there any established way to model this type of relationship?

4

2 回答 2

3

您可以简单地使用二叉树。

于 2012-09-28T07:05:44.617 回答
0

伪代码:

LinkedList <Map <Integer><Decision>>

在哪里:

Integer- 决定编号

Decision- 你的功能

您可以定义Logic结构,例如:

input  1 2 3 4 5 6
true   2 
false  4

这意味着 if 1 ret truethen ret 2else ret4

于 2012-09-28T07:13:26.443 回答