Exemple #1
0
        static void Main(string[] args)
        {
            // ingress data
            var input =
                Observable
                .Range(0, 100)
                .Select(e => new MyStruct {
                field1 = e % 10, field2 = e + 0.5, field3 = "blah"
            })
                .Select(e => StreamEvent.CreateStart(StreamEvent.MinSyncTime, e))
                .ToStreamable(OnCompletedPolicy.EndOfStream(), PeriodicPunctuationPolicy.None())
                .Cache();

            // query
            var query =
                input.SetProperty().IsConstantDuration(true)
                .GroupBy(e => e.field2)
                .SelectMany(str => str.Distinct(f => f.field1), (g, c) => c);

            // egress data
            query
            .ToStreamEventObservable()
            .Where(e => e.IsData)
            .ForEachAsync(e => Console.WriteLine("{0}\t{1}\t{2}", e.SyncTime, e.OtherTime, e.Payload)).Wait();

            Console.ReadLine();
        }
Exemple #2
0
        private static IStreamable <Empty, SensorReading> CreateStream(bool isRealTime)
        {
            if (isRealTime)
            {
                return(SimulateLiveData()
                       .Select(r => StreamEvent.CreateInterval(r.Time, r.Time + 1, r))
                       .ToStreamable(OnCompletedPolicy.EndOfStream(), PeriodicPunctuationPolicy.Count(1)));
            }

            return(HistoricData.ToObservable().Select(r => StreamEvent.CreateInterval(r.Time, r.Time + 1, r)).ToStreamable(OnCompletedPolicy.None()));
        }
Exemple #3
0
        static void CreateData()
        {
            source1 = new StreamEvent <Payload>[] {
                StreamEvent.CreatePoint(100, new Payload {
                    Field1 = "A", Field2 = 4
                }),
                StreamEvent.CreatePoint(110, new Payload {
                    Field1 = "C", Field2 = 3
                }),
                StreamEvent.CreatePoint(120, new Payload {
                    Field1 = "A", Field2 = 1
                }),
                StreamEvent.CreatePoint(130, new Payload {
                    Field1 = "B", Field2 = 6
                }),
                StreamEvent.CreatePoint(140, new Payload {
                    Field1 = "B", Field2 = 8
                }),
                StreamEvent.CreatePoint(150, new Payload {
                    Field1 = "C", Field2 = 7
                }),
                StreamEvent.CreatePoint(160, new Payload {
                    Field1 = "B", Field2 = 9
                }),
            }.ToObservable().ToStreamable(OnCompletedPolicy.EndOfStream()).AlterEventDuration(1000).Cache();

            source2 = new StreamEvent <Payload>[] {
                StreamEvent.CreatePoint(100, new Payload {
                    Field1 = "A", Field2 = 1
                }),
                StreamEvent.CreatePoint(110, new Payload {
                    Field1 = "C", Field2 = 2
                }),
                StreamEvent.CreatePoint(120, new Payload {
                    Field1 = "A", Field2 = 2
                }),
                StreamEvent.CreatePoint(130, new Payload {
                    Field1 = "B", Field2 = 2
                }),
                StreamEvent.CreatePoint(140, new Payload {
                    Field1 = "B", Field2 = 2
                }),
                StreamEvent.CreatePoint(150, new Payload {
                    Field1 = "C", Field2 = 1
                }),
                StreamEvent.CreatePoint(160, new Payload {
                    Field1 = "B", Field2 = 1
                }),
            }.ToObservable().ToStreamable(OnCompletedPolicy.EndOfStream()).AlterEventDuration(1000).Cache();
        }
