我目前正在对传统真相表进行“衍生”。我有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);
}
...
...
只是解释变量:
ALWAYS_TRUE
,ALWAYS FALSE
和BOTH
唯一应该改变的变量是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个布尔值的所有可能组合?理想情况下,我可能最终得到一组布尔列表,这样我就可以避免重复。