Search in sources :

Example 1 with Literal

use of org.logicng.formulas.Literal in project feature-diagram by MontiCore.

the class FD2Formula method visit.

@Override
public void visit(ASTXorGroup node) {
    Set<String> xorGroup = getFeatures(node);
    Set<Literal> G = xorGroup.stream().map(this::Var).collect(Collectors.toSet());
    Formula min1 = ff.or(G);
    Formula max1 = ff.and(Sets.combinations(G, 2).stream().map(ff::and).map(ff::not).collect(Collectors.toList()));
    formulas.add(ff.implication(Var(current), ff.and(min1, max1)));
}
Also used : Formula(org.logicng.formulas.Formula) Literal(org.logicng.formulas.Literal)

Example 2 with Literal

use of org.logicng.formulas.Literal 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 3 with Literal

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

use of org.logicng.formulas.Literal 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 5 with Literal

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

Aggregations

Literal (org.logicng.formulas.Literal)115 Formula (org.logicng.formulas.Formula)51 ArrayList (java.util.ArrayList)38 Test (org.junit.jupiter.api.Test)32 Variable (org.logicng.formulas.Variable)29 FormulaFactory (org.logicng.formulas.FormulaFactory)25 PBConstraint (org.logicng.formulas.PBConstraint)21 TreeSet (java.util.TreeSet)17 SATSolver (org.logicng.solvers.SATSolver)14 Assignment (org.logicng.datastructures.Assignment)13 LNGVector (org.logicng.collections.LNGVector)12 HashMap (java.util.HashMap)11 LogicNGTest (org.logicng.LogicNGTest)11 LNGIntVector (org.logicng.collections.LNGIntVector)8 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)8 PropositionalParser (org.logicng.io.parsers.PropositionalParser)8 BufferedReader (java.io.BufferedReader)7 FileReader (java.io.FileReader)7 BinaryOperator (org.logicng.formulas.BinaryOperator)7 Not (org.logicng.formulas.Not)7