Esempio n. 1
0
        public async Task Batch_pipeline_one_input_accumulate_to_one_output()
        {
            var result = new List <string>();

            var head = new HeadBlock <string>();

            head.Process((ct, str, md) => Task.FromResult(str + "A"))
            .Batch(3)
            .Process((ct, str, md) => Task.FromResult(string.Join(",", str)))
            .Action((ct, str, md) =>
            {
                result.Add(str);
                return(Task.CompletedTask);
            });

            var cts = new CancellationTokenSource();

            await head.PushAsync(cts.Token, "C", new PipelineMetadata()).ConfigureAwait(false);

            await head.PushAsync(cts.Token, "C", new PipelineMetadata()).ConfigureAwait(false);

            await head.PushAsync(cts.Token, "C", new PipelineMetadata()).ConfigureAwait(false);

            // this one will be lost because of the batching
            await head.PushAsync(cts.Token, "C", new PipelineMetadata()).ConfigureAwait(false);

            await head.FlushAsync(cts.Token).ConfigureAwait(false);

            result.Count.Should().Be(2);
            result[0].Should().Be("CA,CA,CA");
            result[1].Should().Be("CA");
        }
Esempio n. 2
0
        public async Task Decision_pipeline_one_input_different_outputs()
        {
            var result = string.Empty;

            var head     = new HeadBlock <string>();
            var positive = new HeadBlock <string>();
            var negative = new HeadBlock <string>();

            head.Process((ct, str, md) => Task.FromResult(str + "A"))
            .Decision((ct, str, md) =>
                      Task.FromResult(str == "AA"), positive, negative);

            positive
            .Process((ct, str, md) => Task.FromResult(str + "B"))
            .Action((ct, str, md) => Task.FromResult(result = str));

            negative
            .Process((ct, str, md) => Task.FromResult(str + "C"))
            .Action((ct, str, md) => Task.FromResult(result = str));

            var cts = new CancellationTokenSource();
            await head.PushAsync(cts.Token, "A", new PipelineMetadata()).ConfigureAwait(false);

            result.Should().Be("AAB");

            await head.PushAsync(cts.Token, "B", new PipelineMetadata()).ConfigureAwait(false);

            result.Should().Be("BAC");
        }
Esempio n. 3
0
        public async Task Distribute_pipeline_one_input_two_outputs()
        {
            var result1 = string.Empty;
            var result2 = string.Empty;

            var head    = new HeadBlock <string>();
            var branch1 = new HeadBlock <string>();
            var branch2 = new HeadBlock <string>();

            head.Process((ct, str, md) => Task.FromResult(str + "A"))
            .Process((ct, str, md) => Task.FromResult(str + "-"))
            .Distribute(branch1, branch2);

            branch1
            .Process((ct, str, md) => Task.FromResult(str + "B"))
            .Action((ct, str, md) => Task.FromResult(result1 = str));

            branch2
            .Process((ct, str, md) => Task.FromResult(str + "C"))
            .Action((ct, str, md) => Task.FromResult(result2 = str));

            var cts = new CancellationTokenSource();
            await head.PushAsync(cts.Token, string.Empty, new PipelineMetadata()).ConfigureAwait(false);

            result1.Should().Be("A-B");
            result2.Should().Be("A-C");
        }
Esempio n. 4
0
        public void VerifyChain()
        {
            if(HeadBlock == null)
            {
                throw new InvalidOperationException("Genesis block not set");
            }

            bool isValid = HeadBlock.IsValidChain(null, true);
        }
Esempio n. 5
0
        public void VerifyChain()
        {
            if (HeadBlock == null)
            {
                throw new InvalidOperationException("Genesis block not set.");
            }

            var isValid = HeadBlock.IsValidChain(null, true);

            Console.WriteLine(isValid ? "Blockchain integrity intact." : "Blockchain integrity NOT intact.");
        }
Esempio n. 6
0
        public void VerifyChain()
        {
            if (HeadBlock == null)
                throw new InvalidOperationException("Genesis block not set");

            bool isValid = HeadBlock.IsValidChain(null, true);

            if (isValid)
                Console.WriteLine("Blockchain integrity intact.");
            else
                Console.WriteLine("Blockchainintegrity NOT intact");
        }
