Ejemplo n.º 1
0
        private MultiDictionary <string, IConceptParser> CreateGenericParsers()
        {
            var stopwatch = Stopwatch.StartNew();

            var conceptMetadata = _conceptInfoPlugins
                                  .Select(conceptInfo => conceptInfo.GetType())
                                  .Distinct()
                                  .Select(conceptInfoType => new
            {
                conceptType    = conceptInfoType,
                conceptKeyword = ConceptInfoHelper.GetKeyword(conceptInfoType)
            })
                                  .Where(cm => cm.conceptKeyword != null)
                                  .ToList();

            _keywordsLogger.Trace(() => string.Join(" ", conceptMetadata.Select(cm => cm.conceptKeyword).OrderBy(keyword => keyword).Distinct()));

            var result = conceptMetadata.ToMultiDictionary(x => x.conceptKeyword, x =>
            {
                var parser           = new GenericParser(x.conceptType, x.conceptKeyword);
                parser.OnMemberRead += _onMemberRead;
                return((IConceptParser)parser);
            }, StringComparer.OrdinalIgnoreCase);

            _performanceLogger.Write(stopwatch, "CreateGenericParsers.");
            return(result);
        }
Ejemplo n.º 2
0
        private void ReportObsoleteConcepts(DslContainer dslContainer)
        {
            var obsoleteConceptsByType = dslContainer.Concepts
                                         .GroupBy(concept => concept.GetType())
                                         .Select(conceptsGroup => new
            {
                ConceptType       = conceptsGroup.Key,
                ConceptKeyword    = ConceptInfoHelper.GetKeywordOrTypeName(conceptsGroup.Key),
                Concepts          = conceptsGroup.ToList(),
                ObsoleteAttribute = (ObsoleteAttribute)conceptsGroup.Key.GetCustomAttributes(typeof(ObsoleteAttribute), false).SingleOrDefault()
            })
                                         .Where(conceptsGroup => conceptsGroup.ObsoleteAttribute != null)
                                         .ToList();

            // Obsolete concepts in the report are grouped by concept keyword and obsolete message.
            var obsoleteConceptsByUserReport = obsoleteConceptsByType
                                               .GroupBy(conceptsGroup => new { conceptsGroup.ConceptKeyword, conceptsGroup.ObsoleteAttribute.Message })
                                               .Select(conceptsGroup => new
            {
                conceptsGroup.Key.ConceptKeyword,
                ObsoleteMessage = conceptsGroup.Key.Message,
                Concepts        = conceptsGroup.SelectMany(group => group.Concepts)
            })
                                               .ToList();

            foreach (var conceptsGroup in obsoleteConceptsByUserReport)
            {
                _logger.Warning(() => string.Format("Obsolete concept {0} ({1} occurrences). {2}",
                                                    conceptsGroup.Concepts.First().GetUserDescription(),
                                                    conceptsGroup.Concepts.Count(),
                                                    conceptsGroup.ObsoleteMessage));
            }
        }
Ejemplo n.º 3
0
        private DslContainer Initialize(DslContainer dslContainer)
        {
            var swTotal = Stopwatch.StartNew();
            var nodes   = _dslParser.GetConcepts();

            var swConvert      = Stopwatch.StartNew();
            var parsedConcepts = ConceptInfoHelper.ConvertNodesToConceptInfos(nodes);

            _performanceLogger.Write(swConvert, nameof(ConceptInfoHelper.ConvertNodesToConceptInfos));

            var alternativeInitializationGeneratedReferences = InitializeAlternativeInitializationConcepts(parsedConcepts);

            var swFirstAdd = Stopwatch.StartNew();

            dslContainer.AddNewConceptsAndReplaceReferences(new[] { CreateInitializationConcept() });
            dslContainer.AddNewConceptsAndReplaceReferences(parsedConcepts);
            dslContainer.AddNewConceptsAndReplaceReferences(alternativeInitializationGeneratedReferences);
            _performanceLogger.Write(swFirstAdd, $"Initialize: First AddNewConceptsAndReplaceReferences ({dslContainer.Concepts.Count()} concepts).");

            ExpandMacroConcepts(dslContainer);
            CheckSemantics(dslContainer);
            dslContainer.SortReferencesBeforeUsingConcept(_buildOptions.InitialConceptsSort);
            ReportObsoleteConcepts(dslContainer);
            _dslModelFile.SaveConcepts(dslContainer.Concepts);

            _performanceLogger.Write(swTotal, $"Initialize ({dslContainer.Concepts.Count()} concepts).");
            return(dslContainer);
        }
        private static ConceptType CreateConceptTypePartial(Type conceptInfoType)
        {
            if (!typeof(IConceptInfo).IsAssignableFrom(conceptInfoType))
            {
                throw new ArgumentException($"Type '{conceptInfoType}' is not an implementation of '{typeof(IConceptInfo)}'.");
            }
            if (typeof(IConceptInfo) == conceptInfoType)
            {
                throw new ArgumentException($"{nameof(ConceptType)} cannot be created from {nameof(IConceptInfo)} interface. An implementation class is required.");
            }

            return(new ConceptType
            {
                AssemblyQualifiedName = conceptInfoType.AssemblyQualifiedName,
                BaseTypes = null, // Will be set later, because it needs to reference other ConceptTypes, after all are created.
                TypeName = conceptInfoType.Name,
                Keyword = ConceptInfoHelper.GetKeyword(conceptInfoType),
                Members = null, // Will be set later, to avoid recursive dependencies when creating these objects.
            });
        }
