Search in sources :

Example 26 with MistDataEvent

use of edu.snu.mist.core.MistDataEvent in project mist by snuspl.

the class AggregateWindowOperatorTest method testAggregateWindowOperator.

/**
 * Test AggregateWindowOperator.
 * It calculates the maximum value from the collection.
 */
@Test
public void testAggregateWindowOperator() {
    // input stream events
    final WindowImpl<Integer> window = new WindowImpl<>(0L, 100L);
    window.putData(new MistDataEvent(10, 10));
    window.putData(new MistDataEvent(20, 20));
    window.putData(new MistDataEvent(15, 30));
    window.putData(new MistDataEvent(30, 90));
    final MistDataEvent dataEvent = new MistDataEvent(window, 90L);
    final MistWatermarkEvent watermarkEvent = new MistWatermarkEvent(101L);
    // functions that dealing with input WindowData
    final MISTFunction<WindowData<Integer>, String> aggregateFunc = (windowData) -> {
        return windowData.getDataCollection().toString() + ", " + windowData.getStart() + ", " + windowData.getEnd();
    };
    final AggregateWindowOperator<Integer, String> aggregateWindowOperator = new AggregateWindowOperator<>(aggregateFunc);
    final List<MistEvent> result = new LinkedList<>();
    aggregateWindowOperator.setOutputEmitter(new OutputBufferEmitter(result));
    aggregateWindowOperator.processLeftData(dataEvent);
    Assert.assertEquals(1, result.size());
    Assert.assertTrue(result.get(0).isData());
    Assert.assertEquals("[10, 20, 15, 30], 0, 99", ((MistDataEvent) result.get(0)).getValue());
    Assert.assertEquals(90L, result.get(0).getTimestamp());
    aggregateWindowOperator.processLeftWatermark(watermarkEvent);
    Assert.assertEquals(2, result.size());
    Assert.assertEquals(watermarkEvent, result.get(1));
}
Also used : WindowImpl(edu.snu.mist.core.operators.window.WindowImpl) MistDataEvent(edu.snu.mist.core.MistDataEvent) List(java.util.List) MistWatermarkEvent(edu.snu.mist.core.MistWatermarkEvent) MISTFunction(edu.snu.mist.common.functions.MISTFunction) OutputBufferEmitter(edu.snu.mist.core.utils.OutputBufferEmitter) MistEvent(edu.snu.mist.core.MistEvent) AggregateWindowOperator(edu.snu.mist.core.operators.window.AggregateWindowOperator) Test(org.junit.Test) WindowData(edu.snu.mist.common.windows.WindowData) Assert(org.junit.Assert) LinkedList(java.util.LinkedList) WindowImpl(edu.snu.mist.core.operators.window.WindowImpl) MistEvent(edu.snu.mist.core.MistEvent) LinkedList(java.util.LinkedList) OutputBufferEmitter(edu.snu.mist.core.utils.OutputBufferEmitter) WindowData(edu.snu.mist.common.windows.WindowData) AggregateWindowOperator(edu.snu.mist.core.operators.window.AggregateWindowOperator) MistDataEvent(edu.snu.mist.core.MistDataEvent) MistWatermarkEvent(edu.snu.mist.core.MistWatermarkEvent) Test(org.junit.Test)

Example 27 with MistDataEvent

use of edu.snu.mist.core.MistDataEvent in project mist by snuspl.

the class ApplyStatefulOperatorTest method testApplyStatefulOperatorSetState.

/**
 * Test setting state of the ApplyStatefulOperator.
 */