Exemple #4
0
        static void Main(string[] args)
        {
            // Subjects to feed pre- and post-checkpoint data to the query
            var preCheckpointSubject  = new Subject <StreamEvent <ValueTuple <int, int> > >();
            var postCheckpointSubject = new Subject <StreamEvent <ValueTuple <int, int> > >();

            // Outputs of queries with and without checkpointing
            var outputListWithCheckpoint    = new List <ValueTuple <int, ulong> >();
            var outputListWithoutCheckpoint = new List <ValueTuple <int, ulong> >();

            // Containers are an abstraction to hold queries and are the unit of checkpointing
            var container1 = new QueryContainer();
            var container2 = new QueryContainer();
            var container3 = new QueryContainer();

            // Query state is written to and read from a .NET stream
            Stream state = new MemoryStream();

            // Input data: first half of the dataset before a checkpoint is taken
            var preCheckpointData = Enumerable.Range(0, 10000).ToList()
                                    .ToObservable()
                                    .Select(e => StreamEvent.CreateStart(e, new ValueTuple <int, int> {
                Item1 = e % 10, Item2 = e
            }));

            // Input data: second half of the dataset after a checkpoint is taken
            var postCheckpointData = Enumerable.Range(10000, 10000).ToList()
                                     .ToObservable()
                                     .Select(e => StreamEvent.CreateStart(e, new ValueTuple <int, int> {
                Item1 = e % 10, Item2 = e
            }));

            // For comparison, we run the same query directly on the full dataset
            var fullData = Enumerable.Range(0, 20000).ToList()
                           .ToObservable()
                           .Select(e => StreamEvent.CreateStart(e, new ValueTuple <int, int> {
                Item1 = e % 10, Item2 = e
            }));

            // Query 1: Run with first half of the dataset, then take a checkpoint
            var input1 = container1.RegisterInput(preCheckpointSubject, OnCompletedPolicy.EndOfStream());
            var query1 = input1.GroupApply(e => e.Item1, str => str.Sum(e => (ulong)e.Item2), (g, c) => new ValueTuple <int, ulong> {
                Item1 = g.Key, Item2 = c
            });

            var output1 = container1.RegisterOutput(query1);

            var outputAsync1 = output1.Where(e => e.IsData).Select(e => e.Payload).ForEachAsync(o => outputListWithCheckpoint.Add(o));
            var pipe1        = container1.Restore(null);

            preCheckpointData.ForEachAsync(e => preCheckpointSubject.OnNext(e)).Wait();
            preCheckpointSubject.OnNext(StreamEvent.CreatePunctuation <ValueTuple <int, int> >(9999));
            pipe1.Checkpoint(state);

            // Seek to the beginning of the stream that represents checkpointed state
            state.Seek(0, SeekOrigin.Begin);

            // Query 2: Restore the state from the saved checkpoint, and feed the second half of the dataset
            var input2 = container2.RegisterInput(postCheckpointSubject, OnCompletedPolicy.EndOfStream());
            var query2 = input2.GroupApply(e => e.Item1, str => str.Sum(e => (ulong)e.Item2), (g, c) => new ValueTuple <int, ulong> {
                Item1 = g.Key, Item2 = c
            });
            var output2 = container2.RegisterOutput(query2);

            var outputAsync2 = output2.Where(e => e.IsData).Select(e => e.Payload).ForEachAsync(o => outputListWithCheckpoint.Add(o));
            var pipe2        = container2.Restore(state);

            postCheckpointData.ForEachAsync(e => postCheckpointSubject.OnNext(e)).Wait();
            postCheckpointSubject.OnCompleted();
            outputAsync2.Wait();

            // Sort the payloads in the query result
            outputListWithCheckpoint.Sort((a, b) => a.Item1.CompareTo(b.Item1) == 0 ? a.Item2.CompareTo(b.Item2) : a.Item1.CompareTo(b.Item1));

            // Query 3: For comparison, run the query directly on the entire dataset without any checkpoint/restore
            var input3 = container3.RegisterInput(fullData, OnCompletedPolicy.EndOfStream());
            var query3 = input3.GroupApply(e => e.Item1, str => str.Sum(e => (ulong)e.Item2), (g, c) => new ValueTuple <int, ulong> {
                Item1 = g.Key, Item2 = c
            });
            var output3 = container3.RegisterOutput(query3);

            var outputAsync3 = output3.Where(e => e.IsData).Select(e => e.Payload).ForEachAsync(o => outputListWithoutCheckpoint.Add(o));

            container3.Restore(null); // The parameter of "null" to restore causes it to run from scratch
            outputAsync3.Wait();

            // Sort the payloads in the query result
            outputListWithoutCheckpoint.Sort((a, b) => a.Item1.CompareTo(b.Item1) == 0 ? a.Item2.CompareTo(b.Item2) : a.Item1.CompareTo(b.Item1));

            // Perform a comparison of the checkpoint/restore query result and the result of the original query run directly on the entire dataset
            if (outputListWithCheckpoint.SequenceEqual(outputListWithoutCheckpoint))
            {
                Console.WriteLine("SUCCESS: Output of query with checkpoint/restore matched output of uninterrupted query");
            }
            else
            {
                Console.WriteLine("ERROR: Output of query with checkpoint/restore did not match the output of uninterrupted query");
            }
            Console.ReadLine();
        }
