org.dmg.pmml.SimpleSetPredicate Java Examples

The following examples show how to use org.dmg.pmml.SimpleSetPredicate. 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: PredicateInterner.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
public Predicate intern(Predicate predicate){

		if(predicate instanceof SimplePredicate){
			return intern((SimplePredicate)predicate);
		} else

		if(predicate instanceof SimpleSetPredicate){
			return intern((SimpleSetPredicate)predicate);
		} else

		if(predicate instanceof True){
			return True.INSTANCE;
		} else

		if(predicate instanceof False){
			return False.INSTANCE;
		}

		return predicate;
	}
 
Example #2
Source File: RDFUpdate.java    From oryx with Apache License 2.0 5 votes vote down vote up
private Predicate buildPredicate(Split split,
                                 CategoricalValueEncodings categoricalValueEncodings) {
  if (split == null) {
    // Left child always applies, but is evaluated second
    return new True();
  }

  int featureIndex = inputSchema.predictorToFeatureIndex(split.feature());
  FieldName fieldName = FieldName.create(inputSchema.getFeatureNames().get(featureIndex));

  if (split.featureType().equals(FeatureType.Categorical())) {
    // Note that categories in MLlib model select the *left* child but the
    // convention here will be that the predicate selects the *right* child
    // So the predicate will evaluate "not in" this set
    // More ugly casting
    @SuppressWarnings("unchecked")
    Collection<Double> javaCategories = (Collection<Double>) (Collection<?>)
        JavaConversions.seqAsJavaList(split.categories());
    Set<Integer> negativeEncodings = javaCategories.stream().map(Double::intValue).collect(Collectors.toSet());

    Map<Integer,String> encodingToValue =
        categoricalValueEncodings.getEncodingValueMap(featureIndex);
    List<String> negativeValues = negativeEncodings.stream().map(encodingToValue::get).collect(Collectors.toList());

    String joinedValues = TextUtils.joinPMMLDelimited(negativeValues);
    return new SimpleSetPredicate(fieldName,
                                  SimpleSetPredicate.BooleanOperator.IS_NOT_IN,
                                  new Array(Array.Type.STRING, joinedValues));

  } else {
    // For MLlib, left means <= threshold, so right means >
    return new SimplePredicate(fieldName,
        SimplePredicate.Operator.GREATER_THAN,
        Double.toString(split.threshold()));
  }
}
 
Example #3
Source File: PredicateInterner.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public ElementKey createKey(SimpleSetPredicate simpleSetPredicate){
	Array array = simpleSetPredicate.getArray();

	Object[] content = {simpleSetPredicate.getField(), simpleSetPredicate.getBooleanOperator(), ArrayUtil.getContent(array)};

	return new ElementKey(content);
}
 
Example #4
Source File: PredicateUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
Boolean evaluatePredicate(Predicate predicate, EvaluationContext context){

	if(predicate instanceof SimplePredicate){
		return evaluateSimplePredicate((SimplePredicate)predicate, context);
	} else

	if(predicate instanceof SimpleSetPredicate){
		return evaluateSimpleSetPredicate((SimpleSetPredicate)predicate, context);
	} else

	if(predicate instanceof CompoundPredicate){
		return evaluateCompoundPredicate((CompoundPredicate)predicate, context);
	} else

	if(predicate instanceof True){
		return evaluateTrue((True)predicate);
	} else

	if(predicate instanceof False){
		return evaluateFalse((False)predicate);
	} // End if

	if(predicate instanceof JavaPredicate){
		return evaluateJavaPredicate((JavaPredicate)predicate, context);
	}

	throw new UnsupportedElementException(predicate);
}
 