@Test
public void testApplyStatefulOperatorSetState() throws InterruptedException {
    // Generate a new state and set it to a new ApplyStatefulOperator.
    final ApplyStatefulFunction applyStatefulFunction = new FindMaxIntFunction();
    final int expectedApplyStatefulFunctionState = 5;
    final Map<String, Object> loadStateMap = new HashMap<>();
    loadStateMap.put("applyStatefulFunctionState", expectedApplyStatefulFunctionState);
    final ApplyStatefulOperator<Integer, Integer> applyStatefulOperator = new ApplyStatefulOperator<>(applyStatefulFunction);
    applyStatefulOperator.setState(loadStateMap);
    // Get the current ApplyStatefulOperator's state.
    final Map<String, Object> operatorState = applyStatefulOperator.getStateSnapshot();
    final int applyStatefulFunctionState = (Integer) operatorState.get("applyStatefulFunctionState");
    // Compare the original and the set operator.
    Assert.assertEquals(expectedApplyStatefulFunctionState, applyStatefulFunctionState);
    // Test if the operator can properly process data.
    final List<MistEvent> result = new LinkedList<>();
    applyStatefulOperator.setOutputEmitter(new OutputBufferEmitter(result));
    final MistDataEvent data10 = new MistDataEvent(10, 0L);
    final MistDataEvent data20 = new MistDataEvent(20, 1L);
    final MistDataEvent data15 = new MistDataEvent(15, 2L);
    applyStatefulOperator.processLeftData(data10);
    Assert.assertEquals(1, result.size());
    Assert.assertEquals(10, ((MistDataEvent) result.get(0)).getValue());
    applyStatefulOperator.processLeftData(data20);
    Assert.assertEquals(2, result.size());
    Assert.assertEquals(20, ((MistDataEvent) result.get(1)).getValue());
    applyStatefulOperator.processLeftData(data15);
    Assert.assertEquals(3, result.size());
    Assert.assertEquals(20, ((MistDataEvent) result.get(2)).getValue());
}
Also used : ApplyStatefulFunction(edu.snu.mist.common.functions.ApplyStatefulFunction) HashMap(java.util.HashMap) MistEvent(edu.snu.mist.core.MistEvent) LinkedList(java.util.LinkedList) OutputBufferEmitter(edu.snu.mist.core.utils.OutputBufferEmitter) MistDataEvent(edu.snu.mist.core.MistDataEvent) FindMaxIntFunction(edu.snu.mist.core.utils.FindMaxIntFunction) Test(org.junit.Test)

Example 28 with MistDataEvent

use of edu.snu.mist.core.MistDataEvent in project mist by snuspl.

the class ApplyStatefulOperatorTest method testApplyStatefulOperatorGetState.

/**
 * Test getting state of the ApplyStatefulOperator.
 */
@Test
public void testApplyStatefulOperatorGetState() throws InterruptedException {
    // Generate the current ApplyStatefulOperator.
    final ApplyStatefulFunction applyStatefulFunction = new FindMaxIntFunction();
    final ApplyStatefulOperator<Integer, Integer> applyStatefulOperator = new ApplyStatefulOperator<>(applyStatefulFunction);
    final MistDataEvent data10 = new MistDataEvent(10, 0L);
    final MistDataEvent data20 = new MistDataEvent(20, 1L);
    final List<MistEvent> result = new LinkedList<>();
    applyStatefulOperator.setOutputEmitter(new OutputBufferEmitter(result));
    applyStatefulOperator.processLeftData(data10);
    applyStatefulOperator.processLeftData(data20);
    // Generate the expected ApplyStatefulOperator's state.
    final int expectedApplyStatefulOperatorState = 20;
    // Get the current ApplyStatefulOperator's state.
    final Map<String, Object> operatorState = applyStatefulOperator.getStateSnapshot();
    final int applyStatefulOperatorState = (int) operatorState.get("applyStatefulFunctionState");
    // Compare the expected and original operator's state.
    Assert.assertEquals(expectedApplyStatefulOperatorState, applyStatefulOperatorState);
}
Also used : ApplyStatefulFunction(edu.snu.mist.common.functions.ApplyStatefulFunction) MistEvent(edu.snu.mist.core.MistEvent) LinkedList(java.util.LinkedList) OutputBufferEmitter(edu.snu.mist.core.utils.OutputBufferEmitter) MistDataEvent(edu.snu.mist.core.MistDataEvent) FindMaxIntFunction(edu.snu.mist.core.utils.FindMaxIntFunction) Test(org.junit.Test)

Example 29 with MistDataEvent

use of edu.snu.mist.core.MistDataEvent in project mist by snuspl.

the class ConditionalBranchOperatorTest method testConditionalBranchOperator.

/**
 * Test conditional branch operation.
 * It classifies the input string according to it's length.
 */
