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;
}
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;
}
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();
}
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();
}
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);
}
Aggregations