UnivariateLookupTableFunction extends LookupTableFunction { @Override public Number evaluate(final EvaluationContext ec) throws CelloException { Double rtn = null; final double[] input = new double[getVariables().size()]; for (int i = 0; i < getVariables().size(); i++) { final Variable v = getVariables().get(i); final String name = v.getName(); final Double value = v.evaluate(ec).doubleValue(); if (!getTable().containsColumn(name)) { throw new RuntimeException(String.format("Missing column %s.", name)); } input[i] = value; } Integer argMinA = -1; Double minA = Double.MAX_VALUE; Integer argMinB = -1; Double minB = Double.MAX_VALUE; for (final Integer row : getTable().rowKeySet()) { final double[] record = new double[getVariables().size()]; for (int i = 0; i < getVariables().size(); i++) { final Variable v = getVariables().get(i); final Double d = getTable().get(row, v.getName()); record[i] = d; } final EuclideanDistance eu = new EuclideanDistance(); final Double distance = eu.compute(input, record); if (distance < minA) { argMinB = argMinA; minB = minA; argMinA = row; minA = distance; } else if (distance < minB) { argMinB = row; minB = distance; } } final Double valueA = getTable().get(argMinA, LookupTableFunction.S_OUTPUT); final Double valueB = getTable().get(argMinB, LookupTableFunction.S_OUTPUT); rtn = (valueA * minA + valueB * minB) / (minA + minB); return rtn; } UnivariateLookupTableFunction(final JSONObject jsonObj); @Override boolean isValid(); @Override Number evaluate(final EvaluationContext ec);  }
@Test public void evaluate_MockData_ShouldBeWithinTolerance() throws CelloException { NetlistNode node = netlist.getVertexByName("$49"); NetlistNode a = netlist.getVertexByName("a"); NetlistNode b = netlist.getVertexByName("b"); List<NetlistNode> inputs = new ArrayList<>(); inputs.add(a); inputs.add(b); State<NetlistNode> state = new State<>(inputs, true, false, 1); EvaluationContext ec = new EvaluationContext(); ec.setNode(node); ec.setState(state); AssignableDevice device = node.getResultNetlistNodeData().getDevice(); UnivariateLookupTableFunction f = (UnivariateLookupTableFunction) device.getModel().getFunctionByName("toxicity"); assert (Math.abs(f.evaluate(ec).doubleValue() - 0.885636) < TOLERANCE); }