Esempio n. 1
0
        public void StitchTestMultisetOneStartCodegen()
        {
            // Two different start times
            var inputList = new[]
            {
                StreamEvent.CreateStart(1, StructTuple.Create("A", 3)),
                StreamEvent.CreateStart(1, StructTuple.Create("A", 3)),
                StreamEvent.CreateEnd(2, 1, StructTuple.Create("A", 3)),
                StreamEvent.CreateStart(2, StructTuple.Create("A", 3)),
                StreamEvent.CreateEnd(3, 2, StructTuple.Create("A", 3)),
                StreamEvent.CreateStart(3, StructTuple.Create("A", 3)),
                StreamEvent.CreateEnd(4, 3, StructTuple.Create("A", 3)),
                StreamEvent.CreateStart(4, StructTuple.Create("A", 3)),
                StreamEvent.CreateEnd(5, 4, StructTuple.Create("A", 3)),
                StreamEvent.CreateEnd(6, 1, StructTuple.Create("A", 3))
            };

            var compareTo = new[]
            {
                StreamEvent.CreateStart(1, StructTuple.Create("A", 3)),
                StreamEvent.CreateStart(1, StructTuple.Create("A", 3)),
                StreamEvent.CreateEnd(5, 1, StructTuple.Create("A", 3)), // 5->4->3->2->1
                StreamEvent.CreateEnd(6, 1, StructTuple.Create("A", 3)), // 6->1
                END
            };

            var input        = inputList.ToObservable().ToStreamable();
            var outputStream = input.Stitch();

            var output = outputStream.ToStreamEventObservable(ReshapingPolicy.None).ToEnumerable().ToArray();

            Assert.IsTrue(output.SequenceEqual(compareTo));
        }
Esempio n. 2
0
        public void StitchTestTimeInvertCodegen()
        {
            // This is the odd little case where we swapped "BEGIN" and "END" at both times 3 and 4
            // so that they arrive in the opposite order. This shouldn't trouble the system at all:
            // Everything that happens at Time 3 is identical. Right? Right!
            var inputList = new[]
            {
                StreamEvent.CreateStart(1, StructTuple.Create("A", 3)),
                StreamEvent.CreateEnd(2, 1, StructTuple.Create("A", 3)),
                StreamEvent.CreateStart(2, StructTuple.Create("A", 3)),
                StreamEvent.CreateStart(3, StructTuple.Create("A", 3)),
                StreamEvent.CreateEnd(3, 2, StructTuple.Create("A", 3)),
                StreamEvent.CreateStart(4, StructTuple.Create("A", 3)),
                StreamEvent.CreateEnd(4, 3, StructTuple.Create("A", 3)),
                StreamEvent.CreateEnd(5, 4, StructTuple.Create("A", 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();
            var outputStream = input.Stitch();

            var output = outputStream.ToStreamEventObservable(ReshapingPolicy.None).ToEnumerable().ToArray();

            Assert.IsTrue(output.SequenceEqual(compareTo));
        }
Esempio n. 3
0
        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));
        }
Esempio n. 4
0
        public void StitchTestSimpleSpliceCodegen()
        {
            // nothing interesting happens here
            var inputList = new[]
            {
                StreamEvent.CreateStart(1, StructTuple.Create("A", 3)),
                StreamEvent.CreateEnd(2, 1, StructTuple.Create("A", 3)),
                StreamEvent.CreateStart(2, StructTuple.Create("A", 3)),
                StreamEvent.CreateEnd(3, 2, StructTuple.Create("A", 3)),
                StreamEvent.CreateStart(3, StructTuple.Create("A", 3)),
                StreamEvent.CreateEnd(4, 3, StructTuple.Create("A", 3)),
                StreamEvent.CreateStart(4, StructTuple.Create("A", 3)),
                StreamEvent.CreateEnd(5, 4, StructTuple.Create("A", 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();
            var outputStream = input.Stitch();
            var output       = outputStream.ToStreamEventObservable(ReshapingPolicy.None).ToEnumerable().ToArray();

            Assert.IsTrue(output.SequenceEqual(compareTo));
        }
Esempio n. 5
0
        public static void StitchTestMultisetMultiStartCodegen()
        {
            // nothing interesting happens here
            var inputList = new[]
            {
                StreamEvent.CreateStart(1, StructTuple.Create("A", 3)),
                StreamEvent.CreateStart(2, StructTuple.Create("A", 3)),
                StreamEvent.CreateEnd(3, 2, StructTuple.Create("A", 3)),
                StreamEvent.CreateStart(3, StructTuple.Create("A", 3)),
                StreamEvent.CreateEnd(4, 3, StructTuple.Create("A", 3)),
                StreamEvent.CreateStart(4, StructTuple.Create("A", 3)),
                StreamEvent.CreateEnd(5, 1, StructTuple.Create("A", 3)),
                StreamEvent.CreateEnd(6, 5, StructTuple.Create("A", 3)),
                StreamEvent.CreateEnd(7, 4, StructTuple.Create("A", 3))
            };

            var compareTo = new[]
            {
                StreamEvent.CreateStart(1, StructTuple.Create("A", 3)),
                StreamEvent.CreateStart(2, StructTuple.Create("A", 3)),
                StreamEvent.CreateEnd(6, 1, StructTuple.Create("A", 3)), // 6->5->1
                StreamEvent.CreateEnd(7, 2, StructTuple.Create("A", 3)), // 7->4->3->2
                END
            };

            var input        = inputList.ToList().ToObservable().ToStreamable();
            var outputStream = input.Stitch();

            var output = outputStream.ToStreamEventObservable(ReshapingPolicy.None).ToEnumerable().ToArray();

            Assert.IsTrue(output.SequenceEqual(compareTo));
        }
Esempio n. 6
0
        public void PointAtEndTest4()
        {
            var payload = StructTuple.Create("A", 3);
            // nothing interesting happens here
            var inputList = new[] {
                StreamEvent.CreateStart(1, payload),
                StreamEvent.CreateEnd(2, 1, payload),
                StreamEvent.CreateStart(2, payload),
                StreamEvent.CreateEnd(3, 2, payload),
                StreamEvent.CreateStart(3, payload),
                StreamEvent.CreateEnd(4, 3, payload),
                StreamEvent.CreateStart(4, payload),
                StreamEvent.CreateEnd(5, 4, payload)
            };

            var compareTo = new StreamEvent <StructTuple <string, int> >[] {
                StreamEvent.CreatePoint(2, payload),
                StreamEvent.CreatePoint(3, payload),
                StreamEvent.CreatePoint(4, payload),
                StreamEvent.CreatePoint(5, payload),
                END2
            };

            var input        = inputList.ToList().ToObservable().ToStreamable().SetProperty().IsIntervalFree(true);
            var outputStream = input.PointAtEnd();

            Assert.IsTrue(outputStream.Select(r => r.Item1).IsEquivalentTo(compareTo.Select(r => r.IsData ? StreamEvent.CreatePoint(r.SyncTime, r.Payload.Item1) : END).ToArray()));
        }
Esempio n. 7
0
        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));
        }
