@Test
public void testContextualDiscreteAttribute() throws Exception {

    //construct a contextual outlier detector
    MacroBaseConf conf = new MacroBaseConf();
    conf.set(MacroBaseConf.METRICS, Arrays.asList("A1"));
    conf.set(MacroBaseConf.TRANSFORM_TYPE, "MAD");
    List<String> contextualDiscreteAttributes = new ArrayList<String>();
    contextualDiscreteAttributes.add("C1_Discrete");
    List<String> contextualDoubleAttributes = new ArrayList<String>();
    List<String> attributes = new ArrayList<>();
    attributes.addAll(contextualDiscreteAttributes);
    attributes.addAll(contextualDoubleAttributes);
    conf.set(MacroBaseConf.ATTRIBUTES, attributes);
    conf.set(ContextualConf.CONTEXTUAL_DISCRETE_ATTRIBUTES, contextualDiscreteAttributes);
    conf.set(ContextualConf.CONTEXTUAL_DOUBLE_ATTRIBUTES, contextualDoubleAttributes);
    conf.set(ContextualConf.CONTEXTUAL_DENSECONTEXTTAU, 0.4);
    conf.set(ContextualConf.CONTEXTUAL_NUMINTERVALS, 10);
    conf.set(MacroBaseConf.OUTLIER_STATIC_THRESHOLD, 3.0);
    conf.getEncoder().recordAttributeName(1, "A1");
    conf.getEncoder().recordAttributeName(2, "C1_Discrete");
    ContextualOutlierDetector contextualDetector = new ContextualOutlierDetector(conf);
    List<Datum> data = new ArrayList<>();
    for (int i = 0; i < 100; ++i) {
        double[] sample = new double[1];
        sample[0] = i;
        Integer[] c1 = new Integer[1];
        if (i < 5) {
            c1[0] = 1;
        } else if (i >= 5 && i < 50) {
            c1[0] = 2;
        } else {
            c1[0] = 1;
        }
        data.add(makeDatum(conf, new ArrayList<>(), new ArrayRealVector(sample),
                           new ArrayList<>(Arrays.asList(c1)),
                           new ArrayRealVector()));
    }
    ContextualTransformer transformer = new ContextualTransformer(conf);
    transformer.consume(data);
    List<ContextualDatum> cdata = transformer.getStream().drain();
    Map<Context, List<OutlierClassificationResult>> context2Outliers = contextualDetector.searchContextualOutliers(cdata);
    TestCase.assertEquals(context2Outliers.size(), 1);
    for (Context context : context2Outliers.keySet()) {
        List<Interval> intervals = context.getIntervals();
        TestCase.assertEquals(intervals.size(), 1);
        assertEquals(intervals.get(0).getColumnName(), "C1_Discrete");
        TestCase.assertEquals(intervals.get(0) instanceof IntervalDiscrete, true);
        assertEquals("1",
                     conf.getEncoder().getAttribute(((IntervalDiscrete) intervals.get(0)).getValue()).getValue());
    }
}