org.dmg.pmml.Array Java Examples

The following examples show how to use org.dmg.pmml.Array. 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: ArrayUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
static
public List<? extends Number> asNumberList(Array array){
	List<?> content = getContent(array);

	Array.Type type = array.getType();
	if(type == null){
		throw new MissingAttributeException(array, PMMLAttributes.ARRAY_TYPE);
	}

	switch(type){
		case INT:
		case REAL:
			return (List)content;
		case STRING:
			throw new InvalidElementException(array);
		default:
			throw new UnsupportedAttributeException(array, type);
	}
}
 
Example #2
Source File: FieldValue.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * <p>
 * Checks if this value is contained in the set of reference values.
 * </p>
 */
public boolean isIn(HasValueSet<?> hasValueSet){
	Array array = hasValueSet.getArray();
	if(array == null){
		throw new MissingElementException(MissingElementException.formatMessage(XPathUtil.formatElement((Class)hasValueSet.getClass()) + "/" + XPathUtil.formatElement(Array.class)), (PMMLObject)hasValueSet);
	} // End if

	if(array instanceof SetHolder){
		SetHolder setHolder = (SetHolder)array;

		return setHolder.contains(getDataType(), getValue());
	}

	List<?> values = ArrayUtil.getContent(array);

	return values.stream()
		.anyMatch(value -> equalsValue(value));
}
 
Example #3
Source File: AppPMMLUtilsTest.java    From oryx with Apache License 2.0 5 votes vote down vote up
@Test
public void testToArrayDouble() {
  Array a = AppPMMLUtils.toArray(-1.0, 2.01, 3.5);
  assertEquals(3, a.getN().intValue());
  assertEquals(Array.Type.REAL, a.getType());
  assertEquals("-1.0 2.01 3.5", a.getValue());
}
 
Example #4
Source File: MatrixUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void anyMatrixDense(){
	Matrix matrix = new Matrix()
		.addArrays(
			new Array(Array.Type.REAL, "0 0 0 42 0"),
			new Array(Array.Type.REAL, "0 1 0 0 0"),
			new Array(Array.Type.REAL, "5 0 0 0 0"),
			new Array(Array.Type.REAL, "0 0 0 0 7"),
			new Array(Array.Type.REAL, "0 0 9 0 0")
		);

	anyMatrix(matrix);
}
 
Example #5
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 #6
Source File: ArrayUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public int getSize(Array array){
	Integer n = array.getN();
	if(n != null){
		return n;
	}

	List<?> content = getContent(array);

	return content.size();
}
 
Example #7
Source File: MatrixUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Number getArrayValue(List<Array> arrays, int row, int column){
	Array array = arrays.get(row - 1);

	List<? extends Number> elements = ArrayUtil.asNumberList(array);

	return elements.get(column - 1);
}
 
Example #8
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 #9
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 #10
Source File: ClusteringModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public List<FieldValue> load(Cluster cluster){
	Array array = cluster.getArray();

	List<? extends Number> values = ArrayUtil.asNumberList(array);

	return ImmutableList.copyOf(Lists.transform(values, value -> FieldValueUtil.create(TypeInfos.CONTINUOUS_DOUBLE, value)));
}
 
Example #11
Source File: ClusteringModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private <V extends Number> ClusterAffinityDistribution<V> evaluateDistance(ValueFactory<V> valueFactory, ComparisonMeasure comparisonMeasure, List<ClusteringField> clusteringFields, List<FieldValue> values){
	ClusteringModel clusteringModel = getModel();

	List<Cluster> clusters = clusteringModel.getClusters();

	Value<V> adjustment;

	MissingValueWeights missingValueWeights = clusteringModel.getMissingValueWeights();
	if(missingValueWeights != null){
		Array array = missingValueWeights.getArray();

		List<? extends Number> adjustmentValues = ArrayUtil.asNumberList(array);
		if(values.size() != adjustmentValues.size()){
			throw new InvalidElementException(missingValueWeights);
		}

		adjustment = MeasureUtil.calculateAdjustment(valueFactory, values, adjustmentValues);
	} else

	{
		adjustment = MeasureUtil.calculateAdjustment(valueFactory, values);
	}

	ClusterAffinityDistribution<V> result = createClusterAffinityDistribution(Classification.Type.DISTANCE, clusters);

	for(Cluster cluster : clusters){
		List<FieldValue> clusterValues = CacheUtil.getValue(cluster, ClusteringModelEvaluator.clusterValueCache);

		if(values.size() != clusterValues.size()){
			throw new InvalidElementException(cluster);
		}

		Value<V> distance = MeasureUtil.evaluateDistance(valueFactory, comparisonMeasure, clusteringFields, values, clusterValues, adjustment);

		result.put(cluster, distance);
	}

	return result;
}
 
