/** * Canonicalize this query * * @return null if the query is valid, an error message if it is invalid */ public static String canonicalize(QueryTree query) { ListIterator<Item> rootItemIterator = query.getItemIterator(); CanonicalizationResult result = recursivelyCanonicalize(rootItemIterator.next(), rootItemIterator); if (query.isEmpty() && ! result.isError()) result = CanonicalizationResult.error("No query"); return result.error().orElse(null); // preserve old API, unfortunately }
static public void andQueryItemWithRoot(QueryTree tree, Item item) { if (tree.isEmpty()) { tree.setRoot(item); } else { Item oldRoot = tree.getRoot(); if (oldRoot.getClass() == AndItem.class) { ((AndItem) oldRoot).addItem(item); } else { AndItem newRoot = new AndItem(); newRoot.addItem(oldRoot); newRoot.addItem(item); tree.setRoot(newRoot); } } }
/** Modifies this query to become the current query AND the given item */ // TODO: Make sure this is complete, unit test and make it public private void and(Item item) { if (isEmpty()) { setRoot(item); } else if (getRoot() instanceof NotItem && item instanceof NotItem) { throw new IllegalArgumentException("Can't AND two NOTs"); // TODO: Complete } else if (getRoot() instanceof NotItem){ NotItem notItem = (NotItem)getRoot(); notItem.addPositiveItem(item); } else if (item instanceof NotItem){ NotItem notItem = (NotItem)item; notItem.addPositiveItem(getRoot()); setRoot(notItem); } else { AndItem andItem = new AndItem(); andItem.addItem(getRoot()); andItem.addItem(item); setRoot(andItem); } }