1

I am currently doing a "spin-off" of a traditional truth table. I got 15 parameters that can have an effect on how a validation method is executed in one way or another. What I constructed was just a simple little program to post the possible combinations and if they would succeed or not.

I have a Truth class that looks like this:

public class Truth
{
    private ECondition condition;
    private boolean value;
    private String varName;
    private String realName;
    private HashMap<String, ECondition> dependencies = new HashMap<>(); 

@SuppressWarnings("javadoc")
public Truth(String varName, String realName, boolean value, ECondition condition)
{
    this.setVariableName(varName);
    this.setRealName(realName);
    this.setCondition(condition);
    this.setValue(value);
}
...
...

Just to explain the variables:

  • varName: Name of the Variable (Like 'A').
  • realName: The actual name (Like 'hasRole').
  • value: The boolean that needs to be either true or false
  • condition: It can have either of three values: ALWAYS_TRUE, ALWAYS FALSE and BOTH

The only variable that should change throughout this is, the value variable. Then there is the special map called Dependencies:

private HashMap<String, ECondition> dependencies = new HashMap<>();

Now this is important, and I will explain why. Since a value could be null but need another one to, in that case, be true or false, I added that map. It's an edge case that fits the BOTH enumeration.

Now what does all this lead to?

The execution of the program here:

public static void main(String[] args)
{
    Truth a = new Truth("A", "HasRole", true, ECondition.ALWAYS_TRUE);
    Truth b = new Truth("B", "orderId", true, ECondition.ALWAYS_TRUE);
    Truth c = new Truth("C", "clientName", true, ECondition.BOTH);
    Truth d = new Truth("D", "clientNumber", true, ECondition.BOTH);
    Truth e = new Truth("E", "clientId", true, ECondition.ALWAYS_TRUE);
    Truth f = new Truth("F", "orderCategory", true, ECondition.ALWAYS_TRUE);
    Truth g = new Truth("G", "locationId", true, ECondition.BOTH);
    Truth h = new Truth("H", "orderName", true, ECondition.ALWAYS_TRUE);
    Truth i = new Truth("I", "proposalExpiration", true, ECondition.BOTH);
    Truth j = new Truth("J", "contactAddressId", true, ECondition.BOTH);
    Truth k = new Truth("K", "billingAddressId", true, ECondition.BOTH);
    Truth l = new Truth("L", "deliveryAddressId", true, ECondition.BOTH);
    Truth m = new Truth("M", "fixedPrice", true, ECondition.BOTH);
    Truth n = new Truth("N", "quoteperspective", true, ECondition.BOTH);
    Truth o = new Truth("O", "detpartmentId", true, ECondition.BOTH);

    c.getDependencies().put("D", ECondition.ALWAYS_TRUE);
    d.getDependencies().put("C", ECondition.ALWAYS_TRUE);

    ArrayList<Truth> truths = new ArrayList<>();
    truths.add(a);
    truths.add(b);
    truths.add(c);
    truths.add(d);
    truths.add(e);
    truths.add(f);
    truths.add(g);
    truths.add(h);
    truths.add(i);
    truths.add(j);
    truths.add(k);
    truths.add(l);
    truths.add(m);
    truths.add(n);
    truths.add(o);

    boolean isSuccessfulCombination = true;
    StringBuilder sb = new StringBuilder();
    for (Truth t : truths)
    {
        sb.append(t.getVariableName() + "  ");
    }
    sb.append("Success");

    sb.append("\n");

    for (Truth t : truths)
    {
        Boolean bool = parseEnum(t.getCondition());
        if (bool == null && t.getDependencies().isEmpty())
        {
            sb.append(t.getValue() ? "T" : "F");
        }
        else if (bool == null && !t.getDependencies().isEmpty())
        {
            Iterator it = t.getDependencies().entrySet().iterator();
            while (it.hasNext())
            {
                Map.Entry pair = (Map.Entry) it.next();
                if (t.getVariableName().equals((String) pair.getKey()))
                {
                    if (t.getValue() != parseEnum((ECondition) pair.getValue()))
                    {
                        isSuccessfulCombination = false;
                        sb.append(t.getValue() ? "T" : "F");
                        break;
                    }
                }
            }
            sb.append(t.getValue() ? "T" : "F");
        }
        else if (bool != null)
        {
            if (t.getValue() != Boolean.parseBoolean(bool.toString()))
            {
                isSuccessfulCombination = false;
            }
            sb.append(t.getValue() ? "T" : "F");
        }

        sb.append("  ");
    }
    sb.append(isSuccessfulCombination);
    System.out.println(sb.toString());
}

It produces a string like so:

A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  Success
T  T  T  T  T  T  T  T  T  T  T  T  T  T  T  true

If we imagined that I changed A's value to false instead of true the end result true should change to false as the condition of A specifically says that it needs to be true for this to succeed.

Here comes the million dollar question:

How would I go about generating all possible combinations of those 15 boolean values I am working with here? Ideally I'd probably end up with a Set of boolean lists, so that I avoid duplicates.

OmniOwl
  • 5,477
  • 17
  • 67
  • 116
  • 3
    Just count in binary from `0` to `2^15`. – SLaks Feb 19 '15 at 14:52
  • Yeah thanks, I finally found this answer for those interested :) http://stackoverflow.com/questions/27007752/creating-all-possible-ways-of-a-boolean-array-of-size-n – OmniOwl Feb 19 '15 at 15:05

0 Answers0