Java Code Examples for org.dmg.pmml.PMML#getDataDictionary()

The following examples show how to use org.dmg.pmml.PMML#getDataDictionary() . 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: KMeansPMMLUtils.java    From oryx with Apache License 2.0 6 votes vote down vote up
/**
 * Validates that the encoded PMML model received matches expected schema.
 *
 * @param pmml {@link PMML} encoding of KMeans Clustering
 * @param schema expected schema attributes of KMeans Clustering
 */
public static void validatePMMLVsSchema(PMML pmml, InputSchema schema) {
  List<Model> models = pmml.getModels();
  Preconditions.checkArgument(models.size() == 1,
      "Should have exactly one model, but had %s", models.size());

  Model model = models.get(0);
  Preconditions.checkArgument(model instanceof ClusteringModel);
  Preconditions.checkArgument(model.getMiningFunction() == MiningFunction.CLUSTERING);

  DataDictionary dictionary = pmml.getDataDictionary();
  Preconditions.checkArgument(
      schema.getFeatureNames().equals(AppPMMLUtils.getFeatureNames(dictionary)),
      "Feature names in schema don't match names in PMML");

  MiningSchema miningSchema = model.getMiningSchema();
  Preconditions.checkArgument(schema.getFeatureNames().equals(
      AppPMMLUtils.getFeatureNames(miningSchema)));

}
 
Example 2
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 3
Source File: RDFPMMLUtils.java    From oryx with Apache License 2.0 5 votes vote down vote up
/**
 * Validates that the encoded PMML model received matches expected schema.
 *
 * @param pmml {@link PMML} encoding of a decision forest
 * @param schema expected schema attributes of decision forest
 */
public static void validatePMMLVsSchema(PMML pmml, InputSchema schema) {
  List<Model> models = pmml.getModels();
  Preconditions.checkArgument(models.size() == 1,
                              "Should have exactly one model, but had %s", models.size());

  Model model = models.get(0);
  MiningFunction function = model.getMiningFunction();
  if (schema.isClassification()) {
    Preconditions.checkArgument(function == MiningFunction.CLASSIFICATION,
                                "Expected classification function type but got %s",
                                function);
  } else {
    Preconditions.checkArgument(function == MiningFunction.REGRESSION,
                                "Expected regression function type but got %s",
                                function);
  }

  DataDictionary dictionary = pmml.getDataDictionary();
  Preconditions.checkArgument(
      schema.getFeatureNames().equals(AppPMMLUtils.getFeatureNames(dictionary)),
      "Feature names in schema don't match names in PMML");

  MiningSchema miningSchema = model.getMiningSchema();
  Preconditions.checkArgument(schema.getFeatureNames().equals(
      AppPMMLUtils.getFeatureNames(miningSchema)));

  Integer pmmlIndex = AppPMMLUtils.findTargetIndex(miningSchema);
  if (schema.hasTarget()) {
    int schemaIndex = schema.getTargetFeatureIndex();
    Preconditions.checkArgument(
        pmmlIndex != null && schemaIndex == pmmlIndex,
        "Configured schema expects target at index %s, but PMML has target at index %s",
        schemaIndex, pmmlIndex);
  } else {
    Preconditions.checkArgument(pmmlIndex == null);
  }
}
 
