Search in sources :

Example 1 with NAryOperator

use of org.logicng.formulas.NAryOperator 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 NAryOperator

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

the class SortedStringRepresentation method toInnerString.

/**
 * Returns the sorted string representation of the given formula.
 * @param formula the formula
 * @return the sorted string representation of the formula with regard to the variable ordering
 */
@Override
public String toInnerString(final Formula formula) {
    switch(formula.type()) {
        case FALSE:
            return falsum();
        case TRUE:
            return verum();
        case LITERAL:
            final Literal lit = (Literal) formula;
            return lit.phase() ? lit.name() : negation() + lit.name();
        case NOT:
            final Not not = (Not) formula;
            return negation() + bracket(not.operand());
        case IMPL:
            return binaryOperator((BinaryOperator) formula, implication());
        case EQUIV:
            return sortedEquivalence((Equivalence) formula);
        case AND:
        case OR:
            final NAryOperator nary = (NAryOperator) formula;
            final String op = formula.type() == FType.AND ? and() : or();
            return naryOperator(nary, String.format("%s", op));
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            return String.format("%s%s%d", pbLhs(pbc.operands(), pbc.coefficients()), 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) NAryOperator(org.logicng.formulas.NAryOperator) PBConstraint(org.logicng.formulas.PBConstraint)

Example 3 with NAryOperator

use of org.logicng.formulas.NAryOperator 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 4 with NAryOperator

use of org.logicng.formulas.NAryOperator 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 NAryOperator

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

the class FactorOutSimplifier method applyRec.

private Formula applyRec(final Formula formula, final boolean cache) {
    switch(formula.type()) {
        case OR:
        case AND:
            final List<Formula> newOps = new ArrayList<>();
            for (final Formula op : formula) {
                newOps.add(apply(op, cache));
            }
            final Formula newFormula = formula.factory().naryOperator(formula.type(), newOps);
            return newFormula instanceof NAryOperator ? simplify((NAryOperator) newFormula) : newFormula;
        case NOT:
            return apply(((Not) formula).operand(), cache).negate();
        case FALSE:
        case TRUE:
        case LITERAL:
        case IMPL:
        case EQUIV:
        case PBC:
            return formula;
        default:
            throw new IllegalStateException("Unknown formula type: " + formula.type());
    }
}
Also used : Formula(org.logicng.formulas.Formula) Not(org.logicng.formulas.Not) ArrayList(java.util.ArrayList) NAryOperator(org.logicng.formulas.NAryOperator)

Aggregations

NAryOperator (org.logicng.formulas.NAryOperator)10 Formula (org.logicng.formulas.Formula)8 Not (org.logicng.formulas.Not)8 BinaryOperator (org.logicng.formulas.BinaryOperator)6 PBConstraint (org.logicng.formulas.PBConstraint)6 Literal (org.logicng.formulas.Literal)5 ArrayList (java.util.ArrayList)4 FormulaFactory (org.logicng.formulas.FormulaFactory)3 SortedSet (java.util.SortedSet)2 TreeSet (java.util.TreeSet)2 Variable (org.logicng.formulas.Variable)2 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 EncodingResult (org.logicng.datastructures.EncodingResult)1 FType (org.logicng.formulas.FType)1 FType.dual (org.logicng.formulas.FType.dual)1 FormulaTransformation (org.logicng.formulas.FormulaTransformation)1 Pair (org.logicng.util.Pair)1