示例#1
0
        protected virtual async Task <ApiResult> UpdateInternal(
            DbContext db, ClassifierType type, ClassifierTree tree, Classifier item, CancellationToken cancellationToken = default)
        {
            var validator = new ClassifierValidator(db, type);

            if (await validator.ValidateUpdate(item, cancellationToken) == false)
            {
                return(new ApiResult {
                    Success = false, Errors = validator.Errors
                });
            }

            var affected = await db.GetTable <DbClassifier>()
                           .Where(x => x.Uid == item.Uid)
                           .Set(x => x.Code, item.Code)
                           .Set(x => x.Name, item.Name)
                           .Set(x => x.ParentUid, type.HierarchyType == HierarchyType.Items ? item.ParentUid : null)
                           .Set(x => x.IsActive, item.IsActive)
                           .UpdateAsync(cancellationToken);

            var result = await UpdateHierarchy(db, type, tree, item, cancellationToken);

            if (result.Success == false)
            {
                return(result);
            }

            return(new ApiResult {
                AffectedRows = affected
            });
        }
示例#2
0
        private async Task <ApiResult> UpdateHierarchy(
            DbContext db, ClassifierType type, ClassifierTree tree, Classifier item, CancellationToken cancellationToken)
        {
            if (type.HierarchyType == HierarchyType.Groups)
            {
                // todo: combine with InsertClassifierLinkHandler in one service

                // delete other links in same tree
                await(
                    from link in db.GetTable <DbClassifierLink>().Where(x => x.ItemUid == item.Uid)
                    join groups in db.GetTable <DbClassifierGroup>() on link.GroupUid equals groups.Uid
                    where groups.TreeUid == tree.Uid
                    select link
                    ).DeleteAsync(cancellationToken);

                // todo: check parent belongs to default tree
                if (item.ParentUid != null)
                {
                    await db.GetTable <DbClassifierLink>()
                    .Value(x => x.GroupUid, item.ParentUid)
                    .Value(x => x.ItemUid, item.Uid)
                    .InsertAsync(cancellationToken);
                }
            }
            else if (type.HierarchyType == HierarchyType.Items)
            {
                var closureTable = new ClosureTableHandler(db, type);

                // ReSharper disable once PossibleInvalidOperationException
                if (await closureTable.Update(item.Uid.Value, item.ParentUid, cancellationToken) == false)
                {
                    return(new ApiResult {
                        Success = false, Errors = closureTable.Errors
                    });
                }
            }

            return(new ApiResult());
        }