Esempio n. 7
0
        private void ExplodeContent()
        {
            this.Order.Root = Element.Root();
            var Exploder = new ElementExploder(this.Order);

            foreach (Element HeadBlock in this.Order.Content)
            {
                HeadBlock.Explode(Exploder);
                this.Order.Root.Content.Add(HeadBlock);
                HeadBlock.Parent = this.Order.Root;
            }
        }
        /// <summary>
        /// Right-align a range of headword blocks.
        /// </summary>
        /// <param name="blocks">The full headword blocks list.</param>
        /// <param name="start">Index of the first character to right-align.</param>
        /// <param name="length">Lengt of range to right-align.</param>
        /// <param name="right">The right edge.</param>
        private static void doRightAlign(List <HeadBlock> blocks,
                                         int start, int length, float right)
        {
            float x = right;

            for (int i = start + length - 1; i >= start; --i)
            {
                HeadBlock block = blocks[i];
                block.Loc = new PointF(x - block.Size.Width, block.Loc.Y);
                x        -= block.Size.Width;
            }
        }
Esempio n. 9
0
        public async Task Head_with_only_pipeline_should_execute_all_blocks()
        {
            var result = string.Empty;
            var head   = new HeadBlock <string>();

            head.Process((ct, str, md) => Task.FromResult(str + "A"))
            .Process((ct, str, md) => Task.FromResult(str + "B"))
            .Process((ct, str, md) => Task.FromResult(str + "C"))
            .Action((ct, str, md) => Task.FromResult(result = str));

            var cts = new CancellationTokenSource().Token;
            await head.PushAsync(cts, string.Empty, new PipelineMetadata()).ConfigureAwait(false);

            result.Should().Be("ABC");
        }
Esempio n. 10
0
        private void ChooseFolderButton_Click(object sender, RoutedEventArgs e)
        {
            // Create a FolderBrowserDialog object to enable the user to
            // select a folder.
            FolderBrowserDialog dlg = new FolderBrowserDialog
            {
                ShowNewFolderButton = false
            };



            // Set the selected path to the common Sample Pictures folder
            // if it exists.
            string initialDirectory = System.IO.Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures),
                "Sample Pictures");

            if (Directory.Exists(initialDirectory))
            {
                dlg.SelectedPath = initialDirectory;
            }

            // Show the dialog and process the dataflow network.
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // Create a new CancellationTokenSource object to enable
                // cancellation.
                CancellationTokenSource = new CancellationTokenSource();

                // Create the image processing network if needed.
                if (HeadBlock == null)
                {
                    HeadBlock = ImageProcessingNetwork.CreateImageProcessingNetwork(CancellationTokenSource);
                }

                // Post the selected path to the network.
                HeadBlock.Post(dlg.SelectedPath);

                // Enable the Cancel button and disable the Choose Folder button.
                ChooseFolderButton.IsEnabled = false;
                CancelButton.IsEnabled       = true;

                // Show a wait cursor.
                Cursor = Cursors.Wait;
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Calculates right-aligned layout in headword area.
        /// </summary>
        private static bool doAnalyzeHanzi(Graphics g, string str, bool isSimp, StringFormat sf,
                                           List <HeadBlock> blocks, ref PointF loc, float right)
        {
            byte  fntZhoHead      = isSimp ? fntZhoHeadSimp : fntZhoHeadTrad;
            float left            = loc.X;
            bool  lineBreak       = false;
            int   firstCharOfLine = 0;
            int   charsOnLine     = 0;

            // Measure and position each character
            for (int i = 0; i != str.Length; ++i)
            {
                ++charsOnLine;
                // Measure each character. They may not all be hanzi: there are latin letters in some HWS
                HeadBlock hb = new HeadBlock
                {
                    Char = str[i],
                    Size = HanziRenderer.MeasureChar(g, Magic.ZhoContentFontFamily, str[i], Magic.ZhoResultFontSize),
                    //Size = g.MeasureString(cstr, getFont(fntZhoHead), 65535, sf),
                    Loc   = loc,
                    Faded = false,
                };
                blocks.Add(hb);
                // Location moves right
                loc.X += hb.Size.Width;
                // If location is beyond headword's right edge, break line now.
                // This involves
                // - moving last added block to next line
                // - right-aligning blocks added so far
                if (loc.X > right)
                {
                    lineBreak = true;
                    loc.X     = left;
                    loc.Y    += ideoLineHeight;
                    doRightAlign(blocks, firstCharOfLine, charsOnLine - 1, right);
                    charsOnLine     = 1;
                    firstCharOfLine = blocks.Count - 1;
                    hb.Loc          = loc;
                    loc.X          += hb.Size.Width;
                }
            }
            // Right-align the final bit
            doRightAlign(blocks, firstCharOfLine, charsOnLine, right);
            // Done - tell call if we had line break or not
            return(lineBreak);
        }
