Search in sources :

Example 6 with MISTPredicate

use of edu.snu.mist.common.functions.MISTPredicate in project mist by snuspl.

the class StateTransitionOperatorTest method testStateTransitionOperator.

/**
 * Test StateTransitionOperator.
 * Final states are "1" and "4".
 */
@Test
public void testStateTransitionOperator() throws InterruptedException {
    // input events
    // expected state transitions: 0 -- 1 -- 0 -- 3 -- 4, "1" and "4" are emitted.
    final Map<String, Integer> value1 = new HashMap<>();
    value1.put("number", 1);
    final MistDataEvent data1 = new MistDataEvent(value1, 0L);
    final Map<String, Integer> value2 = new HashMap<>();
    value2.put("number", 2);
    final MistDataEvent data2 = new MistDataEvent(value2, 1L);
    final MistWatermarkEvent watermarkEvent = new MistWatermarkEvent(7L);
    final Map<String, Integer> value3 = new HashMap<>();
    value3.put("number", 3);
    final MistDataEvent data3 = new MistDataEvent(value3, 10L);
    final Map<String, Integer> value4 = new HashMap<>();
    value4.put("number", 4);
    final MistDataEvent data4 = new MistDataEvent(value4, 11L);
    // make a set of final states
    final Set<String> finalSet = new HashSet<>();
    finalSet.add("1");
    finalSet.add("4");
    // make a state table
    final Map<String, Collection<Tuple2<MISTPredicate, String>>> stateTable = new HashMap<>();
    final Collection<Tuple2<MISTPredicate, String>> list0 = new ArrayList<>();
    list0.add(new Tuple2<>(new RuleBasedEQPredicate("number", 1), "1"));
    list0.add(new Tuple2<>(new RuleBasedEQPredicate("number", 3), "3"));
    final Collection<Tuple2<MISTPredicate, String>> list1 = new ArrayList<>();
    list1.add(new Tuple2<>(new RuleBasedEQPredicate("number", 2), "0"));
    final Collection<Tuple2<MISTPredicate, String>> list3 = new ArrayList<>();
    list3.add(new Tuple2<>(new RuleBasedEQPredicate("number", 4), "4"));
    stateTable.put("0", list0);
    stateTable.put("1", list1);
    stateTable.put("3", list3);
    // make a state transition operator
    final StateTransitionOperator stateTransitionOperator = new StateTransitionOperator("0", finalSet, stateTable);
    final List<MistEvent> result = new LinkedList<>();
    stateTransitionOperator.setOutputEmitter(new OutputBufferEmitter(result));
    stateTransitionOperator.processLeftData(data1);
    Assert.assertEquals(1, result.size());
    Assert.assertEquals(data1, result.get(0));
    Assert.assertEquals("1", stateTransitionOperator.getStateSnapshot().get("stateTransitionOperatorState"));
    stateTransitionOperator.processLeftData(data2);
    Assert.assertEquals(1, result.size());
    Assert.assertEquals("0", stateTransitionOperator.getStateSnapshot().get("stateTransitionOperatorState"));
    stateTransitionOperator.processLeftWatermark(watermarkEvent);
    Assert.assertEquals(2, result.size());
    Assert.assertEquals(watermarkEvent, result.get(1));
    Assert.assertEquals("0", stateTransitionOperator.getStateSnapshot().get("stateTransitionOperatorState"));
    stateTransitionOperator.processLeftData(data3);
    Assert.assertEquals(2, result.size());
    Assert.assertEquals("3", stateTransitionOperator.getStateSnapshot().get("stateTransitionOperatorState"));
    stateTransitionOperator.processLeftData(data4);
    Assert.assertEquals(3, result.size());
    Assert.assertEquals(data4, result.get(2));
    Assert.assertEquals("4", stateTransitionOperator.getStateSnapshot().get("stateTransitionOperatorState"));
}
Also used : RuleBasedEQPredicate(edu.snu.mist.common.predicates.RuleBasedEQPredicate) MistEvent(edu.snu.mist.core.MistEvent) OutputBufferEmitter(edu.snu.mist.core.utils.OutputBufferEmitter) MISTPredicate(edu.snu.mist.common.functions.MISTPredicate) MistDataEvent(edu.snu.mist.core.MistDataEvent) Tuple2(edu.snu.mist.common.types.Tuple2) MistWatermarkEvent(edu.snu.mist.core.MistWatermarkEvent) Test(org.junit.Test)

Example 7 with MISTPredicate

use of edu.snu.mist.common.functions.MISTPredicate in project mist by snuspl.

the class StateTransitionOperatorTest method testStateTransitionOperatorSetState.

/**
 * Test setting state of StateTransitionOperator.
 */
