Search in sources :

Example 1 with BinaryOperator

use of org.logicng.formulas.BinaryOperator in project LogicNG by logic-ng.

the class FormulaStringRepresentation method toInnerString.

/**
 * Returns the string representation of the given formula.
 * <p>
 * This method is used for recursive calls in order to format the sub-formulas.
 * @param formula the formula
 * @return the string representation of the formula
 */
protected String toInnerString(final Formula formula) {
    switch(formula.type()) {
        case FALSE:
            return this.falsum();
        case TRUE:
            return this.verum();
        case LITERAL:
            final Literal lit = (Literal) formula;
            return lit.phase() ? lit.name() : this.negation() + lit.name();
        case NOT:
            final Not not = (Not) formula;
            return this.negation() + this.bracket(not.operand());
        case IMPL:
        case EQUIV:
            final BinaryOperator binary = (BinaryOperator) formula;
            String op = formula.type() == FType.IMPL ? this.implication() : this.equivalence();
            return this.binaryOperator(binary, op);
        case AND:
        case OR:
            final NAryOperator nary = (NAryOperator) formula;
            op = formula.type() == FType.AND ? this.and() : this.or();
            return this.naryOperator(nary, String.format("%s", op));
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            return String.format("%s%s%d", this.pbLhs(pbc.operands(), pbc.coefficients()), this.pbComparator(pbc.comparator()), pbc.rhs());
        default:
            throw new IllegalArgumentException("Cannot print the unknown formula type " + formula.type());
    }
}
Also used : Not(org.logicng.formulas.Not) Literal(org.logicng.formulas.Literal) BinaryOperator(org.logicng.formulas.BinaryOperator) NAryOperator(org.logicng.formulas.NAryOperator) PBConstraint(org.logicng.formulas.PBConstraint)

Example 2 with BinaryOperator

use of org.logicng.formulas.BinaryOperator in project LogicNG by logic-ng.

the class VariablesFunction method apply.

@Override
public SortedSet<Variable> apply(final Formula formula, final boolean cache) {
    final Object cached = formula.functionCacheEntry(VARIABLES);
    if (cached != null) {
        return (SortedSet<Variable>) cached;
    }
    SortedSet<Variable> result = new TreeSet<>();
    switch(formula.type()) {
        case FALSE:
        case TRUE:
            result = new TreeSet<>();
            break;
        case LITERAL:
            final Literal lit = (Literal) formula;
            result.add(lit.variable());
            break;
        case NOT:
            final Not not = (Not) formula;
            result = apply(not.operand(), cache);
            break;
        case IMPL:
        case EQUIV:
            final BinaryOperator binary = (BinaryOperator) formula;
            result.addAll(apply(binary.left(), cache));
            result.addAll(apply(binary.right(), cache));
            break;
        case OR:
        case AND:
            final NAryOperator nary = (NAryOperator) formula;
            for (final Formula op : nary) {
                result.addAll(apply(op, cache));
            }
            break;
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            result = FormulaHelper.variables(pbc.literals());
            break;
        default:
            throw new IllegalStateException("Unknown formula type " + formula.type());
    }
    result = Collections.unmodifiableSortedSet(result);
    if (cache) {
        formula.setFunctionCacheEntry(VARIABLES, result);
    }
    return result;
}
Also used : Variable(org.logicng.formulas.Variable) SortedSet(java.util.SortedSet) Formula(org.logicng.formulas.Formula) Not(org.logicng.formulas.Not) TreeSet(java.util.TreeSet) Literal(org.logicng.formulas.Literal) BinaryOperator(org.logicng.formulas.BinaryOperator) NAryOperator(org.logicng.formulas.NAryOperator) PBConstraint(org.logicng.formulas.PBConstraint)

Example 3 with BinaryOperator

use of org.logicng.formulas.BinaryOperator 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;
    }
}
Also used : Formula(org.logicng.formulas.Formula) Literal(org.logicng.formulas.Literal) BinaryOperator(org.logicng.formulas.BinaryOperator) PBConstraint(org.logicng.formulas.PBConstraint)

Example 4 with BinaryOperator

use of org.logicng.formulas.BinaryOperator in project LogicNG by logic-ng.

the class PureExpansionTransformation method apply.