Esempio n. 12
0
        public bool VerifyChain()
        {
            if (HeadBlock == null)
            {
                return(false);
            }

            bool isValid = HeadBlock.IsValidChain(null, true);

            if (isValid)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 13
0
        public void VerifyChain()
        {
            if (HeadBlock == null)
            {
                throw new InvalidOperationException();
            }

            bool isValid = HeadBlock.IsValidChain(null, true);

            if (isValid)
            {
                Console.WriteLine("Blockchain integrity intack.");
            }
            else
            {
                Console.WriteLine("Blockchain integrity NOT intack. ");
            }
        }
Esempio n. 14
0
        public async Task Head_without_pipeline_should_await_start()
        {
            var result = 0;

            var head = new HeadBlock <int>(async(token, func) =>
            {
                for (var i = 0; i < 4; i++)
                {
                    await func(token, i, new PipelineMetadata());
                    result += i;
                }
            });

            var cts = new CancellationTokenSource().Token;
            await head.Start(cts).ConfigureAwait(false);

            result.Should().Be(6);
        }
Esempio n. 15
0
        public void VerifyChain()
        {
            if (HeadBlock == null)
            {
                throw new InvalidOperationException("Genesis block is not set");
            }

            bool isValid = HeadBlock.IsValidChain(null, true);

            if (isValid)
            {
                Console.WriteLine("Block chain integrity intact");
            }
            else
            {
                Console.WriteLine("Blockchain is not valid. Data has been tampered");
            }
        }
Esempio n. 16
0
        public async Task Decision_pipeline_has_only_negative_branch()
        {
            var result = string.Empty;

            var head     = new HeadBlock <string>();
            var negative = new HeadBlock <string>();

            head.Process((ct, str, md) => Task.FromResult(str + "A"))
            .Decision((ct, str, md) => Task.FromResult(str == "A-"), null, negative);

            negative
            .Process((ct, str, md) => Task.FromResult(str + "B"))
            .Action((ct, str, md) => Task.FromResult(result = str));

            var cts = new CancellationTokenSource();
            await head.PushAsync(cts.Token, "A", new PipelineMetadata());

            result.Should().Be("AAB");
        }
Esempio n. 17
0
        public async Task Head_with_generator_should_await_blocks_when_have_one()
        {
            var result = 0;

            var head = new HeadBlock <int>(async(token, func) =>
            {
                for (var i = 0; i < 4; i++)
                {
                    await func(token, i, new PipelineMetadata());
                }
            });

            head.Action((ct, i, md) => Task.FromResult(result += i));

            var cts = new CancellationTokenSource().Token;
            await head.Start(cts);

            result.Should().Be(6);
        }
Esempio n. 18
0
 /// <summary>
 /// Calculates right-aligned layout in headword area.
 /// </summary>
 private static bool doAnalyzeHanzi(Graphics g, string str, bool isSimp, StringFormat sf,
     List<HeadBlock> blocks, ref PointF loc, float right)
 {
     byte fntZhoHead = isSimp ? fntZhoHeadSimp : fntZhoHeadTrad;
     float left = loc.X;
     bool lineBreak = false;
     int firstCharOfLine = 0;
     int charsOnLine = 0;
     // Measure and position each character
     for (int i = 0; i != str.Length; ++i)
     {
         ++charsOnLine;
         // Measure each character. They may not all be hanzi: there are latin letters in some HWS
         HeadBlock hb = new HeadBlock
         {
             Char = str[i],
             Size = HanziRenderer.MeasureChar(g, Magic.ZhoContentFontFamily, str[i], Magic.ZhoResultFontSize),
             //Size = g.MeasureString(cstr, getFont(fntZhoHead), 65535, sf),
             Loc = loc,
             Faded = false,
         };
         blocks.Add(hb);
         // Location moves right
         loc.X += hb.Size.Width;
         // If location is beyond headword's right edge, break line now.
         // This involves
         // - moving last added block to next line
         // - right-aligning blocks added so far
         if (loc.X > right)
         {
             lineBreak = true;
             loc.X = left;
             loc.Y += ideoLineHeight;
             doRightAlign(blocks, firstCharOfLine, charsOnLine - 1, right);
             charsOnLine = 1;
             firstCharOfLine = blocks.Count - 1;
             hb.Loc = loc;
             loc.X += hb.Size.Width;
         }
     }
     // Right-align the final bit
     doRightAlign(blocks, firstCharOfLine, charsOnLine, right);
     // Done - tell call if we had line break or not
     return lineBreak;
 }