public void AddToLookupTable(Pattern.Pattern pattern)
 {
     if (pattern.IsComplex)
     {
         ComplexPatterns.Add(pattern);
         return;
     }
     tmpSimplePatterns.Add(pattern);
 }
Esempio n. 2
0
        public void MainModelLoadData()
        {
            byte[][] images = new byte[][]
            {
                new byte[] { 0, 1, 2, 3, 4, 5 },
                new byte[] { 9, 8, 7, 6, 5, 4 }
            };

            PatternCollection patterns1 = new PatternCollection();

            patterns1.Height = 2;
            patterns1.Width  = 3;
            patterns1.Add(new Pattern(images[0], 0));
            patterns1.Add(new Pattern(images[1], 1));

            PatternCollection patterns2 = new PatternCollection();

            patterns2.Height = 2;
            patterns2.Width  = 3;
            patterns2.Add(new Pattern(images[0], 0));

            IDataProvider dataProvider1 = Mock.Of <IDataProvider>(e => e.Load() == patterns1);
            IDataProvider dataProvider2 = Mock.Of <IDataProvider>(e => e.Load() == patterns2);
            MainModel     model         = new MainModel(new[] { dataProvider1, dataProvider2 });

            Assert.IsNotNull(model.Collections);
            Assert.AreEqual(2, model.Collections.Length);

            Assert.AreEqual(2, model.Collections[0].Count);
            CollectionAssert.AreEqual(images[0], model.Collections[0][0].Image);
            Assert.AreEqual(0, model.Collections[0][0].Label);
            CollectionAssert.AreEqual(images[1], model.Collections[0][1].Image);
            Assert.AreEqual(1, model.Collections[0][1].Label);

            Assert.AreEqual(1, model.Collections[1].Count);
            CollectionAssert.AreEqual(images[0], model.Collections[0][0].Image);
            Assert.AreEqual(0, model.Collections[0][0].Label);

            Assert.AreEqual(-1, model.ActiveCollectionIndex);
            Assert.IsNull(model.ActiveCollection);

            model.ActiveCollectionIndex = 0;
            Assert.AreEqual(2, model.ActiveCollection.Count);
        }
Esempio n. 3
0
        private PatternCollection LoadToCollection()
        {
            PatternCollection collection = new PatternCollection();

            collection.Height = imageReader.Height;
            collection.Width  = imageReader.Width;
            for (int i = 0; i < imageReader.Data.Length; i++)
            {
                Pattern pattern = new Pattern(imageReader.Data[i], labelReader.Data[i]);
                collection.Add(pattern);
            }
            return(collection);
        }