Example #5
Source File: PredicateUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public Boolean evaluateSimpleSetPredicate(SimpleSetPredicate simpleSetPredicate, EvaluationContext context){
	FieldName name = simpleSetPredicate.getField();
	if(name == null){
		throw new MissingAttributeException(simpleSetPredicate, PMMLAttributes.SIMPLESETPREDICATE_FIELD);
	}

	SimpleSetPredicate.BooleanOperator booleanOperator = simpleSetPredicate.getBooleanOperator();
	if(booleanOperator == null){
		throw new MissingAttributeException(simpleSetPredicate, PMMLAttributes.SIMPLESETPREDICATE_BOOLEANOPERATOR);
	}

	FieldValue value = context.evaluate(name);

	if(FieldValueUtil.isMissing(value)){
		return null;
	}

	Array array = simpleSetPredicate.getArray();
	if(array == null){
		throw new MissingElementException(simpleSetPredicate, PMMLElements.SIMPLESETPREDICATE_ARRAY);
	}

	switch(booleanOperator){
		case IS_IN:
			return value.isIn(simpleSetPredicate);
		case IS_NOT_IN:
			return !value.isIn(simpleSetPredicate);
		default:
			throw new UnsupportedAttributeException(simpleSetPredicate, booleanOperator);
	}
}
 
Example #6
Source File: PredicateInternerTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void internSimpleSetPredicate(){
	FieldName name = FieldName.create("x");

	Predicate left = new SimpleSetPredicate(name, SimpleSetPredicate.BooleanOperator.IS_IN, new Array(Array.Type.STRING, "1"));
	Predicate right = new SimpleSetPredicate(name, SimpleSetPredicate.BooleanOperator.IS_IN, new Array(Array.Type.STRING, "\"1\""));

	checkTree(left, right);
}
 
Example #7
Source File: TreeModelCompactor.java    From jpmml-sparkml with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void enterNode(Node node){
	Object id = node.getId();
	Object score = node.getScore();

	if(id != null){
		throw new IllegalArgumentException();
	} // End if

	if(node.hasNodes()){
		List<Node> children = node.getNodes();

		if(children.size() != 2 || score != null){
			throw new IllegalArgumentException();
		}

		Node firstChild = children.get(0);
		Node secondChild = children.get(1);

		Predicate firstPredicate = firstChild.getPredicate();
		Predicate secondPredicate = secondChild.getPredicate();

		checkFieldReference(firstPredicate, secondPredicate);

		boolean update = true;

		if(hasOperator(firstPredicate, SimplePredicate.Operator.EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.EQUAL)){
			update = isCategoricalField((SimplePredicate)firstPredicate);
		} else

		if(hasOperator(firstPredicate, SimplePredicate.Operator.NOT_EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.EQUAL)){
			children = swapChildren(node);

			firstChild = children.get(0);
			secondChild = children.get(1);
		} else

		if(hasOperator(firstPredicate, SimplePredicate.Operator.EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.NOT_EQUAL)){
			// Ignored
		} else

		if(hasOperator(firstPredicate, SimplePredicate.Operator.LESS_OR_EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.GREATER_THAN)){
			// Ignored
		} else

		if(hasOperator(firstPredicate, SimplePredicate.Operator.EQUAL) && hasBooleanOperator(secondPredicate, SimpleSetPredicate.BooleanOperator.IS_IN)){
			addCategoricalField(secondChild);
		} else

		if(hasBooleanOperator(firstPredicate, SimpleSetPredicate.BooleanOperator.IS_IN) && hasOperator(secondPredicate, SimplePredicate.Operator.EQUAL)){
			children = swapChildren(node);

			firstChild = children.get(0);
			secondChild = children.get(1);

			addCategoricalField(secondChild);
		} else

		if(hasBooleanOperator(firstPredicate, SimpleSetPredicate.BooleanOperator.IS_IN) && hasBooleanOperator(secondPredicate, SimpleSetPredicate.BooleanOperator.IS_IN)){
			addCategoricalField(secondChild);
		} else

		{
			throw new IllegalArgumentException();
		} // End if

		if(update){
			secondChild.setPredicate(True.INSTANCE);
		}
	} else

	{
		if(score == null){
			throw new IllegalArgumentException();
		}
	}
}
 
Example #8
Source File: TreeModelCompactor.java    From jpmml-sparkml with GNU Affero General Public License v3.0 4 votes vote down vote up
private void addCategoricalField(Node node){
	this.replacedPredicates.put(node, (SimpleSetPredicate)node.getPredicate());
}
 