Exemple #5
0
        static void Main(string[] args)
        {
            // Affinitize thread to core 0
            NativeMethods.AffinitizeThread(0);

            // Load a sample dataset into main memory
            Console.WriteLine("Caching the input dataset into main memory...");
            Stopwatch sw0 = new Stopwatch();

            sw0.Start();

            var tumblingWindowDataset =
                Observable.Range(0, TotalInputEvents)
                .Select(e => StreamEvent.CreateInterval(((long)e / NumEventsPerTumble) * NumEventsPerTumble, ((long)e / NumEventsPerTumble) * NumEventsPerTumble + NumEventsPerTumble, new Payload {
                field1 = 0, field2 = 0
            }))
                .ToStreamable(OnCompletedPolicy.EndOfStream())
                .SetProperty().IsConstantDuration(true, NumEventsPerTumble)
                .SetProperty().IsSnapshotSorted(true, e => e.field1)
                .Cache();

            var startEdgeOnlyDataset = tumblingWindowDataset
                                       .AlterEventDuration(StreamEvent.InfinitySyncTime)
                                       .SetProperty().IsSnapshotSorted(true, e => e.field1)
                                       .Cache();

            var singleSnapshotDataset = tumblingWindowDataset
                                        .AlterEventLifetime(a => StreamEvent.MinSyncTime, StreamEvent.InfinitySyncTime)
                                        .SetProperty().IsSnapshotSorted(true, e => e.field1)
                                        .Cache();

            sw0.Stop();
            Console.WriteLine("Time to load cache with {0} tuples: {1} sec", TotalInputEvents, sw0.Elapsed);
            Console.ReadLine();


            Console.WriteLine("\n**** Queries over dataset with tumbling windows ****");
            ProcessQuery(tumblingWindowDataset.Where(e => e.field1 != 0), "input.Where(e => e.field1 != 0)");
            ProcessQuery(tumblingWindowDataset.Select(e => e.field1), "input.Select(e => e.field1)");
            ProcessQuery(tumblingWindowDataset.Count(), "input.Count()");
            ProcessQuery(tumblingWindowDataset.Sum(e => e.field2), "input.Sum(e => e.field2)");
            ProcessQuery(tumblingWindowDataset.Average(e => e.field1), "input.Average(e => e.field1)");
            ProcessQuery(tumblingWindowDataset.Max(e => e.field1), "input.Max(e => e.field1)");
            ProcessQuery(tumblingWindowDataset.Min(e => e.field1), "input.Min(e => e.field1)");
            ProcessQuery(tumblingWindowDataset.GroupApply(e => e.field2, str => str.Count(), (g, c) => new { Key = g, Count = c }), "input.GroupApply(e => e.field2, str => str.Count(), (g, c) => new { Key = g, Count = c })");
            //ProcessQuery(tumblingWindowDataset.Multicast(str => str.Join(str, e => e.field1, e => e.field1, (l, r) => l)), "Join");


            Console.WriteLine("\n**** Queries over start-edge-only dataset ****");
            ProcessQuery(startEdgeOnlyDataset.Count(), "input.Count()");
            ProcessQuery(startEdgeOnlyDataset.Sum(e => e.field2), "input.Sum(e => e.field2)");
            ProcessQuery(startEdgeOnlyDataset.Average(e => e.field1), "input.Average(e => e.field1)");
            ProcessQuery(startEdgeOnlyDataset.Max(e => e.field1), "input.Max(e => e.field1)");
            ProcessQuery(startEdgeOnlyDataset.Min(e => e.field1), "input.Min(e => e.field1)");
            //ProcessQuery(startEdgeOnlyDataset.Multicast(str => str.Join(str, e => e.field1, e => e.field1, (l, r) => l)), "Self-Join");


            Console.WriteLine("\n**** Queries over a single-snapshot (atemporal) dataset ****");
            ProcessQuery(singleSnapshotDataset.Count(), "input.Count()");
            ProcessQuery(singleSnapshotDataset.Sum(e => e.field2), "input.Sum(e => e.field2)");
            ProcessQuery(singleSnapshotDataset.Average(e => e.field1), "input.Average(e => e.field1)");
            ProcessQuery(singleSnapshotDataset.Max(e => e.field1), "input.Max(e => e.field1)");
            ProcessQuery(singleSnapshotDataset.Min(e => e.field1), "input.Min(e => e.field1)");

            /* Dispose off the caches */
            tumblingWindowDataset.Dispose();
            startEdgeOnlyDataset.Dispose();
            singleSnapshotDataset.Dispose();

            Console.WriteLine("Press <ENTER>");
            Console.ReadLine();
        }
Exemple #6
0
        private static void AtemporalExample()
        {
            var points = new Point[]
            {
                new Point {
                    x = 1, y = 2
                },
                new Point {
                    x = 2, y = 4
                },
                new Point {
                    x = 3, y = 6
                },
                new Point {
                    x = 4, y = 8
                },
                new Point {
                    x = 5, y = 10
                },
                new Point {
                    x = 6, y = 12
                },
                new Point {
                    x = 7, y = 14
                },
                new Point {
                    x = 8, y = 16
                },
                new Point {
                    x = 9, y = 18
                }
            };

            // Atemporal ingress operator.
            var input = points.ToObservable().ToAtemporalStreamable(TimelinePolicy.Sequence(5), OnCompletedPolicy.EndOfStream());

            Console.WriteLine("Input =");
            input.ToStreamEventObservable().ForEachAsync(e => Console.WriteLine(e)).Wait();

            var output = input;

            // Atemporal egress operator.
            Console.WriteLine();
            Console.WriteLine("Output =");
            output.ToAtemporalObservable().ForEachAsync(e => Console.WriteLine(e)).Wait();

            Console.ReadLine();
        }
