org.dmg.pmml.Field Java Examples

The following examples show how to use org.dmg.pmml.Field. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RegexTokenizerConverter.java    From jpmml-sparkml with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public List<Feature> encodeFeatures(SparkMLEncoder encoder){
	RegexTokenizer transformer = getTransformer();

	if(!transformer.getGaps()){
		throw new IllegalArgumentException("Expected splitter mode, got token matching mode");
	} // End if

	if(transformer.getMinTokenLength() != 1){
		throw new IllegalArgumentException("Expected 1 as minimum token length, got " + transformer.getMinTokenLength() + " as minimum token length");
	}

	Feature feature = encoder.getOnlyFeature(transformer.getInputCol());

	Field<?> field = feature.getField();

	if(transformer.getToLowercase()){
		Apply apply = PMMLUtil.createApply(PMMLFunctions.LOWERCASE, feature.ref());

		field = encoder.createDerivedField(FeatureUtil.createName("lowercase", feature), OpType.CATEGORICAL, DataType.STRING, apply);
	}

	return Collections.singletonList(new DocumentFeature(encoder, field, transformer.getPattern()));
}
 
Example #2
Source File: ModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
protected boolean assessPurity(){
	List<InputField> inputFields = getInputFields();

	for(InputField inputField : inputFields){
		Field<?> field = inputField.getField();
		MiningField miningField = inputField.getMiningField();

		if(!InputFieldUtil.isDefault(field, miningField)){
			return false;
		}
	}

	if(hasLocalDerivedFields() || hasOutputFields()){
		return false;
	}

	return true;
}
 
Example #3
Source File: ModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
protected boolean assessParentCompatibility(){
	List<InputField> inputFields = getInputFields();

	for(InputField inputField : inputFields){
		Field<?> field = inputField.getField();
		MiningField miningField = inputField.getMiningField();

		if(!(field instanceof DataField)){
			continue;
		} // End if

		if(!InputFieldUtil.isDefault(field, miningField)){
			return false;
		}
	}

	return true;
}
 
Example #4
Source File: ModelManager.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
protected Field<?> resolveField(FieldName name){
	ListMultimap<FieldName, Field<?>> visibleFields = getVisibleFields();

	List<Field<?>> fields = visibleFields.get(name);

	if(fields.isEmpty()){
		return null;
	} else

	if(fields.size() == 1){
		return fields.get(0);
	} else

	{
		throw new DuplicateFieldException(name);
	}
}
 
Example #5
Source File: ModelEvaluationContext.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private Field<?> resolveField(FieldName name, MiningModelEvaluationContext context){

	while(context != null){
		OutputField outputField = context.getOutputField(name);
		if(outputField != null){
			return outputField;
		}

		DerivedField localDerivedField = context.getLocalDerivedField(name);
		if(localDerivedField != null){
			return localDerivedField;
		}

		context = context.getParent();
	}

	return null;
}
 
Example #6
Source File: FieldResolver.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void declareGlobalFields(PMML pmml, boolean transformations){
	List<Field<?>> scope = this.scopes.get(pmml);

	if(scope != null){
		scope.clear();
	}

	DataDictionary dataDictionary = pmml.getDataDictionary();
	if(dataDictionary != null && dataDictionary.hasDataFields()){
		declareFields(pmml, dataDictionary.getDataFields());
	}

	TransformationDictionary transformationDictionary = pmml.getTransformationDictionary();
	if(transformations && (transformationDictionary != null && transformationDictionary.hasDerivedFields())){
		declareFields(pmml, transformationDictionary.getDerivedFields());
	}
}
 
Example #7
Source File: ValueParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public VisitorAction visit(Value value){
	PMMLObject parent = getParent();

	Object simpleValue = value.getValue();
	if(simpleValue == null){
		throw new MissingAttributeException(value, PMMLAttributes.VALUE_VALUE);
	} // End if

	if(parent instanceof Field){
		Field<?> field = (Field<?>)parent;

		DataType dataType = field.getDataType();
		if(dataType != null){
			simpleValue = safeParseOrCast(dataType, simpleValue);

			value.setValue(simpleValue);
		}
	}

	return super.visit(value);
}
 
Example #8
Source File: FieldUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private <F extends Field<F> & HasContinuousDomain<F>> RangeSet<Double> parseValidRanges(F field){
	RangeSet<Double> result = TreeRangeSet.create();

	if(field.hasIntervals()){
		List<Interval> intervals = field.getIntervals();

		for(Interval interval : intervals){
			Range<Double> range = DiscretizationUtil.toRange(interval);

			result.add(range);
		}
	}

	return result;
}
 
