Exemplo n.º 1
0
        public void NewPattern(string name, Action <MatchingPattern> pattern)
        {
            var mp = new MatchingPattern(name);

            pattern(mp);
            Data.Patterns.Add(mp);
        }
Exemplo n.º 2
0
        //public void RemoveForgetPattern(string entityType, string name)
        //{
        //    if (!Data.AutoOptimizePatterns.Value) { return; }
        //    RWLock.EnterWriteLock();
        //    try
        //    {
        //        if (Data.RemovePatternsPerEntityType.TryGetValue(entityType, out var list))
        //        {
        //            list.RemoveAll(p => p.Name == name);
        //            if (list.Count == 0) { Data.RemovePatternsPerEntityType.Remove(entityType); }
        //        }
        //    }
        //    finally
        //    {
        //        RWLock.ExitWriteLock();
        //    }
        //}

        //public void RemoveAddPattern(string entityType, string name)
        //{
        //    RWLock.EnterWriteLock();

        //    try
        //    {
        //        if (Data.AddPatternPerEntityType.TryGetValue(entityType, out var mp))
        //        {
        //            mp.Patterns.RemoveAll(p => p.Name == name);
        //            if (mp.Patterns.Count == 0) { Data.AddPatternPerEntityType.Remove(entityType); }

        //            RWLock.ExitWriteLock();
        //        }
        //    }
        //    finally
        //    {
        //        RWLock.ExitWriteLock();
        //    }
        //}

        public void OptimizeMatchingPattern(string entityType, MatchingPattern mp, bool isAdd)
        {
            var patterns = mp.Patterns.ToArray();

            mp.Patterns.Clear();

            var ignorePatterns = patterns.Where(p => p.All(pu => isAdd ? !IsSimpleAddEntityType(pu, entityType) : !IsSimpleForgetEntityType(pu, entityType))).ToList();

            mp.Patterns.AddRange(ignorePatterns);

            foreach (var ignoreCase in new bool[] { false, true })
            {
                var words = new Dictionary <int, List <List <string[]> > >();
                foreach (var mergePatterns in patterns.Where(p => p.All(pu => isAdd ? IsSimpleAddEntityType(pu, entityType, ignoreCase) : IsSimpleForgetEntityType(pu, entityType, ignoreCase))))
                {
                    if (!words.TryGetValue(mergePatterns.Length, out var wordList))
                    {
                        wordList = new List <List <string[]> >();
                        words[mergePatterns.Length] = wordList;
                    }
                    wordList.Add(mergePatterns.Select(pu => GetTokens(pu).ToArray()).ToList());
                }

                foreach (var kv in words)
                {
                    var len = kv.Key; var wordList = kv.Value;
                    var newPatterns = new List <IPatternUnit>();

                    var hs = new HashSet <string> [len];
                    for (int i = 0; i < len; i++)
                    {
                        hs[i] = new HashSet <string>();
                    }
                    foreach (var wl in wordList)
                    {
                        for (int i = 0; i < len; i++)
                        {
                            foreach (var w in wl[i])
                            {
                                hs[i].Add(w);
                            }
                        }
                    }
                    newPatterns.AddRange(hs.Select(h => PatternUnitPrototype.Single().WithTokens(h, ignoreCase)));

                    foreach (var pu in newPatterns)
                    {
                        if (isAdd)
                        {
                            pu.WithoutEntityType(entityType);
                        }
                        else
                        {
                            pu.WithEntityType(entityType);
                        }
                    }
                    mp.Patterns.Add(newPatterns.Select(prot => new PatternUnit(prot)).ToArray());
                }
            }
        }
Exemplo n.º 3
0
        public void TeachAddPattern(string entityType, string name, Action <MatchingPattern> pattern)
        {
            RWLock.EnterWriteLock();
            try
            {
                if (!Data.AddPatternPerEntityType.TryGetValue(entityType, out var mp))
                {
                    mp = new MatchingPattern(name);
                    Data.AddPatternPerEntityType.Add(entityType, mp);
                }
                pattern(mp);

                if (!Data.ApproximateAddPatternPerEntityType.TryGetValue(entityType, out var mpApp))
                {
                    mpApp = new MatchingPattern(name);
                    Data.ApproximateAddPatternPerEntityType.Add(entityType, mpApp);
                }
                pattern(mpApp);
                OptimizeMatchingPattern(entityType, mpApp, isAdd: true);
            }
            finally
            {
                RWLock.ExitWriteLock();
            }
        }
Exemplo n.º 4
0
 public void From(MatchingPattern mp)
 {
     //Creates new instances of all PatternUnits in the source MatchingPattern
     Patterns.AddRange(mp.Patterns.Select(pu => pu.Select(p => p.Clone()).ToArray()));
 }
Exemplo n.º 5
0
 public void From(MatchingPattern mp)
 {
     Patterns.AddRange(mp.Patterns);
 }