@Test
public void testConditionalBranchOperator() throws InjectionException {
    // input stream events
    final MistDataEvent d1 = new MistDataEvent("1", 1L);
    final MistDataEvent d2 = new MistDataEvent("22", 2L);
    final MistDataEvent d3 = new MistDataEvent("333", 3L);
    final MistDataEvent d4 = new MistDataEvent("4444", 4L);
    final MistDataEvent d5 = new MistDataEvent("55555", 5L);
    final MistWatermarkEvent w1 = new MistWatermarkEvent(6L);
    // classify the string according to it's length
    final List<MISTPredicate<String>> predicates = new ArrayList<>();
    // "1" will be passed with index 1
    predicates.add((input) -> input.length() < 2);
    // "22" will be passed with index 2
    predicates.add((input) -> input.length() < 3);
    // "333", "4444" will be passed with index 3
    predicates.add((input) -> input.length() < 5);
    // "55555" will not be passed
    final List<Tuple<MistEvent, Integer>> result = new LinkedList<>();
    final ConditionalBranchOperator<String> conditionalBranchOperator = new ConditionalBranchOperator<>(predicates);
    conditionalBranchOperator.setOutputEmitter(new IndexOutputEmitter(result));
    conditionalBranchOperator.processLeftData(d1);
    conditionalBranchOperator.processLeftData(d2);
    conditionalBranchOperator.processLeftData(d3);
    conditionalBranchOperator.processLeftData(d4);
    conditionalBranchOperator.processLeftData(d5);
    conditionalBranchOperator.processLeftWatermark(w1);
    Assert.assertEquals(5, result.size());
    Assert.assertEquals(new Tuple<>(d1, 1), result.get(0));
    Assert.assertEquals(new Tuple<>(d2, 2), result.get(1));
    Assert.assertEquals(new Tuple<>(d3, 3), result.get(2));
    Assert.assertEquals(new Tuple<>(d4, 3), result.get(3));
    Assert.assertEquals(new Tuple<>(w1, 0), result.get(4));
}
Also used : ArrayList(java.util.ArrayList) IndexOutputEmitter(edu.snu.mist.core.utils.IndexOutputEmitter) LinkedList(java.util.LinkedList) MISTPredicate(edu.snu.mist.common.functions.MISTPredicate) MistDataEvent(edu.snu.mist.core.MistDataEvent) MistWatermarkEvent(edu.snu.mist.core.MistWatermarkEvent) Tuple(org.apache.reef.io.Tuple) Test(org.junit.Test)

Example 30 with MistDataEvent

use of edu.snu.mist.core.MistDataEvent in project mist by snuspl.

the class OperatorTestUtils method checkWindowData.

/**
 * Checks the windowed result is equal to the expected result.
 */
public static void checkWindowData(final MistEvent result, final Collection<Integer> expectedResult, final long expectedWindowStartMoment, final long expectedWindowSize, final long expectedWindowTimestamp) {
    Assert.assertTrue(result.isData());
    Assert.assertTrue(((MistDataEvent) result).getValue() instanceof WindowData);
    final WindowData windowData = (WindowData) ((MistDataEvent) result).getValue();
    Assert.assertEquals(expectedResult, windowData.getDataCollection());
    Assert.assertEquals(expectedWindowStartMoment, windowData.getStart());
    Assert.assertEquals(expectedWindowSize, windowData.getEnd() - windowData.getStart() + 1);
    Assert.assertEquals(expectedWindowTimestamp, result.getTimestamp());
}
Also used : WindowData(edu.snu.mist.common.windows.WindowData) MistDataEvent(edu.snu.mist.core.MistDataEvent)

Aggregations

MistDataEvent (edu.snu.mist.core.MistDataEvent)30 Test (org.junit.Test)21 MistEvent (edu.snu.mist.core.MistEvent)20 OutputBufferEmitter (edu.snu.mist.core.utils.OutputBufferEmitter)20 LinkedList (java.util.LinkedList)18 MistWatermarkEvent (edu.snu.mist.core.MistWatermarkEvent)9 List (java.util.List)9 Assert (org.junit.Assert)8 MISTPredicate (edu.snu.mist.common.functions.MISTPredicate)7 Tuple2 (edu.snu.mist.common.types.Tuple2)7 ImmutableList (com.google.common.collect.ImmutableList)6 Logger (java.util.logging.Logger)6 InjectionException (org.apache.reef.tang.exceptions.InjectionException)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 CepEventPattern (edu.snu.mist.common.cep.CepEventPattern)4 MISTFunction (edu.snu.mist.common.functions.MISTFunction)4 CepExampleClass (edu.snu.mist.core.utils.CepExampleClass)4 FindMaxIntFunction (edu.snu.mist.core.utils.FindMaxIntFunction)4 HashMap (java.util.HashMap)4