Esempio n. 8
0
        public void Constructor()
        {
            StructTuple <int, int> v = StructTuple.Create(1, 2);

            XAssert.AreEqual(1, v.Item1);
            XAssert.AreEqual(2, v.Item2);
        }
Esempio n. 9
0
        public void StructTupleEqualityWithReferenceTypes()
        {
            var o1 = new TestEquatable(1);
            var o2 = new TestEquatable(2);
            var o3 = new TestEquatable(3);

            StructTester.TestEquality(
                baseValue: StructTuple.Create(o1, o2),
                equalValue: StructTuple.Create(o1, o2),
                notEqualValues: new[]
            {
                StructTuple.Create(o1, o3),
                StructTuple.Create(o2, o2),
                StructTuple.Create(o2, o1),
                StructTuple.Create <TestEquatable, TestEquatable>(null, o2),
                StructTuple.Create <TestEquatable, TestEquatable>(o1, null)
            },
                eq: (left, right) => left == right,
                neq: (left, right) => left != right);

            StructTester.TestEquality(
                baseValue: StructTuple.Create(o1, o2),
                equalValue: StructTuple.Create(new TestEquatable(1), new TestEquatable(2)),
                notEqualValues: new[]
            {
                StructTuple.Create(new TestEquatable(1), new TestEquatable(3)),
                StructTuple.Create(new TestEquatable(2), new TestEquatable(2)),
                StructTuple.Create(new TestEquatable(2), new TestEquatable(1)),
                StructTuple.Create <TestEquatable, TestEquatable>(null, null)
            },
                eq: (left, right) => left == right,
                neq: (left, right) => left != right);
        }
Esempio n. 10
0
            public Node Intersect(Node other, Lineage lineage, ValueSelector <TKey, TValue, TValue, TValue> collision = null)
            {
                var intersection = IntersectElements(other);
                var list         = new List <StructTuple <int, Bucket> >();

#if ASSERTS
                var hsMe    = AllBuckets.Select(x => x.Key).ToHashSet(Eq);
                var hsOther = other.AllBuckets.Select(x => x.Key).ToHashSet(Eq);
                hsMe.IntersectWith(hsOther);
#endif
                foreach (var pair in intersection)
                {
                    var newBucket = pair.First.Bucket.Intersect(pair.Second.Bucket, lineage, collision);
                    if (!newBucket.IsEmpty)
                    {
                        list.Add(StructTuple.Create(pair.First.Hash, newBucket));
                    }
                }
#if ASSERTS
                var duplicates = list.Select(x => new { x, count = list.Count(y => y.First.Equals(x.First)) }).OrderByDescending(x => x.count);
                list.Count.AssertEqual(x => x <= Count && x <= other.Count);
                Debug_Intersect(list.SelectMany(x => x.Second.Buckets.Select(y => y.Key)).ToList(), other);
#endif
                return(FromSortedList(list, 0, list.Count - 1, lineage));
            }
Esempio n. 11
0
        private TextoAnexo CreateAnexo(string code, string name, Pagina curPagina, ProcTipo curProcTipo, ProcSubtipo curProcSubtipo)
        {
            var result = new TextoAnexo
            {
                Cbhpm           = this,
                Codigo          = code,
                Nome            = name,
                PaginaDeclarada = curPagina,
            };

            if (!string.IsNullOrEmpty(code))
            {
                if (code.Contains(".99-"))
                {
                    result.Subtipo = curProcSubtipo;
                    curProcSubtipo.Anexos.Add(result);
                }
                else if (code.Contains(".99."))
                {
                    result.Tipo = curProcTipo;
                    curProcTipo.Anexos.Add(result);
                }
                else
                {
                    throw new Exception("Not supported type of Attachment.");
                }

                var simpleCode = GetCodeSimple(code);
                this.Items.Add(StructTuple.Create(simpleCode, 0), result);
            }

            return(result);
        }
Esempio n. 12
0
 /// <nodoc />
 public static void Deconstruct <T1, T2, T3, T4>(this StructTuple <T1, T2, T3, T4> value, out T1 item1, out T2 item2, out T3 item3, out T4 item4)
 {
     item1 = value.Item1;
     item2 = value.Item2;
     item3 = value.Item3;
     item4 = value.Item4;
 }
Esempio n. 13
0
        public void PointAtEndTest6()
        {
            var payload   = StructTuple.Create("A", 3);
            var inputList = new[]
            {
                StreamEvent.CreateStart(1, payload),
                StreamEvent.CreateEnd(2, 1, payload),
                StreamEvent.CreateStart(2, payload),
                StreamEvent.CreateEnd(3, 2, payload),
                StreamEvent.CreateStart(3, payload),
                StreamEvent.CreateEnd(4, 3, payload),
                StreamEvent.CreateStart(4, payload),
                StreamEvent.CreateEnd(5, 4, payload)
            };

            var compareTo = new StreamEvent <StructTuple <string, int> >[]
            {
                StreamEvent.CreatePoint(101, payload),
                StreamEvent.CreatePoint(102, payload),
                StreamEvent.CreatePoint(103, payload),
                StreamEvent.CreatePoint(104, payload),
                END2
            };

            var input = inputList.ToList().ToObservable().ToStreamable()
                        .AlterEventDuration(100);
            var outputStream = input.PointAtEnd();

            Assert.IsTrue(outputStream.Select(r => r.Item1).IsEquivalentTo(compareTo.Select(r => r.IsData ? StreamEvent.CreatePoint(r.SyncTime, r.Payload.Item1) : END).ToArray()));
        }
Esempio n. 14
0
        public void ExtendTest3()
        {
            var payload   = StructTuple.Create("A", 3);
            var inputList = new[]
            {
                StreamEvent.CreateInterval(1, 5, payload),
                StreamEvent.CreateInterval(2, 10, payload),
                StreamEvent.CreateInterval(3, 8, payload),
                StreamEvent.CreateInterval(4, 6, payload),
                StreamEvent.CreateInterval(8, 9, payload),
            };

            var compareTo = new StreamEvent <StructTuple <string, int> >[]
            {
                StreamEvent.CreateInterval(1, 15, payload),
                StreamEvent.CreateInterval(2, 20, payload),
                StreamEvent.CreateInterval(3, 18, payload),
                StreamEvent.CreateInterval(4, 16, payload),
                StreamEvent.CreateInterval(8, 19, payload),
                END2
            };

            var input        = inputList.ToList().ToObservable().ToStreamable();
            var outputStream = input.ExtendLifetime(10);

            Assert.IsTrue(outputStream.Select(r => r.Item1).IsEquivalentTo(compareTo.Select(r => r.IsData ? StreamEvent.CreateInterval(r.SyncTime, r.OtherTime, r.Payload.Item1) : END).ToArray()));
        }
Esempio n. 15
0
        public void PointAtEndTest5()
        {
            var payload   = StructTuple.Create("A", 3);
            var inputList = new[]
            {
                StreamEvent.CreateInterval(1, 5, payload),
                StreamEvent.CreateInterval(2, 10, payload),
                StreamEvent.CreateInterval(3, 8, payload),
                StreamEvent.CreateInterval(4, 6, payload),
                StreamEvent.CreateInterval(8, 9, payload),
            };

            var compareTo = new StreamEvent <StructTuple <string, int> >[]
            {
                StreamEvent.CreatePoint(5, payload),
                StreamEvent.CreatePoint(6, payload),
                StreamEvent.CreatePoint(8, payload),
                StreamEvent.CreatePoint(9, payload),
                StreamEvent.CreatePoint(10, payload),
                END2
            };

            var input        = inputList.ToList().ToObservable().ToStreamable();
            var outputStream = input.PointAtEnd();

            Assert.IsTrue(outputStream.Select(r => r.Item1).IsEquivalentTo(compareTo.Select(r => r.IsData ? StreamEvent.CreatePoint(r.SyncTime, r.Payload.Item1) : END).ToArray()));
        }
Esempio n. 16
0
        public void StructTupleEqualityWithReferenceTypesInEquatableWrapper()
        {
            var o1 = new TestNonEquatable(1);
            var o2 = new TestNonEquatable(2);
            var o3 = new TestNonEquatable(3);

            StructTester.TestEquality(
                baseValue: StructTuple.Create(EquatableClass.Create(o1), EquatableClass.Create(o2)),
                equalValue: StructTuple.Create(EquatableClass.Create(o1), EquatableClass.Create(o2)),
                notEqualValues: new[]
            {
                StructTuple.Create(EquatableClass.Create(o1), EquatableClass.Create(o3)),
                StructTuple.Create(EquatableClass.Create(o2), EquatableClass.Create(o2)),
                StructTuple.Create(EquatableClass.Create(o2), EquatableClass.Create(o1)),
                StructTuple.Create <EquatableClass <TestNonEquatable>, EquatableClass <TestNonEquatable> >(null, o2),
                StructTuple.Create <EquatableClass <TestNonEquatable>, EquatableClass <TestNonEquatable> >(o1, null)
            },
                eq: (left, right) => left == right,
                neq: (left, right) => left != right);

            StructTester.TestEquality(
                baseValue: StructTuple.Create <EquatableClass <TestNonEquatable>, EquatableClass <TestNonEquatable> >(o1, o2),
                equalValue: StructTuple.Create <EquatableClass <TestNonEquatable>, EquatableClass <TestNonEquatable> >(new TestNonEquatable(1), new TestNonEquatable(2)),
                notEqualValues: new[]
            {
                StructTuple.Create <EquatableClass <TestNonEquatable>, EquatableClass <TestNonEquatable> >(new TestNonEquatable(1), new TestNonEquatable(3)),
                StructTuple.Create <EquatableClass <TestNonEquatable>, EquatableClass <TestNonEquatable> >(new TestNonEquatable(2), new TestNonEquatable(2)),
                StructTuple.Create <EquatableClass <TestNonEquatable>, EquatableClass <TestNonEquatable> >(new TestNonEquatable(2), new TestNonEquatable(1)),
                StructTuple.Create <EquatableClass <TestNonEquatable>, EquatableClass <TestNonEquatable> >(null, null)
            },
                eq: (left, right) => left == right,
                neq: (left, right) => left != right);
        }
Esempio n. 17
0
 public void Add(NodeId node, TimeSpan time)
 {
     m_queue.Push(StructTuple.Create(node, time));
     if (m_queue.Count > MaxCount)
     {
         m_queue.Pop();
     }
 }
Esempio n. 18
0
        public static void StructTupleAsKey()
        {
            var dictionary = new SortedDictionary <StructTuple <int, string>, int>();

            for (var i = 0; i < 1000000; i++)
            {
                var key = new StructTuple <int, string>(i % 1000, i.ToString());
                dictionary.Add(key, i);
            }
        }
Esempio n. 19
0
            public IEnumerable <StructTuple <NodeId, TimeSpan> > Nodes()
            {
                StructTuple <NodeId, TimeSpan>[] nodes = new StructTuple <NodeId, TimeSpan> [m_queue.Count];
                for (int i = nodes.Length - 1; i >= 0; i--)
                {
                    nodes[i] = m_queue.Top;
                    m_queue.Pop();
                }

                return(nodes);
            }
Esempio n. 20
0
 public void StructTupleEqualityWithEnumInEquatableWrapper()
 {
     StructTester.TestEquality(
         baseValue: StructTuple.Create <EquatableEnum <TestEnum>, EquatableEnum <TestEnum> >(TestEnum.A, TestEnum.B),
         equalValue: StructTuple.Create <EquatableEnum <TestEnum>, EquatableEnum <TestEnum> >(TestEnum.A, TestEnum.B),
         notEqualValues: new[]
     {
         StructTuple.Create <EquatableEnum <TestEnum>, EquatableEnum <TestEnum> >(TestEnum.B, TestEnum.A),
         StructTuple.Create <EquatableEnum <TestEnum>, EquatableEnum <TestEnum> >(TestEnum.A, TestEnum.A),
         StructTuple.Create <EquatableEnum <TestEnum>, EquatableEnum <TestEnum> >(TestEnum.C, TestEnum.C)
     },
         eq: (left, right) => left == right,
         neq: (left, right) => left != right);
 }
Esempio n. 21
0
 public void StructTupleEquality()
 {
     StructTester.TestEquality(
         baseValue: StructTuple.Create(1, 2),
         equalValue: StructTuple.Create(1, 2),
         notEqualValues: new[]
     {
         StructTuple.Create(1, 3),
         StructTuple.Create(2, 2),
         StructTuple.Create(2, 1)
     },
         eq: (left, right) => left == right,
         neq: (left, right) => left != right);
 }
Esempio n. 22
0
        private ProcSubtipo CreateProcSubtipo(string code, string name, Pagina startPage, ProcTipo parentProcTipo)
        {
            var result = new ProcSubtipo
            {
                Cbhpm           = this,
                Codigo          = code,
                Nome            = name,
                PaginaDeclarada = startPage,
                Tipo            = parentProcTipo,
            };
            var simpleCode = GetCodeSimple(code);

            this.Items.Add(StructTuple.Create(simpleCode, 0), result);
            parentProcTipo.Subtipos.Add(result);
            return(result);
        }
Esempio n. 23
0
            /// <summary>
            ///     Returns an iterator for retrieving all the elements shared between this tree and another tree.
            /// </summary>
            /// <param name="other"></param>
            /// <returns></returns>
            public IEnumerable <StructTuple <Node, Node> > IntersectElements(Node other)
            {
                if (IsEmpty || other.IsEmpty)
                {
                    yield break;
                }
                var aIterator = new TreeIterator(this);
                var bIterator = new TreeIterator(other);
                var pivotInA  = true;
                var success   = aIterator.MoveNext();
                var pivot     = aIterator.Current;

                while (!aIterator.IsEnded || !bIterator.IsEnded)
                {
                    var trySeekIn = pivotInA ? bIterator : aIterator;
                    int cmpResult;
                    success = trySeekIn.SeekGreaterThan(pivot.Key, out cmpResult);
                    if (!success)
                    {
                        break;
                    }
                    var maybePivot = trySeekIn.Current;
                    if (cmpResult == 0)
                    {
                        yield return(pivotInA ? StructTuple.Create(pivot, maybePivot) : StructTuple.Create(maybePivot, pivot));

                        pivotInA  = !pivotInA;
                        trySeekIn = pivotInA ? aIterator : bIterator;
                        success   = trySeekIn.MoveNext();
                        if (!success)
                        {
                            break;
                        }
                        pivot = trySeekIn.Current;
                    }
                    else
                    {
                        pivot    = maybePivot;
                        pivotInA = !pivotInA;                         //If the pivot was in X, it's now in Y...
                    }
                }
            }
Esempio n. 24
0
        public void ExtendTestNegative5()
        {
            var payload = StructTuple.Create("A", 3);
            // nothing interesting happens here
            var inputList = new[] {
                StreamEvent.CreateInterval(1, 15, payload),
                StreamEvent.CreateInterval(2, 20, payload),
                StreamEvent.CreateInterval(3, 18, payload),
                StreamEvent.CreateInterval(4, 16, payload),
                StreamEvent.CreateInterval(8, 19, payload),
            };

            var compareTo = new[] {
                StreamEvent.CreateInterval(2, 5, payload),
                END2
            };

            var input        = inputList.ToList().ToObservable().ToStreamable();
            var outputStream = input.ExtendLifetime(-15);

            Assert.IsTrue(outputStream.Select(r => r.Item1).IsEquivalentTo(compareTo.Select(r => r.IsData ? StreamEvent.CreateInterval(r.SyncTime, r.OtherTime, r.Payload.Item1) : END).ToArray()));
        }
Esempio n. 25
0
        static long MeasureStringBuilderVsStringFormat()
        {
            const int iterations = 1000000;

            long ran = 0;

            int a = 1, b = -1;

            var x = new StructTuple<int, int, int>(100, 102, 300);

            CodeTimer.Time(
                true,
                "string.Format()",
                iterations,
                () => { ran += string.Format("daily/{0}/apis/{1}/operations/{2}/test", x.First, x.Second, x.Third).Length; });

            CodeTimer.Time(
                true,
                "string.Format with explicit .ToString()",
                iterations,
                () =>
                {
                    ran +=
                        string.Format("daily/{0}/apis/{1}/operations/{2}/test", x.First.ToString(CultureInfo.InvariantCulture), x.Second.ToString(CultureInfo.InvariantCulture), x.Third.ToString(CultureInfo.InvariantCulture))
                            .Length;
                });

            CodeTimer.Time(
                true,
                "StringBuilder",
                iterations,
                () =>
                {
                    var sb = new StringBuilder()
                        .Append("daily/")
                        .Append(x.First)
                        .Append("/apis/")
                        .Append(x.Second)
                        .Append("/operations/")
                        .Append(x.Third)
                        .Append("/test");

                    ran += sb.ToString().Length;
                });

            Console.WriteLine(ran);

            return ran;
        }
Esempio n. 26
0
        public void BeatStreamable2()
        {
            var input = new[]
            {
                StreamEvent.CreateInterval(100, 109, StructTuple.Create("P1", 2)),

                StreamEvent.CreateInterval(100, 110, StructTuple.Create("P2", 2)),

                StreamEvent.CreateInterval(100, 111, StructTuple.Create("P3", 2)),

                StreamEvent.CreateInterval(100, 150, StructTuple.Create("P4", 2)),

                StreamEvent.CreateStart(100, StructTuple.Create("P5", 2)),
                StreamEvent.CreateEnd(109, 100, StructTuple.Create("P5", 2)),

                StreamEvent.CreateStart(100, StructTuple.Create("P6", 2)),
                StreamEvent.CreateEnd(110, 100, StructTuple.Create("P6", 2)),

                StreamEvent.CreateStart(100, StructTuple.Create("P7", 2)),
                StreamEvent.CreateEnd(111, 100, StructTuple.Create("P7", 2)),

                StreamEvent.CreateStart(100, StructTuple.Create("P8", 2)),
                StreamEvent.CreateEnd(150, 100, StructTuple.Create("P8", 2)),

                StreamEvent.CreateInterval(101, 109, StructTuple.Create("P9", 2)),

                StreamEvent.CreateInterval(101, 110, StructTuple.Create("P10", 2)),

                StreamEvent.CreateInterval(101, 111, StructTuple.Create("P11", 2)),

                StreamEvent.CreateInterval(101, 150, StructTuple.Create("P12", 2)),

                StreamEvent.CreateStart(101, StructTuple.Create("P13", 2)),
                StreamEvent.CreateEnd(109, 101, StructTuple.Create("P13", 2)),

                StreamEvent.CreateStart(101, StructTuple.Create("P14", 2)),
                StreamEvent.CreateEnd(110, 101, StructTuple.Create("P14", 2)),

                StreamEvent.CreateStart(101, StructTuple.Create("P15", 2)),
                StreamEvent.CreateEnd(111, 101, StructTuple.Create("P15", 2)),

                StreamEvent.CreateStart(101, StructTuple.Create("P16", 2)),
                StreamEvent.CreateEnd(150, 101, StructTuple.Create("P16", 2)),

                StreamEvent.CreatePunctuation <StructTuple <string, int> >(StreamEvent.InfinitySyncTime)
            };

            var inputStream  = input.ToCleanStreamable();
            var outputStream = inputStream.Chop(0, 10).Select(e => e.Item1);

            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));
        }
