Search in sources :

Example 1 with Or

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

the class FormulaFactoryImporter method apply.

@Override
public Formula apply(final Formula formula, final boolean cache) {
    if (formula.factory() == this.newFormulaFactory) {
        return formula;
    }
    switch(formula.type()) {
        case TRUE:
            return this.newFormulaFactory.verum();
        case FALSE:
            return this.newFormulaFactory.falsum();
        case LITERAL:
            final Literal literal = (Literal) formula;
            return this.newFormulaFactory.literal(literal.name(), literal.phase());
        case NOT:
            final Not not = (Not) formula;
            return this.newFormulaFactory.not(apply(not.operand(), cache));
        case IMPL:
            final Implication implication = (Implication) formula;
            return this.newFormulaFactory.implication(apply(implication.left(), cache), apply(implication.right(), cache));
        case EQUIV:
            final Equivalence equivalence = (Equivalence) formula;
            return this.newFormulaFactory.equivalence(apply(equivalence.left(), cache), apply(equivalence.right(), cache));
        case OR:
            final Or or = (Or) formula;
            return this.newFormulaFactory.or(gatherAppliedOperands(or));
        case AND:
            final And and = (And) formula;
            return this.newFormulaFactory.and(gatherAppliedOperands(and));
        case PBC:
            final PBConstraint pbc = (PBConstraint) formula;
            final Literal[] literals = new Literal[pbc.operands().length];
            for (int i = 0; i < pbc.operands().length; i++) {
                literals[i] = (Literal) apply(pbc.operands()[i], cache);
            }
            return this.newFormulaFactory.pbc(pbc.comparator(), pbc.rhs(), literals, pbc.coefficients());
        default:
            throw new IllegalArgumentException("Unknown LogicNG formula type: " + formula.type());
    }
}
Also used : Not(org.logicng.formulas.Not) Or(org.logicng.formulas.Or) Equivalence(org.logicng.formulas.Equivalence) And(org.logicng.formulas.And) Literal(org.logicng.formulas.Literal) Implication(org.logicng.formulas.Implication) PBConstraint(org.logicng.formulas.PBConstraint) PBConstraint(org.logicng.formulas.PBConstraint)

Example 2 with Or

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

the class FormulaRandomizerTest method testMaximumOperandsOr.

@Test
public void testMaximumOperandsOr() {
    final FormulaRandomizer random = new FormulaRandomizer(this.f, FormulaRandomizerConfig.builder().maximumOperandsOr(10).seed(42).build());
    for (int i = 0; i < 100; i++) {
        final Formula formula = random.or(1);
        assertThat(formula.type()).isEqualTo(FType.OR);
        final Or or = (Or) formula;
        assertThat(or.numberOfOperands()).isLessThanOrEqualTo(10);
    }
}
Also used : Formula(org.logicng.formulas.Formula) Or(org.logicng.formulas.Or) PBConstraint(org.logicng.formulas.PBConstraint) CardinalityConstraint(org.logicng.formulas.CardinalityConstraint) Test(org.junit.jupiter.api.Test)

Aggregations

Or (org.logicng.formulas.Or)2 PBConstraint (org.logicng.formulas.PBConstraint)2 Test (org.junit.jupiter.api.Test)1 And (org.logicng.formulas.And)1 CardinalityConstraint (org.logicng.formulas.CardinalityConstraint)1 Equivalence (org.logicng.formulas.Equivalence)1 Formula (org.logicng.formulas.Formula)1 Implication (org.logicng.formulas.Implication)1 Literal (org.logicng.formulas.Literal)1 Not (org.logicng.formulas.Not)1