Esempio n. 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="Pattern"></param>
        /// <returns></returns>
        public Pattern Add(Pattern Pattern)
        {
            if (Parent != null && Parent.Parent != null && Parent.Parent.Parent != null)
            {
                Pattern.Separators = Parent.Parent.Parent.Separators;
                Parent.Parent.Parent.ChangeVersion();
            }

            if (!Pattern.IsComplex && !Pattern.ContainsSeparator)
            {
                //store pattern in lookuptable if it is a simple pattern
                string s;

                if (Pattern.StringPattern.Length >= 2)
                {
                    s = Pattern.StringPattern.Substring(0, 2);
                }
                else
                {
                    s = Pattern.StringPattern.Substring(0, 1) + " ";
                }

                s = s.ToLowerInvariant();

                if (Pattern.StringPattern.Length == 1)
                {
                    SimplePatterns1Char[Pattern.StringPattern] = Pattern;
                }
                else
                {
                    if (SimplePatterns2Char[s] == null)
                    {
                        SimplePatterns2Char[s] = new PatternCollection();
                    }
                    var ar = (PatternCollection)SimplePatterns2Char[s];
                    ar.Add(Pattern);
                }

                if (CaseSensitive)
                {
                    SimplePatterns[Pattern.LowerStringPattern] = Pattern;
                }
                else
                {
                    SimplePatterns[Pattern.StringPattern] = Pattern;
                }
            }
            else
            {
                ComplexPatterns.Add(Pattern);
            }

            patterns.Add(Pattern);
            if (Pattern.Parent == null)
            {
                Pattern.Parent = this;
            }
            else
            {
                throw (new Exception("Pattern already assigned to another PatternList"));
            }
            return(Pattern);
        }
        /// <summary>
        ///   Validates an XML document against the <see cref="SchemaDocument"/>.
        /// </summary>
        /// <param name="instance">
        ///   The <see cref="IXPathNavigable">XML document</see> to validate.
        /// </param>
        /// <param name="handler">A <see cref="SchematronValidationEventHandler"/> to receive errors.</param>
        /// <remarks>
        ///   <b>Validate</b> validates the <paramref name="instance"/> against the <see cref="SchemaDocument"/>.  
        /// <para>
        ///   If an error is detected and
        ///   the <see cref="AssertionFailed"/> event is <b>null</b>, then a <see cref="SchematronValidationException"/>is <c>thrown</c>.
        ///   Otherwise, the <see cref="AssertionFailed"/> event is raised.
        /// </para>
        /// </remarks>
        public void Validate(IXPathNavigable instance, SchematronValidationEventHandler handler = null)
        {
            if (instance == null)
            throw new ArgumentNullException("instance");
             if (schematron == null)
            throw new InvalidOperationException("The schematron document is not specified.");
             if (SchemaDocument.Patterns == null || SchemaDocument.Patterns.Count == 0)
            throw new InvalidOperationException("The schematron document has no patterns.");

             if (handler != null)
            AssertionFailed += handler;

             instanceNavigator = instance.CreateNavigator();
             if (log.IsDebugEnabled)
             log.Debug(string.Format("Validating '{0}'", instanceNavigator.BaseURI ?? "some XML document"));

             // Bind to the query language.
             if (!Schematron.Default.QueryLanguages.Providers.ContainsKey(schematron.QueryLanguage))
            throw new InvalidOperationException(String.Format("'{0}' is an unknown query language.", schematron.QueryLanguage));
             queryEngine = Schematron.Default.QueryLanguages.Providers[schematron.QueryLanguage];
             queryContext = queryEngine.CreateMatchContext(schematron, instance);

             // Apply the schema parameters.
             if (schematron.HasParameters)
             {
            foreach (string name in schematron.Parameters)
            {
               queryEngine.Let(queryContext, name, schematron.Parameters[name]);
            }
             }

             // Get the patterns to run.
             PatternCollection activePatterns;
             string phaseName = ValidationPhase;
             if (phaseName == Phase.Default)
            phaseName = schematron.DefaultPhase;
             if (phaseName == Phase.All)
            activePatterns = schematron.Patterns;
             else
             {
            Phase phase = schematron.Phases[phaseName];
            activePatterns = new PatternCollection();
            foreach (ActivePattern activePattern in phase.ActivePatterns)
            {
               activePatterns.Add(schematron.Patterns[activePattern.Pattern]);
            }
            // Apply the phase parameters.
            if (phase.HasParameters)
            {
               queryEngine.PushScope(queryContext);
               foreach (string name in phase.Parameters)
               {
                  queryEngine.Let(queryContext, name, phase.Parameters[name]);
               }
            }
             }

             // Apply the parameters specified for this validation.
             if (HasParameters)
             {
            queryEngine.PushScope(queryContext);
            foreach (string name in Parameters)
            {
               queryEngine.Let(queryContext, name, Parameters[name]);
            }
             }

             if (log.IsDebugEnabled)
            log.Debug(String.Format("Schema = '{0}', phase = '{1}'", schematron.Title.ToString(), phaseName));
             if (Start != null)
            Start(this, new SchematronValidationEventArgs(schematron, queryEngine, null, null, null, queryContext, instanceNavigator));
             try
             {
            foreach (Pattern pattern in activePatterns)
            {
               if (log.IsDebugEnabled)
                  log.Debug(string.Format("Running pattern '{0}'", pattern.Title.ToString()));
               if (ActivePattern != null)
                  ActivePattern(this, new SchematronValidationEventArgs(schematron, queryEngine, pattern, null, null, queryContext, instanceNavigator));

               // Apply the parameters
               queryEngine.PushScope(queryContext);
               if (pattern.HasParameters)
               {
                  foreach (string name in pattern.Parameters)
                  {
                     queryEngine.Let(queryContext, name, pattern.Parameters[name]);
                  }
               }

               // Run the pattern.
               bool q = RunPattern(pattern);
               queryEngine.PopScope(queryContext);

               if (log.IsDebugEnabled)
                  log.Debug(String.Format("Pattern '{0}' {1}", pattern.Title.ToString(), q ? "succeeds" : "fails"));
            }
             }
             finally
             {
            if (End != null)
               End(this, new SchematronValidationEventArgs(schematron, queryEngine, null, null, null, queryContext, instanceNavigator));
             }
        }