Example #12
Source File: AppPMMLUtils.java    From oryx with Apache License 2.0 5 votes vote down vote up
/**
 * @param values {@code double} value to make into a PMML {@link Array}
 * @return PMML {@link Array} representation
 */
public static Array toArray(double... values) {
  List<Double> valueList = new ArrayList<>(values.length);
  for (double value : values) {
    valueList.add(value);
  }
  String arrayValue = TextUtils.joinPMMLDelimitedNumbers(valueList);
  return new Array(Array.Type.REAL, arrayValue).setN(valueList.size());
}
 
Example #13
Source File: ArrayUtilTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void formatNumberArray(){
	ComplexValue integerTwoWrapper = new ComplexValue(){

		@Override
		public Object toSimpleValue(){
			return 2;
		}
	};

	assertEquals("1 2 3", ArrayUtil.format(Array.Type.INT, Arrays.asList(1, integerTwoWrapper, 3)));
	assertEquals("1.0 2.0 3.0", ArrayUtil.format(Array.Type.REAL, Arrays.asList(1d, 2d, 3d)));

	assertEquals("1 22 3", ArrayUtil.format(Array.Type.INT, Arrays.asList("1", "22", "3")));
}
 
Example #14
Source File: ArrayUtilTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void parseNumberArray(){
	assertEquals(Arrays.asList("1"), ArrayUtil.parse(Array.Type.INT, "1"));
	assertEquals(Arrays.asList("1.0"), ArrayUtil.parse(Array.Type.REAL, "1.0"));

	assertEquals(Arrays.asList("1", "2", "3"), ArrayUtil.parse(Array.Type.INT, "1 2 3"));
	assertEquals(Arrays.asList("1.0", "2.0", "3.0"), ArrayUtil.parse(Array.Type.REAL, "1.0 2.0 3.0"));

	assertEquals(Arrays.asList("1", "22", "3"), ArrayUtil.parse(Array.Type.INT, "1 22 3"));
}
 
Example #15
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 #16
Source File: ArrayUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
public List<?> getContent(Array array){
	return CacheUtil.getValue(array, ArrayUtil.contentCache);
}
 
Example #17
Source File: ArrayUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
private List<?> parseStringArray(String value){
	return ArrayUtil.parse(new Array(Array.Type.STRING, value));
}
 
Example #18
Source File: ArrayUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
private List<?> parseRealArray(String value){
	return ArrayUtil.parse(new Array(Array.Type.REAL, value));
}
 
Example #19
Source File: ArrayUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
private List<?> parseIntArray(String value){
	return ArrayUtil.parse(new Array(Array.Type.INT, value));
}
 
Example #20
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 #21
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 #22
Source File: ArrayUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public List<?> load(Array array){
	return ImmutableList.copyOf(parse(array));
}
 
Example #23
Source File: ArrayUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
public List<?> parse(Array array){
	Array.Type type = array.getType();
	if(type == null){
		throw new MissingAttributeException(array, PMMLAttributes.ARRAY_TYPE);
	}

	List<String> tokens;

	Object value = array.getValue();

	if(value instanceof String){
		String string = (String)value;

		switch(type){
			case INT:
			case REAL:
			case STRING:
				tokens = org.jpmml.model.ArrayUtil.parse(type, string);
				break;
			default:
				throw new UnsupportedAttributeException(array, type);
		}
	} else

	if(value instanceof List){
		List<?> list = (List<?>)value;

		tokens = Lists.transform(list, TypeUtil::format);
	} else

	if(value instanceof Set){
		Set<?> set = (Set<?>)value;

		tokens = set.stream()
			.map(TypeUtil::format)
			.collect(Collectors.toList());
	} else

	{
		throw new InvalidElementException(array);
	}

	Integer n = array.getN();
	if(n != null && n != tokens.size()){
		throw new InvalidElementException(array);
	}

	switch(type){
		case INT:
			return Lists.transform(tokens, token -> Numbers.INTEGER_INTERNER.intern(Integer.parseInt(token)));
		case REAL:
			return Lists.transform(tokens, token -> Numbers.DOUBLE_INTERNER.intern(Double.parseDouble(token)));
		case STRING:
			return Lists.transform(tokens, token -> Strings.INTERNER.intern(token));
		default:
			throw new UnsupportedAttributeException(array, type);
	}
}
 
