Programmatically generate decision table in C#?

后端 未结 5 1997
旧时难觅i
旧时难觅i 2021-01-06 23:47

I have this situation that I need to let users define decisions based on the number of given conditions. For example my program needs to automatically generate a matrix as b

相关标签:
5条回答
  • 2021-01-06 23:51

    Outputting the matrix is rather trivial:

    int conditions = 3;
    for (int c = 0; c < conditions; c++) {
        Console.WriteLine(
           "Condition {0} : {1}",
           (char)('A' + c),
           new String(
              Enumerable.Range(0, (1 << conditions))
              .Select(n => "TF"[(n >> c) & 1])
              .ToArray()
           )
        );
    }
    

    So, what do you want to do with it?

    0 讨论(0)
  • 2021-01-06 23:55

    I'm not sure what you mean, but maybe this is what you are looking for:

    I made some little adjustments, because your second example wasn't consistent with the first one; and negated the bits (I substituted F with 0, and T with 1), to show my point.

    Condition A   0 0 0 0 1 1 1 1
    Condition B   0 0 1 1 0 0 1 1
    Condition C   0 1 0 1 0 1 0 1
    

    Now, observe the pattern of each column, and think about binary numbers ;).

    (I hope you get the idea.)

    0 讨论(0)
  • 2021-01-07 00:02

    I think I know what you're saying. If your conditions aren't that bad, you can say:

    if (A && B && C) {
         return X;
    }
    if (!A && B && C) {
         return Y;
    }
    

    Oh wait! I think you're looking to generate all the different combinations of conditions! You want permutations! And if you just have binary, well sir, each combo can be found by counting.

    I don't quite follow: Does it look like

    State         1 2 3 4
    Condition A   T T F F
    

    ?

    0 讨论(0)
  • 2021-01-07 00:03

    Unless I'm missing something you're missing something in your problem definition. You've defined the range of possible inputs, that's surely easy to generate?

    Condition A   T T F F
    Condition B   T F T F
    Decision      T F F F
    

    The output needs to be defined, it can't be deduced. As an example I've filled in an AND.

    Seems like my glib "easy to generate" is the problem. Doesn't a recursive solution work?

    for (members of getListOfCombinedStates(n) )
        print theMember
    
    getListOfCombinedStates(int howMany) {
        if (  n == 1 )
            return  list of possible States
        else {
            create empty resultlist
            for ( members of getListofCombinedStates(howMany -1) )
               for ( members of listOfStates ) 
                   create new CombinedState by suffixing state, add to resultList
    
            return resultList
         }
    

    So for n=2 we call getListOfCombinedStates(2), which calls getListOfCombinedStates(1), and that returns { T, F }.

    getListOfCombinedStates(2) then iterates {T, F } and adds first T and them F to each member yielding { T, T } and { T, F} and then {F, T } and {F, F}.

    I hope it's clear how getListOfCombinedStates(3) would in turn call getListOfCombinedStates(2) and generate the required values.

    0 讨论(0)
  • 2021-01-07 00:15

    As djna has mentioned in his/hers answer, you are missing the output for the decision.

    For instance, if you have a operator that takes two inputs (for example: and, or operators) you have to try it for all possible inputs. For a simple operator that is quite simple since there are only four possible inputs, but for more complex operators you will have to generate 2^n possible inputs to calculate all possible outputs.

    I suggest doing this in a array of n boolean variables, where you flip bits to get 2^n possible inputs, then testing your operator with the generated input array and printing the result.

    One simple way of generating the array is to create a loop in which you increment a variable from 0 to 2^n - 1 and then converting the number to binary. You will get something like this: (for n = 3):

    0: 0 0 0
    1: 0 0 1
    2: 0 1 0
    3: 0 1 1
    4: 1 0 0
    5: 1 0 1
    6: 1 1 0
    7: 1 1 1
    

    Hope this helps!

    0 讨论(0)
提交回复
热议问题