Example 4
Source File: ModelManager.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
protected ModelManager(PMML pmml, M model){
	setPMML(Objects.requireNonNull(pmml));
	setModel(Objects.requireNonNull(model));

	DataDictionary dataDictionary = pmml.getDataDictionary();
	if(dataDictionary == null){
		throw new MissingElementException(pmml, PMMLElements.PMML_DATADICTIONARY);
	} // End if

	if(dataDictionary.hasDataFields()){
		this.dataFields = CacheUtil.getValue(dataDictionary, ModelManager.dataFieldCache);
	}

	TransformationDictionary transformationDictionary = pmml.getTransformationDictionary();
	if(transformationDictionary != null && transformationDictionary.hasDerivedFields()){
		this.derivedFields = CacheUtil.getValue(transformationDictionary, ModelManager.derivedFieldCache);
	} // End if

	if(transformationDictionary != null && transformationDictionary.hasDefineFunctions()){
		this.defineFunctions = CacheUtil.getValue(transformationDictionary, ModelManager.defineFunctionCache);
	}

	MiningFunction miningFunction = model.getMiningFunction();
	if(miningFunction == null){
		throw new MissingAttributeException(MissingAttributeException.formatMessage(XPathUtil.formatElement(model.getClass()) + "@miningFunction"), model);
	}

	MiningSchema miningSchema = model.getMiningSchema();
	if(miningSchema == null){
		throw new MissingElementException(MissingElementException.formatMessage(XPathUtil.formatElement(model.getClass()) + "/" + XPathUtil.formatElement(MiningSchema.class)), model);
	} // End if

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

		for(MiningField miningField : miningFields){
			FieldName name = miningField.getName();
			if(name == null){
				throw new MissingAttributeException(miningField, PMMLAttributes.MININGFIELD_NAME);
			}
		}

		this.miningFields = CacheUtil.getValue(miningSchema, ModelManager.miningFieldCache);
	}

	LocalTransformations localTransformations = model.getLocalTransformations();
	if(localTransformations != null && localTransformations.hasDerivedFields()){
		this.localDerivedFields = CacheUtil.getValue(localTransformations, ModelManager.localDerivedFieldCache);
	}

	Targets targets = model.getTargets();
	if(targets != null && targets.hasTargets()){
		this.targets = CacheUtil.getValue(targets, ModelManager.targetCache);
	}

	Output output = model.getOutput();
	if(output != null && output.hasOutputFields()){
		this.outputFields = CacheUtil.getValue(output, ModelManager.outputFieldCache);
		this.resultFeatures = CacheUtil.getValue(output, ModelManager.resultFeaturesCache);
	}
}
 
Example 5
Source File: ObjectMapperTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
@Test
public void jsonClone() throws Exception {
	DataField dataField = new DataField(FieldName.create("x"), OpType.CATEGORICAL, DataType.BOOLEAN);

	DataDictionary dataDictionary = new DataDictionary()
		.addDataFields(dataField);

	MiningField miningField = new MiningField(FieldName.create("x"));

	MiningSchema miningSchema = new MiningSchema()
		.addMiningFields(miningField);

	assertSame(dataField.getName(), miningField.getName());

	SimplePredicate simplePredicate = new SimplePredicate(FieldName.create("x"), SimplePredicate.Operator.IS_NOT_MISSING, null);

	Node node = new ComplexNode(simplePredicate);

	TreeModel treeModel = new TreeModel()
		.setMiningSchema(miningSchema)
		.setNode(node);

	PMML pmml = new PMML()
		.setDataDictionary(dataDictionary)
		.addModels(treeModel);

	DirectByteArrayOutputStream buffer = new DirectByteArrayOutputStream(1024);

	JacksonUtil.writePMML(pmml, buffer);

	PMML jsonPmml;

	try(InputStream is = buffer.getInputStream()){
		jsonPmml = JacksonUtil.readPMML(is);
	}

	DataDictionary jsonDataDictionary = jsonPmml.getDataDictionary();

	List<DataField> jsonDataFields = jsonDataDictionary.getDataFields();

	assertEquals(1, jsonDataFields.size());

	DataField jsonDataField = jsonDataFields.get(0);

	assertEquals(dataField.getName(), jsonDataField.getName());
	assertEquals(dataField.getOpType(), jsonDataField.getOpType());
	assertEquals(dataField.getDataType(), jsonDataField.getDataType());

	List<Model> jsonModels = jsonPmml.getModels();

	assertEquals(1, jsonModels.size());

	TreeModel jsonTreeModel = (TreeModel)jsonModels.get(0);

	MiningSchema jsonMiningSchema = jsonTreeModel.getMiningSchema();

	List<MiningField> jsonMiningFields = jsonMiningSchema.getMiningFields();

	assertEquals(1, jsonMiningFields.size());

	MiningField jsonMiningField = jsonMiningFields.get(0);

	assertEquals(miningField.getName(), jsonMiningField.getName());
	assertEquals(miningField.getUsageType(), jsonMiningField.getUsageType());

	assertSame(jsonDataField.getName(), jsonMiningField.getName());

	Node jsonNode = jsonTreeModel.getNode();

	SimplePredicate jsonSimplePredicate = (SimplePredicate)jsonNode.getPredicate();

	assertEquals(simplePredicate.getField(), jsonSimplePredicate.getField());
	assertEquals(simplePredicate.getOperator(), jsonSimplePredicate.getOperator());

	assertSame(jsonDataField.getName(), jsonSimplePredicate.getField());
	assertSame(jsonMiningField.getName(), jsonSimplePredicate.getField());
}