Search in sources :

Example 6 with Formula

use of org.logicng.formulas.Formula in project ITSTools by lip6.

the class GalToLogicNG method toGal.

private BooleanExpression toGal(Formula f) {
    if (f instanceof Variable) {
        Variable v = (Variable) f;
        return EcoreUtil.copy(map.get(v.name()));
    } else if (f instanceof org.logicng.formulas.Or) {
        org.logicng.formulas.Or or = (org.logicng.formulas.Or) f;
        BooleanExpression res = GalFactory.eINSTANCE.createFalse();
        for (Formula elt : or) {
            res = GF2.or(res, toGal(elt));
        }
        return res;
    } else if (f instanceof org.logicng.formulas.And) {
        org.logicng.formulas.And and = (org.logicng.formulas.And) f;
        BooleanExpression res = GalFactory.eINSTANCE.createTrue();
        for (Formula elt : and) {
            res = GF2.and(res, toGal(elt));
        }
        return res;
    } else if (f instanceof Literal) {
        Literal l = (Literal) f;
        return GF2.not(toGal(l.variable()));
    } else if (f instanceof org.logicng.formulas.Not) {
        org.logicng.formulas.Not l = (org.logicng.formulas.Not) f;
        return GF2.not(toGal(l.operand()));
    } else if (f instanceof CFalse) {
        return GalFactory.eINSTANCE.createFalse();
    } else if (f instanceof CTrue) {
        return GalFactory.eINSTANCE.createTrue();
    } else {
        System.out.println("error");
    }
    return null;
}
Also used : Variable(org.logicng.formulas.Variable) Or(fr.lip6.move.gal.Or) CTrue(org.logicng.formulas.CTrue) Formula(org.logicng.formulas.Formula) BooleanExpression(fr.lip6.move.gal.BooleanExpression) Not(fr.lip6.move.gal.Not) And(fr.lip6.move.gal.And) Literal(org.logicng.formulas.Literal) CFalse(org.logicng.formulas.CFalse)

Example 7 with Formula

use of org.logicng.formulas.Formula in project ITSTools by lip6.

the class ToLogicNG method toLogic.

private BooleanExpression toLogic(Formula f) {
    if (f instanceof Variable) {
        Variable v = (Variable) f;
        return EcoreUtil.copy(map.get(v.name()));
    } else if (f instanceof org.logicng.formulas.Or) {
        org.logicng.formulas.Or or = (org.logicng.formulas.Or) f;
        Or oc = LogicFactory.eINSTANCE.createOr();
        oc.setLeft(LogicFactory.eINSTANCE.createFalse());
        for (Formula elt : or) {
            oc.setRight(toLogic(elt));
            Or oc2 = LogicFactory.eINSTANCE.createOr();
            oc2.setLeft(oc);
            oc = oc2;
        }
        return oc.getLeft();
    } else if (f instanceof org.logicng.formulas.And) {
        org.logicng.formulas.And or = (org.logicng.formulas.And) f;
        And oc = LogicFactory.eINSTANCE.createAnd();
        oc.setLeft(LogicFactory.eINSTANCE.createTrue());
        for (Formula elt : or) {
            oc.setRight(toLogic(elt));
            And oc2 = LogicFactory.eINSTANCE.createAnd();
            oc2.setLeft(oc);
            oc = oc2;
        }
        return oc.getLeft();
    } else if (f instanceof Literal) {
        Literal l = (Literal) f;
        Not n = LogicFactory.eINSTANCE.createNot();
        n.setValue(toLogic(l.variable()));
        return n;
    } else if (f instanceof org.logicng.formulas.Not) {
        org.logicng.formulas.Not l = (org.logicng.formulas.Not) f;
        Not n = LogicFactory.eINSTANCE.createNot();
        n.setValue(toLogic(l.operand()));
        return n;
    } else if (f instanceof CFalse) {
        return LogicFactory.eINSTANCE.createFalse();
    } else if (f instanceof CTrue) {
        return LogicFactory.eINSTANCE.createTrue();
    } else {
        System.out.println("error");
    }
    return null;
}
Also used : Variable(org.logicng.formulas.Variable) Or(fr.lip6.move.gal.logic.Or) CTrue(org.logicng.formulas.CTrue) Formula(org.logicng.formulas.Formula) Not(fr.lip6.move.gal.logic.Not) And(fr.lip6.move.gal.logic.And) Literal(org.logicng.formulas.Literal) CFalse(org.logicng.formulas.CFalse)