Ejemplo n.º 5
0
        protected IEnumerable <IConceptParser> CreateGenericParsers()
        {
            var stopwatch = Stopwatch.StartNew();

            var conceptMetadata = _conceptInfoPlugins
                                  .Select(conceptInfo => conceptInfo.GetType())
                                  .Distinct()
                                  .Select(conceptInfoType => new
            {
                conceptType    = conceptInfoType,
                conceptKeyword = ConceptInfoHelper.GetKeyword(conceptInfoType)
            })
                                  .Where(cm => cm.conceptKeyword != null)
                                  .ToList();

            _keywordsLogger.Trace(() => string.Join(" ", conceptMetadata.Select(cm => cm.conceptKeyword).OrderBy(keyword => keyword).Distinct()));

            var result = conceptMetadata.Select(cm => new GenericParser(cm.conceptType, cm.conceptKeyword)).ToList <IConceptParser>();

            _performanceLogger.Write(stopwatch, "DslParser.CreateGenericParsers.");
            return(result);
        }
Ejemplo n.º 6
0
        public ValueOrError <object> ReadMemberValue(ConceptMember member, ITokenReader tokenReader, IConceptInfo lastConcept,
                                                     bool firstMember, ref bool lastPropertyWasInlineParent, ref bool lastConceptUsed, bool readingAReference)
        {
            try
            {
                if (lastPropertyWasInlineParent && member.IsKey && !member.IsConceptInfo) // TODO: Removing "IsConceptInfo" from this condition would produce a mismatch. Think of a better solution for parsing the concept key.
                {
                    if (!tokenReader.TryRead("."))
                    {
                        return(ValueOrError <object> .CreateError(string.Format(
                                                                      "Parent property and the following key value ({0}) must be separated with a dot. Expected \".\"",
                                                                      member.Name)));
                    }
                }
                lastPropertyWasInlineParent = false;

                if (member.IsStringType)
                {
                    return(tokenReader.ReadText().ChangeType <object>());
                }

                if (member.ValueType == typeof(IConceptInfo))
                {
                    if (firstMember && lastConcept != null)
                    {
                        lastConceptUsed = true;
                        return((object)lastConcept);
                    }
                    else
                    {
                        return(ValueOrError <object> .CreateError("Member of type IConceptInfo can only be used as a first member and enclosed within the referenced parent concept."));
                    }
                }

                if (member.IsConceptInfo && lastConcept != null && member.ValueType.IsInstanceOfType(lastConcept) &&
                    member.ValueType.IsAssignableFrom(ConceptInfoType))         // Recursive "parent" property
                {
                    lastConceptUsed = true;
                    return((object)lastConcept);
                }

                if (member.IsConceptInfo && lastConcept != null && member.ValueType.IsInstanceOfType(lastConcept) &&
                    firstMember)
                {
                    lastConceptUsed = true;
                    return((object)lastConcept);
                }

                if (member.IsConceptInfo && firstMember)
                {
                    if (member.ValueType == ConceptInfoType)
                    {
                        return(ValueOrError.CreateError(string.Format(
                                                            "Recursive concept {0} cannot be used as a root because its parent property ({1}) must reference another concept. Use a non-recursive concept for the root and a derivation of the root concept with additional parent property as a recursive concept.",
                                                            ConceptInfoHelper.GetKeywordOrTypeName(ConceptInfoType), member.Name)));
                    }

                    if (!readingAReference && Members.Where(m => m.IsParsable).Count() == 1)
                    {
                        // This validation is not necessary for consistent parsing. It is enforced simply to avoid ambiguity when parsing
                        // similar concepts such as "Logging { AllProperties; }", "History { AllProperties; }" and "Persisted { AllProperties; }".

                        var parentMembers = ConceptMembers.Get(member.ValueType).Where(m => m.IsParsable).ToArray();
                        if (parentMembers.Count() == 1 && parentMembers.Single().IsConceptInfo)
                        {
                            return(ValueOrError.CreateError(string.Format(
                                                                "{0} must be enclosed within the referenced parent concept {1}. A single-reference concept that references another single-reference concept must always be used with embedded syntax to avoid ambiguity.",
                                                                ConceptInfoHelper.GetKeywordOrTypeName(ConceptInfoType),
                                                                ConceptInfoHelper.GetKeywordOrTypeName(member.ValueType))));
                        }
                    }

                    GenericParser subParser = new GenericParser(member.ValueType, "");
                    lastConceptUsed             = true;
                    lastPropertyWasInlineParent = true;
                    return(subParser.ParseMembers(tokenReader, lastConcept, true).ChangeType <object>());
                }

                if (member.IsConceptInfo)
                {
                    GenericParser subParser = new GenericParser(member.ValueType, "");
                    return(subParser.ParseMembers(tokenReader, null, true).ChangeType <object>());
                }

                return(ValueOrError.CreateError(string.Format(
                                                    "GenericParser does not support members of type \"{0}\". Try using string or implementation of IConceptInfo.",
                                                    member.ValueType.Name)));
            }
            catch (DslSyntaxException ex)
            {
                return(ValueOrError <object> .CreateError(ex.Message));
            }
        }
