public void Stitch_Edges() { StreamEvent <int>[] input = new[] { StreamEvent.CreateStart(104, 1), StreamEvent.CreateStart(105, 2), StreamEvent.CreateEnd(106, 104, 1), StreamEvent.CreateEnd(106, 105, 2), }; IList <StreamEvent <int> > result = Execute(input, x => x.Stitch()); StreamEvent <int>[] expected = new[] { StreamEvent.CreateStart(104, 1), StreamEvent.CreateStart(105, 2), StreamEvent.CreateEnd(106, 104, 1), StreamEvent.CreateEnd(106, 105, 2), StreamEvent.CreatePunctuation <int>(StreamEvent.InfinitySyncTime), }; Assert.IsTrue(expected.SequenceEqual(result)); }
public void StitchTestPassthrough() { var inputList = new List <StreamEvent <string> > { // nothing interesting happens here StreamEvent.CreateStart(1, "A"), StreamEvent.CreateStart(2, "B"), StreamEvent.CreateEnd(3, 1, "A"), StreamEvent.CreateEnd(4, 2, "B") }; var input = inputList.ToObservable().ToStreamable(); var outputStream = input.Stitch(); var compareTo = new[] { StreamEvent.CreateStart(1, "A"), StreamEvent.CreateStart(2, "B"), StreamEvent.CreateEnd(3, 1, "A"), StreamEvent.CreateEnd(4, 2, "B"), END }; Assert.IsTrue(outputStream.IsEquivalentTo(compareTo)); }
public void StitchTestDictionaryCodegen() { // We have done horrible thigns to the dictionary: all elements are // identical! var inputList = new[] { StreamEvent.CreateStart(1, StructTuple.Create("A", 3)), StreamEvent.CreateEnd(2, 1, StructTuple.Create("A", 3)), StreamEvent.CreateStart(2, StructTuple.Create("B", 3)), StreamEvent.CreateEnd(3, 2, StructTuple.Create("B", 3)), StreamEvent.CreateStart(3, StructTuple.Create("B", 3)), StreamEvent.CreateEnd(4, 3, StructTuple.Create("A", 3)), StreamEvent.CreateStart(4, StructTuple.Create("A", 3)), StreamEvent.CreateEnd(5, 4, StructTuple.Create("B", 3)) }; var compareTo = new[] { StreamEvent.CreateStart(1, StructTuple.Create("A", 3)), StreamEvent.CreateEnd(5, 1, StructTuple.Create("A", 3)), END }; var input = inputList.ToObservable().ToStreamable() .SetProperty() .PayloadEqualityComparer(new EqualityComparerExpression <StructTuple <string, int> >( (st1, st2) => (st1.Item1 == st2.Item1) || (st1.Item1 == "A" && st2.Item1 == "B") || (st1.Item1 == "B" && st2.Item1 == "A"), str => 7)); var outputStream = input.Stitch(); var output = outputStream.ToStreamEventObservable(ReshapingPolicy.None).ToEnumerable().ToArray(); Assert.IsTrue(output.SequenceEqual(compareTo)); }
public void StitchTestPassthroughCodegen() { var inputList = new List <StreamEvent <StructTuple <string, int> > > { // nothing interesting happens here StreamEvent.CreateStart(1, StructTuple.Create("A", 3)), StreamEvent.CreateStart(2, StructTuple.Create("B", 3)), StreamEvent.CreateEnd(3, 1, StructTuple.Create("A", 3)), StreamEvent.CreateEnd(4, 2, StructTuple.Create("B", 3)) }; var input = inputList.ToObservable().ToStreamable(); var outputStream = input.Stitch(); var compareTo = new[] { StreamEvent.CreateStart(1, StructTuple.Create("A", 3)), StreamEvent.CreateStart(2, StructTuple.Create("B", 3)), StreamEvent.CreateEnd(3, 1, StructTuple.Create("A", 3)), StreamEvent.CreateEnd(4, 2, StructTuple.Create("B", 3)), END }; var output = outputStream.ToStreamEventObservable(ReshapingPolicy.None).ToEnumerable().ToArray(); Assert.IsTrue(output.SequenceEqual(compareTo)); }
public static void Main(string[] args) { // This program is a supplement to the Trill Query Writing Guide #region Section 2 IObservable <ContextSwitch> contextSwitchObservable = 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"); IObservable <StreamEvent <ContextSwitch> > contextSwitchStreamEventObservable = contextSwitchObservable.Select(e => StreamEvent.CreateInterval(e.Tick, e.Tick + 1, e)); IObservableIngressStreamable <ContextSwitch> contextSwitchIngressStreamable = contextSwitchStreamEventObservable.ToStreamable(DisorderPolicy.Drop()); var contextSwitchStreamable = (IStreamable <Empty, ContextSwitch>)contextSwitchIngressStreamable; IObservable <StreamEvent <ContextSwitch> > passthroughContextSwitchStreamEventObservable = contextSwitchStreamable.ToStreamEventObservable(); passthroughContextSwitchStreamEventObservable.Where(e => e.IsData) .ForEachAsync(e => WriteEvent(e)).Wait(); Console.WriteLine(); Console.WriteLine("Figure 12: Non-StreamEvent Passthrough"); var payloadStreamable = contextSwitchObservable.ToTemporalStreamable(cs => cs.Tick); var passthroughPayloadObservable = payloadStreamable.ToTemporalObservable((start, cs) => cs); passthroughPayloadObservable.ForEachAsync(cs => Console.WriteLine(cs.ToString())).Wait(); Console.WriteLine(); #endregion #region Section 3 Console.WriteLine("Figure 14: Where Query Code"); var contextSwitchTwoCores = contextSwitchStreamable.Where(p => p.CpuId == 1 || p.CpuId == 2); contextSwitchTwoCores.ToStreamEventObservable() .Where(e => e.IsData) .ForEachAsync(e => WriteEvent(e)) .Wait(); Console.WriteLine(); Console.WriteLine("Figure 17: Select Query Code"); var contextSwitchTwoCoresNoTemp = contextSwitchTwoCores.Select( e => new { e.Tick, e.ProcessId, e.CpuId }); contextSwitchTwoCoresNoTemp.ToStreamEventObservable() .Where(e => e.IsData) .ForEachAsync(e => Console.WriteLine( $"StartTime = {e.StartTime, 4}\tEndTime = {e.EndTime, 4}\t" + $"Tick = {e.Payload.Tick, 4}\tProcessId = {e.Payload.ProcessId}\tCpuId = {e.Payload.CpuId}")) .Wait(); Console.WriteLine(); Console.WriteLine("Figure 18: Alternate Select Query Code"); contextSwitchTwoCoresNoTemp = contextSwitchTwoCores.Select( e => new { e.Tick, e.ProcessId, e.CpuId }); contextSwitchTwoCoresNoTemp.ToStreamEventObservable() .Where(e => e.IsData) .ForEachAsync(e => Console.WriteLine( $"StartTime = {e.StartTime, 4}\tEndTime = {e.EndTime, 4}\t" + $"Tick = {e.Payload.Tick, 4}\tProcessId = {e.Payload.ProcessId}\tCpuId = {e.Payload.CpuId}")) .Wait(); Console.WriteLine(); Console.WriteLine("Figure 20: Where and Select with the LINQ Comprehension Syntax"); contextSwitchTwoCoresNoTemp = from e in contextSwitchStreamable where e.CpuId == 1 || e.CpuId == 2 select new { e.Tick, e.ProcessId, e.CpuId }; contextSwitchTwoCoresNoTemp.ToStreamEventObservable() .Where(e => e.IsData) .ForEachAsync(e => Console.WriteLine( $"StartTime = {e.StartTime, 4}\tEndTime = {e.EndTime, 4}\t" + $"Tick = {e.Payload.Tick, 4}\tProcessId = {e.Payload.ProcessId}\tCpuId = {e.Payload.CpuId}")) .Wait(); Console.WriteLine(); #endregion #region Section 4 Console.WriteLine("Figure 23: Join Query Code"); var processNamesObservable = 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 namesStream = processNamesObservable. Select(e => StreamEvent.CreateInterval(0, 10000, e)). ToStreamable(); var contextSwitchWithNames = contextSwitchTwoCoresNoTemp.Join(namesStream, e => e.ProcessId, e => e.ProcessId, (leftPayload, rightPayload) => new { leftPayload.Tick, leftPayload.ProcessId, leftPayload.CpuId, rightPayload.Name }); contextSwitchWithNames.ToStreamEventObservable() .Where(e => e.IsData) .ForEachAsync(e => Console.WriteLine( $"StartTime = {e.StartTime, 4}\tEndTime = {e.EndTime, 4}\t" + $"Tick = {e.Payload.Tick, 4}\tProcessId = {e.Payload.ProcessId}\t" + $"CpuId = {e.Payload.CpuId}\tName = {e.Payload.Name, 18}")) .Wait(); Console.WriteLine(); Console.WriteLine("Figure 25: Join Query Comprehension Syntax"); contextSwitchWithNames = from leftPayload in contextSwitchTwoCoresNoTemp join rightPayload in namesStream on leftPayload.ProcessId equals rightPayload.ProcessId select new { leftPayload.Tick, leftPayload.ProcessId, leftPayload.CpuId, rightPayload.Name }; contextSwitchWithNames.ToStreamEventObservable().Where(e => e.IsData) .ForEachAsync(e => Console.WriteLine( $"StartTime = {e.StartTime, 4}\tEndTime = {e.EndTime, 4}\t" + $"Tick = {e.Payload.Tick, 4}\tProcessId = {e.Payload.ProcessId}\t" + $"CpuId = {e.Payload.CpuId}\tName = {e.Payload.Name, 18}")) .Wait(); Console.WriteLine(); Console.WriteLine("Figure 26: Entire Join Query Comprehension Syntax"); contextSwitchWithNames = from leftPayload in contextSwitchStreamable join rightPayload in namesStream on leftPayload.ProcessId equals rightPayload.ProcessId where leftPayload.CpuId == 1 || leftPayload.CpuId == 2 select new { leftPayload.Tick, leftPayload.ProcessId, leftPayload.CpuId, rightPayload.Name }; contextSwitchWithNames.ToStreamEventObservable().Where(e => e.IsData) .ForEachAsync(e => Console.WriteLine( $"StartTime = {e.StartTime, 4}\tEndTime = {e.EndTime, 4}\t" + $"Tick = {e.Payload.Tick, 4}\t" + $"ProcessId = {e.Payload.ProcessId}\tCpuId = {e.Payload.CpuId}\tName = {e.Payload.Name, 18}")) .Wait(); Console.WriteLine(); #endregion #region Section 5 Console.WriteLine("Figure 27: AlterEventDuration Query Code"); var infiniteContextSwitch = contextSwitchWithNames.AlterEventDuration(StreamEvent.InfinitySyncTime); infiniteContextSwitch.ToStreamEventObservable() .Where(e => e.IsData) .ForEachAsync(e => Console.WriteLine( $"StartTime = {e.StartTime, 4}\tEndTime = {e.EndTime, 20}\tTick = {e.Payload.Tick, 4}\t" + $"ProcessId = {e.Payload.ProcessId}\tCpuId = {e.Payload.CpuId}\tName = {e.Payload.Name, 18}")) .Wait(); Console.WriteLine(); #endregion #region Section 6 Console.WriteLine("Figure 29: ClipEventDuration Query Code"); var clippedContextSwitch = infiniteContextSwitch .ClipEventDuration(infiniteContextSwitch, e => e.CpuId, e => e.CpuId); clippedContextSwitch.ToStreamEventObservable(ReshapingPolicy.CoalesceEndEdges) .Where(e => e.IsData) .ForEachAsync(e => Console.WriteLine( $"StartTime = {e.StartTime, 4}\tEndTime = {e.EndTime, 20}\tTick = {e.Payload.Tick, 4}\t" + $"ProcessId = {e.Payload.ProcessId}\tCpuId = {e.Payload.CpuId}\tName = {e.Payload.Name, 18}")) .Wait(); Console.WriteLine(); #endregion #region Section 7 Console.WriteLine("Figure 31: Multicast Version of ClipEventDuration Query Code"); clippedContextSwitch = infiniteContextSwitch.Multicast( s => s.ClipEventDuration(s, e => e.CpuId, e => e.CpuId)); clippedContextSwitch.ToStreamEventObservable(ReshapingPolicy.CoalesceEndEdges) .Where(e => e.IsData) .ForEachAsync(e => Console.WriteLine( $"StartTime = {e.StartTime, 4}\tEndTime = {e.EndTime, 20}\tTick = {e.Payload.Tick, 4}\t" + $"ProcessId = {e.Payload.ProcessId}\tCpuId = {e.Payload.CpuId}\tName = {e.Payload.Name, 18}")) .Wait(); Console.WriteLine(); #endregion #region Section 8 Console.WriteLine("Figure 33: ShiftEventLifetime Query Code"); var shiftedClippedContextSwitch = clippedContextSwitch.ShiftEventLifetime(1); shiftedClippedContextSwitch.ToStreamEventObservable(ReshapingPolicy.CoalesceEndEdges) .Where(e => e.IsData) .ForEachAsync(e => Console.WriteLine( $"StartTime = {e.StartTime, 4}\tEndTime = {e.EndTime, 20}\tTick = {e.Payload.Tick, 4}\t" + $"ProcessId = {e.Payload.ProcessId}\tCpuId = {e.Payload.CpuId}\tName = {e.Payload.Name, 18}")) .Wait(); Console.WriteLine(); Console.WriteLine("Figure 35: Timeslices Query Code"); var timeslices = shiftedClippedContextSwitch.Join(contextSwitchWithNames, e => e.CpuId, e => e.CpuId, (left, right) => new { left.ProcessId, left.CpuId, left.Name, Timeslice = right.Tick - left.Tick }); timeslices.ToStreamEventObservable(ReshapingPolicy.CoalesceEndEdges) .Where(e => e.IsData) .ForEachAsync(e => Console.WriteLine( $"StartTime = {e.StartTime, 4}\tEndTime = {e.EndTime, 4}\tProcessId = {e.Payload.ProcessId}\t" + $"CpuId = {e.Payload.CpuId}\tName = {e.Payload.Name, 18}\tTimeslice = {e.Payload.Timeslice}")) .Wait(); Console.WriteLine(); Console.WriteLine("Figure 37: Timeslices Query Code Using Multicast"); timeslices = contextSwitchWithNames.Multicast(t => t .AlterEventDuration(StreamEvent.InfinitySyncTime) .Multicast(s => s .ClipEventDuration(s, e => e.CpuId, e => e.CpuId)) .ShiftEventLifetime(1) .Join(t, e => e.CpuId, e => e.CpuId, (left, right) => new { left.ProcessId, left.CpuId, left.Name, Timeslice = right.Tick - left.Tick })); timeslices.ToStreamEventObservable(ReshapingPolicy.CoalesceEndEdges) .Where(e => e.IsData) .ForEachAsync(e => Console.WriteLine( $"StartTime = {e.StartTime, 4}\tEndTime = {e.EndTime, 4}\tProcessId = {e.Payload.ProcessId}\t" + $"CpuId = {e.Payload.CpuId}\tName = {e.Payload.Name, 18}\tTimeslice = {e.Payload.Timeslice}")) .Wait(); Console.WriteLine(); #endregion #region Section 9 Console.WriteLine("Figure 41: Rassigning Timeslice Lifetimes with AlterEventLifetime"); var timeslicesForProcess1Cpu1 = timeslices.Where(e => e.ProcessId == 1 && e.CpuId == 1); var windowedTimeslicesForProcess1Cpu1 = timeslicesForProcess1Cpu1. AlterEventLifetime(origStartTime => (1 + ((origStartTime - 1) / 3600)) * 3600, 3600); windowedTimeslicesForProcess1Cpu1.ToStreamEventObservable() .Where(e => e.IsData) .ForEachAsync(e => Console.WriteLine( $"StartTime = {e.StartTime, 4}\tEndTime = {e.EndTime, 4}\tProcessId = {e.Payload.ProcessId}\t" + $"CpuId = {e.Payload.CpuId}\tName = {e.Payload.Name}\tTimeslice = {e.Payload.Timeslice}")) .Wait(); Console.WriteLine(); Console.WriteLine("Figure 42: Reassigning Lifetimes with HoppingWindowLifetime"); var windowedTimeslices2 = timeslicesForProcess1Cpu1.HoppingWindowLifetime(3600, 3600); windowedTimeslices2.ToStreamEventObservable() .Where(e => e.IsData) .ForEachAsync(e => Console.WriteLine( $"StartTime = {e.StartTime, 4}\tEndTime = {e.EndTime, 4}\tProcessId = {e.Payload.ProcessId}\t" + $"CpuId = {e.Payload.CpuId}\tName = {e.Payload.Name}\tTimeslice = {e.Payload.Timeslice}")) .Wait(); Console.WriteLine(); Console.WriteLine("Figure 43: Sum Query Code"); var totalConsumptionPerPeriodForProcess1Cpu1 = windowedTimeslicesForProcess1Cpu1.Sum(e => e.Timeslice); totalConsumptionPerPeriodForProcess1Cpu1.ToStreamEventObservable(ReshapingPolicy.CoalesceEndEdges) .Where(e => e.IsData) .ForEachAsync(e => Console.WriteLine( $"StartTime = {e.StartTime, 4}\tEndTime = {e.EndTime, 4}\tPayload = {e.Payload}")) .Wait(); Console.WriteLine(); #endregion #region Section 10 Console.WriteLine("Figure 45: Group and Apply Query Code"); var totalConsumptionPerPeriod = timeslices.GroupApply( e => new { e.CpuId, e.ProcessId, e.Name }, s => s.HoppingWindowLifetime(3600, 3600).Sum(e => e.Timeslice), (g, p) => new { g.Key.CpuId, g.Key.Name, TotalTime = p }); totalConsumptionPerPeriod.ToStreamEventObservable(ReshapingPolicy.CoalesceEndEdges) .Where(e => e.IsData) .ForEachAsync(e => Console.WriteLine( $"StartTime = {e.StartTime, 4}\tEndTime = {e.EndTime, 4}\t" + $"CpuId = {e.Payload.CpuId}\tName = {e.Payload.Name, 18}\tTotalTime = {e.Payload.TotalTime}")) .Wait(); Console.WriteLine(); #endregion #region Section 11 Console.WriteLine("Figure 50: Chop Query Code"); Console.WriteLine("NB: This query is expected to fail!"); var alternativeContextSwitchStream = 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() .Select(e => StreamEvent.CreateInterval(e.Tick, e.Tick + 1, e)) .ToStreamable(DisorderPolicy.Drop()); var contextSwitchChoppedUnbounded = alternativeContextSwitchStream .AlterEventDuration(StreamEvent.InfinitySyncTime) .Multicast(s => s.ClipEventDuration(s, e => e.CpuId, e => e.CpuId)) .Chop(0, 3600) .Select((origStartTime, e) => new { Tick = origStartTime, e.ProcessId, e.CpuId, e.CpuTemp }) .AlterEventDuration(1); try { contextSwitchChoppedUnbounded.ToStreamEventObservable() .Where(e => e.IsData) .ForEachAsync(e => Console.WriteLine( $"StartTime = {e.StartTime, 4}\tEndTime = {e.EndTime, 4}\tTick{e.Payload.Tick, 4}\t" + $"ProcessId = {e.Payload.ProcessId}\tCpuId = {e.Payload.CpuId}\tCPUTemp{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 52: Improved Chop Query Code"); var fixedInterval = new[] { StreamEvent.CreateInterval(0, 10800, Unit.Default) } .ToObservable().ToStreamable(); var contextSwitchChopped = alternativeContextSwitchStream .AlterEventDuration(StreamEvent.InfinitySyncTime) .Multicast(s => s.ClipEventDuration(s, e => e.CpuId, e => e.CpuId)) .Join(fixedInterval, (left, right) => left) .Chop(0, 3600) .Select((origStartTime, e) => new { e.CpuId, e.ProcessId, e.CpuTemp, Tick = origStartTime }) .AlterEventDuration(1); contextSwitchChopped.ToStreamEventObservable() .Where(e => e.IsData) .ForEachAsync(e => Console.WriteLine( $"StartTime = {e.StartTime, 4}\tEndTime = {e.EndTime, 4}\tTick = {e.Payload.Tick, 4}\t" + $"ProcessId = {e.Payload.ProcessId}\tCpuId = {e.Payload.CpuId}\tCPUTemp = {e.Payload.CpuTemp}")) .Wait(); Console.WriteLine(); #endregion #region Section 12 Console.WriteLine("Figure 54: Final Query"); var choppedContextSwitch = alternativeContextSwitchStream .Where(cs => cs.CpuId == 1 || cs.CpuId == 2) .AlterEventDuration(StreamEvent.InfinitySyncTime) .Multicast(s => s.ClipEventDuration(s, e => e.CpuId, e => e.CpuId)) .Join(fixedInterval, (left, right) => left) .Chop(0, 3600) .Select((start, e) => new { Tick = start, e.ProcessId, e.CpuId, e.CpuTemp }) .AlterEventDuration(1); var choppedContextSwitchWithNames = choppedContextSwitch .Join(namesStream, e => e.ProcessId, e => e.ProcessId, (left, right) => new { left.Tick, left.ProcessId, left.CpuId, right.Name }); var timeslicesPerCpu = choppedContextSwitchWithNames .Multicast(t => t .AlterEventDuration(StreamEvent.InfinitySyncTime) .Multicast(s => s.ClipEventDuration(s, e => e.CpuId, e => e.CpuId)) .ShiftEventLifetime(1) .Join(t, e => e.CpuId, e => e.CpuId, (left, right) => new { left.ProcessId, left.CpuId, left.Name, Timeslice = right.Tick - left.Tick })); var mostCpuConsumedPerPeriod = timeslicesPerCpu .GroupApply( e => new { e.ProcessId, e.Name }, s => s.HoppingWindowLifetime(3600, 3600).Sum(e => e.Timeslice), (g, p) => new { g.Key.Name, TotalTime = p }) .Max((l, r) => l.TotalTime.CompareTo(r.TotalTime)) .Select((startTime, payload) => new { PeriodStart = startTime - 3600, PeriodEnd = startTime, ProcessName = payload.Name, TotalCpuConsumed = payload.TotalTime }); mostCpuConsumedPerPeriod.ToStreamEventObservable(ReshapingPolicy.CoalesceEndEdges) .Where(e => e.IsData) .ForEachAsync(e => Console.WriteLine( $"PeriodStart = {e.Payload.PeriodStart, 4}\tPeriodEnd = {e.Payload.PeriodEnd, 4}\t" + $"Name = {e.Payload.ProcessName}\tTotalTime = {e.Payload.TotalCpuConsumed}")) .Wait(); Console.WriteLine(); #endregion #region Section 13.1 Console.WriteLine("Figure 56: Out of Order Input (Throw)"); var outOfOrderStreamableThrow = new[] { StreamEvent.CreateInterval(10, 100, 1), StreamEvent.CreateInterval(0, 50, 2), StreamEvent.CreateInterval(0, 10, 3), StreamEvent.CreateInterval(11, 90, 4) }.ToObservable().ToStreamable(DisorderPolicy.Throw()); try { outOfOrderStreamableThrow.ToStreamEventObservable() .Where(e => e.IsData) .ForEachAsync(e => WriteEvent(e)) .Wait(); } catch (Exception e) { Console.WriteLine("{0}", e.Message); } Console.WriteLine(); Console.WriteLine("Figure 57: Out of Order Input (Drop)"); var outOfOrderStreamableDrop = new[] { StreamEvent.CreateInterval(10, 100, 1), StreamEvent.CreateInterval(0, 50, 2), StreamEvent.CreateInterval(0, 10, 3), StreamEvent.CreateInterval(11, 90, 4) }.ToObservable().ToStreamable(DisorderPolicy.Drop()); outOfOrderStreamableDrop.ToStreamEventObservable() .Where(e => e.IsData) .ForEachAsync(e => WriteEvent(e)) .Wait(); Console.WriteLine(); Console.WriteLine("Figure 58: Out of Order Input (Adjust)"); var outOfOrderStreamableAdjust = new[] { StreamEvent.CreateInterval(10, 100, 1), StreamEvent.CreateInterval(0, 50, 2), StreamEvent.CreateInterval(0, 10, 3), StreamEvent.CreateInterval(11, 90, 4) }.ToObservable().ToStreamable(DisorderPolicy.Adjust()); outOfOrderStreamableAdjust.ToStreamEventObservable() .Where(e => e.IsData) .ForEachAsync(e => WriteEvent(e)) .Wait(); Console.WriteLine(); #endregion #region Section 13.3 Console.WriteLine("Figure 60: Creating namesStream with Edge Events"); namesStream = processNamesObservable.Select(e => StreamEvent.CreateStart(0, e)). Concat(processNamesObservable.Select(e => StreamEvent.CreateEnd(10000, 0, e))). ToStreamable(); namesStream.ToStreamEventObservable() .Where(e => e.IsData) .ForEachAsync(e => WriteEvent(e)) .Wait(); Console.WriteLine(); Console.WriteLine("Figure 61: Creating namesStream using Interval and Edge Events"); var namesObservable1 = new[] { new ProcessName(1, "Word"), new ProcessName(2, "Internet Explorer"), new ProcessName(3, "Excel"), }.ToObservable(); var namesObservable2 = new[] { new ProcessName(4, "Visual Studio"), new ProcessName(5, "Outlook"), }.ToObservable(); namesStream = namesObservable1.Select(e => StreamEvent.CreateInterval(0, 10000, e)) .Concat(namesObservable2.Select(e => StreamEvent.CreateStart(0, e))) .Concat(namesObservable2.Select(e => StreamEvent.CreateEnd(10000, 0, e))) .ToStreamable(); namesStream.ToStreamEventObservable() .Where(e => e.IsData) .ForEachAsync(e => WriteEvent(e)) .Wait(); Console.WriteLine(); Console.WriteLine("Figure 62: Coalescing Matching Edges"); namesStream.ToStreamEventObservable(ReshapingPolicy.CoalesceEndEdges) .Where(e => e.IsData) .ForEachAsync(e => WriteEvent(e)) .Wait(); Console.WriteLine(); #endregion #region Section 13.4 Console.WriteLine("Figure 63: 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(DisorderPolicy.Drop(), FlushPolicy.FlushOnPunctuation, null, OnCompletedPolicy.None); streamablePunctuations.ToStreamEventObservable() .ForEachAsync(e => WriteEvent(e)).Wait(); Console.WriteLine(); Console.WriteLine("Figure 64: 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(DisorderPolicy.Drop(), FlushPolicy.FlushOnPunctuation, PeriodicPunctuationPolicy.Time(10), OnCompletedPolicy.None); streamableTimePeriodEventPunctuations.ToStreamEventObservable() .ForEachAsync(e => WriteEvent(e)).Wait(); Console.WriteLine(); Console.WriteLine("Figure 65: Query with No Output"); var incompleteOutputQuery = new[] { StreamEvent.CreateInterval(0, 10, 1), StreamEvent.CreateInterval(1, 11, 2) }.ToObservable() .ToStreamable(null, FlushPolicy.FlushOnPunctuation, null, OnCompletedPolicy.None) .Count() .ToStreamEventObservable(); incompleteOutputQuery.ForEachAsync(e => WriteEvent(e)).Wait(); Console.WriteLine(); Console.WriteLine("Figure 66: Query with Output up to Time 1"); incompleteOutputQuery = new[] { StreamEvent.CreateInterval(0, 10, 1), StreamEvent.CreateInterval(1, 11, 2), StreamEvent.CreatePunctuation <int>(1) }.ToObservable() .ToStreamable(null, FlushPolicy.FlushOnPunctuation, null, OnCompletedPolicy.None) .Count() .ToStreamEventObservable(); incompleteOutputQuery.ForEachAsync(e => WriteEvent(e)).Wait(); Console.WriteLine(); Console.WriteLine("Figure 67: Alternate Query with Output up to Time 1"); incompleteOutputQuery = new[] { StreamEvent.CreateInterval(0, 10, 1), StreamEvent.CreateInterval(1, 11, 2) }.ToObservable() .ToStreamable(null, FlushPolicy.FlushOnPunctuation, null, OnCompletedPolicy.Flush) .Count() .ToStreamEventObservable(); incompleteOutputQuery.ForEachAsync(e => WriteEvent(e)).Wait(); Console.WriteLine(); Console.WriteLine("Figure 68: Query with all Output"); var completeOutputQuery = new[] { StreamEvent.CreateInterval(0, 10, 1), StreamEvent.CreateInterval(1, 11, 2), StreamEvent.CreatePunctuation <int>(StreamEvent.InfinitySyncTime) }.ToObservable() .ToStreamable(null, FlushPolicy.FlushOnPunctuation, null, OnCompletedPolicy.None) .Count() .ToStreamEventObservable(); completeOutputQuery.ForEachAsync(e => WriteEvent(e)).Wait(); Console.WriteLine(); Console.WriteLine("Figure 69: Alternate Query with all Output"); completeOutputQuery = new[] { StreamEvent.CreateInterval(0, 10, 1), StreamEvent.CreateInterval(1, 11, 2) }.ToObservable() .ToStreamable(null, FlushPolicy.FlushOnPunctuation, null, OnCompletedPolicy.EndOfStream) .Count() .ToStreamEventObservable(); completeOutputQuery.ForEachAsync(e => WriteEvent(e)).Wait(); Console.WriteLine(); #endregion Console.ReadLine(); }
public void BeatStreamable() { var input = new[] { StreamEvent.CreateInterval(100, 109, "P1"), StreamEvent.CreateInterval(100, 110, "P2"), StreamEvent.CreateInterval(100, 111, "P3"), StreamEvent.CreateInterval(100, 150, "P4"), StreamEvent.CreateStart(100, "P5"), StreamEvent.CreateEnd(109, 100, "P5"), StreamEvent.CreateStart(100, "P6"), StreamEvent.CreateEnd(110, 100, "P6"), StreamEvent.CreateStart(100, "P7"), StreamEvent.CreateEnd(111, 100, "P7"), StreamEvent.CreateStart(100, "P8"), StreamEvent.CreateEnd(150, 100, "P8"), StreamEvent.CreateInterval(101, 109, "P9"), StreamEvent.CreateInterval(101, 110, "P10"), StreamEvent.CreateInterval(101, 111, "P11"), StreamEvent.CreateInterval(101, 150, "P12"), StreamEvent.CreateStart(101, "P13"), StreamEvent.CreateEnd(109, 101, "P13"), StreamEvent.CreateStart(101, "P14"), StreamEvent.CreateEnd(110, 101, "P14"), StreamEvent.CreateStart(101, "P15"), StreamEvent.CreateEnd(111, 101, "P15"), StreamEvent.CreateStart(101, "P16"), StreamEvent.CreateEnd(150, 101, "P16"), StreamEvent.CreatePunctuation <string>(StreamEvent.InfinitySyncTime) }; var inputStream = input.ToCleanStreamable(); var outputStream = inputStream.Chop(0, 10); var correct = new[] { StreamEvent.CreateInterval(100, 109, "P1"), StreamEvent.CreateInterval(100, 110, "P2"), StreamEvent.CreateInterval(100, 110, "P3"), StreamEvent.CreateInterval(110, 111, "P3"), StreamEvent.CreateInterval(100, 110, "P4"), StreamEvent.CreateInterval(110, 120, "P4"), StreamEvent.CreateInterval(120, 130, "P4"), StreamEvent.CreateInterval(130, 140, "P4"), StreamEvent.CreateInterval(140, 150, "P4"), StreamEvent.CreateStart(100, "P5"), StreamEvent.CreateEnd(109, 100, "P5"), StreamEvent.CreateStart(100, "P6"), StreamEvent.CreateEnd(110, 100, "P6"), StreamEvent.CreateStart(100, "P7"), StreamEvent.CreateEnd(110, 100, "P7"), StreamEvent.CreateStart(110, "P7"), StreamEvent.CreateEnd(111, 110, "P7"), StreamEvent.CreateStart(100, "P8"), StreamEvent.CreateEnd(110, 100, "P8"), StreamEvent.CreateStart(110, "P8"), StreamEvent.CreateEnd(120, 110, "P8"), StreamEvent.CreateInterval(120, 130, "P8"), StreamEvent.CreateInterval(130, 140, "P8"), StreamEvent.CreateInterval(140, 150, "P8"), StreamEvent.CreateInterval(101, 109, "P9"), StreamEvent.CreateInterval(101, 110, "P10"), StreamEvent.CreateInterval(101, 110, "P11"), StreamEvent.CreateInterval(110, 111, "P11"), StreamEvent.CreateInterval(101, 110, "P12"), StreamEvent.CreateInterval(110, 120, "P12"), StreamEvent.CreateInterval(120, 130, "P12"), StreamEvent.CreateInterval(130, 140, "P12"), StreamEvent.CreateInterval(140, 150, "P12"), StreamEvent.CreateStart(101, "P13"), StreamEvent.CreateEnd(109, 101, "P13"), StreamEvent.CreateStart(101, "P14"), StreamEvent.CreateEnd(110, 101, "P14"), StreamEvent.CreateStart(101, "P15"), StreamEvent.CreateEnd(110, 101, "P15"), StreamEvent.CreateStart(110, "P15"), StreamEvent.CreateEnd(111, 110, "P15"), StreamEvent.CreateStart(101, "P16"), StreamEvent.CreateEnd(110, 101, "P16"), StreamEvent.CreateStart(110, "P16"), StreamEvent.CreateEnd(120, 110, "P16"), StreamEvent.CreateInterval(120, 130, "P16"), StreamEvent.CreateInterval(130, 140, "P16"), StreamEvent.CreateInterval(140, 150, "P16"), StreamEvent.CreatePunctuation <string>(StreamEvent.InfinitySyncTime) }; Assert.IsTrue(outputStream.IsEquivalentTo(correct)); }
public void ClipStreamable03() { var input1 = new[] { StreamEvent.CreateInterval(100, 105, 1), StreamEvent.CreateInterval(101, 104, 2), StreamEvent.CreateInterval(105, 110, 3), StreamEvent.CreateStart(105, 4), StreamEvent.CreateEnd(110, 105, 4), StreamEvent.CreateInterval(115, 120, 5), StreamEvent.CreateStart(115, 6), StreamEvent.CreateEnd(120, 115, 6), StreamEvent.CreateInterval(100, 112, 10), StreamEvent.CreatePunctuation <int>(StreamEvent.InfinitySyncTime) }; var input2 = new[] { StreamEvent.CreateInterval(105, 110, 0), StreamEvent.CreateStart(105, 0), StreamEvent.CreateEnd(106, 105, 0), StreamEvent.CreateInterval(106, 108, 0), StreamEvent.CreateStart(107, 0), StreamEvent.CreateEnd(110, 107, 0), StreamEvent.CreateInterval(114, 120, 0), StreamEvent.CreateStart(116, 0), StreamEvent.CreateEnd(120, 116, 0), StreamEvent.CreateInterval(117, 118, 0), StreamEvent.CreateInterval(99, 112, 2), StreamEvent.CreatePunctuation <int>(StreamEvent.InfinitySyncTime) }; var inputStream1 = input1.ToCleanStreamable(); var inputStream2 = input2.ToCleanStreamable(); var outputStream = inputStream1.ClipEventDuration( inputStream2, l => l % 10, r => r); var correct = new[] { StreamEvent.CreateInterval(100, 105, 1), StreamEvent.CreateStart(100, 10), StreamEvent.CreateInterval(101, 104, 2), StreamEvent.CreateEnd(105, 100, 10), StreamEvent.CreateStart(105, 3), StreamEvent.CreateStart(105, 4), StreamEvent.CreateEnd(110, 105, 3), StreamEvent.CreateEnd(110, 105, 4), StreamEvent.CreateStart(115, 5), StreamEvent.CreateStart(115, 6), StreamEvent.CreateEnd(120, 115, 5), StreamEvent.CreateEnd(120, 115, 6), StreamEvent.CreatePunctuation <int>(StreamEvent.InfinitySyncTime) }; Assert.IsTrue(outputStream.IsEquivalentTo(correct)); }
public void ClipStreamable01() { var input1 = new[] { StreamEvent.CreateInterval(100, 105, "A1"), StreamEvent.CreateInterval(101, 104, "A2"), StreamEvent.CreateInterval(105, 110, "A3"), StreamEvent.CreateStart(105, "A4"), StreamEvent.CreateEnd(110, 105, "A4"), StreamEvent.CreateInterval(115, 120, "A5"), StreamEvent.CreateStart(115, "A6"), StreamEvent.CreateEnd(120, 115, "A6"), StreamEvent.CreateInterval(100, 112, "B1"), StreamEvent.CreatePunctuation <string>(StreamEvent.InfinitySyncTime) }; var input2 = new[] { StreamEvent.CreateInterval(105, 110, "A"), StreamEvent.CreateStart(105, "A"), StreamEvent.CreateEnd(106, 105, "A"), StreamEvent.CreateInterval(106, 108, "A"), StreamEvent.CreateStart(107, "A"), StreamEvent.CreateEnd(110, 107, "A"), StreamEvent.CreateInterval(114, 120, "A"), StreamEvent.CreateStart(116, "A"), StreamEvent.CreateEnd(120, 116, "A"), StreamEvent.CreateInterval(117, 118, "A"), StreamEvent.CreateInterval(99, 112, "C"), StreamEvent.CreatePunctuation <string>(StreamEvent.InfinitySyncTime) }; var inputStream1 = input1.ToCleanStreamable(); var inputStream2 = input2.ToCleanStreamable(); var outputStream = inputStream1.ClipEventDuration( inputStream2, l => (l != null ? l[0].ToString() : null), r => (r != null ? r[0].ToString() : null)); var correct = new[] { StreamEvent.CreateInterval(100, 105, "A1"), StreamEvent.CreateInterval(101, 104, "A2"), StreamEvent.CreateStart(105, "A3"), StreamEvent.CreateEnd(106, 105, "A3"), StreamEvent.CreateStart(105, "A4"), StreamEvent.CreateEnd(106, 105, "A4"), StreamEvent.CreateStart(115, "A5"), StreamEvent.CreateEnd(116, 115, "A5"), StreamEvent.CreateStart(115, "A6"), StreamEvent.CreateEnd(116, 115, "A6"), StreamEvent.CreateStart(100, "B1"), StreamEvent.CreateEnd(112, 100, "B1"), StreamEvent.CreatePunctuation <string>(StreamEvent.InfinitySyncTime) }; Assert.IsTrue(outputStream.IsEquivalentTo(correct)); }
public void TestCompoundAggregate() { /* * Time: 1 2 3 4 5 * Input: 1 |---------------| * 2 |-------| * 4 |---| * 8 |-------| * 16 |---| * Sum: 3 15 25 1 0 * Count: 2 4 3 1 0 */ var input = new[] { StreamEvent.CreateInterval(1, 5, 1), StreamEvent.CreateInterval(1, 3, 2), StreamEvent.CreateInterval(2, 3, 4), StreamEvent.CreateInterval(2, 4, 8), StreamEvent.CreateInterval(3, 4, 16), StreamEvent.CreatePunctuation <int>(StreamEvent.InfinitySyncTime) }.ToStreamable(); var output = input.Aggregate(a => a.Sum(v => v), b => b.Count(), (a, b) => new SumCount { Sum = a, Count = b }); // Also test max supported number of aggs (15). var output2 = input.Aggregate( a => a.Sum(v => v), b => b.Count(), b => b.Count(), b => b.Count(), b => b.Count(), b => b.Count(), b => b.Count(), b => b.Count(), b => b.Count(), b => b.Count(), b => b.Count(), b => b.Count(), b => b.Count(), b => b.Count(), b => b.Count(), (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) => new SumCount { Sum = a, Count = (b + c + d + e + f + g + h + i + j + k + l + m + n + o) / 14 }); var correct = new[] { StreamEvent.CreateStart(1, new SumCount { Sum = 3, Count = 2 }), StreamEvent.CreateEnd(2, 1, new SumCount { Sum = 3, Count = 2 }), StreamEvent.CreateStart(2, new SumCount { Sum = 15, Count = 4 }), StreamEvent.CreateEnd(3, 2, new SumCount { Sum = 15, Count = 4 }), StreamEvent.CreateStart(3, new SumCount { Sum = 25, Count = 3 }), StreamEvent.CreateEnd(4, 3, new SumCount { Sum = 25, Count = 3 }), StreamEvent.CreateStart(4, new SumCount { Sum = 1, Count = 1 }), StreamEvent.CreateEnd(5, 4, new SumCount { Sum = 1, Count = 1 }), StreamEvent.CreatePunctuation <SumCount>(StreamEvent.InfinitySyncTime) }; Assert.IsTrue(output.IsEquivalentTo(correct)); Assert.IsTrue(output2.IsEquivalentTo(correct)); }
public static void Test() { // 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(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.StartTime, e.EndTime, 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.StartTime, e.EndTime, 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.StartTime, e.EndTime, 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.StartTime, e.EndTime, 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.StartTime, e.EndTime, 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(); 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.StartTime, e.EndTime, 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.StartTime, e.EndTime, 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.StartTime, e.EndTime, 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.StartTime, e.EndTime, 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.StartTime, e.EndTime, 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.StartTime, e.EndTime, 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.StartTime, e.EndTime, 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.StartTime, e.EndTime, 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.StartTime, e.EndTime, 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.StartTime, e.EndTime, 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.StartTime, e.EndTime, 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.StartTime, e.EndTime, 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.StartTime, e.EndTime, 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(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.StartTime, e.EndTime, 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(), (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.StartTime, e.EndTime, 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(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(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(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(); 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(); 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(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(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(DisorderPolicy.Drop(), FlushPolicy.FlushOnPunctuation, 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().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().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().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().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().Count(). ToStreamEventObservable(); CompleteOutputQuery2.Where(e => e.IsData || e.IsPunctuation).ForEachAsync(e => WriteEvent(e)).Wait(); Console.WriteLine("The end ..."); #endregion }
public void Group2ColumnarSmallBatch() { var gameInput = new[] { StreamEvent.CreateStart(9900, new GameData { EventType = 0, GameId = 10, UserId = 100 }), // start game StreamEvent.CreateStart(10000, new GameData { EventType = 1, GameId = 10, UserId = 100, NumKills = 1 }), StreamEvent.CreateStart(10020, new GameData { EventType = 1, GameId = 10, UserId = 100, NumKills = 1 }), StreamEvent.CreateStart(10025, new GameData { EventType = 1, GameId = 10, UserId = 100, NumKills = 30 }), StreamEvent.CreateStart(10030, new GameData { EventType = 1, GameId = 10, UserId = 100 }), // end game StreamEvent.CreateStart(10040, new GameData { EventType = 2, GameId = 10 }) }.ToObservable().ToStreamable(); var thresholdInput = new[] { StreamEvent.CreateStart(9000, new ThresholdData { Threshold = 2, Medal = 43 }), StreamEvent.CreateStart(9000, new ThresholdData { Threshold = 10, Medal = 53 }), StreamEvent.CreateStart(9000, new ThresholdData { Threshold = 20, Medal = 63 }), StreamEvent.CreateEnd(10010, 9000, new ThresholdData { Threshold = 2, Medal = 43 }), StreamEvent.CreateStart(10010, new ThresholdData { Threshold = 3, Medal = 43 }) // at time 10010, we change threshold for medal 43, from 2 to 3 }.ToObservable().ToStreamable(); // clip each game event to end at the time of game completion var clippedGameInput = gameInput.Where(e => e.EventType < 2).ClipEventDuration(gameInput.Where(e => e.EventType == 2), e => e.GameId, e => e.GameId); var result = clippedGameInput .GroupApply(e => new { e.GameId, e.UserId }, str => StreamableInternal.ComputeSignalChangeStream(str.Sum(e => e.NumKills)), (g, c) => new { g.Key.GameId, g.Key.UserId, FromKills = c.Item1, ToKills = c.Item2 }) // count #kills per {game,user} combination ; var finalResultSequence = result .ToStreamEventObservable() .ToEnumerable(); var finalResult = finalResultSequence.First(); Assert.IsTrue(finalResultSequence.Count() == 1 && finalResult.IsPunctuation && finalResult.SyncTime == StreamEvent.InfinitySyncTime); // Result interpretation: // 1. we award user 100 with medal 43 at timestamp 10030 (3rd kill) // 2. the event ends at 10040 because the game ends at that timestamp (due to clip) }
private static void LeftAntiSemiJoinEdgesTest() { var input1 = new[] { StreamEvent.CreateStart(100, "A1"), StreamEvent.CreateEnd(110, 100, "A1"), StreamEvent.CreateStart(101, "A2"), StreamEvent.CreateEnd(105, 101, "A2"), StreamEvent.CreateStart(102, "B1"), StreamEvent.CreateEnd(112, 102, "B1"), StreamEvent.CreatePunctuation <string>(StreamEvent.InfinitySyncTime) }; var input2 = new[] { StreamEvent.CreateStart(101, "A"), StreamEvent.CreateEnd(103, 101, "A"), StreamEvent.CreateStart(106, "A"), StreamEvent.CreateEnd(108, 106, "A"), StreamEvent.CreateStart(106, "A"), StreamEvent.CreateEnd(109, 106, "A"), StreamEvent.CreateStart(100, "C"), StreamEvent.CreateEnd(110, 100, "C"), StreamEvent.CreatePunctuation <string>(StreamEvent.InfinitySyncTime) }; var inputStream1 = input1.ToCleanStreamable(); var inputStream2 = input2.ToCleanStreamable(); var outputStream = inputStream1.WhereNotExists( inputStream2, l => (l != null ? l[0].ToString() : null), r => (r != null ? r[0].ToString() : null)); var correct = new[] { StreamEvent.CreateStart(100, "A1"), StreamEvent.CreateEnd(101, 100, "A1"), StreamEvent.CreateStart(103, "A2"), StreamEvent.CreateEnd(105, 103, "A2"), StreamEvent.CreateStart(103, "A1"), StreamEvent.CreateEnd(106, 103, "A1"), StreamEvent.CreateStart(109, "A1"), StreamEvent.CreateEnd(110, 109, "A1"), StreamEvent.CreateStart(102, "B1"), StreamEvent.CreateEnd(112, 102, "B1"), StreamEvent.CreatePunctuation <string>(StreamEvent.InfinitySyncTime) }; Assert.IsTrue(outputStream.IsEquivalentTo(correct)); }