Example #9
Source File: InputFieldUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
static
public FieldValue prepareInputValue(Field<?> field, MiningField miningField, Object value){
	InputTypeInfo typeInfo = getTypeInfo(field, miningField);

	if(value instanceof Collection){
		Collection<?> rawValues = (Collection<?>)value;

		List<Object> pmmlValues = new ArrayList<>(rawValues.size());

		for(Object rawValue : rawValues){
			FieldValue fieldValue = prepareScalarInputValue(typeInfo, rawValue);

			pmmlValues.add(FieldValueUtil.getValue(fieldValue));
		}

		return createInputValue(typeInfo, pmmlValues);
	} else

	{
		return prepareScalarInputValue(typeInfo, value);
	}
}
 
Example #10
Source File: FieldUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public OpType getOpType(Field<?> field, MiningField miningField, Target target){
	OpType opType = field.getOpType();

	// "A MiningField overrides a (Data)Field, and a Target overrides a MiningField"
	if(miningField != null){
		opType = miningField.getOpType(opType);

		if(target != null){
			opType = target.getOpType(opType);
		}
	}

	return opType;
}
 
Example #11
Source File: FieldUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public OpType getOpType(Field<?> field, MiningField miningField){
	OpType opType = field.getOpType();

	// "A MiningField overrides a (Data)Field"
	if(miningField != null){
		opType = miningField.getOpType(opType);
	}

	return opType;
}
 
Example #12
Source File: FieldUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private <F extends Field<F> & HasDiscreteDomain<F>> List<Object> parseValidValues(F field){
	List<Object> result = new ArrayList<>();

	DataType dataType = field.getDataType();
	if(dataType == null){
		throw new MissingAttributeException(MissingAttributeException.formatMessage(XPathUtil.formatElement(field.getClass()) + "@dataType"), field);
	} // End if

	if(field.hasValues()){
		List<Value> pmmlValues = field.getValues();

		for(Value pmmlValue : pmmlValues){
			Object simpleValue = pmmlValue.getValue();
			if(simpleValue == null){
				throw new MissingAttributeException(pmmlValue, PMMLAttributes.VALUE_VALUE);
			}

			Value.Property property = pmmlValue.getProperty();
			switch(property){
				case VALID:
					result.add(TypeUtil.parseOrCast(dataType, simpleValue));
					break;
				default:
					break;
			}
		}
	}

	return result;
}
 
Example #13
Source File: AbstractParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
protected DataType resolveTargetDataType(FieldName name){
	DataType dataType = this.dataTypes.get(name);

	if(dataType == null && !this.dataTypes.containsKey(name)){
		Collection<Field<?>> fields = getFields();

		for(Field<?> field : fields){

			if((name).equals(field.getName())){

				if(dataType != null){
					throw new DuplicateFieldException(name);
				}

				dataType = field.getDataType();
			}
		}

		if(dataType == null){
			throw new MissingFieldException(name);
		}

		this.dataTypes.put(name, dataType);
	}

	return dataType;
}
 
Example #14
Source File: AbstractParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
protected DataType resolveDataType(FieldName name){
	DataType dataType = this.dataTypes.get(name);

	if(dataType == null && !this.dataTypes.containsKey(name)){
		Collection<Field<?>> fields = getFields();

		for(Field<?> field : fields){

			if((name).equals(field.getName())){

				if((dataType == null) || (dataType).equals(field.getDataType())){
					dataType = field.getDataType();
				} else

				// Two or more conflicting data types
				{
					dataType = null;

					break;
				}
			}
		}

		this.dataTypes.put(name, dataType);
	}

	return dataType;
}
 
Example #15
Source File: ModelEvaluationContext.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private FieldValue inheritOrPrepareInputValue(Field<?> field, MiningField miningField, FieldValue value){

	if(InputFieldUtil.isDefault(field, miningField)){
		return value;
	}

	return InputFieldUtil.prepareInputValue(field, miningField, FieldValueUtil.getValue(value));
}
 
Example #16
Source File: ModelManager.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
protected List<InputField> createInputFields(MiningField.UsageType usageType){
	M model = getModel();

	MiningSchema miningSchema = model.getMiningSchema();

	List<InputField> inputFields = new ArrayList<>();

	if(miningSchema.hasMiningFields()){
		List<MiningField> miningFields = miningSchema.getMiningFields();

		for(MiningField miningField : miningFields){
			FieldName name = miningField.getName();

			if(!(miningField.getUsageType()).equals(usageType)){
				continue;
			}

			Field<?> field = getDataField(name);
			if(field == null){
				field = new VariableField(name);
			}

			InputField inputField = new InputField(field, miningField);

			inputFields.add(inputField);
		}
	}

	return ImmutableList.copyOf(inputFields);
}
 