Example #9
Source File: RandomForestCompactor.java    From jpmml-r with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void enterNode(Node node){
	Object id = node.getId();
	Object score = node.getScore();

	if(id == null){
		throw new IllegalArgumentException();
	} // End if

	if(node.hasNodes()){
		List<Node> children = node.getNodes();

		if(children.size() != 2 || score != null){
			throw new IllegalArgumentException();
		}

		Node firstChild = children.get(0);
		Node secondChild = children.get(1);

		Predicate firstPredicate = firstChild.getPredicate();
		Predicate secondPredicate = secondChild.getPredicate();

		checkFieldReference(firstPredicate, secondPredicate);

		boolean update = isDefinedField((HasFieldReference<?>)firstPredicate);

		if(hasOperator(firstPredicate, SimplePredicate.Operator.EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.EQUAL)){
			// Ignored
		} else

		if(hasOperator(firstPredicate, SimplePredicate.Operator.LESS_OR_EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.GREATER_THAN)){
			update = true;
		} else

		if(hasOperator(firstPredicate, SimplePredicate.Operator.EQUAL) && hasBooleanOperator(secondPredicate, SimpleSetPredicate.BooleanOperator.IS_IN)){
			// Ignored
		} else

		if(hasBooleanOperator(firstPredicate, SimpleSetPredicate.BooleanOperator.IS_IN) && hasOperator(secondPredicate, SimplePredicate.Operator.EQUAL)){

			if(update){
				children = swapChildren(node);

				firstChild = children.get(0);
				secondChild = children.get(1);
			}
		} else

		if(hasBooleanOperator(firstPredicate, SimpleSetPredicate.BooleanOperator.IS_IN) && hasBooleanOperator(secondPredicate, SimpleSetPredicate.BooleanOperator.IS_IN)){
			// Ignored
		} else

		{
			throw new IllegalArgumentException();
		} // End if

		if(update){
			secondChild.setPredicate(True.INSTANCE);
		}
	} else

	{
		if(score == null){
			throw new IllegalArgumentException();
		}
	}

	node.setId(null);
}
 
Example #10
Source File: RDFPMMLUtilsTest.java    From oryx with Apache License 2.0 4 votes vote down vote up
private static PMML buildDummyClassificationModel(int numTrees) {
  PMML pmml = PMMLUtils.buildSkeletonPMML();

  List<DataField> dataFields = new ArrayList<>();
  DataField predictor =
      new DataField(FieldName.create("color"), OpType.CATEGORICAL, DataType.STRING);
  predictor.addValues(new Value("yellow"), new Value("red"));
  dataFields.add(predictor);
  DataField target =
      new DataField(FieldName.create("fruit"), OpType.CATEGORICAL, DataType.STRING);
  target.addValues(new Value("banana"), new Value("apple"));
  dataFields.add(target);
  DataDictionary dataDictionary =
      new DataDictionary(dataFields).setNumberOfFields(dataFields.size());
  pmml.setDataDictionary(dataDictionary);

  List<MiningField> miningFields = new ArrayList<>();
  MiningField predictorMF = new MiningField(FieldName.create("color"))
      .setOpType(OpType.CATEGORICAL)
      .setUsageType(MiningField.UsageType.ACTIVE)
      .setImportance(0.5);
  miningFields.add(predictorMF);
  MiningField targetMF = new MiningField(FieldName.create("fruit"))
      .setOpType(OpType.CATEGORICAL)
      .setUsageType(MiningField.UsageType.PREDICTED);
  miningFields.add(targetMF);
  MiningSchema miningSchema = new MiningSchema(miningFields);

  double dummyCount = 2.0;
  Node rootNode =
    new ComplexNode().setId("r").setRecordCount(dummyCount).setPredicate(new True());

  double halfCount = dummyCount / 2;

  Node left = new ComplexNode().setId("r-").setRecordCount(halfCount).setPredicate(new True());
  left.addScoreDistributions(new ScoreDistribution("apple", halfCount));
  Node right = new ComplexNode().setId("r+").setRecordCount(halfCount)
      .setPredicate(new SimpleSetPredicate(FieldName.create("color"),
                                           SimpleSetPredicate.BooleanOperator.IS_NOT_IN,
                                           new Array(Array.Type.STRING, "red")));
  right.addScoreDistributions(new ScoreDistribution("banana", halfCount));

  rootNode.addNodes(right, left);

  TreeModel treeModel = new TreeModel(MiningFunction.CLASSIFICATION, miningSchema, rootNode)
      .setSplitCharacteristic(TreeModel.SplitCharacteristic.BINARY_SPLIT)
      .setMissingValueStrategy(TreeModel.MissingValueStrategy.DEFAULT_CHILD);

  if (numTrees > 1) {
    MiningModel miningModel = new MiningModel(MiningFunction.CLASSIFICATION, miningSchema);
    List<Segment> segments = new ArrayList<>();
    for (int i = 0; i < numTrees; i++) {
      segments.add(new Segment()
          .setId(Integer.toString(i))
          .setPredicate(new True())
          .setModel(treeModel)
          .setWeight(1.0));
    }
    miningModel.setSegmentation(
        new Segmentation(Segmentation.MultipleModelMethod.WEIGHTED_MAJORITY_VOTE, segments));
    pmml.addModels(miningModel);
  } else {
    pmml.addModels(treeModel);
  }

  return pmml;
}
 
