Code Examples
A repository of 155 code examples for BeepBeep
basic.SlicerSimple Class Reference

Use the SlicerMap to perform a computation on multiple subsets of an input stream. More...

Static Public Member Functions

static void main (String[] args)
 

Detailed Description

Use the SlicerMap to perform a computation on multiple subsets of an input stream.

The principle of the SlicerMap processor is to "slice" an input stream into multiple sub-streams. For this, a function, called the slicing function, is evaluated on each input event. The value of that function decides what sub-stream that event belongs to. A different instance of a processor is created for each possible value of the slicing function, and the input event is pushed to the corresponding processor.

The slicer keeps an associative map; keys correspond to values of the slicing function, and values are the last event produced by the corresponding processor. Every time an event is received, it returns as its output the updated map.

In this program, the slicing function is the identity, and the processor given to the slicer is a simple counter that increments every time an event is received. Since there is one such counter for each different input event, the slicer effectively maintains the count of how many times each value has been seen in its input stream. Graphically, this can be represented as:

Processor chain

The expected output of this program is:

{1=1.0}
{1=1.0, 6=1.0}
{1=1.0, 4=1.0, 6=1.0}
{1=1.0, 3=1.0, 4=1.0, 6=1.0}
{1=1.0, 2=1.0, 3=1.0, 4=1.0, 6=1.0}
{1=2.0, 2=1.0, 3=1.0, 4=1.0, 6=1.0}
{1=2.0, 2=1.0, 3=1.0, 4=1.0, 6=1.0, 9=1.0}
…
Author
Sylvain Hallé Easy

Definition at line 75 of file SlicerSimple.java.

Member Function Documentation

◆ main()

static void basic.SlicerSimple.main ( String []  args)
static

We first setup a stream of numbers to be used as a source

Definition at line 77 of file SlicerSimple.java.

78  {
79  /// We first setup a stream of numbers to be used as a source
80  QueueSource source = new QueueSource();
81  source.setEvents(1, 6, 4, 3, 2, 1, 9);
82 
83  /* The function we'll use to create slices is the identity.
84  * This will create one distinct subtrace per number .*/
85  Function slicing_fct = new IdentityFunction(1);
86 
87  /* The processor chain to apply to each subtrace is a simple
88  * counter of events. */
89  GroupProcessor counter = new GroupProcessor(1, 1);
90  {
91  TurnInto to_one = new TurnInto(new Constant(1));
92  Cumulate sum = new Cumulate(
93  new CumulativeFunction<Number>(Numbers.addition));
94  Connector.connect(to_one, sum);
95  counter.addProcessors(to_one, sum);
96  counter.associateInput(INPUT, to_one, INPUT);
97  counter.associateOutput(OUTPUT, sum, OUTPUT);
98  }
99 
100  /* Create the slicer processor, by giving it the slicing function and
101  * the processor to apply on each slide. */
102  Slice slicer = new Slice(slicing_fct, counter);
103  Connector.connect(source, slicer);
104 
105  /* Let us now pull and print 10 events from the slicer. */
106  Pullable p = slicer.getPullableOutput();
107  for (int i = 0; i < 10; i++)
108  {
109  Object o = p.pull();
110  System.out.println(o);
111  }
112  ///
113  }

The documentation for this class was generated from the following file: