获得所有可能的n布尔组合?

时间:2015-02-19 14:51:09

标签: java

我目前正在对传统真相表进行“衍生”。我有15个参数可以影响验证方法以这种或那种方式执行的方式。我构建的只是一个简单的小程序来发布可能的组合,如果它们会成功与否。

我有一个Truth类看起来像这样:

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

只是解释变量:

  • varName:变量的名称(如“A”)。
  • realName:实际名称(如'hasRole')。
  • value:需要为true或false的布尔值
  • 条件:它可以包含以下三个值之一:ALWAYS_TRUEALWAYS FALSEBOTH

唯一应该改变的变量是value变量。然后是名为Dependencies的特殊地图:

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

现在这很重要,我将解释原因。由于值可能为null但需要另一个值,在这种情况下,为真或假,我添加了该映射。这是符合BOTH枚举的边缘情况。

现在这一切导致了什么?

这里执行程序:

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

它产生一个如下字符串:

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

如果我们想象我将A的值更改为false而不是true,则最终结果true应更改为false condition 1 {} A明确表示需要true才能成功。

这是百万美元的问题:

如何生成我正在使用的15个布尔值的所有可能组合?理想情况下,我可能最终得到一组布尔列表,这样我就可以避免重复。

0 个答案:

没有答案