use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class DFSOrdering method dfs.
private void dfs(final Formula formula, final LinkedHashSet<Variable> variables) {
switch(formula.type()) {
case LITERAL:
variables.add(((Literal) formula).variable());
break;
case NOT:
dfs(((Not) formula).operand(), variables);
break;
case IMPL:
case EQUIV:
final BinaryOperator op = (BinaryOperator) formula;
dfs(op.left(), variables);
dfs(op.right(), variables);
break;
case AND:
case OR:
for (final Formula o : formula) {
dfs(o, variables);
}
break;
case PBC:
final PBConstraint pbc = (PBConstraint) formula;
for (final Literal lit : pbc.operands()) {
variables.add(lit.variable());
}
break;
}
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class ForceOrdering method getOrder.
@Override
public List<Variable> getOrder(final Formula formula) {
final SortedSet<Variable> originalVariables = new TreeSet<>(formula.variables());
final Formula nnf = formula.nnf();
originalVariables.addAll(nnf.variables());
final Formula cnf = nnf.cnf();
final Hypergraph<Variable> hypergraph = HypergraphGenerator.fromCNF(cnf);
final Map<Variable, HypergraphNode<Variable>> nodes = new HashMap<>();
for (final HypergraphNode<Variable> node : hypergraph.nodes()) {
nodes.put(node.content(), node);
}
final List<Variable> ordering = force(cnf, hypergraph, nodes).stream().filter(originalVariables::contains).collect(Collectors.toList());
originalVariables.stream().filter(v -> !ordering.contains(v)).forEach(ordering::add);
return ordering;
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class FormulaDotFileWriter method generateNaryDotString.
private static void generateNaryDotString(final NAryOperator formula, final StringBuilder sb, final Map<Formula, Integer> ids, final String op) {
for (final Formula operand : formula) {
if (!ids.containsKey(operand)) {
generateDotString(operand, sb, ids);
}
}
final int id = ids.size();
ids.put(formula, id);
sb.append(" id").append(id).append(" [label=\"").append(op).append(String.format("\"];%n"));
for (final Formula operand : formula) {
sb.append(" id").append(id).append(" -> id").append(ids.get(operand)).append(String.format(";%n"));
}
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class FormulaWriter method write.
/**
* Writes a given formula to a file with a given formula formatter.
* @param file the file
* @param formula the formula to write
* @param splitAndMultiline indicates whether - if the formula is an conjunction - the single operands should be
* written to different lines without a conjoining operator
* @param formatter the formatter for the formula
* @throws IOException if there was a problem writing the file
*/
public static void write(final File file, final Formula formula, final boolean splitAndMultiline, final FormulaStringRepresentation formatter) throws IOException {
final StringBuilder sb = new StringBuilder();
if (splitAndMultiline && formula.type() == FType.AND) {
for (final Formula f : formula) {
sb.append(formatter.toString(f)).append(System.lineSeparator());
}
} else {
sb.append(formatter.toString(formula));
}
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8))) {
writer.append(sb);
writer.flush();
}
}
use of org.logicng.formulas.Formula in project LogicNG by logic-ng.
the class BDDCNFFunction method apply.
@Override
public Formula apply(final BDD bdd) {
final BDDKernel kernel = bdd.underlyingKernel();
final List<byte[]> unsatPaths = new BDDOperations(kernel).allUnsat(bdd.index());
final List<Formula> clauses = new ArrayList<>();
List<Formula> literals;
for (final byte[] path : unsatPaths) {
literals = new ArrayList<>();
for (int i = 0; i < path.length; i++) {
if (path[i] == 0) {
literals.add(kernel.getVariableForIndex(i));
} else if (path[i] == 1) {
literals.add(kernel.getVariableForIndex(i).negate());
}
}
clauses.add(kernel.factory().or(literals));
}
return kernel.factory().and(clauses);
}
Aggregations