Example #24
Source File: ArrayUtilTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Test
public void formatStringArray(){
	assertEquals("", ArrayUtil.format(Array.Type.STRING, Collections.emptyList()));

	assertEquals("\"\"", ArrayUtil.format(Array.Type.STRING, Arrays.asList("")));
	assertEquals("\"\" \" \"", ArrayUtil.format(Array.Type.STRING, Arrays.asList("", " ")));

	assertEquals("a b c", ArrayUtil.format(Array.Type.STRING, Arrays.asList("a", "b", "c")));
	assertEquals("a \"\" b c", ArrayUtil.format(Array.Type.STRING, Arrays.asList("a", "", "b", "c")));

	assertEquals("\"a b c\"", ArrayUtil.format(Array.Type.STRING, Arrays.asList("a b c")));

	assertEquals("\\a \\b\\ c\\", ArrayUtil.format(Array.Type.STRING, Arrays.asList("\\a", "\\b\\", "c\\")));

	assertEquals("ab \"a b\" \"with \\\"quotes\\\" \"", ArrayUtil.format(Array.Type.STRING, Arrays.asList("ab", "a b", "with \"quotes\" ")));
}
 
Example #25
Source File: ArrayUtil.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static
public List<String> parse(Array.Type type, String string){
	List<String> result = new ArrayList<>(Math.max(16, string.length() / 8));

	StringBuilder sb = new StringBuilder();

	boolean enableQuotes;

	switch(type){
		case INT:
		case REAL:
			enableQuotes = false;
			break;
		case STRING:
			enableQuotes = true;
			break;
		default:
			throw new IllegalArgumentException();
	}

	boolean quoted = false;

	tokens:
	for(int i = 0; i < string.length(); i++){
		char c = string.charAt(i);

		if(quoted){

			if(c == '\\' && i < (string.length() - 1)){
				c = string.charAt(i + 1);

				if(c == '\"'){
					sb.append('\"');

					i++;
				} else

				{
					sb.append('\\');
				}

				continue tokens;
			}

			sb.append(c);

			if(c == '\"'){
				result.add(createToken(sb, enableQuotes));

				quoted = false;
			}
		} else

		{
			if(c == '\"' && enableQuotes){

				if(sb.length() > 0){
					result.add(createToken(sb, enableQuotes));
				}

				sb.append('\"');

				quoted = true;
			} else

			if(Character.isWhitespace(c)){

				if(sb.length() > 0){
					result.add(createToken(sb, enableQuotes));
				}
			} else

			{
				sb.append(c);
			}
		}
	}

	if(sb.length() > 0){
		result.add(createToken(sb, enableQuotes));
	}

	return result;
}
 
Example #26
Source File: SupportVectorMachineModelEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
private Map<String, Object> parseVectorDictionary(SupportVectorMachineModel supportVectorMachineModel){
	VectorDictionary vectorDictionary = supportVectorMachineModel.getVectorDictionary();

	VectorFields vectorFields = vectorDictionary.getVectorFields();

	List<PMMLObject> content = vectorFields.getContent();

	Map<String, Object> result = new LinkedHashMap<>();

	List<VectorInstance> vectorInstances = vectorDictionary.getVectorInstances();
	for(VectorInstance vectorInstance : vectorInstances){
		String id = vectorInstance.getId();
		if(id == null){
			throw new MissingAttributeException(vectorInstance, PMMLAttributes.VECTORINSTANCE_ID);
		}

		Array array = vectorInstance.getArray();
		RealSparseArray sparseArray = vectorInstance.getRealSparseArray();

		List<? extends Number> values;

		if(array != null && sparseArray == null){
			values = ArrayUtil.asNumberList(array);
		} else

		if(array == null && sparseArray != null){
			values = SparseArrayUtil.asNumberList(sparseArray);
		} else

		{
			throw new InvalidElementException(vectorInstance);
		} // End if

		if(content.size() != values.size()){
			throw new InvalidElementException(vectorInstance);
		}

		Object vector = toArray(supportVectorMachineModel, values);

		result.put(id, vector);
	}

	return result;
}
 
