/** * Adds an item. The first item is the positive * the rest is negative */ public void addItem(Item item) { super.addItem(item); }
/** Return a deep copy of this object */ public SegmentItem clone() { SegmentItem copy; synchronized(this) { boolean tmpLock = locked; locked = false; copy = (SegmentItem) super.clone(); locked = tmpLock; copy.locked = tmpLock; } return copy; }
private void addItem(CompositeItem parent,int index,Item item,TermType desiredParentType) { if (parent instanceof NotItem) { if (index==0 && parent.getItem(0)==null) { // Case 1: The current positive is null and we are adding a positive parent.setItem(0,item); } else if (index<=1 && !(parent.getItem(0) instanceof CompositeItem)) { // Case 2: The positive must become a composite CompositeItem positiveComposite=(CompositeItem)desiredParentType.createItemClass(); positiveComposite.addItem(parent.getItem(0)); positiveComposite.addItem(index,item); parent.setItem(0,positiveComposite); } else if (parent.getItem(0)!=null && parent.getItem(0) instanceof CompositeItem // Case 3: Add to the positive composite && index<=((CompositeItem)parent.getItem(0)).getItemCount()) { ((CompositeItem)parent.getItem(0)).addItem(index,item); } else { // Case 4: Add negative parent.addItem(index,item); } } else if (parent.getItemCount()>0 && parent instanceof QueryTree) { CompositeItem composite=(CompositeItem)desiredParentType.createItemClass(); composite.addItem(parent.getItem(0)); composite.addItem(index,item); parent.setItem(0,composite); } else { parent.addItem(index,item); } }
public void ensureNotInSubtree(CompositeItem item) { for (Iterator<Item> i = item.getItemIterator(); i.hasNext();) { Item possibleCycle = i.next(); if (this == possibleCycle) { throw new QueryException("Cannot add " + item + " to " + this + " as it would create a cycle"); } else if (possibleCycle instanceof CompositeItem) { ensureNotInSubtree((CompositeItem) possibleCycle); } } }
private void insertIncompatibleItem(Item item,CompositeItem parent,Query query,TermType desiredParentType) { // Create new parent CompositeItem newParent; if (desiredParentType==TermType.DEFAULT) newParent=new AndItem(); else newParent=(CompositeItem)desiredParentType.createItemClass(); // Save previous parent parent CompositeItem parentsParent=parent.getParent(); // Add items to new parent newParent.addItem(parent); newParent.addItem(item); // Insert new parent as root or child of old parents parent if (parentsParent==null) { query.getModel().getQueryTree().setRoot(newParent); } else { int parentIndex=0; if (parentsParent!=null) { parentIndex=parentsParent.getItemIndex(parent); } parentsParent.setItem(parentIndex,newParent); } }
/** * Splits the given item into n-grams and adds them as a CompositeItem containing WordItems searching the * index of the input term. If the result is a single gram, that single WordItem is returned rather than the AndItem * * @param term the term to split, must be an item which implement the IndexedItem and BlockItem "mixins" * @param text the text of the item, just stringValue() if the item is a TermItem * @param gramSize the gram size to split to * @param query the query in which this rewriting is done * @return the root of the query subtree produced by this, containing the split items */ protected Item splitToGrams(Item term, String text, int gramSize, Query query) { String index = ((HasIndexItem)term).getIndexName(); CompositeItem gramsItem = createGramRoot(query); gramsItem.setIndexName(index); Substring origin = ((BlockItem)term).getOrigin(); for (Iterator<GramSplitter.Gram> i = getGramSplitter().split(text,gramSize); i.hasNext(); ) { GramSplitter.Gram gram = i.next(); WordItem gramWord = new WordItem(gram.extractFrom(text), index, false, origin); gramWord.setWeight(term.getWeight()); gramWord.setProtected(true); gramsItem.addItem(gramWord); } return gramsItem.getItemCount()==1 ? gramsItem.getItem(0) : gramsItem; // return the AndItem, or just the single gram if not multiple }
} else if (topLevelItem instanceof CompositeItem && !(topLevelItem instanceof SegmentItem)) { ((CompositeItem) topLevelItem).addItem(item); expectingOperator = true; } else { && ((CompositeItem) topLevelItem).getItemCount() == 1) { return ((CompositeItem) topLevelItem).removeItem(0);
/** * Convert segment items into their mutable counterpart, do not update query tree. * Non-segment items are returned directly. * * @return a mutable CompositeItem instance */ private CompositeItem convertSegmentItem(CompositeItem item) { if (!(item instanceof SegmentItem)) { return item; } CompositeItem converted = null; if (item instanceof AndSegmentItem) { converted = new AndItem(); } else if (item instanceof PhraseSegmentItem) { PhraseItem p = new PhraseItem(); PhraseSegmentItem old = (PhraseSegmentItem) item; p.setIndexName(old.getIndexName()); converted = p; } else { // TODO: Do something else than nothing for unknowns? return item; } for (Iterator<Item> i = item.getItemIterator(); i.hasNext();) { converted.addItem(i.next()); } return converted; }
parent.removeItem(matchIndex); parent.addItem(matchIndex, expansionGrp); } else { RewriterUtils.log(logger, query, "Single root item");
public int encode(ByteBuffer buffer) { encodeThis(buffer); int itemCount = 1; for (Iterator<Item> i = getItemIterator(); i.hasNext();) { Item subitem = i.next(); itemCount += subitem.encode(buffer); } return itemCount; }
private void fixConnexity(CompositeItem copy) { List<Item> flatland = new ArrayList<>(); List<Item> flatCopy = new ArrayList<>(); taggingFlatten(this, flatland); taggingFlatten(copy, flatCopy); int barrier = flatland.size(); for (int i = 0; i < barrier; ++i) { Item orig = flatland.get(i); int connectedTo = find(orig.connectedItem, flatland); if (connectedTo >= 0) { TaggableItem tagged = (TaggableItem) flatCopy.get(i); tagged.setConnectivity(flatCopy.get(connectedTo), orig.connectivity); } } }
/** Returns a deep copy of this item */ public CompositeItem clone() { CompositeItem copy = (CompositeItem) super.clone(); copy.subitems = new java.util.ArrayList<>(); for (Item subItem : subitems) { Item subItemCopy = subItem.clone(); copy.adding(subItemCopy); copy.subitems.add(subItemCopy); } fixConnexity(copy); return copy; }
public void add(Item o) { Item newItem = o; owner.adding(newItem); // TODO: Change current here? Check javadoc wrapped.add(o); }
@Override public boolean equals(Object o) { if( !(o instanceof QueryTree)) return false; return super.equals(o); }
@Override public void disclose(Discloser discloser) { super.disclose(discloser); discloser.addProperty("limit", distance); }
protected void encodeThis(ByteBuffer buffer) { super.encodeThis(buffer); IntegerCompressor.putCompressedPositiveNumber(distance, buffer); }
/** * Encodes just this item, not it's usual subitems, to the given buffer. */ protected void encodeThis(ByteBuffer buffer) { super.encodeThis(buffer); IntegerCompressor.putCompressedPositiveNumber(encodingArity(), buffer); }
protected void adding(Item item) { Validator.ensureNotNull("A composite item child", item); Validator.ensure("Attempted to add a composite to itself", item != this); if (item instanceof CompositeItem) { ensureNotInSubtree((CompositeItem) item); } item.setParent(this); }