public void apply(SDField field) { WeightedSetDataType ctype = (WeightedSetDataType) field.getDataType(); if (createIfNonExistent != null) { field.setDataType(DataType.getWeightedSet(ctype.getNestedType(), createIfNonExistent, ctype.removeIfZero())); } ctype = (WeightedSetDataType) field.getDataType(); if (removeIfZero != null) { field.setDataType(DataType.getWeightedSet(ctype.getNestedType(), ctype.createIfNonExistent(), removeIfZero)); } ctype = (WeightedSetDataType) field.getDataType(); for (Object o : field.getAttributes().values()) { Attribute attribute = (Attribute) o; attribute.setRemoveIfZero(ctype.removeIfZero()); attribute.setCreateIfNonExistent(ctype.createIfNonExistent()); } }
/** * Returns a weighted set datatype, where the elements are of the given type, and which supports the properties * createIfNonExistent and removeIfZero * * @param type the type to create a weighted set of * @param createIfNonExistent whether the type has createIfNonExistent * @param removeIfZero whether the type has removeIfZero * @return the weighted set data type */ public static WeightedSetDataType getWeightedSet(DataType type, boolean createIfNonExistent, boolean removeIfZero) { return new WeightedSetDataType(type, createIfNonExistent, removeIfZero); }
@SuppressWarnings({ "unchecked", "rawtypes" }) private static FieldValue asWset(WeightedSetDataType wsetType, List<FieldValue> values) { WeightedSet out = wsetType.createFieldValue(); for (FieldValue val : values) { if (val == null) { continue; } out.putAll((WeightedSet)val); } return out; } }
public WeightedSetDataType(DataType nestedType, boolean createIfNonExistent, boolean removeIfZero) { this(nestedType, createIfNonExistent, removeIfZero, 0); if ((nestedType == STRING) && createIfNonExistent && removeIfZero) { // the tag type definition setId(18); } else { setId(getName().toLowerCase().hashCode()); } }
private void replaceTemporaryTypesInWeightedSet(WeightedSetDataType weightedSetDataType, List<DataType> seenStructs) { if (weightedSetDataType.getNestedType() instanceof TemporaryDataType) { weightedSetDataType.setNestedType(getDataType(weightedSetDataType.getNestedType().getCode(), "")); } else { replaceTemporaryTypes(weightedSetDataType.getNestedType(), seenStructs); } }
/** Creates an attribute with default settings */ public Attribute(String name, DataType fieldType) { this(name, convertDataType(fieldType), convertCollectionType(fieldType), convertTensorType(fieldType), convertTargetType(fieldType)); setRemoveIfZero(fieldType instanceof WeightedSetDataType ? ((WeightedSetDataType)fieldType).removeIfZero() : false); setCreateIfNonExistent(fieldType instanceof WeightedSetDataType ? ((WeightedSetDataType)fieldType).createIfNonExistent() : false); }
private void processField(Search search, SDField uriField) { String uriName = uriField.getName(); uriField.setStemming(Stemming.NONE); DataType generatedType = DataType.STRING; if (uriField.getDataType() instanceof ArrayDataType) { generatedType = new ArrayDataType(DataType.STRING); } else if (uriField.getDataType() instanceof WeightedSetDataType) { WeightedSetDataType wdt = (WeightedSetDataType) uriField.getDataType(); generatedType = new WeightedSetDataType(DataType.STRING, wdt.createIfNonExistent(), wdt.removeIfZero()); } for (String suffix : URL_SUFFIX) { String partName = uriName + "." + suffix; // I wonder if this is explicit in qrs or implicit in backend? // search.addFieldSetItem(uriName, partName); SDField partField = new SDField(partName, generatedType, true); partField.setIndexStructureField(uriField.doesIndexing()); partField.setRankType(uriField.getRankType()); partField.setStemming(Stemming.NONE); partField.getNormalizing().inferLowercase(); if (uriField.getIndex(suffix) != null) { partField.addIndex(uriField.getIndex(suffix)); } search.addExtraField(partField); search.fieldSets().addBuiltInFieldSetItem(BuiltInFieldSets.INTERNAL_FIELDSET_NAME, partField.getName()); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) private FieldValue convertWset(WeightedSet val) { Map<FieldValue, Integer> next = new LinkedHashMap<FieldValue, Integer>(); DataType nextType = null; for (Iterator<FieldValue> it = val.fieldValueIterator(); it.hasNext();) { FieldValue prevKey = it.next(); Integer prevVal = val.get(prevKey); FieldValue nextKey = convert(prevKey); if (nextKey == null) { continue; } if (nextType == null) { nextType = nextKey.getDataType(); } else if (!nextType.isValueCompatible(nextKey)) { throw new IllegalArgumentException("Expected " + nextType.getName() + ", got " + nextKey.getDataType().getName() + "."); } next.put(nextKey, prevVal); } if (nextType == null) { return null; } WeightedSet ret = DataType.getWeightedSet(nextType, val.getDataType().createIfNonExistent(), val.getDataType().removeIfZero()).createFieldValue(); for (Map.Entry<FieldValue, Integer> entry : next.entrySet()) { ret.put(entry.getKey(), entry.getValue()); } return ret; }
@Override public FieldPath buildFieldPath(String remainFieldName) { return MapDataType.buildFieldPath(remainFieldName, getNestedType(), DataType.INT); }
public WeightedSetDataType(DataType nestedType, boolean createIfNonExistent, boolean removeIfZero, int id) { super(createName(nestedType, createIfNonExistent, removeIfZero), id, nestedType); this.createIfNonExistent = createIfNonExistent; this.removeIfZero = removeIfZero; }
@Override public void process(boolean validate, boolean documentsOnly) { for (SDField field : search.allConcreteFields()) { if (field.getDataType() instanceof WeightedSetDataType && ((WeightedSetDataType)field.getDataType()).isTag()) implementTagType(field); } }
@Override public void assign(Object o) { if (!checkAssign(o)) { return; } if (o instanceof WeightedSet) { WeightedSet wset = (WeightedSet) o; if (getDataType().equals(wset.getDataType())) { map.assign(wset.map); } else { throw new IllegalArgumentException("Cannot assign a weighted set of type " + wset.getDataType() + " to a weighted set of type " + getDataType()); } } else if (o instanceof Map) { map = new WeightedSetWrapper((Map)o, map.getDataType()); } else { throw new IllegalArgumentException("Class " + o.getClass() + " not applicable to an " + this.getClass() + " instance."); } }
@Override public FieldValue applyTo(FieldValue fval) { if (fval instanceof Array) { Array array = (Array) fval; FieldValue element = array.getFieldValue(((IntegerFieldValue) value).getInteger()); element = update.applyTo(element); array.set(((IntegerFieldValue) value).getInteger(), element); } else if (fval instanceof WeightedSet) { WeightedSet wset = (WeightedSet) fval; WeightedSetDataType wtype = wset.getDataType(); Integer weight = wset.get(value); if (weight == null) { if (wtype.createIfNonExistent() && update instanceof ArithmeticValueUpdate) { weight = 0; } else { return fval; } } weight = (Integer) update.applyTo(new IntegerFieldValue(weight)).getWrappedValue(); wset.put(value, weight); if (wtype.removeIfZero() && update instanceof ArithmeticValueUpdate && weight == 0) { wset.remove(value); } } return fval; }
private static FieldValue keyTypeForMapUpdate(TokenBuffer.Token element, DataType expectedType) { FieldValue v; if (expectedType instanceof ArrayDataType) { v = new IntegerFieldValue(Integer.valueOf(element.text)); } else if (expectedType instanceof WeightedSetDataType) { v = ((WeightedSetDataType) expectedType).getNestedType().createFieldValue(element.text); } else if (expectedType instanceof MapDataType) { v = ((MapDataType) expectedType).getKeyType().createFieldValue(element.text); } else { throw new IllegalArgumentException("Container type " + expectedType + " not supported for match update."); } return v; } }
public WeightedSetDataType(String typeName, int code, DataType nestedType, boolean createIfNonExistent, boolean removeIfZero) { super(typeName != null ? createName(nestedType, createIfNonExistent, removeIfZero) : null, code, nestedType); if ((code >= 0) && (code <= DataType.lastPredefinedDataTypeId()) && (code != 18)) // 18 == DataType.TAG.getId() is not yet initialized throw new IllegalArgumentException("Cannot create a weighted set datatype with code " + code); this.createIfNonExistent = createIfNonExistent; this.removeIfZero = removeIfZero; }
private void buildConfig(WeightedSetDataType type, DocumenttypesConfig.Documenttype.Datatype.Builder dataTypeBuilder, DocumenttypesConfig.Documenttype.Builder documentBuilder, Set<Integer> built) { dataTypeBuilder.type(DocumenttypesConfig.Documenttype.Datatype.Type.Enum.WSET). wset(new DocumenttypesConfig.Documenttype.Datatype.Wset.Builder(). key(new DocumenttypesConfig.Documenttype.Datatype.Wset.Key.Builder(). id(type.getNestedType().getId())). createifnonexistent(type.createIfNonExistent()). removeifzero(type.removeIfZero())); buildConfig(type.getNestedType(), documentBuilder, built); }
@Override protected void doVerify(VerificationContext context) { DataType input = context.getValue(); if (input instanceof ArrayDataType || input instanceof WeightedSetDataType) { context.setValue(((CollectionDataType)input).getNestedType()).execute(exp); if (input instanceof ArrayDataType) { context.setValue(DataType.getArray(context.getValue())); } else { WeightedSetDataType wset = (WeightedSetDataType)input; context.setValue(DataType.getWeightedSet(context.getValue(), wset.createIfNonExistent(), wset.removeIfZero())); } } else if (input instanceof StructDataType) { for (Field field : ((StructDataType)input).getFields()) { DataType fieldType = field.getDataType(); DataType valueType = context.setValue(fieldType).execute(exp).getValue(); if (!fieldType.isAssignableFrom(valueType)) { throw new VerificationException(this, "Expected " + fieldType.getName() + " output, got " + valueType.getName() + "."); } } context.setValue(input); } else { throw new VerificationException(this, "Expected Array, Struct or WeightedSet input, got " + input.getName() + "."); } }
public <T extends FieldValue> void read(FieldBase field, WeightedSet<T> value) { try { while (reader.hasNext()) { int type = reader.next(); if (type == XMLStreamReader.START_ELEMENT) { if ("item".equals(reader.getName().toString())) { FieldValue fv = value.getDataType().getNestedType().createFieldValue(); int weight = 1; for (int i = 0; i < reader.getAttributeCount(); i++) { if ("weight".equals(reader.getAttributeName(i).toString())) { weight = Integer.parseInt(reader.getAttributeValue(i)); } } deserializeFieldValue(field, fv); // noinspection unchecked value.put((T)fv, weight); skipToEnd("item"); } else { throw newDeserializeException(field, "Illegal tag " + reader.getName() + " expected 'item'"); } } else if (type == XMLStreamReader.END_ELEMENT) { return; } } } catch (XMLStreamException e) { throw newException(field, e); } }
private static void registerWeightedSetType(DocumentTypeManager manager, int id, DocumentmanagerConfig.Datatype.Weightedsettype wset) { DataType nestedType = manager.getDataType(wset.datatype(), ""); WeightedSetDataType type = new WeightedSetDataType( nestedType, wset.createifnonexistant(), wset.removeifzero(), id); manager.register(type); }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override protected void doExecute(ExecutionContext ctx) { FieldValue input = ctx.getValue(); DataType inputType = input.getDataType(); WeightedSetDataType outputType = DataType.getWeightedSet(inputType, createIfNonExistent, removeIfZero); WeightedSet output = outputType.createFieldValue(); output.add(input); ctx.setValue(output); }