Example #27
Source File: ArrayUtil.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static
public String format(Array.Type type, Collection<?> values){
	StringBuilder sb = new StringBuilder(values.size() * 16);

	boolean enableQuotes;

	switch(type){
		case INT:
		case REAL:
			enableQuotes = false;
			break;
		case STRING:
			enableQuotes = true;
			break;
		default:
			throw new IllegalArgumentException();
	}

	for(Object value : values){

		if(value instanceof ComplexValue){
			ComplexValue complexValue = (ComplexValue)value;

			value = complexValue.toSimpleValue();
		}

		String string = value.toString();

		if(sb.length() > 0){
			sb.append(' ');
		} // End if

		if(enableQuotes){
			boolean quoted = ("").equals(string) || (string.indexOf(' ') > -1);

			if(quoted){
				sb.append('\"');
			} // End if

			if(string.indexOf('\"') > -1){

				for(int i = 0; i < string.length(); i++){
					char c = string.charAt(i);

					if(c == '\"'){
						sb.append('\\');
					}

					sb.append(c);
				}
			} else

			{
				sb.append(string);
			} // End if

			if(quoted){
				sb.append('\"');
			}
		} else

		{
			sb.append(string);
		}
	}

	return sb.toString();
}
 
Example #28
Source File: MatrixUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * @return The number of rows.
 */
static
public int getRows(Matrix matrix){
	Integer nbRows = matrix.getNbRows();
	if(nbRows != null){
		return nbRows;
	}

	List<Array> arrays = matrix.getArrays();
	List<MatCell> matCells = matrix.getMatCells();

	Matrix.Kind kind = matrix.getKind();
	switch(kind){
		case DIAGONAL:
			{
				if(arrays.size() == 1){
					Array array = arrays.get(0);

					return ArrayUtil.getSize(array);
				}
			}
			break;
		case SYMMETRIC:
			{
				if(!arrays.isEmpty()){
					return arrays.size();
				}
			}
			break;
		case ANY:
			{
				if(!arrays.isEmpty()){
					return arrays.size();
				} // End if

				if(!matCells.isEmpty()){
					MatCell matCell = Collections.max(matCells, MatrixUtil.rowComparator);

					return matCell.getRow();
				}
			}
			break;
		default:
			throw new UnsupportedAttributeException(matrix, kind);
	}

	throw new InvalidElementException(matrix);
}
 
Example #29
Source File: MatrixUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * @param row The row index. The index of the first row is <code>1</code>.
 * @param column The column index. The index of the first column is <code>1</code>.
 *
 * @return The element at the specified location, or <code>null</code>.
 *
 * @throws IndexOutOfBoundsException If either the row or column index is out of range.
 */
static
public Number getElementAt(Matrix matrix, int row, int column){
	List<Array> arrays = matrix.getArrays();
	List<MatCell> matCells = matrix.getMatCells();

	Matrix.Kind kind = matrix.getKind();
	switch(kind){
		case DIAGONAL:
			{
				// "The content is just one Array of numbers representing the diagonal values"
				if(arrays.size() == 1){
					Array array = arrays.get(0);

					List<? extends Number> elements = ArrayUtil.asNumberList(array);

					// Diagonal element
					if(row == column){
						return elements.get(row - 1);
					} else

					// Off-diagonal element
					{
						int min = 1;
						int max = elements.size();

						if((row < min || row > max) || (column < min || column > max)){
							throw new IndexOutOfBoundsException();
						}

						return matrix.getOffDiagDefault();
					}
				}
			}
			break;
		case SYMMETRIC:
			{
				// "The content must be represented by Arrays"
				if(!arrays.isEmpty()){

					// Make sure the specified coordinates target the lower left triangle
					if(column > row){
						int temp = row;

						row = column;
						column = temp;
					}

					return getArrayValue(arrays, row, column);
				}
			}
			break;
		case ANY:
			{
				if(!arrays.isEmpty()){
					return getArrayValue(arrays, row, column);
				} // End if

				if(!matCells.isEmpty()){

					if(row < 1 || column < 1){
						throw new IndexOutOfBoundsException();
					}

					Number value = getMatCellValue(matCells, row, column);
					if(value == null){

						if(row == column){
							return matrix.getDiagDefault();
						}

						return matrix.getOffDiagDefault();
					}

					return value;
				}
			}
			break;
		default:
			throw new UnsupportedAttributeException(matrix, kind);
	}

	throw new InvalidElementException(matrix);
}
 
Example #30
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);
}