Esempio n. 27
0
        public void PartitionedChop()
        {
            var input = new[]
            {
                PartitionedStreamEvent.CreateInterval(0, 100, 109, StructTuple.Create("P1", 2)),
                PartitionedStreamEvent.CreateInterval(0, 100, 110, StructTuple.Create("P2", 2)),
                PartitionedStreamEvent.CreateInterval(0, 100, 111, StructTuple.Create("P3", 2)),
                PartitionedStreamEvent.CreateInterval(0, 100, 150, StructTuple.Create("P4", 2)),

                PartitionedStreamEvent.CreateInterval(1, 100, 109, StructTuple.Create("P1", 2)),
                PartitionedStreamEvent.CreateInterval(1, 100, 110, StructTuple.Create("P2", 2)),
                PartitionedStreamEvent.CreateInterval(1, 100, 111, StructTuple.Create("P3", 2)),
                PartitionedStreamEvent.CreateInterval(1, 100, 150, StructTuple.Create("P4", 2)),

                PartitionedStreamEvent.CreateLowWatermark <int, StructTuple <string, int> >(100),

                PartitionedStreamEvent.CreateStart(0, 100, StructTuple.Create("P5", 2)),
                PartitionedStreamEvent.CreateEnd(0, 109, 100, StructTuple.Create("P5", 2)),
                PartitionedStreamEvent.CreateStart(0, 100, StructTuple.Create("P6", 2)),
                PartitionedStreamEvent.CreateEnd(0, 110, 100, StructTuple.Create("P6", 2)),

                PartitionedStreamEvent.CreateStart(1, 100, StructTuple.Create("P5", 2)),
                PartitionedStreamEvent.CreateEnd(1, 109, 100, StructTuple.Create("P5", 2)),
                PartitionedStreamEvent.CreateStart(1, 100, StructTuple.Create("P6", 2)),
                PartitionedStreamEvent.CreateEnd(1, 110, 100, StructTuple.Create("P6", 2)),

                PartitionedStreamEvent.CreateLowWatermark <int, StructTuple <string, int> >(StreamEvent.InfinitySyncTime)
            }
            .OrderBy(v => v.SyncTime)
            .ToArray();

            var outputStream = input
                               .ToObservable()
                               .ToStreamable()
                               .Chop(0, 10)
                               .Select(payload => payload.Item1);

            var correct = new PartitionedStreamEvent <int, string>[]
            {
                PartitionedStreamEvent.CreateInterval(0, 100, 109, "P1"),
                PartitionedStreamEvent.CreateInterval(0, 100, 110, "P2"),
                PartitionedStreamEvent.CreateInterval(0, 100, 110, "P3"),
                PartitionedStreamEvent.CreateInterval(0, 100, 110, "P4"),

                PartitionedStreamEvent.CreateInterval(1, 100, 109, "P1"),
                PartitionedStreamEvent.CreateInterval(1, 100, 110, "P2"),
                PartitionedStreamEvent.CreateInterval(1, 100, 110, "P3"),
                PartitionedStreamEvent.CreateInterval(1, 100, 110, "P4"),

                PartitionedStreamEvent.CreateLowWatermark <int, string>(100),

                PartitionedStreamEvent.CreateStart(0, 100, "P5"),
                PartitionedStreamEvent.CreateStart(0, 100, "P6"),
                PartitionedStreamEvent.CreateEnd(0, 109, 100, "P5"),
                PartitionedStreamEvent.CreateEnd(0, 110, 100, "P6"),

                PartitionedStreamEvent.CreateStart(1, 100, "P5"),
                PartitionedStreamEvent.CreateStart(1, 100, "P6"),
                PartitionedStreamEvent.CreateEnd(1, 109, 100, "P5"),
                PartitionedStreamEvent.CreateEnd(1, 110, 100, "P6"),

                PartitionedStreamEvent.CreateInterval(0, 110, 111, "P3"),
                PartitionedStreamEvent.CreateInterval(1, 110, 111, "P3"),

                PartitionedStreamEvent.CreateInterval(0, 110, 120, "P4"),
                PartitionedStreamEvent.CreateInterval(0, 120, 130, "P4"),
                PartitionedStreamEvent.CreateInterval(0, 130, 140, "P4"),
                PartitionedStreamEvent.CreateInterval(0, 140, 150, "P4"),

                PartitionedStreamEvent.CreateInterval(1, 110, 120, "P4"),
                PartitionedStreamEvent.CreateInterval(1, 120, 130, "P4"),
                PartitionedStreamEvent.CreateInterval(1, 130, 140, "P4"),
                PartitionedStreamEvent.CreateInterval(1, 140, 150, "P4"),

                PartitionedStreamEvent.CreateLowWatermark <int, string>(StreamEvent.InfinitySyncTime)
            };

            var events = outputStream.ToStreamEventObservable().ToEnumerable().ToArray();

            // Compare each key separately (this is solely for readability of the test)
            var expectedKey0 = correct.Where(e => !e.IsData || e.PartitionKey == 0).ToArray();
            var expectedKey1 = correct.Where(e => !e.IsData || e.PartitionKey == 1).ToArray();
            var actualKey0   = events.Where(e => !e.IsData || e.PartitionKey == 0).ToArray();
            var actualKey1   = events.Where(e => !e.IsData || e.PartitionKey == 1).ToArray();

            Assert.IsTrue(actualKey0.SequenceEqual(expectedKey0));
            Assert.IsTrue(actualKey1.SequenceEqual(expectedKey1));
        }
Esempio n. 28
0
        static int MeasureTupleVsKeyValuePairInDictionaryLookup()
        {
            const int iterations = 1000000;

            int ran = 0;

            var tupleMap = new DictionaryEx<Tuple<int, int, int, DateTime>, int>(200);
            var structMap = new DictionaryEx<StructTuple<int, int, int, DateTime>, int>(200);
            for (int i = 200 - 1; i >= 0; i--)
            {
                tupleMap.Add(new Tuple<int, int, int, DateTime>(400 - i, i + 1, i - 1, new DateTime(1990 + i % 10, 5, 2)), 0);
                structMap.Add(new StructTuple<int, int, int, DateTime>(400 - i, i + 1, i - 1, new DateTime(1990 + i % 10, 5, 2)), 0);
            }

            //CodeTimer.Time(true, "Tuple lookup",
            //    iterations,
            //    () =>
            //    {
            //        int value;
            //        if (tupleMap.TryGetValue(new Tuple<int, int, int, DateTime>(390, 11, 9, new DateTime(1990, 5, 2)), out value))
            //            ran += 1;
            //    });

            CodeTimer.Time(true, "Struct lookup outer add or update",
                iterations,
                () =>
                {
                    var key = new StructTuple<int, int, int, DateTime>(390, 11, 9, new DateTime(1990, 5, 2));
                    int a = 1;
                    int value;
                    if (structMap.TryGetValue(key, out value))
                        structMap[key] = value + a;
                    else
                        structMap.Add(key, a);
                    ran += 1;
                });

            CodeTimer.Time(true, "Struct lookup AddOrUpdate",
                iterations,
                () =>
                {
                    int a = 1;
                    structMap.AddOrUpdate(
                        new StructTuple<int, int, int, DateTime>(390, 11, 9, new DateTime(1990, 5, 2)),
                        a,
                        (k, cv, nv) => cv + nv);
                    ran += 1;
                });

            return ran;
        }
Esempio n. 29
0
 private static int Compare(StructTuple <NodeId, TimeSpan> x, StructTuple <NodeId, TimeSpan> y)
 {
     // Compare with least first
     return(x.Item2.CompareTo(y.Item2));
 }