@Test
public void testStateTransitionOperatorSetState() throws InterruptedException {
    // Generate a new state and set it to StateTransitionOperator.
    final String newFunctionState = "1";
    final Map<String, Object> loadStateMap = new HashMap<>();
    loadStateMap.put("stateTransitionOperatorState", newFunctionState);
    // generate a set of final states
    final Set<String> finalSet = new HashSet<>();
    finalSet.add("2");
    finalSet.add("3");
    // generate a state table
    final Map<String, Collection<Tuple2<MISTPredicate, String>>> stateTable = new HashMap<>();
    final Collection<Tuple2<MISTPredicate, String>> list1 = new ArrayList<>();
    list1.add(new Tuple2<>(new RuleBasedEQPredicate("number", 2), "2"));
    final Collection<Tuple2<MISTPredicate, String>> list2 = new ArrayList<>();
    list2.add(new Tuple2<>(new RuleBasedEQPredicate("number", 3), "3"));
    stateTable.put("1", list1);
    stateTable.put("2", list2);
    final StateTransitionOperator stateTransitionOperator = new StateTransitionOperator("0", finalSet, stateTable);
    stateTransitionOperator.setState(loadStateMap);
    // Get the current StateTransitionOperator's state.
    final Map<String, Object> operatorState = stateTransitionOperator.getStateSnapshot();
    final String stateTransitionOperatorState = (String) operatorState.get("stateTransitionOperatorState");
    // Compare the original and the set operator
    Assert.assertEquals(newFunctionState, stateTransitionOperatorState);
    // Test if the operator can properly process data.
    final List<MistEvent> result = new LinkedList<>();
    stateTransitionOperator.setOutputEmitter(new OutputBufferEmitter(result));
    // expected result: 2--3
    // generate input data event
    final Map<String, Integer> value2 = new HashMap<>();
    value2.put("number", 2);
    final MistDataEvent data2 = new MistDataEvent(value2, 0L);
    final Map<String, Integer> value3 = new HashMap<>();
    value3.put("number", 3);
    final MistDataEvent data3 = new MistDataEvent(value3, 1L);
    stateTransitionOperator.processLeftData(data2);
    Assert.assertEquals(1, result.size());
    Assert.assertEquals(data2, result.get(0));
    Assert.assertEquals("2", stateTransitionOperator.getStateSnapshot().get("stateTransitionOperatorState"));
    stateTransitionOperator.processLeftData(data3);
    Assert.assertEquals(2, result.size());
    Assert.assertEquals(data3, result.get(1));
    Assert.assertEquals("3", stateTransitionOperator.getStateSnapshot().get("stateTransitionOperatorState"));
}
Also used : RuleBasedEQPredicate(edu.snu.mist.common.predicates.RuleBasedEQPredicate) MistEvent(edu.snu.mist.core.MistEvent) OutputBufferEmitter(edu.snu.mist.core.utils.OutputBufferEmitter) MISTPredicate(edu.snu.mist.common.functions.MISTPredicate) Tuple2(edu.snu.mist.common.types.Tuple2) MistDataEvent(edu.snu.mist.core.MistDataEvent) Test(org.junit.Test)

Example 8 with MISTPredicate

use of edu.snu.mist.common.functions.MISTPredicate in project mist by snuspl.

the class StatelessOperatorTest method testFilterOperator.

/**
 * Test filter operator.
 * It filters string values which start with "a".
 */
@Test
public void testFilterOperator() throws InjectionException {
    // input stream
    final List<MistDataEvent> inputStream = ImmutableList.of(new MistDataEvent("alpha", 1L), new MistDataEvent("gamma", 2L), new MistDataEvent("bravo", 3L), new MistDataEvent("area", 4L), new MistDataEvent("charlie", 5L), new MistDataEvent("delta", 6L), new MistDataEvent("application", 7L), new MistDataEvent("echo", 8L), new MistDataEvent("ally", 9L), new MistDataEvent("foxtrot", 10L));
    // expected output
    final List<MistEvent> expectedStream = ImmutableList.of(new MistDataEvent("alpha", 1L), new MistDataEvent("area", 4L), new MistDataEvent("application", 7L), new MistDataEvent("ally", 9L));
    // create a filter function
    final MISTPredicate<String> filterFunc = (input) -> input.startsWith("a");
    final FilterOperator<String> filterOperator = new FilterOperator<>(filterFunc);
    testStatelessOperator(inputStream, expectedStream, filterOperator);
}
Also used : Arrays(java.util.Arrays) MISTFunction(edu.snu.mist.common.functions.MISTFunction) MISTPredicate(edu.snu.mist.common.functions.MISTPredicate) MistEvent(edu.snu.mist.core.MistEvent) Tuple(org.apache.reef.io.Tuple) Test(org.junit.Test) Logger(java.util.logging.Logger) MistDataEvent(edu.snu.mist.core.MistDataEvent) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) OutputBufferEmitter(edu.snu.mist.core.utils.OutputBufferEmitter) InjectionException(org.apache.reef.tang.exceptions.InjectionException) Assert(org.junit.Assert) LinkedList(java.util.LinkedList) MistDataEvent(edu.snu.mist.core.MistDataEvent) MistEvent(edu.snu.mist.core.MistEvent) Test(org.junit.Test)

Example 9 with MISTPredicate

use of edu.snu.mist.common.functions.MISTPredicate 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)

Aggregations

MISTPredicate (edu.snu.mist.common.functions.MISTPredicate)9 MistDataEvent (edu.snu.mist.core.MistDataEvent)6 Test (org.junit.Test)6 MISTFunction (edu.snu.mist.common.functions.MISTFunction)4 MistEvent (edu.snu.mist.core.MistEvent)4 OutputBufferEmitter (edu.snu.mist.core.utils.OutputBufferEmitter)4 List (java.util.List)4 RuleBasedEQPredicate (edu.snu.mist.common.predicates.RuleBasedEQPredicate)3 Tuple2 (edu.snu.mist.common.types.Tuple2)3 MistWatermarkEvent (edu.snu.mist.core.MistWatermarkEvent)3 LinkedList (java.util.LinkedList)3 Tuple (org.apache.reef.io.Tuple)3 InjectionException (org.apache.reef.tang.exceptions.InjectionException)3 WatermarkTimestampFunction (edu.snu.mist.common.functions.WatermarkTimestampFunction)2 Arrays (java.util.Arrays)2 Logger (java.util.logging.Logger)2 Tang (org.apache.reef.tang.Tang)2 ImmutableList (com.google.common.collect.ImmutableList)1 APIQueryControlResult (edu.snu.mist.client.APIQueryControlResult)1 MISTQueryBuilder (edu.snu.mist.client.MISTQueryBuilder)1