DiscretizationUtil { static public Range<Double> toRange(Interval interval){ Double leftMargin = NumberUtil.asDouble(interval.getLeftMargin()); Double rightMargin = NumberUtil.asDouble(interval.getRightMargin()); if(leftMargin == null && rightMargin == null){ throw new MissingAttributeException(interval, PMMLAttributes.INTERVAL_LEFTMARGIN); } if(leftMargin != null && rightMargin != null && NumberUtil.compare(leftMargin, rightMargin) > 0){ throw new InvalidElementException(interval); } Interval.Closure closure = interval.getClosure(); if(closure == null){ throw new MissingAttributeException(interval, PMMLAttributes.INTERVAL_CLOSURE); } switch(closure){ case OPEN_OPEN: { if(leftMargin == null){ return Range.lessThan(rightMargin); } else if(rightMargin == null){ return Range.greaterThan(leftMargin); } return Range.open(leftMargin, rightMargin); } case OPEN_CLOSED: { if(leftMargin == null){ return Range.atMost(rightMargin); } else if(rightMargin == null){ return Range.greaterThan(leftMargin); } return Range.openClosed(leftMargin, rightMargin); } case CLOSED_OPEN: { if(leftMargin == null){ return Range.lessThan(rightMargin); } else if(rightMargin == null){ return Range.atLeast(leftMargin); } return Range.closedOpen(leftMargin, rightMargin); } case CLOSED_CLOSED: { if(leftMargin == null){ return Range.atMost(rightMargin); } else if(rightMargin == null){ return Range.atLeast(leftMargin); } return Range.closed(leftMargin, rightMargin); } default: throw new UnsupportedAttributeException(interval, closure); } } private  DiscretizationUtil(); static FieldValue discretize(Discretize discretize, FieldValue value); static Object discretize(Discretize discretize, Double value); static FieldValue mapValue(MapValues mapValues, Map<String, FieldValue> values); static Range<Double> toRange(Interval interval);  }
@Test public void boundedRange(){ Range<Double> open = toRange(Interval.Closure.OPEN_OPEN, -1d, 1d); assertFalse(open.contains(-Double.MAX_VALUE)); assertFalse(open.contains(-1d)); assertTrue(open.contains(0d)); assertFalse(open.contains(1d)); assertFalse(open.contains(Double.MAX_VALUE)); Range<Double> openClosed = toRange(Interval.Closure.OPEN_CLOSED, -1d, 1d); assertFalse(openClosed.contains(-Double.MAX_VALUE)); assertFalse(openClosed.contains(-1d)); assertTrue(openClosed.contains(0d)); assertTrue(openClosed.contains(1d)); assertFalse(openClosed.contains(Double.MAX_VALUE)); Range<Double> closedOpen = toRange(Interval.Closure.CLOSED_OPEN, -1d, 1d); assertFalse(closedOpen.contains(-Double.MAX_VALUE)); assertTrue(closedOpen.contains(-1d)); assertTrue(closedOpen.contains(0d)); assertFalse(closedOpen.contains(1d)); assertFalse(closedOpen.contains(Double.MAX_VALUE)); Range<Double> closed = toRange(Interval.Closure.CLOSED_CLOSED, -1d, 1d); assertFalse(closed.contains(-Double.MAX_VALUE)); assertTrue(closed.contains(-1d)); assertTrue(closed.contains(0d)); assertTrue(closed.contains(1d)); assertFalse(closed.contains(Double.MAX_VALUE)); }
@Test public void unboundedRange(){ Range<Double> lessThan = toRange(Interval.Closure.OPEN_OPEN, null, 0d); assertTrue(lessThan.contains(-Double.MAX_VALUE)); assertFalse(lessThan.contains(0d)); assertFalse(lessThan.contains(Double.MAX_VALUE)); Range<Double> atMost = toRange(Interval.Closure.OPEN_CLOSED, null, 0d); assertTrue(atMost.contains(-Double.MAX_VALUE)); assertTrue(atMost.contains(0d)); assertFalse(atMost.contains(Double.MAX_VALUE)); Range<Double> greaterThan = toRange(Interval.Closure.OPEN_OPEN, 0d, null); assertFalse(greaterThan.contains(-Double.MAX_VALUE)); assertFalse(greaterThan.contains(0d)); assertTrue(greaterThan.contains(Double.MAX_VALUE)); Range<Double> atLeast = toRange(Interval.Closure.CLOSED_OPEN, 0d, null); assertFalse(atLeast.contains(-Double.MAX_VALUE)); assertTrue(atLeast.contains(0d)); assertTrue(atLeast.contains(Double.MAX_VALUE)); }