Exemplo n.º 1
0
        private void ButtonWasPressed(object obj)
        {
            StringBuilder sb = new StringBuilder();
            var           ra = base.RuleApplicationService.RuleApplicationDef;

            // var ra = new RuleApplicationDef();
            // var e1Def = ra.Entities.Add(new EntityDef("e1"));
            // var f1Def = e1Def.Fields.Add(new FieldDef("f1", DataType.Integer));
            // var f2Def = e1Def.Fields.Add(new FieldDef("f2", DataType.Integer));
            // var f3Def = e1Def.Fields.Add(new FieldDef("f3", DataType.Integer));
            // var calc1Def = e1Def.Fields.Add(new FieldDef("calc1", DataType.Integer) { IsCalculated = true });
            // calc1Def.Calc.FormulaText = $"{f1Def.Name} + {f2Def.Name}";

            sb.AppendLine("Finding unused FieldDefs...");
            DefUsageNetwork usages = DefUsageNetwork.Create(ra);

            List <FieldDef> allFields = ra.GetChildDefsByType <FieldDef>();

            foreach (FieldDef field in allFields)
            {
                if (!usages.GetDefUsages(field.Guid).Any(usage => usage.UsageType == DefUsageType.ConsumedBy ||
                                                         usage.UsageType == DefUsageType.InvalidatedBy ||
                                                         usage.UsageType == DefUsageType.UpdatedBy))
                {
                    sb.AppendLine($"Field: {field.AuthoringElementPath} is unused.");
                }
            }

            DisplayForm form = new DisplayForm();

            form.TheText = sb.ToString();
            form.Show();
        }
        public static List <ConsumedDefInfo> GetConsumedFields(this InRule.Repository.RuleApplicationDef ruleAppDef)
        {
            var consumedList = new List <ConsumedDefInfo>();
            var network      = DefUsageNetwork.Create(ruleAppDef);

            foreach (var entityDef in ruleAppDef.Entities.OfType <EntityDef>())
            {
                foreach (var fieldDef in entityDef.Fields.OfType <FieldDef>())
                {
                    var usages = network.GetDefUsages(fieldDef.Guid, false);
                    foreach (var usage in usages)
                    {
                        if (usage.UsageType == DefUsageType.ConsumedBy)
                        {
                            var defInfo = consumedList.FirstOrDefault(x => x.TargetDef == fieldDef);
                            if (defInfo == null)
                            {
                                defInfo           = new ConsumedDefInfo();
                                defInfo.TargetDef = fieldDef;
                                consumedList.Add(defInfo);
                            }
                            var relatedDef = usage.Element.GetDef(ruleAppDef);
                            defInfo.ConsumedBy.Add(relatedDef);
                        }
                    }
                }
            }
            return(consumedList);
        }
        public static List <UpdatedDefInfo> GetUpdatedFields(this InRule.Repository.RuleApplicationDef ruleAppDef)
        {
            var updatedList = new List <UpdatedDefInfo>();
            var network     = DefUsageNetwork.Create(ruleAppDef);

            foreach (var entityDef in ruleAppDef.Entities.OfType <EntityDef>())
            {
                foreach (var fieldDef in entityDef.Fields.OfType <FieldDef>())
                {
                    if (fieldDef.IsCalculated)
                    {
                        if (!updatedList.Any(x => x.TargetDef == fieldDef))
                        {
                            var defInfo = new UpdatedDefInfo();
                            defInfo.TargetDef     = fieldDef;
                            defInfo.IsCalculation = true;
                            updatedList.Add(defInfo);
                        }
                    }
                    else
                    {
                        var usages = network.GetDefUsages(fieldDef.Guid, false);
                        foreach (var usage in usages)
                        {
                            if (usage.UsageType == DefUsageType.UpdatedBy)
                            {
                                UpdatedDefInfo defInfo =
                                    updatedList.FirstOrDefault(x => x.TargetDef == fieldDef);
                                if (defInfo == null)
                                {
                                    defInfo           = new UpdatedDefInfo();
                                    defInfo.TargetDef = fieldDef;
                                    updatedList.Add(defInfo);
                                }
                                var relatedDef = usage.Element.GetDef(ruleAppDef);
                                defInfo.UpdatedBy.Add(relatedDef);
                            }
                        }
                    }
                }
            }
            return(updatedList);
        }