Example #11
Source File: FieldReferenceFinder.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public VisitorAction visit(SimpleSetPredicate simpleSetPredicate){
	process(simpleSetPredicate.getField());

	return super.visit(simpleSetPredicate);
}
 
Example #12
Source File: ValueParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public VisitorAction visit(SimpleSetPredicate simpleSetPredicate){
	FieldName name = simpleSetPredicate.getField();
	if(name == null){
		throw new MissingAttributeException(simpleSetPredicate, PMMLAttributes.SIMPLESETPREDICATE_FIELD);
	}

	Array array = simpleSetPredicate.getArray();
	if(array == null){
		throw new MissingElementException(simpleSetPredicate, PMMLElements.SIMPLESETPREDICATE_ARRAY);
	}

	DataType dataType = resolveDataType(name);
	if(dataType != null){
		Set<?> values;

		Object value = array.getValue();

		if(value instanceof List){
			values = new LinkedHashSet<>((List<?>)value);
		} else

		if(value instanceof Set){
			values = (Set<?>)value;
		} else

		{
			values = new LinkedHashSet<>(ArrayUtil.parse(array));
		}

		try {
			array = new RichComplexArray(dataType)
				.setType(array.getType())
				.setValue(values);
		} catch(IllegalArgumentException | TypeCheckException e){
			Mode mode = getMode();

			if((Mode.LOOSE).equals(mode)){
				return super.visit(simpleSetPredicate);
			}

			throw e;
		}

		simpleSetPredicate.setArray(array);
	}

	return super.visit(simpleSetPredicate);
}
 
Example #13
Source File: PredicateInterner.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
private SimpleSetPredicate intern(SimpleSetPredicate simpleSetPredicate){
	return this.simpleSetPredicateCache.intern(simpleSetPredicate);
}
 
Example #14
Source File: PredicateUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void evaluateSimpleSetPredicate(){
	FieldName fruit = FieldName.create("fruit");

	SimpleSetPredicate simpleSetPredicate = new SimpleSetPredicate(fruit, SimpleSetPredicate.BooleanOperator.IS_IN, new Array(Array.Type.STRING, "apple orange"));

	assertEquals(null, evaluate(simpleSetPredicate, fruit, null));

	assertEquals(Boolean.TRUE, evaluate(simpleSetPredicate, fruit, "apple"));
	assertEquals(Boolean.FALSE, evaluate(simpleSetPredicate, fruit, "pineapple"));

	simpleSetPredicate.setBooleanOperator(SimpleSetPredicate.BooleanOperator.IS_NOT_IN);

	assertEquals(Boolean.FALSE, evaluate(simpleSetPredicate, fruit, "apple"));
	assertEquals(Boolean.TRUE, evaluate(simpleSetPredicate, fruit, "pineapple"));
}
 
