src/main/java/com/upcrob/tagexp/Evaluator.java
private boolean eval(Node root, Set<String> tags, boolean caseSensitive) {
		switch (root.type) {
			case OR:
				// Do a logical OR on all children of this node
				for (Node n : root.children) {
					if (eval(n, tags, caseSensitive)) {
						// Return true if any of the
						// sub-trees are true
						return true;
					}
				}
				return false;
			case XOR:
				// Do a logical XOR on all children of this node
				boolean found = false;
				for (Node n : root.children) {
					if (eval(n, tags, caseSensitive)) {
						if (found) {
							// Sub-tree already evaluated to
							// true, return false
							return false;
						}
						found = true;
					}
				}
				return found;
			case AND:
				// Do a logical AND on all children of this node
				for (Node n : root.children) {
					if (!eval(n, tags, caseSensitive)) {
						// Return false if any of the
						// sub-trees are false
						return false;
					}
				}
				return true;
			case NOT:
				// Do a logical NOT on this node's sub-tree
				Node child = root.children.get(0);
				return !eval(child, tags, caseSensitive);
			case TERM:
				// Determine if this term is in the tag set
				if (caseSensitive) {
					// Case-sensitive search
					return tags.contains(root.value);
				} else {
					// Case-insensitive search
					for (String tag : tags) {
						if (tag.equalsIgnoreCase(root.value)) {
							return true;
						}
					}
					return false;
				}
		}
		throw new ParseException("Invalid parse tree.");
	}
/**
	 * Helper method that does most of the evaluation work.
	 */
Helper method that does most of the evaluation work.
Helper method that does most of the evaluation work .
private boolean eval(Node root, Set<String> tags, boolean caseSensitive) {
		switch (root.type) {
			case OR:
				for (Node n : root.children) {
					if (eval(n, tags, caseSensitive)) {
						return true;
					}
				}
				return false;
			case XOR:
				boolean found = false;
				for (Node n : root.children) {
					if (eval(n, tags, caseSensitive)) {
						if (found) {
							return false;
						}
						found = true;
					}
				}
				return found;
			case AND:
				for (Node n : root.children) {
					if (!eval(n, tags, caseSensitive)) {
						return false;
					}
				}
				return true;
			case NOT:
				Node child = root.children.get(0);
				return !eval(child, tags, caseSensitive);
			case TERM:
				if (caseSensitive) {
					return tags.contains(root.value);
				} else {
					for (String tag : tags) {
						if (tag.equalsIgnoreCase(root.value)) {
							return true;
						}
					}
					return false;
				}
		}
		throw new ParseException("Invalid parse tree.");
	}
private boolean eval ( Node root , Set < String > tags , boolean caseSensitive ) { switch ( root . type ) { case OR : for ( Node n : root . children ) { if ( eval ( n , tags , caseSensitive ) ) { return true ; } } return false ; case XOR : boolean found = false ; for ( Node n : root . children ) { if ( eval ( n , tags , caseSensitive ) ) { if ( found ) { return false ; } found = true ; } } return found ; case AND : for ( Node n : root . children ) { if ( ! eval ( n , tags , caseSensitive ) ) { return false ; } } return true ; case NOT : Node child = root . children . get ( 0 ) ; return ! eval ( child , tags , caseSensitive ) ; case TERM : if ( caseSensitive ) { return tags . contains ( root . value ) ; } else { for ( String tag : tags ) { if ( tag . equalsIgnoreCase ( root . value ) ) { return true ; } } return false ; } } throw new ParseException ( "Invalid parse tree." ) ; }
Helper method that does most of the evaluation work.
Helper method that does most of the evaluation work .
// Do a logical OR on all children of this node // Return true if any of the // sub-trees are true // Do a logical XOR on all children of this node // Sub-tree already evaluated to // true, return false // Do a logical AND on all children of this node // Return false if any of the // sub-trees are false // Do a logical NOT on this node's sub-tree // Determine if this term is in the tag set // Case-sensitive search // Case-insensitive search