Class LogicExpression<E>
java.lang.Object
edu.washington.cs.knowitall.logic.LogicExpression<E>
- Type Parameters:
E
- the type of the base expressions
- All Implemented Interfaces:
com.google.common.base.Predicate<E>
,Predicate<E>
A logic expression engine that operates over user specified objects.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
static <E> Expression.Apply<E>
buildAst
(List<Expression<E>> rpn) Compile a rpn list of tokens into an expression tree.static <E> LogicExpression<E>
compile
(String input, com.google.common.base.Function<String, Expression.Arg<E>> factoryDelegate) Helper factory method to instantiate a LogicExpression.static <E> LogicExpression<E>
compile
(List<Expression<E>> expressions) Compile an infix list of tokens into an expression tree.getArgs()
Return a list of the arguments contained in the expression.private void
getArgs
(Expression.Apply<?> apply, List<String> args) Private helper method to recursively find arguments.boolean
isEmpty()
If the expression is empty, it returns true for all inputs.static void
Iteractively interpret logic statements from stdin such as "true | (true & false)".List<Expression<E>>
rpn
(List<Expression<E>> tokens) Converts an infix logic representation into a postfix logic representation.toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.google.common.base.Predicate
equals, test
-
Field Details
-
expression
-
-
Constructor Details
-
LogicExpression
protected LogicExpression(List<Expression<E>> expressions) throws LogicException.TokenizeLogicException, LogicException.CompileLogicException - Parameters:
input
- an infix representation of the logic expression.- Throws:
LogicException.TokenizeLogicException
LogicException.CompileLogicException
-
-
Method Details
-
compile
Compile an infix list of tokens into an expression tree.- Parameters:
rpn
- a list of tokens in infix form.- Returns:
- an expression tree.
-
compile
public static <E> LogicExpression<E> compile(String input, com.google.common.base.Function<String, Expression.Arg<E>> factoryDelegate) Helper factory method to instantiate a LogicExpression.- Parameters:
input
- The string to parse.factoryDelegate
- The factory to build tokens.- Returns:
- a new LogicExpression
-
toString
-
isEmpty
public boolean isEmpty()If the expression is empty, it returns true for all inputs.- Returns:
- true iff the expression is empty.
-
apply
- Specified by:
apply
in interfacecom.google.common.base.Predicate<E>
-
buildAst
Compile a rpn list of tokens into an expression tree.- Parameters:
rpn
- a list of tokens in infix form.- Returns:
- an expression tree.
-
getArgs
Return a list of the arguments contained in the expression.- Returns:
-
getArgs
Private helper method to recursively find arguments.- Parameters:
apply
- the expression tree to search.args
- the resulting list of arguments.
-
rpn
public List<Expression<E>> rpn(List<Expression<E>> tokens) throws LogicException.CompileLogicException Converts an infix logic representation into a postfix logic representation.- Parameters:
tokens
- a list of tokens in infix form.- Returns:
- a list of tokens in postfix (rpn) form.
- Throws:
LogicException.CompileLogicException
-
main
Iteractively interpret logic statements from stdin such as "true | (true & false)".- Parameters:
args
-
-