Logic library provides a convenient way of representing a logic expression. It also allows you to perform basic operations with a logic expression(generate a truth table, find the solution with a given tuple of values). Additionally, the library features include a generation of a random logic expression, equivalent and not equivalent to a particular expression.
##Usage ####LogicExpression class First of all, you should instantiate a LogicExpression class by passing string representation into its constructor:
LogicExpression expression = new LogicExpression("p" + LogicSymbols.AND + "q" + LogicSymbols.EQU + "r");
Note that you can use either a set of LogicSymbols constants or raw unicode symbols, while creating an object:
LogicExpression expression2 = new LogicExpression("p∧q→r");
There are four operations supported by this library:
- conjunction (
LogicSymbols.AND
or \u2227 unicode symbol); - disjunction (
LogicSymbols.OR
or \u2227 unicode symbol); - implication (
LogicSymbols.IMP
or \u2227 unicode symbol); - equivalence (
LogicSymbols.EQU
or \u2227 unicode symbol).
Before performing some operation, a reversed polish notation should be created, you can do it by calling makeRPolishRecord method:
expression.makeRPolishRecord();
This method creates a notation and stores it in a California property. After that, this property is used in other methods of the class. Let's see all the rest of the methods:
EOperand calculate()
method performs a computation with existing values stored in aoperands
property;EOperand calculate(params bool[] x)
method performs a computation with given tuple of boolean values;TruthTable generateTruthTable()
method generates a truth table.
First of all, you should create an object-generator:
LEGenerator g = new LEGenerator();
LEGenerator class has an interface that's similar to a Random class. There are few methods in the class:
LogicExpression next()
generates a random logic expression;LogicExpression next(int maxVarsNumber)
does as same as thenext
method, but it allows you to limit a max amount of variables;LogicExpression nextEqual(LogicExpression exp)
alters the expression using propositional logic transformation rules;LogicExpression nextNotEqual(LogicExpression exp)
generates a not equal expression to the passed one;LogicExpression nextNotEqual(LogicExpression exp, int maxVarsNumber)
does the same as thenextNotEqual
method with limitation of a max amount of variables.
Also, there are few additional classes in the library:
- ExpressionPart represents a single part of a logic expression(bracket, operation, operand ect.);
- EOperand is one of the ExpressionPart derived classes, represents a particular operand in logic expression, there's a boolean "value" property that's used in calculate methods;
- TruthTable represents truth table for a particular logic expression, contains method isTautology() for its further analyzying.