예제 #1
0
        /// <summary>
        /// Verifies if the trait argument data type matches what is specified on the trait definition.
        /// </summary>
        /// <param name="objects"></param>
        /// <param name="resOpt"></param>
        internal void ResolveTraitArguments(ResolveOptions resOpt)
        {
            ResolveContext ctx = this.Ctx as ResolveContext;

            foreach (var obj in this.InternalObjects)
            {
                if (obj is CdmTraitReference traitRef)
                {
                    CdmTraitDefinition traitDef = traitRef.FetchObjectDefinition <CdmTraitDefinition>(resOpt);
                    if (traitDef == null)
                    {
                        continue;
                    }

                    for (int argumentIndex = 0; argumentIndex < traitRef.Arguments.Count; ++argumentIndex)
                    {
                        CdmArgumentDefinition argument = traitRef.Arguments[argumentIndex];
                        try
                        {
                            ctx.RelativePath = argument.DeclaredPath;

                            ParameterCollection    paramCollection = traitDef.FetchAllParameters(resOpt);
                            CdmParameterDefinition paramFound      = paramCollection.ResolveParameter(argumentIndex, argument.Name);
                            argument.ResolvedParameter = paramFound;

                            // if parameter type is entity, then the value should be an entity or ref to one
                            // same is true of 'dataType' dataType
                            dynamic argumentValue = paramFound.ConstTypeCheck(resOpt, this, argument.Value);
                            if (argumentValue != null)
                            {
                                argument.Value = argumentValue;
                            }
                        }
                        catch (Exception e)
                        {
                            Logger.Error(ctx, Tag, nameof(ResolveTraitArguments), this.AtCorpusPath, CdmLogCode.ErrTraitResolutionFailure, traitDef.GetName(), e.ToString());
                        }
                    }

                    traitRef.ResolvedArguments = true;
                }
            }
        }
예제 #2
0
        internal override ResolvedTraitSet FetchResolvedTraits(ResolveOptions resOpt = null)
        {
            if (resOpt == null)
            {
                resOpt = new ResolveOptions(this);
            }

            const string   kind = "rtsb";
            ResolveContext ctx  = this.Ctx as ResolveContext;
            // get referenced trait
            CdmTraitDefinition trait    = this.FetchObjectDefinition <CdmTraitDefinition>(resOpt) as CdmTraitDefinition;
            ResolvedTraitSet   rtsTrait = null;

            if (trait == null)
            {
                return(((CdmCorpusDefinition)ctx.Corpus).CreateEmptyResolvedTraitSet(resOpt));
            }

            // see if one is already cached
            // cache by name unless there are parameter
            if (trait.ThisIsKnownToHaveParameters == null)
            {
                // never been resolved, it will happen soon, so why not now?
                rtsTrait = trait.FetchResolvedTraits(resOpt);
            }

            bool cacheByName = true;

            if (trait.ThisIsKnownToHaveParameters != null)
            {
                cacheByName = !((bool)trait.ThisIsKnownToHaveParameters);
            }

            string  cacheTag  = ((CdmCorpusDefinition)ctx.Corpus).CreateDefinitionCacheTag(resOpt, this, kind, "", cacheByName);
            dynamic rtsResult = null;

            if (cacheTag != null)
            {
                ctx.Cache.TryGetValue(cacheTag, out rtsResult);
            }

            // store the previous reference symbol set, we will need to add it with
            // children found from the constructResolvedTraits call
            SymbolSet currSymRefSet = resOpt.SymbolRefSet;

            if (currSymRefSet == null)
            {
                currSymRefSet = new SymbolSet();
            }
            resOpt.SymbolRefSet = new SymbolSet();

            // if not, then make one and save it
            if (rtsResult == null)
            {
                // get the set of resolutions, should just be this one trait
                if (rtsTrait == null)
                {
                    // store current symbol ref set
                    SymbolSet newSymbolRefSet = resOpt.SymbolRefSet;
                    resOpt.SymbolRefSet = new SymbolSet();

                    rtsTrait = trait.FetchResolvedTraits(resOpt);

                    // bubble up symbol reference set from children
                    if (newSymbolRefSet != null)
                    {
                        newSymbolRefSet.Merge(resOpt.SymbolRefSet);
                    }
                    resOpt.SymbolRefSet = newSymbolRefSet;
                }
                if (rtsTrait != null)
                {
                    rtsResult = rtsTrait.DeepCopy();
                }

                // now if there are argument for this application, set the values in the array
                if (this.Arguments != null && rtsResult != null)
                {
                    // if never tried to line up arguments with parameters, do that
                    if (!this.ResolvedArguments)
                    {
                        this.ResolvedArguments = true;
                        ParameterCollection    param      = trait.FetchAllParameters(resOpt);
                        CdmParameterDefinition paramFound = null;
                        dynamic aValue = null;

                        int iArg = 0;
                        if (this.Arguments != null)
                        {
                            foreach (CdmArgumentDefinition a in this.Arguments)
                            {
                                paramFound          = param.ResolveParameter(iArg, a.Name);
                                a.ResolvedParameter = paramFound;
                                aValue  = a.Value;
                                aValue  = ((CdmCorpusDefinition)ctx.Corpus).ConstTypeCheck(resOpt, this.InDocument, paramFound, aValue);
                                a.Value = aValue;
                                iArg++;
                            }
                        }
                    }
                    if (this.Arguments != null)
                    {
                        foreach (CdmArgumentDefinition a in this.Arguments)
                        {
                            rtsResult.SetParameterValueFromArgument(trait, a);
                        }
                    }
                }

                // register set of possible symbols
                ((CdmCorpusDefinition)ctx.Corpus).RegisterDefinitionReferenceSymbols(this.FetchObjectDefinition <CdmObjectDefinition>(resOpt), kind, resOpt.SymbolRefSet);

                // get the new cache tag now that we have the list of symbols
                cacheTag = ((CdmCorpusDefinition)ctx.Corpus).CreateDefinitionCacheTag(resOpt, this, kind, "", cacheByName);
                if (!string.IsNullOrWhiteSpace(cacheTag))
                {
                    ctx.Cache[cacheTag] = rtsResult;
                }
            }
            else
            {
                // cache was found
                // get the SymbolSet for this cached object
                string key = CdmCorpusDefinition.CreateCacheKeyFromObject(this, kind);
                ((CdmCorpusDefinition)ctx.Corpus).DefinitionReferenceSymbols.TryGetValue(key, out SymbolSet tempDocRefSet);
                resOpt.SymbolRefSet = tempDocRefSet;
            }

            // merge child document set with current
            currSymRefSet.Merge(resOpt.SymbolRefSet);
            resOpt.SymbolRefSet = currSymRefSet;

            return(rtsResult);
        }