@NonNull private CompositeItem buildAnd(OperatorNode<ExpressionOperator> ast) { AndItem andItem = new AndItem(); NotItem notItem = new NotItem(); convertVarArgsAnd(ast, 0, andItem, notItem); Preconditions .checkArgument(andItem.getItemCount() > 0, "Vespa does not support AND with no logically positive branches."); if (notItem.getItemCount() == 0) { return andItem; } if (andItem.getItemCount() == 1) { notItem.setPositiveItem(andItem.getItem(0)); } else { notItem.setPositiveItem(andItem); } return notItem; }
/** * Convenience method for adding a positive item. * If a positive item is already present * the positive item becomes an AndItem with the items added */ public void addPositiveItem(Item item) { if (getPositiveItem() == null) { setPositiveItem(item); } else if (getPositiveItem() instanceof AndItem) { ((AndItem) getPositiveItem()).addItem(item); } else { AndItem positives = new AndItem(); positives.addItem(getPositiveItem()); positives.addItem(item); setPositiveItem(positives); } }
notItem.setPositiveItem(newPositive);
private static CompositeItem extractAndNot(AndItem parent) { NotItem theOnlyNot = null; for (int i = 0; i < parent.getItemCount(); i++) { Item child = parent.getItem(i); if (child instanceof NotItem) { NotItem thisNot = (NotItem) child; parent.setItem(i, thisNot.getPositiveItem()); if (theOnlyNot == null) { theOnlyNot = thisNot; theOnlyNot.setPositiveItem(parent); } else { for (int j=1; j < thisNot.getItemCount(); j++) { theOnlyNot.addNegativeItem(thisNot.getItem(j)); } } } } return (theOnlyNot != null) ? theOnlyNot : parent; }
protected Item parseItemsBody() { // Algorithm: Collect positive, negative, and and'ed items, then combine. AndItem and=null; NotItem not=null; // Store negatives here as we go Item current; // Find all items do { current=negativeItem(); if (current!=null) { not=addNot(current,not); continue; } current=positiveItem(); if (current==null) current = indexableItem(); if (current == null) current = compositeItem(); if (current!=null) and=addAnd(current,and); if (current == null) tokens.skip(); } while (tokens.hasNext()); // Combine the items Item topLevel=and; if (not!=null && topLevel!=null) { not.setPositiveItem(topLevel); topLevel=not; } return simplifyUnnecessaryComposites(topLevel); }
not.setPositiveItem(topLevel); topLevel=not;