Esempio n. 6
0
        public WindowMain()
        {
            InitializeComponent();

            (Top, Left)     = Preferences.Default.LastMainWindowPosition;
            (Height, Width) = Preferences.Default.MainWindowSize;

            #region Menu Population

            Pref.Command = new RelayCommand(o => {
                var preferences = new WindowPreferences {
                    Owner = this
                };
                preferences.ShowDialog();
                Topmost         = Preferences.Default.MainWindowTopmost;
                appIcon.Visible = Preferences.Default.AlwaysShowTrayIcon;
            });

            AddRuleItem.Command = FileNew.Command = new RelayCommand(true, o => {
                var editRule = new WindowEditRule(new MyTask())
                {
                    Owner = this
                };
                editRule.ShowDialog();
                Tasks.Add(WindowEditRule.Task);
            });
            AddExtItem.Command = AddExtension.Command = new RelayCommand(o => ItemsGrid.SelectedIndex != -1, o => {
                var addExtensionDialog = new WindowAddPattern {
                    Owner = this
                };
                addExtensionDialog.ShowDialog();
                if (!string.IsNullOrWhiteSpace(addExtensionDialog.NewExtension))
                {
                    PatternCollection.Add(addExtensionDialog.NewExtension);
                }
            });

            EditRuleItem.Command = EditRule.Command = new RelayCommand(o => ItemsGrid.SelectedIndex != -1, o => {
                var editRule = new WindowEditRule(Tasks[ItemsGrid.SelectedIndex])
                {
                    Owner = this
                };
                editRule.ShowDialog();
                if (!Tasks.Contains(WindowEditRule.Task))
                {
                    Tasks.Add(WindowEditRule.Task);
                }
            });
            EditExtItem.Command = EditExtension.Command = new RelayCommand(o => PatternList.SelectedIndex != -1 && ItemsGrid.SelectedIndex != -1, o => {
                var addExtensionDialog = new WindowAddPattern(PatternCollection[PatternList.SelectedIndex].Clone() as string)
                {
                    Owner = this
                };
                addExtensionDialog.ShowDialog();
                if (!string.IsNullOrWhiteSpace(addExtensionDialog.NewExtension))
                {
                    PatternCollection.RemoveAt(PatternList.SelectedIndex);
                    PatternCollection.Add(addExtensionDialog.NewExtension);
                }
            });
            RemoveRuleItem.Command = RemoveRule.Command = new RelayCommand(o => ItemsGrid.SelectedIndex != -1, o => {
                Tasks[ItemsGrid.SelectedIndex].IsMoving = false;
                Tasks.RemoveAt(ItemsGrid.SelectedIndex);
            });
            RemoveExtItem.Command = RemoveExtension.Command = new RelayCommand(o => PatternList.SelectedIndex != -1 && ItemsGrid.SelectedIndex != -1, o => {
                PatternCollection.RemoveAt(PatternList.SelectedIndex);
            });

            #endregion
        }