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>
,java.util.function.Predicate<E>
public class LogicExpression<E> extends java.lang.Object implements com.google.common.base.Predicate<E>
A logic expression engine that operates over user specified objects.
-
-
Field Summary
Fields Modifier and Type Field Description private Expression.Apply<E>
expression
-
Constructor Summary
Constructors Modifier Constructor Description protected
LogicExpression(java.util.List<Expression<E>> expressions)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
apply(E entity)
static <E> Expression.Apply<E>
buildAst(java.util.List<Expression<E>> rpn)
Compile a rpn list of tokens into an expression tree.static <E> LogicExpression<E>
compile(java.lang.String input, com.google.common.base.Function<java.lang.String,Expression.Arg<E>> factoryDelegate)
Helper factory method to instantiate a LogicExpression.static <E> LogicExpression<E>
compile(java.util.List<Expression<E>> expressions)
Compile an infix list of tokens into an expression tree.java.util.List<java.lang.String>
getArgs()
Return a list of the arguments contained in the expression.private void
getArgs(Expression.Apply<?> apply, java.util.List<java.lang.String> args)
Private helper method to recursively find arguments.boolean
isEmpty()
If the expression is empty, it returns true for all inputs.static void
main(java.lang.String[] args)
Iteractively interpret logic statements from stdin such as "true | (true & false)".java.util.List<Expression<E>>
rpn(java.util.List<Expression<E>> tokens)
Converts an infix logic representation into a postfix logic representation.java.lang.String
toString()
-
-
-
Field Detail
-
expression
private final Expression.Apply<E> expression
-
-
Constructor Detail
-
LogicExpression
protected LogicExpression(java.util.List<Expression<E>> expressions) throws LogicException.TokenizeLogicException, LogicException.CompileLogicException
- Parameters:
input
- an infix representation of the logic expression.- Throws:
LogicException.TokenizeLogicException
LogicException.CompileLogicException
-
-
Method Detail
-
compile
public static <E> LogicExpression<E> compile(java.util.List<Expression<E>> expressions)
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(java.lang.String input, com.google.common.base.Function<java.lang.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
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
isEmpty
public boolean isEmpty()
If the expression is empty, it returns true for all inputs.- Returns:
- true iff the expression is empty.
-
apply
public boolean apply(E entity)
- Specified by:
apply
in interfacecom.google.common.base.Predicate<E>
-
buildAst
public static <E> Expression.Apply<E> buildAst(java.util.List<Expression<E>> rpn)
Compile a rpn list of tokens into an expression tree.- Parameters:
rpn
- a list of tokens in infix form.- Returns:
- an expression tree.
-
getArgs
public java.util.List<java.lang.String> getArgs()
Return a list of the arguments contained in the expression.- Returns:
-
getArgs
private void getArgs(Expression.Apply<?> apply, java.util.List<java.lang.String> args)
Private helper method to recursively find arguments.- Parameters:
apply
- the expression tree to search.args
- the resulting list of arguments.
-
rpn
public java.util.List<Expression<E>> rpn(java.util.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
public static void main(java.lang.String[] args)
Iteractively interpret logic statements from stdin such as "true | (true & false)".- Parameters:
args
-
-
-