Example #17
Source File: ModelManager.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
protected ListMultimap<FieldName, Field<?>> getVisibleFields(){

		if(this.visibleFields == null){
			this.visibleFields = collectVisibleFields();
		}

		return this.visibleFields;
	}
 
Example #18
Source File: InputFieldUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private ScalarValue performInvalidValueTreatment(InputTypeInfo typeInfo, Object value){
	MiningField miningField = typeInfo.getMiningField();

	InvalidValueTreatmentMethod invalidValueTreatmentMethod = miningField.getInvalidValueTreatment();
	switch(invalidValueTreatmentMethod){
		case AS_IS:
			break;
		case AS_MISSING:
		case RETURN_INVALID:
			Object invalidValueReplacement = miningField.getInvalidValueReplacement();
			if(invalidValueReplacement != null){
				throw new MisplacedAttributeException(miningField, PMMLAttributes.MININGFIELD_INVALIDVALUEREPLACEMENT, invalidValueReplacement);
			}
			break;
		default:
			throw new UnsupportedAttributeException(miningField, invalidValueTreatmentMethod);
	} // End switch

	switch(invalidValueTreatmentMethod){
		case RETURN_INVALID:
			Field<?> field = typeInfo.getField();

			throw new InvalidResultException("Field " + PMMLException.formatKey(field.getName()) + " cannot accept user input value " + PMMLException.formatValue(value), miningField);
		case AS_IS:
			return createInvalidInputValue(typeInfo, value);
		case AS_MISSING:
			return createMissingInputValue(typeInfo);
		default:
			throw new UnsupportedAttributeException(miningField, invalidValueTreatmentMethod);
	}
}
 
Example #19
Source File: InputField.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <p>
 * Returns the domain of valid values for this continuous field.
 * If specified, then all input values that are contained in this set shall be considered valid, and all others invalid.
 * If not specified, then all input values shall be considered valid.
 * </p>
 *
 * @return A non-empty set, or <code>null</code>.
 *
 * @see #getOpType()
 */
public RangeSet<Double> getContinuousDomain(){
	Field<?> field = getField();

	if(field instanceof HasContinuousDomain){
		RangeSet<Double> validRanges = FieldUtil.getValidRanges((Field & HasContinuousDomain)field);

		if(validRanges != null && !validRanges.isEmpty()){
			return validRanges;
		}
	}

	return null;
}
 
Example #20
Source File: InputField.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
public InputField(Field<?> field, MiningField miningField){
	super(field);

	setMiningField(Objects.requireNonNull(miningField));

	if(!Objects.equals(field.getName(), miningField.getName())){
		throw new IllegalArgumentException();
	}
}
 
Example #21
Source File: InputFieldUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private ScalarValue performMissingValueTreatment(InputTypeInfo typeInfo){
	MiningField miningField = typeInfo.getMiningField();

	MissingValueTreatmentMethod missingValueTreatmentMethod = miningField.getMissingValueTreatment();
	if(missingValueTreatmentMethod == null){
		missingValueTreatmentMethod = MissingValueTreatmentMethod.AS_IS;
	}

	switch(missingValueTreatmentMethod){
		case AS_IS:
		case AS_MEAN:
		case AS_MODE:
		case AS_MEDIAN:
		case AS_VALUE:
			return createMissingInputValue(typeInfo);
		case RETURN_INVALID:
			Field<?> field = typeInfo.getField();

			Object missingValueReplacement = miningField.getMissingValueReplacement();
			if(missingValueReplacement != null){
				throw new MisplacedAttributeException(miningField, PMMLAttributes.MININGFIELD_MISSINGVALUEREPLACEMENT, missingValueReplacement);
			}

			throw new InvalidResultException("Field " + PMMLException.formatKey(field.getName()) + " requires user input value", miningField);
		default:
			throw new UnsupportedAttributeException(miningField, missingValueTreatmentMethod);
	}
}
 
Example #22
Source File: NeuralNetworkEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private Expression getOutputExpression(NeuralOutput neuralOutput){
	DerivedField derivedField = neuralOutput.getDerivedField();
	if(derivedField == null){
		throw new MissingElementException(neuralOutput, PMMLElements.NEURALOUTPUT_DERIVEDFIELD);
	}

	Expression expression = ExpressionUtil.ensureExpression(derivedField);

	if(expression instanceof FieldRef){
		FieldRef fieldRef = (FieldRef)expression;

		FieldName name = fieldRef.getField();
		if(name == null){
			throw new MissingAttributeException(fieldRef, org.dmg.pmml.PMMLAttributes.FIELDREF_FIELD);
		}

		Field<?> field = resolveField(name);
		if(field == null){
			throw new MissingFieldException(name, fieldRef);
		} // End if

		if(field instanceof DataField){
			return expression;
		} else

		if(field instanceof DerivedField){
			DerivedField targetDerivedField = (DerivedField)field;

			Expression targetExpression = ExpressionUtil.ensureExpression(targetDerivedField);

			return targetExpression;
		} else

		{
			throw new InvalidAttributeException(fieldRef, org.dmg.pmml.PMMLAttributes.FIELDREF_FIELD, name);
		}
	}

	return expression;
}
 