Ejemplo n.º 7
0
        private ValueOrError <object> ReadMemberValue(ConceptMember member, ITokenReader tokenReader, IConceptInfo useLastConcept,
                                                      bool firstMember, ref bool parsedFirstReferenceElement, bool readingAReference)
        {
            try
            {
                if (member.IsStringType)
                {
                    if (useLastConcept != null)
                    {
                        return(ValueOrError <object> .CreateError($"This concept cannot be nested within {useLastConcept.GetType().Name}. Trying to read {ConceptInfoType.Name}."));
                    }

                    if (readingAReference && parsedFirstReferenceElement)
                    {
                        if (!tokenReader.TryRead("."))
                        {
                            return(ValueOrError <object> .CreateError(string.Format(
                                                                          "Parent property and the following key value ({0}) must be separated with a dot. Expected \".\"",
                                                                          member.Name)));
                        }
                    }

                    parsedFirstReferenceElement = true;

                    // Legacy syntax:
                    if (!readingAReference && member.IsKey && member.IsStringType && !firstMember)
                    {
                        if (tokenReader.TryRead("."))
                        {
                            AddWarning($"Obsolete syntax: Remove '.' from {Keyword} statement. {((TokenReader)tokenReader).ReportPosition()}.");
                        }
                    }

                    return(tokenReader.ReadText().ChangeType <object>());
                }

                if (member.ValueType == typeof(IConceptInfo))
                {
                    if (useLastConcept != null)
                    {
                        return((object)useLastConcept);
                    }
                    else
                    {
                        return(ValueOrError <object> .CreateError($"Member of type IConceptInfo can only be nested within" +
                                                                  $" the referenced parent concept. It must be a first member or marked with {nameof(ConceptParentAttribute)}."));
                    }
                }

                if (useLastConcept != null && member.ValueType.IsInstanceOfType(useLastConcept))
                {
                    return((object)useLastConcept);
                }

                if (member.IsConceptInfo && firstMember && !readingAReference && Members.Count(m => m.IsParsable) == 1)
                {
                    // This validation is not necessary for consistent parsing. It is enforced simply to avoid ambiguity for future concept overloads
                    // when parsing similar concepts such as "Logging { AllProperties; }", "History { AllProperties; }" and "Persisted { AllProperties; }".
                    var parentMembers = ConceptMembers.Get(member.ValueType).Where(m => m.IsParsable).ToArray();
                    if (parentMembers.Count() == 1 && parentMembers.Single().IsConceptInfo)
                    {
                        return(ValueOrError.CreateError($"{ConceptInfoHelper.GetKeywordOrTypeName(ConceptInfoType)} must be nested" +
                                                        $" within the referenced parent concept {ConceptInfoHelper.GetKeywordOrTypeName(member.ValueType)}." +
                                                        $" A single-reference concept that references another single-reference concept must always be used with nested syntax to avoid ambiguity."));
                    }
                }

                if (member.IsConceptInfo)
                {
                    if (firstMember && member.ValueType == ConceptInfoType)
                    {
                        return(ValueOrError.CreateError(string.Format(
                                                            "Recursive concept {0} cannot be used as a root because its parent property ({1}) must reference another concept. Use a non-recursive concept for the root and a derivation of the root concept with additional parent property as a recursive concept.",
                                                            ConceptInfoHelper.GetKeywordOrTypeName(ConceptInfoType), member.Name)));
                    }

                    GenericParser subParser = new GenericParser(member.ValueType, "");
                    return(subParser.ParseMembers(tokenReader, useLastConcept, true, ref parsedFirstReferenceElement).ChangeType <object>());
                }

                return(ValueOrError.CreateError(string.Format(
                                                    "GenericParser does not support members of type \"{0}\". Try using string or implementation of IConceptInfo.",
                                                    member.ValueType.Name)));
            }
            catch (DslSyntaxException ex)
            {
                return(ValueOrError <object> .CreateError(ex.Message));
            }
        }