@Override
public Formula apply(final Formula formula, final boolean cache) {
    final FormulaFactory f = formula.factory();
    switch(formula.type()) {
        case FALSE:
        case TRUE:
        case LITERAL:
            return formula;
        case NOT:
            final Not not = (Not) formula;
            return f.not(apply(not.operand(), cache));
        case OR:
        case AND:
            final NAryOperator nary = (NAryOperator) formula;
            final List<Formula> newOps = new ArrayList<>(nary.numberOfOperands());
            for (final Formula op : nary) {
                newOps.add(apply(op, cache));
            }
            return f.naryOperator(formula.type(), newOps);
        case IMPL:
        case EQUIV:
            final BinaryOperator binary = (BinaryOperator) formula;
            final Formula newLeft = apply(binary.left(), cache);
            final Formula newRight = apply(binary.right(), cache);
            return f.binaryOperator(formula.type(), newLeft, newRight);
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            if (pbc.isAmo() || pbc.isExo()) {
                final EncodingResult encodingResult = EncodingResult.resultForFormula(f);
                final Variable[] vars = literalsAsVariables(pbc.operands());
                this.amoEncoder.build(encodingResult, vars);
                final List<Formula> encoding = encodingResult.result();
                if (pbc.isExo()) {
                    encoding.add(f.or(vars));
                }
                return f.and(encoding);
            } else {
                throw new UnsupportedOperationException("Pure encoding for a PBC of type other than AMO or EXO is currently not supported.");
            }
        default:
            throw new IllegalStateException("Unknown formula type: " + formula.type());
    }
}
Also used : Variable(org.logicng.formulas.Variable) ArrayList(java.util.ArrayList) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Not(org.logicng.formulas.Not) EncodingResult(org.logicng.datastructures.EncodingResult) BinaryOperator(org.logicng.formulas.BinaryOperator) NAryOperator(org.logicng.formulas.NAryOperator) PBConstraint(org.logicng.formulas.PBConstraint)

Example 5 with BinaryOperator

use of org.logicng.formulas.BinaryOperator in project LogicNG by logic-ng.

the class LiteralSubstitution method apply.

@Override
public Formula apply(final Formula formula, final boolean cache) {
    final FormulaFactory f = formula.factory();
    switch(formula.type()) {
        case TRUE:
        case FALSE:
            return formula;
        case LITERAL:
            final Literal literal = (Literal) formula;
            Literal lit = this.substitution.get(literal);
            if (lit != null) {
                return lit;
            }
            if (!literal.phase()) {
                lit = this.substitution.get(literal.variable());
                return lit != null ? lit.negate() : formula;
            }
            return formula;
        case NOT:
            return f.not(apply(((Not) formula).operand(), cache));
        case EQUIV:
        case IMPL:
            final BinaryOperator binOp = (BinaryOperator) formula;
            return f.binaryOperator(formula.type(), apply(binOp.left(), cache), apply(binOp.right(), cache));
        case OR:
        case AND:
            final List<Formula> operands = new ArrayList<>();
            for (final Formula op : formula) {
                operands.add(apply(op, cache));
            }
            return f.naryOperator(formula.type(), operands);
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            final Literal[] originalOperands = pbc.operands();
            final Literal[] literals = new Literal[originalOperands.length];
            for (int i = 0; i < literals.length; i++) {
                literals[i] = (Literal) apply(originalOperands[i], false);
            }
            return f.pbc(pbc.comparator(), pbc.rhs(), literals, pbc.coefficients());
        default:
            throw new IllegalArgumentException("Unknown formula type: " + formula.type());
    }
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) Not(org.logicng.formulas.Not) Literal(org.logicng.formulas.Literal) ArrayList(java.util.ArrayList) BinaryOperator(org.logicng.formulas.BinaryOperator) PBConstraint(org.logicng.formulas.PBConstraint) PBConstraint(org.logicng.formulas.PBConstraint)

Aggregations

BinaryOperator (org.logicng.formulas.BinaryOperator)10 Formula (org.logicng.formulas.Formula)8 PBConstraint (org.logicng.formulas.PBConstraint)8 Literal (org.logicng.formulas.Literal)7 Not (org.logicng.formulas.Not)7 NAryOperator (org.logicng.formulas.NAryOperator)6 Variable (org.logicng.formulas.Variable)3 ArrayList (java.util.ArrayList)2 SortedSet (java.util.SortedSet)2 TreeSet (java.util.TreeSet)2 FormulaFactory (org.logicng.formulas.FormulaFactory)2 Iterator (java.util.Iterator)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 EncodingResult (org.logicng.datastructures.EncodingResult)1 And (org.logicng.formulas.And)1 Implication (org.logicng.formulas.Implication)1