Exemple #7
0
        static void Main(string[] args)
        {
            // This program is a supplement to the Trill Users Guide

            #region Section 2

            IObservable <ContextSwitch> cSTicksObs = new[]
            {
                new ContextSwitch(0, 1, 1, 120),
                new ContextSwitch(0, 3, 2, 121),
                new ContextSwitch(0, 5, 3, 124),
                new ContextSwitch(120, 2, 1, 123),
                new ContextSwitch(300, 1, 1, 122),
                new ContextSwitch(1800, 4, 2, 125),
                new ContextSwitch(3540, 2, 1, 119),
                new ContextSwitch(3600, 1, 1, 120),
            }.ToObservable();

            Console.WriteLine("Figure 11: Complete Pass-Through Trill Query Program");

            var cSTicksEventObs = cSTicksObs.Select(
                e => StreamEvent.CreateInterval(e.CSTicks, e.CSTicks + 1, e));
            var cSTicksStream =
                cSTicksEventObs.ToStreamable(OnCompletedPolicy.EndOfStream(),
                                             DisorderPolicy.Drop());
            var origCSTicksEventObs = cSTicksStream.ToStreamEventObservable();
            origCSTicksEventObs.Where(e => e.IsData).ForEachAsync(e => Console.WriteLine(
                                                                      "Start Time={0}\tEnd Time={1}\tCSTicks={2}\tPID={3}\tCID={4}\tCPUTemp={5}",
                                                                      e.SyncTime, e.OtherTime, e.Payload.CSTicks, e.Payload.PID, e.Payload.CID,
                                                                      e.Payload.CPUTemp)).Wait();
            Console.WriteLine();

            #endregion

            #region Section 3

            Console.WriteLine("Figure 13: Where Query Code");

            var cSTicks2Cores = cSTicksStream.Where(p => p.CID == 1 || p.CID == 2);
            cSTicks2Cores.ToStreamEventObservable().Where(e => e.IsData).ForEachAsync(e => Console.WriteLine(
                                                                                          "Start Time={0}\tEnd Time={1}\tCSTicks={2}\tPID={3}\tCID={4}\tCPUTemp={5}",
                                                                                          e.SyncTime, e.OtherTime, e.Payload.CSTicks, e.Payload.PID, e.Payload.CID,
                                                                                          e.Payload.CPUTemp)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 16: Select Query Code");
            var cSNarrowTicks2Cores = cSTicks2Cores.Select(
                e => new { CSTicks = e.CSTicks, PID = e.PID, CID = e.CID });
            cSNarrowTicks2Cores.ToStreamEventObservable().Where(e => e.IsData).ForEachAsync(e => Console.WriteLine(
                                                                                                "Start Time={0}\tEnd Time={1}\tCSTicks={2}\tPID={3}\tCID={4}",
                                                                                                e.SyncTime, e.OtherTime, e.Payload.CSTicks, e.Payload.PID, e.Payload.CID)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 17: Alternate Select Query Code");
            var cSNarrowTicks2Cores2 = cSTicks2Cores.Select(
                e => new { CSTicks = e.CSTicks, PID = e.PID, CID = e.CID });
            cSNarrowTicks2Cores2.ToStreamEventObservable().Where(e => e.IsData).ForEachAsync(e => Console.WriteLine(
                                                                                                 "Start Time={0}\tEnd Time={1}\tCSTicks={2}\tPID={3}\tCID={4}",
                                                                                                 e.SyncTime, e.OtherTime, e.Payload.CSTicks, e.Payload.PID, e.Payload.CID)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 19: Where and Select with the LinQ Comprehension Syntax");
            var cSNarrowTicks2Cores3 = from e in cSTicksStream
                                       where e.CID == 1 || e.CID == 2
                                       select new { e.CSTicks, e.PID, e.CID };
            cSNarrowTicks2Cores3.ToStreamEventObservable().Where(e => e.IsData).ForEachAsync(e => Console.WriteLine(
                                                                                                 "Start Time={0}\tEnd Time={1}\tCSTicks={2}\tPID={3}\tCID={4}",
                                                                                                 e.SyncTime, e.OtherTime, e.Payload.CSTicks, e.Payload.PID, e.Payload.CID)).Wait();
            Console.WriteLine();

            #endregion

            #region Section 4

            Console.WriteLine("Figure 22: Join Query Code");

            var processNamesObs = new[]
            {
                new ProcessName(1, "Word"),
                new ProcessName(2, "Internet Explorer"),
                new ProcessName(3, "Excel"),
                new ProcessName(4, "Visual Studio"),
                new ProcessName(5, "Outlook"),
            }.ToObservable();
            var pNamesStream = processNamesObs.
                               Select(e => StreamEvent.CreateInterval(0, 10000, e)).
                               ToStreamable(OnCompletedPolicy.EndOfStream());
            var cSTicks2CoresWithPNames = cSNarrowTicks2Cores.Join(pNamesStream, e => e.PID, e => e.PID,
                                                                   (leftPayload, rightPayload) => new
            {
                leftPayload.CSTicks,
                leftPayload.PID,
                leftPayload.CID,
                PName = rightPayload.PName
            });
            cSTicks2CoresWithPNames.ToStreamEventObservable().Where(e => e.IsData).ForEachAsync(e => Console.WriteLine(
                                                                                                    "Start Time={0}\tEnd Time={1}\tCSTicks={2}\tPID={3}\tCID={4}\tPName={5}",
                                                                                                    e.SyncTime, e.OtherTime, e.Payload.CSTicks, e.Payload.PID, e.Payload.CID, e.Payload.PName)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 24: Join Query Comprehension Syntax");
            var cSTicks2CoresWithPNames2 = from leftPayload in cSNarrowTicks2Cores
                                           join rightPayload in pNamesStream on
                                           leftPayload.PID equals rightPayload.PID
                                           select new { leftPayload.CSTicks,
                                                        leftPayload.PID,
                                                        leftPayload.CID,
                                                        PName = rightPayload.PName };
            cSTicks2CoresWithPNames2.ToStreamEventObservable().Where(e => e.IsData).ForEachAsync(e => Console.WriteLine(
                                                                                                     "Start Time={0}\tEnd Time={1}\tCSTicks={2}\tPID={3}\tCID={4}\tPName={5}",
                                                                                                     e.SyncTime, e.OtherTime, e.Payload.CSTicks, e.Payload.PID, e.Payload.CID, e.Payload.PName)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 25: Entire Join Query Comprehension Syntax");
            var cSTicks2CoresWithPNames3 = from leftPayload in cSTicksStream
                                           join rightPayload in pNamesStream on
                                           leftPayload.PID equals rightPayload.PID
                                           where leftPayload.CID == 1 || leftPayload.CID == 2
                                           select new
            {
                leftPayload.CSTicks,
                leftPayload.PID,
                leftPayload.CID,
                rightPayload.PName
            };
            cSTicks2CoresWithPNames3.ToStreamEventObservable().Where(e => e.IsData).ForEachAsync(e => Console.WriteLine(
                                                                                                     "Start Time={0}\tEnd Time={1}\tCSTicks={2}\tPID={3}\tCID={4}\tPName={5}",
                                                                                                     e.SyncTime, e.OtherTime, e.Payload.CSTicks, e.Payload.PID, e.Payload.CID, e.Payload.PName)).Wait();
            Console.WriteLine();

            #endregion

            #region Section 5

            Console.WriteLine("Figure 26: AlterEventDuration Query Code");
            var infinitecSTicks2Cores =
                cSTicks2CoresWithPNames.AlterEventDuration(StreamEvent.InfinitySyncTime);
            infinitecSTicks2Cores.ToStreamEventObservable().Where(e => e.IsData).ForEachAsync(e => Console.WriteLine(
                                                                                                  "Start Time={0}\tEnd Time={1}\tCSTicks={2}\tPID={3}\tCID={4}\tPName={5}",
                                                                                                  e.SyncTime, e.OtherTime, e.Payload.CSTicks, e.Payload.PID, e.Payload.CID, e.Payload.PName)).Wait();
            Console.WriteLine();

            #endregion Section 6

            #region Section 6

            Console.WriteLine("Figure 28: ClipEventDuration Query Code");
            var clippedCSTicks2Cores = infinitecSTicks2Cores.
                                       ClipEventDuration(infinitecSTicks2Cores, e => e.CID, e => e.CID);
            clippedCSTicks2Cores.ToStreamEventObservable(ReshapingPolicy.CoalesceEndEdges()).Where(e => e.IsData).ForEachAsync(e => Console.WriteLine(
                                                                                                                                   "Start Time={0}\tEnd Time={1}\tCSTicks={2}\tPID={3}\tCID={4}\tPName={5}",
                                                                                                                                   e.SyncTime, e.OtherTime, e.Payload.CSTicks, e.Payload.PID, e.Payload.CID, e.Payload.PName)).Wait();
            Console.WriteLine();

            #endregion

            #region Section 7

            Console.WriteLine("Figure 30: Multicast Version of ClipEventDuration Query Code");
            var clippedCSTicks2Cores2 = infinitecSTicks2Cores.Multicast(
                s => s.ClipEventDuration(s, e => e.CID, e => e.CID));
            clippedCSTicks2Cores2.ToStreamEventObservable(ReshapingPolicy.CoalesceEndEdges()).Where(e => e.IsData).ForEachAsync(e => Console.WriteLine(
                                                                                                                                    "Start Time={0}\tEnd Time={1}\tCSTicks={2}\tPID={3}\tCID={4}\tPName={5}",
                                                                                                                                    e.SyncTime, e.OtherTime, e.Payload.CSTicks, e.Payload.PID, e.Payload.CID, e.Payload.PName)).Wait();
            Console.WriteLine();

            #endregion

            #region Section 8

            Console.WriteLine("Figure 32: ShiftEventLifetime Query Code");
            var shiftedClippedCS = clippedCSTicks2Cores.ShiftEventLifetime(startTime => 1);
            shiftedClippedCS.ToStreamEventObservable(ReshapingPolicy.CoalesceEndEdges()).Where(e => e.IsData).ForEachAsync(e => Console.WriteLine(
                                                                                                                               "Start Time={0}\tEnd Time={1}\tCSTicks={2}\tPID={3}\tCID={4}\tPName={5}",
                                                                                                                               e.SyncTime, e.OtherTime, e.Payload.CSTicks, e.Payload.PID, e.Payload.CID, e.Payload.PName)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 34: Timeslices Query Code");
            var timeslices = shiftedClippedCS.Join(cSTicks2CoresWithPNames, e => e.CID, e => e.CID,
                                                   (left, right) => new
            {
                left.PID,
                left.CID,
                left.PName,
                Timeslice = right.CSTicks - left.CSTicks
            });
            timeslices.ToStreamEventObservable(ReshapingPolicy.CoalesceEndEdges()).Where(e => e.IsData).ForEachAsync(e => Console.WriteLine(
                                                                                                                         "Start Time={0}\tEnd Time={1}\tPID={2}\tCID={3}\tPName={4}\tTimeslice={5}",
                                                                                                                         e.SyncTime, e.OtherTime, e.Payload.PID, e.Payload.CID, e.Payload.PName, e.Payload.Timeslice)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 36: Timeslices Query Code Using Multicast");
            var timeslices2 = cSTicks2CoresWithPNames.Multicast(
                t => t.AlterEventDuration(StreamEvent.InfinitySyncTime).
                Multicast(s => s.ClipEventDuration(s, e => e.CID, e => e.CID)).
                ShiftEventLifetime(startTime => 1).
                Join(t, e => e.CID, e => e.CID, (left, right) => new
            {
                left.PID,
                left.CID,
                left.PName,
                Timeslice = right.CSTicks - left.CSTicks
            }));
            timeslices2.ToStreamEventObservable(ReshapingPolicy.CoalesceEndEdges()).Where(e => e.IsData).ForEachAsync(e => Console.WriteLine(
                                                                                                                          "Start Time={0}\tEnd Time={1}\tPID={2}\tCID={3}\tPName={4}\tTimeslice={5}",
                                                                                                                          e.SyncTime, e.OtherTime, e.Payload.PID, e.Payload.CID, e.Payload.PName, e.Payload.Timeslice)).Wait();
            Console.WriteLine();

            #endregion

            #region Section 9

            Console.WriteLine("Figure 40: Rassigning Timeslice Lifetimes with AlterEventLifetime");
            var pID1CID1Timeslices = timeslices.Where(e => e.PID == 1 && e.CID == 1);
            var windowedTimeslices = pID1CID1Timeslices.
                                     AlterEventLifetime(origStartTime => (1 + (origStartTime - 1) / 3600) * 3600, 3600);
            windowedTimeslices.ToStreamEventObservable().Where(e => e.IsData).ForEachAsync(e => Console.WriteLine(
                                                                                               "Start Time={0}\tEnd Time={1}\tPID={2}\tCID={3}\tPName={4}\tTimeslice={5}",
                                                                                               e.SyncTime, e.OtherTime, e.Payload.PID, e.Payload.CID, e.Payload.PName, e.Payload.Timeslice)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 41: Reassigning Lifetimes with HoppingWindowLifetime");
            var windowedTimeslices2 = pID1CID1Timeslices.HoppingWindowLifetime(3600, 3600);
            windowedTimeslices2.ToStreamEventObservable().Where(e => e.IsData).ForEachAsync(e => Console.WriteLine(
                                                                                                "Start Time={0}\tEnd Time={1}\tPID={2}\tCID={3}\tPName={4}\tTimeslice={5}",
                                                                                                e.SyncTime, e.OtherTime, e.Payload.PID, e.Payload.CID, e.Payload.PName, e.Payload.Timeslice)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 42: Sum Query Code");
            var totalConsumptionPerPeriod = windowedTimeslices.Sum(e => e.Timeslice);
            totalConsumptionPerPeriod.ToStreamEventObservable(ReshapingPolicy.CoalesceEndEdges()).Where(e => e.IsData).ForEachAsync(e => Console.WriteLine(
                                                                                                                                        "Start Time={0}\tEnd Time={1}\tPayload={2}",
                                                                                                                                        e.SyncTime, e.OtherTime, e.Payload)).Wait();
            Console.WriteLine();

            #endregion

            #region Section 10

            Console.WriteLine("Figure 44: Group and Apply Query Code");
            var totalConsumptionPerPeriodAllCoresAllProcesses = timeslices.GroupApply(
                e => new { e.CID, e.PID, e.PName },
                s => s.HoppingWindowLifetime(3600, 3600).Sum(e => e.Timeslice),
                (g, p) => new { g.Key.CID, g.Key.PName, TotalTime = p });
            totalConsumptionPerPeriodAllCoresAllProcesses.ToStreamEventObservable(ReshapingPolicy.CoalesceEndEdges()).Where(e => e.IsData).ForEachAsync(e => Console.WriteLine(
                                                                                                                                                            "Start Time={0}\tEnd Time={1}\tCID={2}\tPName={3}\tTotaltime={4}",
                                                                                                                                                            e.SyncTime, e.OtherTime, e.Payload.CID, e.Payload.PName, e.Payload.TotalTime)).Wait();
            Console.WriteLine();

            #endregion

            #region Section 11

            Console.WriteLine("Figure 49: Chop Query Code");
            Console.WriteLine("NB: This query is expected to fail!");
            var altCSTicksObs = new[]
            {
                new ContextSwitch(0, 1, 1, 120),
                new ContextSwitch(0, 3, 2, 121),
                new ContextSwitch(0, 5, 3, 124),
                new ContextSwitch(120, 2, 1, 123),
                new ContextSwitch(300, 1, 1, 122),
                new ContextSwitch(1800, 4, 2, 125),
                new ContextSwitch(3540, 2, 1, 119),
                new ContextSwitch(3600, 1, 1, 120),
                new ContextSwitch(5400, 3, 2, 122),
                new ContextSwitch(7200, 4, 2, 121),
            }.ToObservable();
            var altCSTicksEventObs = altCSTicksObs.Select(
                e => StreamEvent.CreateInterval(e.CSTicks, e.CSTicks + 1, e));
            var altCSTicksStream =
                altCSTicksEventObs.ToStreamable(OnCompletedPolicy.EndOfStream(), DisorderPolicy.Drop());
            var cSTicksWithExtraCS = altCSTicksStream.AlterEventDuration(StreamEvent.InfinitySyncTime).
                                     Multicast(s => s.ClipEventDuration(s, e => e.CID, e => e.CID)).
                                     Chop(0, 3600).
                                     Select((origStartTime, e) =>
                                            new { CSTicks = origStartTime, e.PID, e.CID, e.CPUTemp }).
                                     AlterEventDuration(1);
            try
            {
                cSTicksWithExtraCS.ToStreamEventObservable().Where(e => e.IsData).ForEachAsync(e => Console.WriteLine(
                                                                                                   "Start Time={0}\tEnd Time={1}\tCSTicks{2}\tPID={3}\tCID={4}\tCPUTemp{5}",
                                                                                                   e.SyncTime, e.OtherTime, e.Payload.CSTicks, e.Payload.PID, e.Payload.CID, e.Payload.CPUTemp)).Wait();
            }
            catch (AggregateException e)
            {
                foreach (var ex in e.InnerExceptions)
                {
                    Console.WriteLine("{0}", ex.Message);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("{0}", e.Message);
            }
            Console.WriteLine();

            Console.WriteLine("Figure 51: Improved Chop Query Code");
            var cSTicksWithExtraCSImp = altCSTicksStream.
                                        AlterEventDuration(StreamEvent.InfinitySyncTime).
                                        Multicast(s => s.ClipEventDuration(s, e => e.CID, e => e.CID)).
                                        Join(new[] { StreamEvent.CreateInterval(0, 10800, Unit.Default) }.
                                             ToObservable().ToStreamable(OnCompletedPolicy.EndOfStream()),
                                             (left, right) => left).Chop(0, 3600).Select((origStartTime, e) =>
                                                                                         new { e.CID, e.PID, e.CPUTemp, CSTicks = origStartTime }).
                                        AlterEventDuration(1);
            cSTicksWithExtraCSImp.ToStreamEventObservable().Where(e => e.IsData).ForEachAsync(e => Console.WriteLine(
                                                                                                  "Start Time={0}\tEnd Time={1}\tCSTicks{2}\tPID={3}\tCID={4}\tCPUTemp{5}",
                                                                                                  e.SyncTime, e.OtherTime, e.Payload.CSTicks, e.Payload.PID, e.Payload.CID, e.Payload.CPUTemp)).Wait();
            Console.WriteLine();

            #endregion

            #region Section 12

            Console.WriteLine("Figure 53: Out of Order Input (Throw)");
            var ooStreamableThrow = new[]
            {
                StreamEvent.CreateInterval(10, 100, 1),
                StreamEvent.CreateInterval(0, 50, 2),
                StreamEvent.CreateInterval(0, 10, 3),
                StreamEvent.CreateInterval(11, 90, 4)
            }.ToObservable().ToStreamable(OnCompletedPolicy.EndOfStream(), DisorderPolicy.Throw());
            try
            {
                ooStreamableThrow.ToStreamEventObservable().Where(e => e.IsData || e.IsPunctuation).ForEachAsync(e => WriteEvent(e)).Wait();
            }
            catch (Exception e)
            {
                Console.WriteLine("{0}", e.Message);
            }
            Console.WriteLine();

            Console.WriteLine("Figure 54: Out of Order Input (Drop)");
            var ooStreamableDrop = new[]
            {
                StreamEvent.CreateInterval(10, 100, 1),
                StreamEvent.CreateInterval(0, 50, 2),
                StreamEvent.CreateInterval(0, 10, 3),
                StreamEvent.CreateInterval(11, 90, 4)
            }.ToObservable().ToStreamable(OnCompletedPolicy.EndOfStream(), DisorderPolicy.Drop());
            ooStreamableDrop.ToStreamEventObservable().Where(e => e.IsData || e.IsPunctuation).ForEachAsync(e => WriteEvent(e)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 55: Out of Order Input (Adjust)");
            var ooStreamableAdjust = new[]
            {
                StreamEvent.CreateInterval(10, 100, 1),
                StreamEvent.CreateInterval(0, 50, 2),
                StreamEvent.CreateInterval(0, 10, 3),
                StreamEvent.CreateInterval(11, 90, 4)
            }.ToObservable().ToStreamable(OnCompletedPolicy.EndOfStream(), DisorderPolicy.Adjust());
            ooStreamableAdjust.ToStreamEventObservable().Where(e => e.IsData || e.IsPunctuation).ForEachAsync(e => WriteEvent(e)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 57: Creating PNamesStream with Edge Events");
            var pNamesStream2 = processNamesObs.Select(e => StreamEvent.CreateStart(0, e)).
                                Concat(processNamesObs.Select(
                                           e => StreamEvent.CreateEnd(10000, 0, e))).
                                ToStreamable(OnCompletedPolicy.EndOfStream());
            pNamesStream2.ToStreamEventObservable().Where(e => e.IsData || e.IsPunctuation).ForEachAsync(e => WriteEvent(e)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 58: Creating PNameStream using Interval and Edge Events");
            var processNamesObs1 = new[]
            {
                new ProcessName(1, "Word"),
                new ProcessName(2, "Internet Explorer"),
                new ProcessName(3, "Excel"),
            }.ToObservable();
            var processNamesObs2 = new[]
            {
                new ProcessName(4, "Visual Studio"),
                new ProcessName(5, "Outlook"),
            }.ToObservable();
            var pNamesStream3 = processNamesObs1.Select(e => StreamEvent.CreateInterval(0, 10000, e)).
                                Concat(processNamesObs2.Select(e => StreamEvent.CreateStart(0, e))).
                                Concat(processNamesObs2.Select(e => StreamEvent.CreateEnd(10000, 0, e))).
                                ToStreamable(OnCompletedPolicy.EndOfStream());
            pNamesStream3.ToStreamEventObservable().Where(e => e.IsData || e.IsPunctuation).ForEachAsync(e => WriteEvent(e)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 59: Coalescing Matching Edges");
            pNamesStream3.ToStreamEventObservable(ReshapingPolicy.CoalesceEndEdges()).Where(e => e.IsData || e.IsPunctuation).ForEachAsync(e => WriteEvent(e)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 60: Input with Punctuations");
            var streamablePunctuations = new[]
            {
                StreamEvent.CreateInterval(0, 1, 1),
                StreamEvent.CreateInterval(3, 4, 2),
                StreamEvent.CreatePunctuation <int>(10),
                StreamEvent.CreatePunctuation <int>(20),
                StreamEvent.CreatePunctuation <int>(30),
                StreamEvent.CreatePunctuation <int>(40),
                StreamEvent.CreateInterval(40, 41, 3)
            }.ToObservable().ToStreamable(OnCompletedPolicy.None(), DisorderPolicy.Drop());
            streamablePunctuations.ToStreamEventObservable().Where(e => e.IsData || e.IsPunctuation).ForEachAsync(e => WriteEvent(e)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 61: Input with Generated Punctuations After Every Event");
            var streamableIthEventPunctuations = new[]
            {
                StreamEvent.CreateInterval(0, 1, 1),
                StreamEvent.CreateInterval(3, 4, 2),
                StreamEvent.CreateInterval(40, 41, 3)
            }.ToObservable().ToStreamable(OnCompletedPolicy.None(), DisorderPolicy.Drop(),
                                          PeriodicPunctuationPolicy.Count(1));
            streamableIthEventPunctuations.ToStreamEventObservable().Where(e => e.IsData || e.IsPunctuation).ForEachAsync(e => WriteEvent(e)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 62: Input with Generated Punctuations After 10 Tick Periods");
            var streamableTimePeriodEventPunctuations = new[]
            {
                StreamEvent.CreateInterval(0, 1, 1),
                StreamEvent.CreateInterval(10, 4, 2),
                StreamEvent.CreateInterval(19, 4, 3),
                StreamEvent.CreateInterval(40, 41, 4)
            }.ToObservable().ToStreamable(OnCompletedPolicy.None(), DisorderPolicy.Drop(),
                                          PeriodicPunctuationPolicy.Time(10));
            streamableTimePeriodEventPunctuations.ToStreamEventObservable().Where(e => e.IsData || e.IsPunctuation).ForEachAsync(e => WriteEvent(e)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 63: Query with No Output");
            var IncompleteOutputQuery1 = new[]
            {
                StreamEvent.CreateInterval(0, 10, 1),
                StreamEvent.CreateInterval(1, 11, 2)
            }.ToObservable().ToStreamable(OnCompletedPolicy.None()).Count().ToStreamEventObservable();
            IncompleteOutputQuery1.Where(e => e.IsData || e.IsPunctuation).ForEachAsync(e => WriteEvent(e)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 64: Query with Output up to Time 1");
            var IncompleteOutputQuery2 = new[]
            {
                StreamEvent.CreateInterval(0, 10, 1),
                StreamEvent.CreateInterval(1, 11, 2),
                StreamEvent.CreatePunctuation <int>(1)
            }.ToObservable().ToStreamable(OnCompletedPolicy.None()).Count().ToStreamEventObservable();
            IncompleteOutputQuery2.Where(e => e.IsData || e.IsPunctuation).ForEachAsync(e => WriteEvent(e)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 65: Alternate Query with Output up to Time 1");
            var IncompleteOutputQuery3 = new[]
            {
                StreamEvent.CreateInterval(0, 10, 1),
                StreamEvent.CreateInterval(1, 11, 2)
            }.ToObservable().ToStreamable(OnCompletedPolicy.Flush()).Count().ToStreamEventObservable();
            IncompleteOutputQuery3.Where(e => e.IsData || e.IsPunctuation).ForEachAsync(e => WriteEvent(e)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 66: Query with all Output");
            var CompleteOutputQuery1 = new[]
            {
                StreamEvent.CreateInterval(0, 10, 1),
                StreamEvent.CreateInterval(1, 11, 2),
                StreamEvent.CreatePunctuation <int>(StreamEvent.InfinitySyncTime)
            }.ToObservable().ToStreamable(OnCompletedPolicy.None()).Count().ToStreamEventObservable();
            CompleteOutputQuery1.Where(e => e.IsData || e.IsPunctuation).ForEachAsync(e => WriteEvent(e)).Wait();
            Console.WriteLine();

            Console.WriteLine("Figure 67: Alternate Query with all Output");
            var CompleteOutputQuery2 = new[]
            {
                StreamEvent.CreateInterval(0, 10, 1),
                StreamEvent.CreateInterval(1, 11, 2)
            }.ToObservable().ToStreamable(OnCompletedPolicy.EndOfStream()).Count().
            ToStreamEventObservable();
            CompleteOutputQuery2.Where(e => e.IsData || e.IsPunctuation).ForEachAsync(e => WriteEvent(e)).Wait();
            Console.WriteLine();

            #endregion

            Console.ReadLine();
        }