Esempio n. 30
0
 /// <nodoc />
 public static void Deconstruct <T1, T2>(this StructTuple <T1, T2> value, out T1 item1, out T2 item2)
 {
     item1 = value.Item1;
     item2 = value.Item2;
 }
Esempio n. 31
0
        public void LoadFromTextCopiedOfPdf(string text)
        {
            // Reading all codes that exist in the text, so that we can compare with
            // the list of processed codes to know if any is missing at the end.
            var codigosExistentes = new HashSet <string>();
            var matchCodigos      = Regex.Matches(text, @"\d\s?\.\s?\d\d\s?\.\s?\d\d\s?\.\s?\d\d\s?\-\s?\d\d?");

            foreach (Match eachMatch in matchCodigos)
            {
                codigosExistentes.Add(eachMatch.Value);
            }
            var codigosUsados = new HashSet <string>();

            // Regexes that are going to be used.
            var regexCapitulo = new Regex(@"^\s*(?<NUM>\d+)?\s*CAPÍTULO\s*(?<NUM>\d+)?\s*(?<NAME>.*?)\s*$", RegexOptions.IgnoreCase | RegexOptions.Singleline);

            var regexProcedimentoTipo = new Regex(
                @"^([\s\w/\-]+?)\s+(\d\.\d\d\.\d\d\.\d\d\-\d)$",
                RegexOptions.IgnoreCase);

            var regexProcedimentoSubtipo = new Regex(
                @"^(.*?)\s+\(\s?(\d\.\d\d\.\d\d\.\d\d\-\d)\)$",
                RegexOptions.IgnoreCase);

            Regex regexProcedimentosCapitulo = null; // this one is dynamic

            var regexColumnTitles = new Regex(
                @"^(?:(?:(Código|Procedimentos|Porte|Custo|Oper\.|Nº de|Aux\.?|Anest\.|Inc\.|Filme|ou Doc|UR)|(\w+\.?))(?:\s+|$))+$",
                RegexOptions.IgnoreCase);

            var mapColNamePattern = new Dictionary <string, string>
            {
                { "código", null },
                { "procedimentos", null },
                { "porte", @"(?<PORTE>(?:(?:[\d,]+\s*de\s*)?\d+[a-z]+)|-)" },
                { "custo oper", @"(?<CUSTO>-|\d+,\d{2,3})" },
                { "porte anest", @"(?<ANEST>-|\d+)" },
                { "nº de aux", @"(?<AUX>-|\d+)" },
                { "ur", @"(?<UR>\*)" },
                { "filme ou doc", @"(?<DOC>-|\d+,\d{4})" },
                { "inc", @"(?<INC>-|\d+)" },
            };

            const string patternProcLine = @"
                ^
                (?<CODE>\d\.\d\d\.\d\d\.\d\d\-\d)?\s*
                (?<NAME>.+?)\s*
                (?:
                  (?<DOTS>[\.\s]*\.\s*)
                  {0}
                )?
                \s*
                (?:(?=\r\n|\r|\n|$))
                ";

            Regex regexProcLine = null;

            var regexAnexo = new Regex(@"^(OBSERVAÇÕES.*?|OBSERVAÇão.*?|INSTRUÇÕES.*?)\s*\:?$", RegexOptions.IgnoreCase);

            var regexPageFooter = new Regex(
                @"^(?<PAGE>\d+)?\s*Classificação Brasileira Hierarquizada de Procedimentos Médicos - (?:\d{4}|(?:\d{1,2}.*?edi[cç][aã]o))\s*(?<PAGE>\d+)?$",
                RegexOptions.IgnoreCase);

            var regexNumber = new Regex(@"^\d+$");

            // State variables.
            Pagina      curPagina            = null;
            Capitulo    curCapitulo          = null;
            ProcTipo    curProcTipo          = null;
            ProcSubtipo curProcSubtipo       = null;
            TextoAnexo  curAnexo             = null; // this can span multiples pages
            var         builder              = new StringBuilder(10000);
            int         pageNum              = 0;
            string      curGroupName         = null;  // this happens at page 154 e 155
            bool        isExplicitAttachment = false; // this happens at page 202

            // Processing all pages and lines of the text.
            var splitPages = text.Split('\f');

            for (int itPage = 0; itPage < splitPages.Length; itPage++)
            {
                var pageText = splitPages[itPage];

                var matchExplicitMissing = Regex.Matches(pageText, @"####EXPLICIT-MISSING:(.*?)(?=####|\r\n|\n|\r|$)");
                foreach (var eachMatch in matchExplicitMissing.Cast <Match>())
                {
                    codigosUsados.Add(eachMatch.Groups[1].Value);
                }

                pageText = Regex.Replace(pageText, @" *####(?:ERRO|EXPLICIT-MISSING).*?(?=\r|\n|$)", "");

                curPagina = this.CreatePagina(itPage, Regex.Replace(pageText, @" *####.*?(?=\r|\n|$)", ""));

                var matchCapitulo = regexCapitulo.Match(pageText);

                if (matchCapitulo.Success)
                {
                    // If there is an attachment being read, we need to finalize it.
                    if (curAnexo != null)
                    {
                        curAnexo.Texto = builder.ToString().Trim();
                        CheckForMultipleLines(curAnexo.Texto);
                        builder.Clear();
                        curAnexo = null;
                    }

                    // If this page is the chapter title we must create a new chapter object.
                    int    num  = int.Parse(matchCapitulo.Groups["NUM"].Value);
                    string name = Regex.Replace(matchCapitulo.Groups["NAME"].Value, @"\s+", " ");

                    curCapitulo = this.CreateCapitulo(num, name, curPagina);

                    // Setting the new value of the regex, that depends on the name of the chapter.
                    regexProcedimentosCapitulo = new Regex(
                        string.Format(@"^{0}$", Regex.Replace(name, @"\s+", @"\s+")),
                        RegexOptions.IgnoreCase);

                    continue;
                }

                if (curCapitulo != null)
                {
                    // State variables to read the lines.
                    bool canReadCapitulo    = true;
                    bool canReadProcTipo    = true;
                    bool canReadColumnTitle = true;
                    int  columnTitleLines   = 0; // lines of text devoted to the column titles.

                    bool foundPageFooter = false;
                    bool foundPageNum    = false;

                    var splitLines = Regex.Split(pageText, @"\r\n|\r|\n");
                    for (int itLine = 0; itLine < splitLines.Length; itLine++)
                    {
                        var lineText = splitLines[itLine].Trim();

                        if (lineText.Contains("####ANEXO-INICIO"))
                        {
                            isExplicitAttachment = true;
                        }

                        if (lineText.Contains("####ANEXO-FIM"))
                        {
                            isExplicitAttachment = false;
                            lineText             = Regex.Replace(lineText, @" *####.*?(?=\r|\n|$)", "");
                        }

                        if (string.IsNullOrEmpty(lineText))
                        {
                            // We use blank lines only in the attachments, such as observations and instructions.
                            bool canReadAnexo = !canReadCapitulo && !canReadProcTipo && !canReadColumnTitle && !foundPageFooter;
                            if (curAnexo != null && canReadAnexo)
                            {
                                builder.AppendLine();
                            }

                            continue;
                        }

                        if (lineText.Contains("####GROUP"))
                        {
                            lineText     = Regex.Replace(lineText, @" *####.*?(?=\r|\n|$)", "");
                            curGroupName = lineText;

                            continue;
                        }

                        if (lineText.Contains("####ANEXO-CAPITULO"))
                        {
                            // This item has no specific code.
                            lineText = Regex.Replace(lineText, @" *####.*?(?=\r|\n|$)", "");
                            curAnexo = this.CreateAnexo(
                                null,
                                lineText.Trim(':').Trim().ToUpperInvariant(),
                                curPagina,
                                null,
                                null);

                            curAnexo.Capitulo = curCapitulo;
                            curCapitulo.Anexos.Add(curAnexo);

                            builder.Clear();
                            builder.AppendLine(lineText);

                            continue;
                        }

                        // Chapter heading that exists in every page.
                        // When we find this heading, then we set the page's chapter property.
                        if (canReadCapitulo)
                        {
                            var matchProcCapitulo = regexProcedimentosCapitulo.Match(lineText);

                            if (matchProcCapitulo.Success)
                            {
                                // Seting the chapter that the page belongs to.
                                curPagina.Capitulo = curCapitulo;

                                canReadCapitulo = false;

                                continue;
                            }
                        }

                        // Reading the ProcTipo heading that exists after the Chapter heading.
                        // This heading contains the level-2 node of the CBHPM tree.
                        if (canReadProcTipo)
                        {
                            var matchProcedimentoTipo = regexProcedimentoTipo.Match(lineText);

                            if (matchProcedimentoTipo.Success)
                            {
                                // All pages inside the same ProcTipo have the same heading,
                                // so we use a method that creates the object if it does not exist,
                                // or returns the existing one.
                                var curProcTipo2 = this.GetOrCreateProcTipo(
                                    matchProcedimentoTipo.Groups[2].Value,
                                    matchProcedimentoTipo.Groups[1].Value,
                                    curPagina,
                                    curCapitulo);

                                if (curProcTipo != curProcTipo2)
                                {
                                    // If there is an attachment being read, we need to finalize it.
                                    if (curAnexo != null)
                                    {
                                        curAnexo.Texto = builder.ToString().Trim();
                                        CheckForMultipleLines(curAnexo.Texto);
                                        builder.Clear();
                                        curAnexo = null;
                                    }
                                }

                                curProcTipo = curProcTipo2;

                                codigosUsados.Add(curProcTipo.Codigo);

                                canReadProcTipo = false;

                                continue;
                            }
                        }

                        // All pages that contains medical procedures, have the titles of the columns.
                        // We need these columns to know what is inside each column, and set the values
                        // correctly.
                        if (canReadColumnTitle)
                        {
                            var matchColumnTitles = regexColumnTitles.Match(lineText);

                            if (matchColumnTitles.Success && !matchColumnTitles.Groups[2].Success)
                            {
                                var columnCaptures = matchColumnTitles.Groups[1].Captures.Cast <Capture>().ToArray();
                                foreach (var eachCapture in columnCaptures)
                                {
                                    curPagina.ColunaAdd(eachCapture.Value.TrimEnd('.'));
                                }

                                columnTitleLines++;

                                if (columnTitleLines > 2)
                                {
                                    throw new Exception("More than 2 lines for column titles.");
                                }

                                continue;
                            }

                            if (columnTitleLines == 0)
                            {
                                throw new Exception("No column titles have been found.");
                            }

                            // If there is no more columns to read, then we create the regex to read
                            // each medical procedure.
                            var valuePatternsOfPage = new List <string>();
                            foreach (var eachColName in curPagina.Colunas)
                            {
                                var colPattern = mapColNamePattern[eachColName];
                                if (colPattern != null)
                                {
                                    valuePatternsOfPage.Add(colPattern);
                                }
                            }
                            var patternValues = string.Format(@"(?:{0})", string.Join(@"\s+", valuePatternsOfPage));
                            regexProcLine = new Regex(
                                string.Format(patternProcLine, patternValues),
                                RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

                            canReadColumnTitle = false;
                        }

                        // Reading the footer of the page.
                        // The footer can not come before the end of the page.
                        {
                            var matchPageFooter = regexPageFooter.Match(lineText);

                            if (matchPageFooter.Success)
                            {
                                if (foundPageFooter)
                                {
                                    throw new Exception("Page footer duplicated.");
                                }

                                var strPageNum = matchPageFooter.Groups["PAGE"].Value;
                                if (int.TryParse(strPageNum, out pageNum))
                                {
                                    if (curPagina.Numero != null && curPagina.Numero != pageNum)
                                    {
                                        throw new Exception("Page already has a number.");
                                    }

                                    if (foundPageNum)
                                    {
                                        throw new Exception("Page number duplicated.");
                                    }

                                    foundPageNum = true;

                                    curPagina.Numero = pageNum;
                                    pageNum          = 0;
                                }

                                foundPageFooter = true;

                                continue;
                            }

                            var matchNumber = regexNumber.Match(lineText);

                            if (matchNumber.Success)
                            {
                                pageNum = int.Parse(lineText);

                                if (!foundPageFooter || curPagina.Numero != null && curPagina.Numero != pageNum)
                                {
                                    throw new Exception("Found a lost number.");
                                }

                                if (foundPageNum)
                                {
                                    throw new Exception("Page number duplicated.");
                                }

                                foundPageNum = true;

                                curPagina.Numero = pageNum;
                                pageNum          = 0;

                                continue;
                            }
                        }

                        if (foundPageFooter)
                        {
                            throw new Exception("Cannot read after page footer.");
                        }

                        // Reading the remaining of the attachment, if there is a current attachment.
                        // The text of the attachment always start by multiple spaces.
                        if (curAnexo != null)
                        {
                            if (splitLines[itLine].StartsWith("    ") || isExplicitAttachment)
                            {
                                lineText = Regex.Replace(splitLines[itLine], @" *####.*?(?=\r|\n|$)", "");
                                builder.AppendLine(lineText);
                                continue;
                            }
                        }

                        // If there is an attachment being read, we need to finalize it.
                        if (curAnexo != null)
                        {
                            curAnexo.Texto = builder.ToString().Trim();
                            CheckForMultipleLines(curAnexo.Texto);
                            builder.Clear();
                            curAnexo = null;
                        }

                        // Reading ProcSubtipo. A page may contain multiple subtipos.
                        // Each subtipo exists only once in the document, it is not repeated in all
                        // pages like ProcTipo.
                        {
                            var   subtipoText = lineText;
                            Match matchProcedimentoSubtipo = null;
                            for (int itNext = 0; itNext < 3; itNext++)
                            {
                                matchProcedimentoSubtipo = regexProcedimentoSubtipo.Match(subtipoText);
                                if (matchProcedimentoSubtipo.Success)
                                {
                                    itLine += itNext;
                                    break;
                                }

                                var nextLine = splitLines[itLine + itNext + 1];
                                if (!Regex.IsMatch(nextLine, @"^\s?\w+") || Regex.IsMatch(nextLine, @"(?<!\()\d\.\d\d\.\d\d\.\d\d-\d(?!\))"))
                                {
                                    break;
                                }

                                // If not match, then we try to append the next line, and do the match again.
                                // Some SubtipoTexts are split in 2/3 lines.
                                subtipoText = string.Format("{0} {1}", subtipoText, nextLine);
                            }

                            if (matchProcedimentoSubtipo.Success)
                            {
                                curProcSubtipo = this.CreateProcSubtipo(
                                    matchProcedimentoSubtipo.Groups[2].Value,
                                    matchProcedimentoSubtipo.Groups[1].Value,
                                    curPagina,
                                    curProcTipo);

                                curGroupName = null;

                                codigosUsados.Add(curProcSubtipo.Codigo);

                                continue;
                            }
                        }

                        // Reading the medical procedures in the current page.
                        // Note that some of these items may not be procedures but observations.
                        {
                            var matchProcedimento = regexProcLine.Match(lineText);

                            if (matchProcedimento.Success && matchProcedimento.Groups["CODE"].Success)
                            {
                                string code = matchProcedimento.Groups["CODE"].Value;

                                var nameLine1 = matchProcedimento.Groups["NAME"].Value;

                                if (regexAnexo.IsMatch(nameLine1))
                                {
                                    curAnexo = this.CreateAnexo(
                                        code,
                                        nameLine1.Trim(':').Trim().ToUpperInvariant(),
                                        curPagina,
                                        curProcTipo,
                                        curProcSubtipo);

                                    codigosUsados.Add(code);

                                    builder.Clear();
                                    builder.AppendLine(nameLine1);
                                }
                                else
                                {
                                    builder.Clear();
                                    builder.Append(nameLine1);
                                    // Reading next lines until we find the end of the medical procedure.
                                    for (; itLine < splitLines.Length;)
                                    {
                                        if (matchProcedimento.Groups["PORTE"].Success)
                                        {
                                            string procName = builder.ToString();
                                            builder.Clear();

                                            string procPorte = matchProcedimento.Groups["PORTE"].Value;
                                            string procCusto = matchProcedimento.Groups["CUSTO"].Value;
                                            string procAnest = matchProcedimento.Groups["ANEST"].Value;
                                            string procAux   = matchProcedimento.Groups["AUX"].Value;
                                            string procUR    = matchProcedimento.Groups["UR"].Value;
                                            string procInc   = matchProcedimento.Groups["INC"].Value;
                                            string procDoc   = matchProcedimento.Groups["DOC"].Value;

                                            var proc = new Proc();
                                            proc.Cbhpm      = this;
                                            proc.Codigo     = code;
                                            proc.Nome       = procName;
                                            proc.Porte      = procPorte;
                                            proc.CustoOper  = procCusto;
                                            proc.PorteAnest = procAnest;
                                            proc.NumAux     = procAux;
                                            proc.Ur         = procUR;
                                            proc.Inc        = procInc;
                                            proc.FilmeOuDoc = procDoc;

                                            proc.GrupoNoSubtipo = curGroupName;

                                            proc.Subtipo = curProcSubtipo;
                                            curProcSubtipo.Procedimentos.Add(proc);

                                            proc.PaginaDeclarada = curPagina;

                                            var simpleCode = GetCodeSimple(code);
                                            this.Items.Add(StructTuple.Create(simpleCode, 0), proc);

                                            codigosUsados.Add(code);

                                            break;
                                        }

                                        itLine++;
                                        lineText = splitLines[itLine].Trim();

                                        matchProcedimento = regexProcLine.Match(lineText);

                                        if (Regex.IsMatch(lineText, @"\d\.\d\d\.\d\d\.\d\d-\d"))
                                        {
                                            throw new Exception("Code must be found only in first line of medical procedure.");
                                        }

                                        if (!splitLines[itLine].StartsWith("    "))
                                        {
                                            throw new Exception("All lines after the first one of medical procedure, must start with spaces.");
                                        }

                                        builder.Append(' ' + matchProcedimento.Groups["NAME"].Value);
                                    }

                                    if (itLine >= splitLines.Length)
                                    {
                                        throw new Exception("Medical procedure not terminated.");
                                    }
                                }

                                continue;
                            }

                            throw new Exception("Unknown line found.");
                        }
                    }
                }

                // Checking the columns of the page.
                FinalizarPagina(curPagina);

                // Finishing the page and checking page properties.
                if (curPagina.Capitulo != curCapitulo && !string.IsNullOrEmpty(pageText))
                {
                    throw new Exception();
                }

                if (curPagina.ProcTipo != curProcTipo && !string.IsNullOrEmpty(pageText))
                {
                    throw new Exception();
                }
            }

            // Checking codes that were not processed.
            var codigosNaoUsados = new HashSet <string>(codigosExistentes);

            codigosNaoUsados.ExceptWith(codigosUsados);

            if (codigosNaoUsados.Any())
            {
                throw new Exception("There are unused codes!");
            }
        }
Esempio n. 32
0
        private ProcTipo GetOrCreateProcTipo(string code, string name, Pagina startOrCurrentPage, Capitulo parentChapter)
        {
            name = name.ToUpperInvariant();

            var      simpleCode = GetCodeSimple(code);
            bool     createNew  = false;
            ProcTipo found      = null;
            object   item;

            if (this.Items.TryGetValue(StructTuple.Create(simpleCode, 0), out item))
            {
                found = (ProcTipo)item;
                found = found.FindInSetByName(name) ?? found;
                if (found.Cbhpm != this)
                {
                    throw new Exception();
                }
                if (found.Codigo != code)
                {
                    throw new Exception();
                }
                if (found.Capitulo != parentChapter)
                {
                    throw new Exception();
                }
                if (found.Nome != name)
                {
                    createNew = true;
                }
            }
            else
            {
                createNew = true;
            }

            ProcTipo result = null;

            if (createNew)
            {
                result = new ProcTipo(found)
                {
                    Cbhpm         = this,
                    Codigo        = code,
                    Nome          = name,
                    Capitulo      = parentChapter,
                    PaginaInicial = startOrCurrentPage,
                };

                parentChapter.ProcTipos.Add(result);

                this.Items.Add(StructTuple.Create(simpleCode, result.SetOfProcTipos.Count - 1), result);
            }
            else
            {
                result = found;
            }

            startOrCurrentPage.ProcTipo = result;

            return(result);
        }