示例#3
0
        protected void LoadHackingClassifier()
        {
            foreach (var option in new string[] { HACKING }) // Might want to add more classifiers later on.
            {
                try
                {
                    ClassifierTree cTree = null;
                    string         contents;

                    AssetManager assets   = this.Assets;
                    var          filename = $"{option}.{Classifier.FileExtension}";
                    var          filepath = $"{GetExternalFilesDir(null)}/{filename}";

                    try
                    {
                        using (var streamReader = new StreamReader(filepath))
                        {
                            contents = streamReader.ReadToEnd();

                            cTree = Serializer.Deserialize <ClassifierTree>(contents);
                            if (cTree != null)
                            {
                                Log.Debug("Loading classifier", $"Loading our {option} classifier tree (from phone-specific file)");
                            }
                        }
                    }
                    catch (Exception)
                    {
                        cTree = null;
                    }

                    if (cTree == null)
                    {
                        using (StreamReader sr = new StreamReader(assets.Open(filename)))
                        {
                            contents = sr.ReadToEnd();
                            cTree    = Serializer.Deserialize <ClassifierTree>(contents);
                            if (cTree != null)
                            {
                                Log.Debug("Loading classifier", $"Loaded our {option} classifier tree (from asset file).");
                            }
                        }
                    }
                    if (cTree == null)
                    {
                        throw new Exception($"Classifier deserialization failed - filename {filepath}");
                    }

                    CueClassifiers[option] = cTree.MainClassifier;
                    if (option == HACKING)
                    {
                        Classifier = cTree.MainClassifier;
                    }
                    Dataset = new DataSet <DKS> {
                        Name = cTree.MainClassifier.MatchingDatasetName
                    };
                    foreach (var gClass in cTree.GestureClasses)
                    {
                        Dataset.AddClass(gClass);
                    }
                    SelectedGestureClass = Dataset.Classes.FirstOrDefault();
                }
                catch (Exception ex)
                {
                    Log.Debug("MachineLearning|Load Classifier", ex.ToString());
                    Speech.Say("Cannot launch hacking - no hacking classifier found.");
                    Finish();
                }
            }
        }
        protected override async Task <ApiResult> UpdateInternal(DbContext db,
                                                                 ClassifierType type, ClassifierTree tree, Classifier item, CancellationToken cancellationToken = default)
        {
            var result = await base.UpdateInternal(db, type, tree, item, cancellationToken);

            if (result.Success)
            {
                var template = (MessageTemplate)item;

                await db.GetTable <DbMessageTemplate>()
                .Where(x => x.Uid == item.Uid)
                .Set(x => x.Subject, template.Subject)
                .Set(x => x.Body, template.Body)
                .UpdateAsync(cancellationToken);
            }

            return(result);
        }
        private static async Task <SearchResult <ClassifierGroup> > GetGroupsByFocus(DbContext db,
                                                                                     ClassifierType type, ClassifierTree tree, ClassifierGroupSearchRequest request, CancellationToken cancellationToken)
        {
            // get all parent uids of focused item
            var path = await(
                from focus in db.GetTable <DbClassifierGroup>()
                join closureUp in db.GetTable <DbClassifierClosure>() on focus.Uid equals closureUp.ChildUid
                join item in db.GetTable <DbClassifierGroup>() on closureUp.ParentUid equals item.Uid
                where /*focus.TypeUid == type.Uid &&*/ focus.Uid == request.FocusUid
                orderby closureUp.Level descending
                select item.ParentUid)
                       .ToListAsync(cancellationToken);

            SearchResult <ClassifierGroup> result = null;

            List <ClassifierGroup> currentLevel = null;

            foreach (var parentUid in path)
            {
                if (parentUid == request.ParentUid)
                {
                    // found requested parent, init result and starting level
                    result = new SearchResult <ClassifierGroup>
                    {
                        Rows = currentLevel = new List <ClassifierGroup>()
                    };
                }

                // if current level is not already inited
                if (currentLevel == null)
                {
                    continue;
                }

                // try to move to deeper level...
                if (parentUid.HasValue)
                {
                    var parent = currentLevel.SingleOrDefault(x => x.Uid == parentUid);

                    if (parent != null)
                    {
                        parent.Children = currentLevel = new List <ClassifierGroup>();
                    }
                }

                // ... and load children
                var chldrn = await GetGroupsByParent(db, type, tree, parentUid, request, false);

                currentLevel.AddRange(chldrn.Rows);
            }

            return(result);
        }
        private static async Task <SearchResult <ClassifierGroup> > GetGroupsByParent(DbContext db,
                                                                                      ClassifierType type, ClassifierTree tree, Guid?parentUid, ClassifierGroupSearchRequest request, bool calculateTotalCount)
        {
            var query = from tree1 in db.GetTable <DbClassifierTree>()
                        join item in db.GetTable <DbClassifierGroup>() on tree.Uid equals item.TreeUid
                        where tree1.Uid == tree.Uid && tree1.TypeUid == type.Uid && item.ParentUid == parentUid
                        select item;

            var data = query
                       .Apply(request, x => x.Code)
                       .Select(x => new ClassifierGroup
            {
                Uid       = x.Uid,
                Code      = x.Code,
                Name      = x.Name,
                TreeUid   = x.TreeUid,
                ParentUid = x.ParentUid
            })
                       .ToList();

            var result = new SearchResult <ClassifierGroup> {
                Rows = data
            };

            if (calculateTotalCount)
            {
                result.TotalCount = await query.CountAsync();
            }

            if (request.ExpandSingleChild && data.Count == 1)
            {
                var singleChild = data[0];

                var children = await GetGroupsByParent(db, type, tree, singleChild.Uid, request, false);

                singleChild.Children = children.Rows;
            }

            return(result);
        }
示例#7
0
        protected override async Task <ApiResult> UpdateInternal(DbContext db,
                                                                 ClassifierType type, ClassifierTree tree, Classifier item, CancellationToken cancellationToken = default)
        {
            var result = await base.UpdateInternal(db, type, tree, item, cancellationToken);

            if (result.Success)
            {
                var numerator = (Numerator)item;

                await db.GetTable <DbNumerator>()
                .Where(x => x.Uid == item.Uid)
                .Set(x => x.Pattern, numerator.Pattern)
                .Set(x => x.Periodicity, numerator.Periodicity.ToString())
                .UpdateAsync(cancellationToken);
            }

            return(result);
        }
示例#8
0
        protected override async Task <ApiResult> UpdateInternal(DbContext db,
                                                                 ClassifierType type, ClassifierTree tree, Classifier item, CancellationToken cancellationToken = default)
        {
            var result = await base.UpdateInternal(db, type, tree, item, cancellationToken);

            /*if (result.Success)
             * {
             *      var numerator = (Numerator)item;
             *
             *      await db.GetTable<DbDocumentType>()
             *              .Where(x => x.Uid == item.Uid)
             *              .Set(x => x.Pattern, numerator.Pattern)
             *              .UpdateAsync(cancellationToken);
             * }*/

            return(result);
        }