Example #23
Source File: FieldHasher.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public VisitorAction visit(Field<?> field){
	FieldName name = field.getName();

	if(name != null){
		this.mappings.put(name, hash(name));
	}

	return super.visit(field);
}
 
Example #24
Source File: FieldResolverTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static
private void checkFields(Set<FieldName> names, Collection<Field<?>> fields){
	Set<FieldName> fieldNames = fields.stream()
		.map(field -> field.getName())
		.collect(Collectors.toSet());

	assertEquals(names, fieldNames);
}
 
Example #25
Source File: FieldResolver.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void declareFields(PMMLObject object, Collection<? extends Field<?>> fields){
	List<Field<?>> scope = this.scopes.get(object);

	if(scope == null){
		scope = new ArrayList<>(fields.size());

		this.scopes.put(object, scope);
	}

	scope.addAll(fields);
}
 
Example #26
Source File: FieldResolver.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void declareLocalFields(Model model, boolean transformations){
	List<Field<?>> scope = this.scopes.get(model);

	if(scope != null){
		scope.clear();
	}

	LocalTransformations localTransformations = model.getLocalTransformations();
	if(transformations && (localTransformations != null && localTransformations.hasDerivedFields())){
		declareFields(model, localTransformations.getDerivedFields());
	}
}
 
Example #27
Source File: FieldResolver.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private List<Field<?>> getScope(PMMLObject object){

		if(this.customScopes.size() > 0){
			return this.customScopes.getOrDefault(object, this.scopes.get(object));
		} else

		{
			return this.scopes.get(object);
		}
	}
 
Example #28
Source File: StringNormalizer.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public List<Feature> encodeFeatures(List<Feature> features, SkLearnEncoder encoder){
	String function = getFunction();
	Boolean trimBlanks = getTrimBlanks();

	if(function == null && !trimBlanks){
		return features;
	}

	List<Feature> result = new ArrayList<>();

	for(Feature feature : features){
		Expression expression = feature.ref();

		if(function != null){
			expression = PMMLUtil.createApply(translateFunction(function), expression);
		} // End if

		if(trimBlanks){
			expression = PMMLUtil.createApply(PMMLFunctions.TRIMBLANKS, expression);
		}

		Field<?> field = encoder.toCategorical(feature.getName(), Collections.emptyList());

		// XXX: Should have been set by the previous transformer
		field.setDataType(DataType.STRING);

		DerivedField derivedField = encoder.createDerivedField(FeatureUtil.createName("normalize", feature), OpType.CATEGORICAL, DataType.STRING, expression);

		feature = new StringFeature(encoder, derivedField);

		result.add(feature);
	}

	return result;
}
 
Example #29
Source File: FieldResolver.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Collection<Field<?>> getFields(PMMLObject... virtualParents){
	Deque<PMMLObject> parents = new ArrayDeque<>(getParents());

	for(PMMLObject virtualParent : virtualParents){
		parents.push(virtualParent);
	}

	return getFields(parents);
}
 
Example #30
Source File: Formula.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
public void addField(Field<?> field){
	RExpEncoder encoder = getEncoder();

	Feature feature = new ContinuousFeature(encoder, field);

	if(field instanceof DerivedField){
		DerivedField derivedField = (DerivedField)field;

		Expression expression = derivedField.getExpression();
		if(expression instanceof Apply){
			Apply apply = (Apply)expression;

			if(checkApply(apply, PMMLFunctions.POW, FieldRef.class, Constant.class)){
				List<Expression> expressions = apply.getExpressions();

				FieldRef fieldRef = (FieldRef)expressions.get(0);
				Constant constant = (Constant)expressions.get(1);

				try {
					String string = ValueUtil.asString(constant.getValue());

					int power = Integer.parseInt(string);

					feature = new PowerFeature(encoder, fieldRef.getField(), DataType.DOUBLE, power);
				} catch(NumberFormatException nfe){
					// Ignored
				}
			}
		}
	}

	putFeature(field.getName(), feature);

	this.fields.add(field);
}