Example #15
Source File: ArrayListTransformerTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
@Test
public void transform(){
	Node node1a = new BranchNode();

	Node node2a = new BranchNode();
	Node node2b = new LeafNode();

	node1a.addNodes(node2a, node2b);

	Array array = new ComplexArray()
		.setType(Array.Type.INT)
		.setValue(Arrays.asList(-1, 1));

	Predicate predicate = new SimpleSetPredicate(FieldName.create("x"), SimpleSetPredicate.BooleanOperator.IS_IN, array);

	Node node3a = new LeafNode(null, predicate);

	node2a.addNodes(node3a);

	assertTrue(node1a.getNodes() instanceof ArrayList);
	assertTrue(node2a.getNodes() instanceof ArrayList);

	Object value = array.getValue();

	assertTrue(value instanceof ArrayList);
	assertTrue(value instanceof ComplexValue);

	TreeModel treeModel = new TreeModel(MiningFunction.CLASSIFICATION, new MiningSchema(), node1a);

	ArrayListTransformer transformer = new ArrayListTransformer();
	transformer.applyTo(treeModel);

	assertTrue(node1a.getNodes() instanceof DoubletonList);
	assertTrue(node2a.getNodes() instanceof SingletonList);

	value = array.getValue();

	assertTrue(value instanceof ArrayList);
	assertTrue(value instanceof ComplexValue);
}
 
Example #16
Source File: ValueParserTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 2 votes vote down vote up
@Test
public void parseTreeModel(){
	DataField dataField = new DataField(FieldName.create("x1"), OpType.CATEGORICAL, DataType.STRING);

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

	NormDiscrete normDiscrete = new NormDiscrete(dataField.getName(), "1");

	DerivedField derivedField = new DerivedField(FieldName.create("global(" + dataField.getName() + ")"), OpType.CATEGORICAL, DataType.STRING, normDiscrete);

	TransformationDictionary transformationDictionary = new TransformationDictionary()
		.addDerivedFields(derivedField);

	SimplePredicate simplePredicate = new SimplePredicate(derivedField.getName(), SimplePredicate.Operator.EQUAL, "1");

	Node child = new LeafNode("1", simplePredicate);

	SimpleSetPredicate simpleSetPredicate = new SimpleSetPredicate(dataField.getName(), SimpleSetPredicate.BooleanOperator.IS_IN, new Array(Array.Type.STRING, "0 1"));

	Node root = new BranchNode("0", simpleSetPredicate)
		.addNodes(child);

	MiningField miningField = new MiningField(dataField.getName());

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

	TreeModel treeModel = new TreeModel(MiningFunction.REGRESSION, miningSchema, null)
		.setNode(root);

	PMML pmml = new PMML(Version.PMML_4_3.getVersion(), new Header(), dataDictionary)
		.setTransformationDictionary(transformationDictionary)
		.addModels(treeModel);

	List<DataField> dataFields = dataDictionary.getDataFields();

	ValueParser parser = new ValueParser(ValueParser.Mode.STRICT);
	parser.applyTo(pmml);

	dataField = dataFields.get(0);

	assertEquals("1", normDiscrete.getValue());
	assertEquals("1", simplePredicate.getValue());

	Array array = simpleSetPredicate.getArray();

	assertEquals(ImmutableSet.of("0", "1"), array.getValue());

	dataField.setDataType(DataType.INTEGER);

	parser.applyTo(pmml);

	dataField = dataFields.get(0);

	assertEquals(1, normDiscrete.getValue());
	assertEquals("1", simplePredicate.getValue());

	array = simpleSetPredicate.getArray();

	assertTrue(array instanceof RichComplexArray);
	assertEquals(ImmutableSet.of(0, 1), array.getValue());

	dataField.setDataType(DataType.DOUBLE);
	derivedField.setDataType(DataType.INTEGER);

	parser.applyTo(pmml);

	dataField = dataFields.get(0);

	assertEquals(1.0d, normDiscrete.getValue());
	assertEquals(1, simplePredicate.getValue());

	array = simpleSetPredicate.getArray();

	assertEquals(ImmutableSet.of(0.0d, 1.0d), array.getValue());

	dataField.setDataType(DataType.BOOLEAN);
	derivedField.setDataType(DataType.DOUBLE);

	parser.applyTo(pmml);

	dataField = dataFields.get(0);

	assertEquals(true, normDiscrete.getValue());
	assertEquals(1.0d, simplePredicate.getValue());

	array = simpleSetPredicate.getArray();

	assertEquals(ImmutableSet.of(false, true), array.getValue());

	derivedField.setDataType(DataType.BOOLEAN);

	parser.applyTo(pmml);

	dataField = dataFields.get(0);

	assertEquals(true, normDiscrete.getValue());
	assertEquals(true, simplePredicate.getValue());
}