Example 8 with Formula

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

the class FormulaStringRepresentation method naryOperator.

/**
 * Returns the string representation of an n-ary operator.
 * @param operator the n-ary operator
 * @param opString the operator string
 * @return the string representation
 */
protected String naryOperator(final NAryOperator operator, final String opString) {
    final StringBuilder sb = new StringBuilder();
    int count = 0;
    final int size = operator.numberOfOperands();
    Formula last = null;
    for (final Formula op : operator) {
        if (++count == size) {
            last = op;
        } else {
            sb.append(operator.type().precedence() < op.type().precedence() ? this.toInnerString(op) : this.bracket(op));
            sb.append(opString);
        }
    }
    if (last != null) {
        sb.append(operator.type().precedence() < last.type().precedence() ? this.toInnerString(last) : this.bracket(last));
    }
    return sb.toString();
}
Also used : Formula(org.logicng.formulas.Formula) PBConstraint(org.logicng.formulas.PBConstraint)

Example 9 with Formula

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

the class SortedStringRepresentation method naryOperator.

/**
 * Returns the sorted string representation of an n-ary operator.
 * @param operator the n-ary operator
 * @param opString the operator string
 * @return the string representation
 */
@Override
protected String naryOperator(final NAryOperator operator, final String opString) {
    final List<Formula> operands = new ArrayList<>();
    for (final Formula op : operator) {
        operands.add(op);
    }
    final int size = operator.numberOfOperands();
    operands.sort(this.comparator);
    final StringBuilder sb = new StringBuilder();
    int count = 0;
    Formula last = null;
    for (final Formula op : operands) {
        if (++count == size) {
            last = op;
        } else {
            sb.append(operator.type().precedence() < op.type().precedence() ? toInnerString(op) : bracket(op));
            sb.append(opString);
        }
    }
    if (last != null) {
        sb.append(operator.type().precedence() < last.type().precedence() ? toInnerString(last) : bracket(last));
    }
    return sb.toString();
}
Also used : Formula(org.logicng.formulas.Formula) ArrayList(java.util.ArrayList) PBConstraint(org.logicng.formulas.PBConstraint)

Example 10 with Formula

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

the class SortedStringRepresentation method sortedEquivalence.

/**
 * Returns the string representation of an equivalence.
 * @param equivalence the equivalence
 * @return the string representation
 */
private String sortedEquivalence(final Equivalence equivalence) {
    final Formula right;
    final Formula left;
    if (this.comparator.compare(equivalence.left(), equivalence.right()) <= 0) {
        right = equivalence.right();
        left = equivalence.left();
    } else {
        right = equivalence.left();
        left = equivalence.right();
    }
    final String leftString = FType.EQUIV.precedence() < left.type().precedence() ? toInnerString(left) : bracket(left);
    final String rightString = FType.EQUIV.precedence() < right.type().precedence() ? toInnerString(right) : bracket(right);
    return String.format("%s%s%s", leftString, equivalence(), rightString);
}
Also used : Formula(org.logicng.formulas.Formula)

Aggregations

Formula (org.logicng.formulas.Formula)349 Test (org.junit.jupiter.api.Test)190 FormulaFactory (org.logicng.formulas.FormulaFactory)129 ArrayList (java.util.ArrayList)58 Variable (org.logicng.formulas.Variable)55 Literal (org.logicng.formulas.Literal)54 Assignment (org.logicng.datastructures.Assignment)50 PropositionalParser (org.logicng.io.parsers.PropositionalParser)50 PBConstraint (org.logicng.formulas.PBConstraint)45 SATSolver (org.logicng.solvers.SATSolver)36 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)25 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)25 MethodSource (org.junit.jupiter.params.provider.MethodSource)24 TreeSet (java.util.TreeSet)21 BDDKernel (org.logicng.knowledgecompilation.bdds.jbuddy.BDDKernel)20 TautologyPredicate (org.logicng.predicates.satisfiability.TautologyPredicate)20 HashMap (java.util.HashMap)17 LinkedHashSet (java.util.LinkedHashSet)17 List (java.util.List)17 LogicNGTest (org.logicng.LogicNGTest)16