/** * Sets the current constraint logic. * * @param logic the constraint logic */ public synchronized void setConstraintLogic(String logic) { // TODO method does not work properly allowing (A and B) or C on Outer Joins deVerify(); if (constraints.isEmpty()) { this.logic = null; } else { this.logic = new LogicExpression(logic); for (String code : constraints.values()) { if (!this.logic.getVariableNames().contains(code)) { this.logic = new LogicExpression("(" + this.logic.toString() + ") and " + code); } } this.logic.removeAllVariablesExcept(constraints.values()); } }
/** * Remove a variable from the expression. * @param name variable to remove */ public void removeVariable(String name) { if (root instanceof Operator) { removeVariable(name, (Operator) root); } else if (root instanceof Variable && ((Variable) root).getName().equals(name)) { throw new IllegalArgumentException("Removing root node"); } String logic = toString(); root = parse(logic); }
/** * Returns a List of logic Strings according to the different outer join sections of the query. * * @return a List of String * @throws PathException if the query does not verify */ public synchronized List<String> getGroupedConstraintLogic() throws PathException { if (logic == null) { return Collections.emptyList(); } Map<String, Set<String>> groups = getConstraintGroups(); List<LogicExpression> grouped = logic.split(new ArrayList<Set<String>>(groups.values())); List<String> retval = new ArrayList<String>(); for (LogicExpression group : grouped) { if (group != null) { retval.add(group.toString()); } } return retval; }
/** * Add a constraint code to a logic expression, ANDed with any constraints already in the * expression, e.g. 'A OR B' + code C -> '(A OR B) AND C'. If the expression is null a new * expression is created. * @param logic an existing constraint logic * @param code the code to add * @return a new logic expression including the new code */ protected static LogicExpression addToConstraintLogic(LogicExpression logic, String code) { LogicExpression newLogic = logic; if (logic == null) { newLogic = new LogicExpression(code); } else { newLogic = new LogicExpression("(" + logic.toString() + ") AND " + code); } return newLogic; }
/** * Remove any variables that aren't in the given set. * @param variables Collection of variable names */ public void removeAllVariablesExcept(Collection<String> variables) { if (root instanceof Operator) { removeAllVariablesExcept(variables, (Operator) root); } else if (root instanceof Variable && !variables.contains(((Variable) root).getName())) { throw new IllegalArgumentException("Removing root node"); } String logic = toString(); root = parse(logic); }
public void test5() { LogicExpression l = new LogicExpression("A and (B or C)"); assertEquals(new HashSet<String>(Arrays.asList("A", "B", "C")), l.getVariableNames()); assertEquals("A and (B or C)", l.toString()); assertEquals(Arrays.asList(new LogicExpression("A"), new LogicExpression("B or C"), null), l.split(Arrays.asList(Arrays.asList("A"), Arrays.asList("B", "C"), empty))); assertEquals(new LogicExpression("A and (B or C)"), l.getSection(Arrays.asList("A", "B", "C"))); assertEquals(Arrays.asList(new LogicExpression("A and (B or C)")), l.split(Arrays.asList(Arrays.asList("A", "B", "C")))); assertTrue(l == l.validateForGroups(Arrays.asList(Arrays.asList("A", "B", "C")))); assertTrue(l == l.validateForGroups(Arrays.asList(Arrays.asList("A"), Arrays.asList("B", "C")))); assertEquals(new LogicExpression("A and B and C"), l.validateForGroups(Arrays.asList(Arrays.asList("A", "B"), Arrays.asList("C")))); }
return null; if (!getVariableNames().containsAll(variables)) { throw new IllegalArgumentException("Unrecognised variables in request"); return this; } else if (root instanceof Or) { if (variables.containsAll(getVariableNames())) { return this; } else { throw new IllegalArgumentException("Expression " + toString() + " cannot be split"); for (Node node : and.getChildren()) { Set<String> hasVariables = new HashSet<String>(); getVariableNames(hasVariables, node); boolean containsAll = true; boolean containsNone = true; return new LogicExpression(retval.toString());
public void testRemoveVariables2() { LogicExpression l = new LogicExpression("(A or B) and (C or D)"); assertEquals(new HashSet<String>(Arrays.asList("A", "B", "C", "D")), l.getVariableNames()); assertEquals("(A or B) and (C or D)", l.toString()); l.removeVariable("A"); assertEquals("B and (C or D)", l.toString()); l.removeVariable("B"); assertEquals("C or D", l.toString()); }
List<Set<String>> groups = new ArrayList<Set<String>>(constraintGroups.values()); try { logic.split(groups); } catch (IllegalArgumentException e) { String oldLogic = logic.toString(); logic = logic.validateForGroups(groups); messages.add("Changed constraint logic from " + oldLogic + " to " + logic.toString() + " because of outer joins");
public void testSimpleBrackets() { new LogicExpression("(A and B)"); }
/** * Returns the current constraint logic. The logic is returned in groups, according to the outer * join layout of the query. Two codes in separate groups can only be combined with an AND * operation and OR operation. * * @return the current constraint logic */ public synchronized String getConstraintLogic() { return (logic == null ? "" : logic.toString()); }
public void testGetPartialString() { LogicExpression l = new LogicExpression("(A and B) or C"); assertEquals("(A and B)", l.getPartialString(Arrays.asList("A", "B"))); assertEquals("(A and B) or C", l.getPartialString(Arrays.asList("A", "C"))); assertEquals("C", l.getPartialString(Arrays.asList("C"))); LogicExpression l2 = new LogicExpression("A and B or C"); assertEquals("(A and B)", l2.getPartialString(Arrays.asList("A"))); assertEquals("(A and B) or C", l2.getPartialString(Arrays.asList("A", "C"))); assertEquals("C", l2.getPartialString(Arrays.asList("C"))); assertEquals("(A and B)", l2.getPartialString(Arrays.asList("A", "B"))); LogicExpression l3 = new LogicExpression("A and (B or C)"); assertEquals("A and (B or C)", l3.getPartialString(Arrays.asList("A", "B"))); assertEquals("A and (B or C)", l3.getPartialString(Arrays.asList("A", "C"))); assertEquals("(B or C)", l3.getPartialString(Arrays.asList("C"))); } }
/** * Removes a constraint from this query. The PathConstraint should be a constraint that already * exists in this query. The constraint will also be removed from the constraint logic. * * @param constraint the PathConstraint to remove from this query * @throws NullPointerException if the constraint is null * @throws NoSuchElementException if the constraint is not present in the query */ public synchronized void removeConstraint(PathConstraint constraint) { deVerify(); if (constraint == null) { throw new NullPointerException("Cannot remove null constraint from this query"); } if (constraints.containsKey(constraint)) { String code = constraints.remove(constraint); if (code != null) { if (logic.getVariableNames().size() > 1) { logic.removeVariable(code); } else { logic = null; } } } else { throw new NoSuchElementException("Constraint to remove is not present in query"); } }
/** * Remove a constraint code from a logic expression, e.g. '(A OR B) AND C' -> 'B AND C'. If * there is only one code in the expression return null. * @param logic an existing constraint logic * @param code the code to remove * @return a new logic expression or null if the expression is now empty */ protected static LogicExpression removeFromConstraintLogic(LogicExpression logic, String code) { if (logic != null) { try { logic.removeVariable(code); } catch (IllegalArgumentException e) { // an IllegalArgumentException is thrown if we try to remove the root node, this // would make an empty expression so we can just set it to null return null; } } return logic; }
/** * Get the Set of variable names. * @return set of variable names in this expression */ public Set<String> getVariableNames() { Set<String> variables = new HashSet<String>(); getVariableNames(variables, root); return variables; }
/** * Return the constraint logic modified to contain only the blocks with editable constraints * @return the string representing the logic expression modified */ public synchronized String getConstraintLogicForEditableConstraints() { List<String> editableConstraintCodes = new ArrayList<String>(); Map<PathConstraint, String> allConstraints = getConstraints(); for (PathConstraint pc : editableConstraints) { editableConstraintCodes.add(allConstraints.get(pc)); } LogicExpression logicExpression = getLogicExpression(); if (logicExpression == null) { return ""; } return getLogicExpression().getPartialString(editableConstraintCodes); } }
/** * Remove any variables that aren't in the given set. * * @param variables Collection of variable names * @param node root of subtree */ private void removeAllVariablesExcept(Collection<String> variables, Operator node) { for (Node child : new ArrayList<Node>(node.getChildren())) { if (child instanceof Operator) { removeAllVariablesExcept(variables, (Operator) child); } else if (child instanceof Variable && !variables.contains(((Variable) child).getName())) { node.removeChild(child); } } }
/** * Returns the constraint logic for the given outer join group, if the query verifies correctly. * * @param group an outer join group * @return the constraint logic for the constraints in that outer join group * @throws PathException if the query does not verify * @throws IllegalArgumentException if the group is not present in this query */ public synchronized LogicExpression getConstraintLogicForGroup(String group) throws PathException { List<String> problems = verifyQuery(); if (problems.isEmpty()) { if (logic == null) { return null; } else { Set<String> codes = constraintGroups.get(group); if (codes == null) { throw new IllegalArgumentException("Outer join group " + group + " does not seem to be in this query. Valid inputs are " + constraintGroups.keySet()); } if (codes.isEmpty()) { return null; } else { return logic.getSection(codes); } } } throw new PathException("Query does not verify: " + problems, null); }
if (!doNotVerifyLogic) { try { logic.split(new ArrayList<Set<String>>(constraintGroups.values())); } catch (IllegalArgumentException e) { problems.add("Logic expression is not compatible with outer join status: "
/** * Add a constraint code to a logic expression, ANDed with any constraints already in the * expression, e.g. 'A OR B' + code C -> '(A OR B) AND C'. If the expression is null a new * expression is created. * @param logic an existing constraint logic * @param code the code to add * @return a new logic expression including the new code */ protected static LogicExpression addToConstraintLogic(LogicExpression logic, String code) { LogicExpression newLogic = logic; if (logic == null) { newLogic = new LogicExpression(code); } else { newLogic = new LogicExpression("(" + logic.toString() + ") AND " + code); } return newLogic; }