Пример #1
0
        internal override ResolvedAttributeSetBuilder ConstructResolvedAttributes(ResolveOptions resOpt, CdmAttributeContext under = null)
        {
            // find and cache the complete set of attributes
            // attributes definitions originate from and then get modified by subsequent re-defintions from (in this order):
            // the datatype used as an attribute, traits applied to that datatype,
            // the purpose of the attribute, dynamic traits applied to the attribute.
            ResolvedAttributeSetBuilder rasb = new ResolvedAttributeSetBuilder();

            rasb.ResolvedAttributeSet.AttributeContext = under;

            // add this attribute to the set
            // make a new one and apply dynamic traits
            ResolvedAttribute newAtt = new ResolvedAttribute(resOpt, this, this.Name, under as CdmAttributeContext);

            rasb.OwnOne(newAtt);

            ResolvedTraitSet rts = this.FetchResolvedTraits(resOpt);
            // this context object holds all of the info about what needs to happen to resolve these attribute
            // make a copy and add defaults if missing
            CdmAttributeResolutionGuidance resGuideWithDefault;

            if (this.ResolutionGuidance != null)
            {
                resGuideWithDefault = (CdmAttributeResolutionGuidance)this.ResolutionGuidance.Copy(resOpt);
            }
            else
            {
                resGuideWithDefault = new CdmAttributeResolutionGuidance(this.Ctx);
            }

            // renameFormat is not currently supported for type attributes
            resGuideWithDefault.renameFormat = null;

            resGuideWithDefault.UpdateAttributeDefaults(null, this);
            AttributeResolutionContext arc = new AttributeResolutionContext(resOpt, resGuideWithDefault, rts);

            // TODO: remove the resolution guidance if projection is being used
            // from the traits of the datatype, purpose and applied here, see if new attributes get generated
            rasb.ApplyTraits(arc);
            rasb.GenerateApplierAttributes(arc, false); // false = don't apply these traits to added things
            // this may have added symbols to the dependencies, so merge them
            resOpt.SymbolRefSet.Merge(arc.ResOpt.SymbolRefSet);

            if (this.Projection != null)
            {
                ProjectionDirective projDirective = new ProjectionDirective(resOpt, this);
                ProjectionContext   projCtx       = this.Projection.ConstructProjectionContext(projDirective, under, rasb.ResolvedAttributeSet);

                ResolvedAttributeSet ras = this.Projection.ExtractResolvedAttributes(projCtx, under);
                rasb.ResolvedAttributeSet = ras;
            }

            return(rasb);
        }
Пример #2
0
        public override CdmObject Copy(ResolveOptions resOpt = null)
        {
            if (resOpt == null)
            {
                resOpt = new ResolveOptions(this);
            }

            CdmAttributeResolutionGuidance c = new CdmAttributeResolutionGuidance(this.Ctx)
            {
                Ctx                    = this.Ctx,
                removeAttribute        = this.removeAttribute,
                imposedDirectives      = this.imposedDirectives,
                removedDirectives      = this.removedDirectives,
                addSupportingAttribute = this.addSupportingAttribute,
                cardinality            = this.cardinality,
                renameFormat           = this.renameFormat
            };

            if (this.expansion != null)
            {
                c.expansion = new Expansion()
                {
                    startingOrdinal  = this.expansion.startingOrdinal,
                    maximumExpansion = this.expansion.maximumExpansion,
                    countAttribute   = this.expansion.countAttribute
                };
            }
            if (this.entityByReference != null)
            {
                c.entityByReference = new CdmAttributeResolutionGuidance_EntityByReference()
                {
                    alwaysIncludeForeignKey = this.entityByReference.alwaysIncludeForeignKey,
                    referenceOnlyAfterDepth = this.entityByReference.referenceOnlyAfterDepth,
                    allowReference          = this.entityByReference.allowReference,
                    foreignKeyAttribute     = this.entityByReference.foreignKeyAttribute
                };
            }
            if (this.selectsSubAttribute != null)
            {
                c.selectsSubAttribute = new CdmAttributeResolutionGuidance_SelectsSubAttribute()
                {
                    selects = this.selectsSubAttribute.selects,
                    selectedTypeAttribute = this.selectsSubAttribute.selectedTypeAttribute,
                    selectsSomeTakeNames  = this.selectsSubAttribute.selectsSomeTakeNames,
                    selectsSomeAvoidNames = this.selectsSubAttribute.selectsSomeAvoidNames
                };
            }
            return(c);
        }
Пример #3
0
        /// <summary>
        /// Creates an AttributeResolutionContext object based off of resolution guidance information
        /// </summary>
        /// <param name="resOpt"></param>
        /// <returns>An AttributeResolutionContext used for correctly resolving an entity attribute.</returns>
        private AttributeResolutionContext FetchAttResContext(ResolveOptions resOpt)
        {
            ResolvedTraitSet rtsThisAtt = this.FetchResolvedTraits(resOpt);

            // this context object holds all of the info about what needs to happen to resolve these attributes.
            // make a copy and add defaults if missing
            CdmAttributeResolutionGuidance resGuideWithDefault;

            if (this.ResolutionGuidance != null)
            {
                resGuideWithDefault = (CdmAttributeResolutionGuidance)this.ResolutionGuidance.Copy(resOpt);
            }
            else
            {
                resGuideWithDefault = new CdmAttributeResolutionGuidance(this.Ctx);
            }
            resGuideWithDefault.UpdateAttributeDefaults(this.Name, this);

            return(new AttributeResolutionContext(resOpt, resGuideWithDefault, rtsThisAtt));
        }
Пример #4
0
        internal CdmAttributeResolutionGuidance combineResolutionGuidance(CdmAttributeResolutionGuidance addIn)
        {
            CdmAttributeResolutionGuidance startWith = this;

            if (addIn == null)
            {
                return(startWith);
            }
            if (startWith == null)
            {
                return(addIn);
            }

            CdmAttributeResolutionGuidance result = new CdmAttributeResolutionGuidance(this.Ctx);

            // can remove and then un-remove later
            if (startWith.removeAttribute == true)
            {
                if (addIn.removeAttribute == null || addIn.removeAttribute == true)
                {
                    result.removeAttribute = true;
                }
            }
            else
            {
                if (addIn.removeAttribute != null && addIn.removeAttribute == true)
                {
                    result.removeAttribute = true;
                }
            }

            // copy and combine if needed
            if (addIn.imposedDirectives != null)
            {
                if (startWith.imposedDirectives != null)
                {
                    result.imposedDirectives = new List <string>(startWith.imposedDirectives);
                }
                else
                {
                    result.imposedDirectives = new List <string>();
                }
                result.imposedDirectives.AddRange(addIn.imposedDirectives);
            }
            else
            {
                result.imposedDirectives = startWith.imposedDirectives;
            }

            if (addIn.removedDirectives != null)
            {
                if (startWith.removedDirectives != null)
                {
                    result.removedDirectives = new List <string>(startWith.removedDirectives);
                }
                else
                {
                    result.removedDirectives = new List <string>();
                }
                result.removedDirectives.AddRange(addIn.removedDirectives);
            }
            else
            {
                result.removedDirectives = startWith.removedDirectives;
            }

            result.addSupportingAttribute = startWith.addSupportingAttribute;
            if (addIn.addSupportingAttribute != null)
            {
                result.addSupportingAttribute = addIn.addSupportingAttribute;
            }

            result.cardinality = startWith.cardinality;
            if (addIn.cardinality != null)
            {
                result.cardinality = addIn.cardinality;
            }

            result.renameFormat = startWith.renameFormat;
            if (addIn.renameFormat != null)
            {
                result.renameFormat = addIn.renameFormat;
            }

            // for these sub objects, ok to just use the same objects unless something is combined. assumption is that these are static during the resolution
            if (addIn.expansion != null)
            {
                if (startWith.expansion != null)
                {
                    result.expansion = new Expansion();
                    result.expansion.startingOrdinal = startWith.expansion.startingOrdinal;
                    if (addIn.expansion.startingOrdinal != null)
                    {
                        result.expansion.startingOrdinal = addIn.expansion.startingOrdinal;
                    }
                    result.expansion.maximumExpansion = startWith.expansion.maximumExpansion;
                    if (addIn.expansion.maximumExpansion != null)
                    {
                        result.expansion.maximumExpansion = addIn.expansion.maximumExpansion;
                    }
                    result.expansion.countAttribute = startWith.expansion.countAttribute;
                    if (addIn.expansion.countAttribute != null)
                    {
                        result.expansion.countAttribute = addIn.expansion.countAttribute;
                    }
                }
                else
                {
                    result.expansion = addIn.expansion;
                }
            }
            else
            {
                result.expansion = startWith.expansion;
            }

            if (addIn.entityByReference != null)
            {
                if (startWith.entityByReference != null)
                {
                    result.entityByReference = new CdmAttributeResolutionGuidance_EntityByReference();
                    result.entityByReference.alwaysIncludeForeignKey = startWith.entityByReference.alwaysIncludeForeignKey;
                    if (addIn.entityByReference.alwaysIncludeForeignKey != null)
                    {
                        result.entityByReference.alwaysIncludeForeignKey = addIn.entityByReference.alwaysIncludeForeignKey;
                    }
                    result.entityByReference.referenceOnlyAfterDepth = startWith.entityByReference.referenceOnlyAfterDepth;
                    if (addIn.entityByReference.referenceOnlyAfterDepth != null)
                    {
                        result.entityByReference.referenceOnlyAfterDepth = addIn.entityByReference.referenceOnlyAfterDepth;
                    }
                    result.entityByReference.foreignKeyAttribute = startWith.entityByReference.foreignKeyAttribute;
                    if (addIn.entityByReference.foreignKeyAttribute != null)
                    {
                        result.entityByReference.foreignKeyAttribute = addIn.entityByReference.foreignKeyAttribute;
                    }
                    result.entityByReference.allowReference = startWith.entityByReference.allowReference;
                    if (addIn.entityByReference.allowReference != null)
                    {
                        result.entityByReference.allowReference = addIn.entityByReference.allowReference;
                    }
                }
                else
                {
                    result.entityByReference = addIn.entityByReference;
                }
            }
            else
            {
                result.entityByReference = startWith.entityByReference;
            }

            if (addIn.selectsSubAttribute != null)
            {
                if (startWith.selectsSubAttribute != null)
                {
                    result.selectsSubAttribute = new CdmAttributeResolutionGuidance_SelectsSubAttribute();
                    result.selectsSubAttribute.selectedTypeAttribute = startWith.selectsSubAttribute.selectedTypeAttribute;
                    if (addIn.selectsSubAttribute.selectedTypeAttribute != null)
                    {
                        result.selectsSubAttribute.selectedTypeAttribute = addIn.selectsSubAttribute.selectedTypeAttribute;
                    }
                    result.selectsSubAttribute.selects = startWith.selectsSubAttribute.selects;
                    if (addIn.selectsSubAttribute.selects != null)
                    {
                        result.selectsSubAttribute.selects = addIn.selectsSubAttribute.selects;
                    }
                    if (addIn.selectsSubAttribute.selectsSomeTakeNames != null)
                    {
                        if (startWith.selectsSubAttribute.selectsSomeTakeNames != null)
                        {
                            result.selectsSubAttribute.selectsSomeTakeNames = new List <string>(startWith.selectsSubAttribute.selectsSomeTakeNames);
                        }
                        else
                        {
                            result.selectsSubAttribute.selectsSomeTakeNames = new List <string>();
                        }
                        result.selectsSubAttribute.selectsSomeTakeNames.AddRange(addIn.selectsSubAttribute.selectsSomeTakeNames);
                    }
                    if (addIn.selectsSubAttribute.selectsSomeAvoidNames != null)
                    {
                        if (startWith.selectsSubAttribute.selectsSomeAvoidNames != null)
                        {
                            result.selectsSubAttribute.selectsSomeAvoidNames = new List <string>(startWith.selectsSubAttribute.selectsSomeAvoidNames);
                        }
                        else
                        {
                            result.selectsSubAttribute.selectsSomeAvoidNames = new List <string>();
                        }
                        result.selectsSubAttribute.selectsSomeAvoidNames.AddRange(addIn.selectsSubAttribute.selectsSomeAvoidNames);
                    }
                }
                else
                {
                    result.selectsSubAttribute = addIn.selectsSubAttribute;
                }
            }
            else
            {
                result.selectsSubAttribute = startWith.selectsSubAttribute;
            }

            return(result);
        }
Пример #5
0
        /// <inheritdoc />
        public override CdmObject Copy(ResolveOptions resOpt = null, CdmObject host = null)
        {
            if (resOpt == null)
            {
                resOpt = new ResolveOptions(this, this.Ctx.Corpus.DefaultResolutionDirectives);
            }

            CdmAttributeResolutionGuidance copy;

            if (host == null)
            {
                copy = new CdmAttributeResolutionGuidance(this.Ctx);
            }
            else
            {
                copy                     = host as CdmAttributeResolutionGuidance;
                copy.Ctx                 = this.Ctx;
                copy.expansion           = null;
                copy.entityByReference   = null;
                copy.selectsSubAttribute = null;
            }

            copy.removeAttribute = this.removeAttribute;
            if (this.imposedDirectives != null)
            {
                copy.imposedDirectives = new List <string>(this.imposedDirectives);
            }

            if (this.removedDirectives != null)
            {
                copy.removedDirectives = new List <string>(this.removedDirectives);
            }

            copy.addSupportingAttribute = this.addSupportingAttribute;
            copy.cardinality            = this.cardinality;
            copy.renameFormat           = this.renameFormat;

            if (this.expansion != null)
            {
                copy.expansion = new Expansion()
                {
                    startingOrdinal  = this.expansion.startingOrdinal,
                    maximumExpansion = this.expansion.maximumExpansion,
                    countAttribute   = this.expansion.countAttribute
                };
            }
            if (this.entityByReference != null)
            {
                copy.entityByReference = new CdmAttributeResolutionGuidance_EntityByReference()
                {
                    alwaysIncludeForeignKey = this.entityByReference.alwaysIncludeForeignKey,
                    referenceOnlyAfterDepth = this.entityByReference.referenceOnlyAfterDepth,
                    allowReference          = this.entityByReference.allowReference,
                    foreignKeyAttribute     = this.entityByReference.foreignKeyAttribute
                };
            }
            if (this.selectsSubAttribute != null)
            {
                copy.selectsSubAttribute = new CdmAttributeResolutionGuidance_SelectsSubAttribute()
                {
                    selects = this.selectsSubAttribute.selects,
                    selectedTypeAttribute = this.selectsSubAttribute.selectedTypeAttribute,
                    selectsSomeTakeNames  = this.selectsSubAttribute.selectsSomeTakeNames,
                    selectsSomeAvoidNames = this.selectsSubAttribute.selectsSomeAvoidNames
                };
            }
            return(copy);
        }
Пример #6
0
        internal override ResolvedAttributeSetBuilder ConstructResolvedAttributes(ResolveOptions resOpt, CdmAttributeContext under = null)
        {
            // find and cache the complete set of attributes
            // attributes definitions originate from and then get modified by subsequent re-definitions from (in this order):
            // an extended entity, traits applied to extended entity, exhibited traits of main entity, the (datatype or entity) used as an attribute, traits applied to that datatype or entity,
            // the relationsip of the attribute, the attribute definition itself and included attribute groups, dynamic traits applied to the attribute.
            this.Rasb = new ResolvedAttributeSetBuilder();
            this.Rasb.ResolvedAttributeSet.AttributeContext = under;

            if (this.ExtendsEntity != null)
            {
                CdmObjectReference         extRef          = this.ExtendsEntityRef;
                CdmAttributeContext        extendsRefUnder = null;
                AttributeContextParameters acpExtEnt       = null;

                if (under != null)
                {
                    AttributeContextParameters acpExt = new AttributeContextParameters
                    {
                        under         = under,
                        type          = CdmAttributeContextType.EntityReferenceExtends,
                        Name          = "extends",
                        Regarding     = null,
                        IncludeTraits = false
                    };
                    extendsRefUnder = this.Rasb.ResolvedAttributeSet.CreateAttributeContext(resOpt, acpExt);
                }

                if (extRef.ExplicitReference != null && extRef.FetchObjectDefinition <CdmObjectDefinition>(resOpt).ObjectType == CdmObjectType.ProjectionDef)
                {
                    // A Projection

                    CdmObjectDefinition extRefObjDef = extRef.FetchObjectDefinition <CdmObjectDefinition>(resOpt);
                    if (extendsRefUnder != null)
                    {
                        acpExtEnt = new AttributeContextParameters
                        {
                            under         = extendsRefUnder,
                            type          = CdmAttributeContextType.Projection,
                            Name          = extRefObjDef.GetName(),
                            Regarding     = extRef,
                            IncludeTraits = false
                        };
                    }

                    ProjectionDirective projDirective = new ProjectionDirective(resOpt, this, ownerRef: extRef);
                    CdmProjection       projDef       = (CdmProjection)extRefObjDef;
                    ProjectionContext   projCtx       = projDef.ConstructProjectionContext(projDirective, extendsRefUnder);

                    this.Rasb.ResolvedAttributeSet = projDef.ExtractResolvedAttributes(projCtx);
                }
                else
                {
                    // An Entity Reference

                    if (extendsRefUnder != null)
                    {
                        // usually the extended entity is a reference to a name.
                        // it is allowed however to just define the entity inline.
                        acpExtEnt = new AttributeContextParameters
                        {
                            under         = extendsRefUnder,
                            type          = CdmAttributeContextType.Entity,
                            Name          = extRef.NamedReference ?? extRef.ExplicitReference.GetName(),
                            Regarding     = extRef,
                            IncludeTraits = false
                        };
                    }

                    // save moniker, extended entity may attach a different moniker that we do not
                    // want to pass along to getting this entities attributes
                    string oldMoniker = resOpt.FromMoniker;

                    this.Rasb.MergeAttributes((this.ExtendsEntityRef as CdmObjectReferenceBase).FetchResolvedAttributes(resOpt, acpExtEnt));

                    if (!resOpt.CheckAttributeCount(this.Rasb.ResolvedAttributeSet.ResolvedAttributeCount))
                    {
                        Logger.Error(nameof(CdmEntityDefinition), this.Ctx, $"Maximum number of resolved attributes reached for the entity: {this.EntityName}.");
                        return(null);
                    }

                    if (this.ExtendsEntityResolutionGuidance != null)
                    {
                        // some guidance was given on how to integrate the base attributes into the set. apply that guidance
                        ResolvedTraitSet rtsBase = this.FetchResolvedTraits(resOpt);

                        // this context object holds all of the info about what needs to happen to resolve these attributes.
                        // make a copy and set defaults if needed
                        CdmAttributeResolutionGuidance resGuide = (CdmAttributeResolutionGuidance)this.ExtendsEntityResolutionGuidance.Copy(resOpt);
                        resGuide.UpdateAttributeDefaults(resGuide.FetchObjectDefinitionName());
                        // holds all the info needed by the resolver code
                        AttributeResolutionContext arc = new AttributeResolutionContext(resOpt, resGuide, rtsBase);

                        this.Rasb.GenerateApplierAttributes(arc, false); // true = apply the prepared traits to new atts
                    }

                    // reset to the old moniker
                    resOpt.FromMoniker = oldMoniker;
                }
            }

            this.Rasb.MarkInherited();
            this.Rasb.ResolvedAttributeSet.AttributeContext = under;

            if (this.Attributes != null)
            {
                int l = this.Attributes.Count;
                for (int i = 0; i < l; i++)
                {
                    dynamic                    att      = this.Attributes.AllItems[i];
                    CdmAttributeContext        attUnder = under;
                    AttributeContextParameters acpAtt   = null;
                    if (under != null)
                    {
                        acpAtt = new AttributeContextParameters
                        {
                            under         = under,
                            type          = CdmAttributeContextType.AttributeDefinition,
                            Name          = att.FetchObjectDefinitionName(),
                            Regarding     = att,
                            IncludeTraits = false
                        };
                    }
                    this.Rasb.MergeAttributes(att.FetchResolvedAttributes(resOpt, acpAtt));

                    if (!resOpt.CheckAttributeCount(this.Rasb.ResolvedAttributeSet.ResolvedAttributeCount))
                    {
                        Logger.Error(nameof(CdmEntityDefinition), this.Ctx, $"Maximum number of resolved attributes reached for the entity: {this.EntityName}.");
                        return(null);
                    }
                }
            }
            this.Rasb.MarkOrder();
            this.Rasb.ResolvedAttributeSet.AttributeContext = under;

            // things that need to go away
            this.Rasb.RemoveRequestedAtts();

            return(this.Rasb);
        }
        // the only thing we need this code for is testing!!!
        public override ResolvedEntityReferenceSet FetchResolvedEntityReferences(ResolveOptions resOpt = null)
        {
            if (resOpt == null)
            {
                resOpt = new ResolveOptions(this);
            }

            ResolvedTraitSet rtsThisAtt = this.FetchResolvedTraits(resOpt);
            CdmAttributeResolutionGuidance resGuide = (CdmAttributeResolutionGuidance)this.ResolutionGuidance;

            // this context object holds all of the info about what needs to happen to resolve these attributes
            AttributeResolutionContext arc = new AttributeResolutionContext(resOpt, resGuide, rtsThisAtt);

            RelationshipInfo relInfo = this.GetRelationshipInfo(resOpt, arc);
            if (relInfo.IsByRef && !relInfo.IsArray)
            {
                {
                    // only place this is used, so logic here instead of encapsulated. 
                    // make a set and the one ref it will hold
                    ResolvedEntityReferenceSet rers = new ResolvedEntityReferenceSet(resOpt);
                    ResolvedEntityReference rer = new ResolvedEntityReference();
                    // referencing attribute(s) come from this attribute
                    rer.Referencing.ResolvedAttributeSetBuilder.MergeAttributes(this.FetchResolvedAttributes(resOpt, null));
                    Func<CdmEntityReference, ResolvedEntityReferenceSide> resolveSide = entRef =>
                    {
                        ResolvedEntityReferenceSide sideOther = new ResolvedEntityReferenceSide(null, null);
                        if (entRef != null)
                        {
                            // reference to the other entity, hard part is the attribue name.
                            // by convention, this is held in a trait that identifies the key
                            sideOther.Entity = entRef.FetchObjectDefinition<CdmEntityDefinition>(resOpt);
                            if (sideOther.Entity != null)
                            {
                                CdmAttribute otherAttribute;
                                ResolveOptions otherOpts = new ResolveOptions { WrtDoc = resOpt.WrtDoc, Directives = resOpt.Directives };
                                ResolvedTrait t = entRef.FetchResolvedTraits(otherOpts).Find(otherOpts, "is.identifiedBy");
                                if (t?.ParameterValues?.Length > 0)
                                {
                                    dynamic otherRef = (t.ParameterValues.FetchParameterValueByName("attribute").Value);
                                    if (typeof(CdmObject).IsAssignableFrom(otherRef?.GetType()))
                                    {
                                        otherAttribute = (otherRef as CdmObject).FetchObjectDefinition<CdmObjectDefinition>(otherOpts) as CdmAttribute;
                                        if (otherAttribute != null)
                                        {
                                            sideOther.ResolvedAttributeSetBuilder.OwnOne(sideOther.Entity.FetchResolvedAttributes(otherOpts).Get(otherAttribute.GetName()).Copy());
                                        }
                                    }
                                }
                            }
                        }

                        return sideOther;
                    };

                    // either several or one entity
                    // for now, a sub for the 'select one' idea
                    if ((this.Entity as CdmEntityReference).ExplicitReference != null)
                    {
                        CdmEntityDefinition entPickFrom = (this.Entity as CdmEntityReference).FetchObjectDefinition<CdmEntityDefinition>(resOpt);
                        CdmCollection<CdmAttributeItem> attsPick = entPickFrom.GetAttributeDefinitions();
                        if (attsPick != null && attsPick != null)
                        {
                            for (int i = 0; i < attsPick.Count; i++)
                            {
                                if (attsPick.AllItems[i].ObjectType == CdmObjectType.EntityAttributeDef)
                                {
                                    CdmEntityReference er = (attsPick.AllItems[i] as CdmEntityAttributeDefinition).Entity;
                                    rer.Referenced.Add(resolveSide(er));
                                }
                            }
                        }
                    }
                    else
                    {
                        rer.Referenced.Add(resolveSide(this.Entity as CdmEntityReference));
                    }

                    rers.Set.Add(rer);
                    return rers;
                }
            }
            return null;
        }
        internal override ResolvedAttributeSetBuilder ConstructResolvedAttributes(ResolveOptions resOpt, CdmAttributeContext under = null)
        {
            // find and cache the complete set of attributes
            // attributes definitions originate from and then get modified by subsequent re-defintions from (in this order):
            // the entity used as an attribute, traits applied to that entity,
            // the purpose of the attribute, any traits applied to the attribute.
            ResolvedAttributeSetBuilder rasb = new ResolvedAttributeSetBuilder();
            CdmEntityReference ctxEnt = this.Entity as CdmEntityReference;
            CdmAttributeContext underAtt = (CdmAttributeContext)under;
            AttributeContextParameters acpEnt = null;
            if (underAtt != null)
            {
                // make a context for this attreibute that holds the attributes that come up from the entity
                acpEnt = new AttributeContextParameters
                {
                    under = underAtt,
                    type = CdmAttributeContextType.Entity,
                    Name = ctxEnt.FetchObjectDefinitionName(),
                    Regarding = ctxEnt,
                    IncludeTraits = true
                };
            }

            ResolvedTraitSet rtsThisAtt = this.FetchResolvedTraits(resOpt);

            // this context object holds all of the info about what needs to happen to resolve these attributes.
            // make a copy and add defaults if missing
            CdmAttributeResolutionGuidance resGuideWithDefault;
            if (this.ResolutionGuidance != null)
                resGuideWithDefault = (CdmAttributeResolutionGuidance)this.ResolutionGuidance.Copy(resOpt);
            else
                resGuideWithDefault = new CdmAttributeResolutionGuidance(this.Ctx);
            resGuideWithDefault.UpdateAttributeDefaults(this.Name);

            AttributeResolutionContext arc = new AttributeResolutionContext(resOpt, resGuideWithDefault, rtsThisAtt);

            // complete cheating but is faster.
            // this purpose will remove all of the attributes that get collected here, so dumb and slow to go get them
            RelationshipInfo relInfo = this.GetRelationshipInfo(arc.ResOpt, arc);
            if (relInfo.IsByRef)
            {
                // make the entity context that a real recursion would have give us
                if (under != null)
                    under = rasb.ResolvedAttributeSet.CreateAttributeContext(resOpt, acpEnt);
                // if selecting from one of many attributes, then make a context for each one
                if (under != null && relInfo.SelectsOne)
                {
                    // the right way to do this is to get a resolved entity from the embedded entity and then 
                    // look through the attribute context hierarchy for non-nested entityReferenceAsAttribute nodes
                    // that seems like a disaster waiting to happen given endless looping, etc.
                    // for now, just insist that only the top level entity attributes declared in the ref entity will work
                    CdmEntityDefinition entPickFrom = (this.Entity as CdmEntityReference).FetchObjectDefinition<CdmEntityDefinition>(resOpt) as CdmEntityDefinition;
                    CdmCollection<CdmAttributeItem> attsPick = entPickFrom?.GetAttributeDefinitions();
                    if (entPickFrom != null && attsPick != null)
                    {
                        for (int i = 0; i < attsPick.Count; i++)
                        {
                            if (attsPick.AllItems[i].ObjectType == CdmObjectType.EntityAttributeDef)
                            {
                                // a table within a table. as expected with a selectsOne attribute
                                // since this is by ref, we won't get the atts from the table, but we do need the traits that hold the key
                                // these are the same contexts that would get created if we recursed
                                // first this attribute
                                AttributeContextParameters acpEntAtt = new AttributeContextParameters
                                {
                                    under = under,
                                    type = CdmAttributeContextType.AttributeDefinition,
                                    Name = attsPick.AllItems[i].FetchObjectDefinitionName(),
                                    Regarding = attsPick.AllItems[i],
                                    IncludeTraits = true
                                };
                                CdmAttributeContext pickUnder = rasb.ResolvedAttributeSet.CreateAttributeContext(resOpt, acpEntAtt);
                                CdmEntityReference pickEnt = (attsPick.AllItems[i] as CdmEntityAttributeDefinition).Entity as CdmEntityReference;
                                AttributeContextParameters acpEntAttEnt = new AttributeContextParameters
                                {
                                    under = pickUnder,
                                    type = CdmAttributeContextType.Entity,
                                    Name = pickEnt.FetchObjectDefinitionName(),
                                    Regarding = pickEnt,
                                    IncludeTraits = true
                                };
                                rasb.ResolvedAttributeSet.CreateAttributeContext(resOpt, acpEntAttEnt);
                            }
                        }
                    }
                }

                // if we got here because of the max depth, need to impose the directives to make the trait work as expected
                if (relInfo.MaxDepthExceeded)
                {
                    if (arc.ResOpt.Directives == null)
                        arc.ResOpt.Directives = new AttributeResolutionDirectiveSet();
                    arc.ResOpt.Directives.Add("referenceOnly");
                }
            }
            else
            {
                ResolveOptions resLink = CopyResolveOptions(resOpt);
                resLink.SymbolRefSet = resOpt.SymbolRefSet;
                resLink.RelationshipDepth = relInfo.NextDepth;
                rasb.MergeAttributes((this.Entity as CdmEntityReference).FetchResolvedAttributes(resLink, acpEnt));
            }

            // from the traits of purpose and applied here, see if new attributes get generated
            rasb.ResolvedAttributeSet.AttributeContext = underAtt;
            rasb.ApplyTraits(arc);
            rasb.GenerateApplierAttributes(arc, true); // true = apply the prepared traits to new atts
            // this may have added symbols to the dependencies, so merge them
            resOpt.SymbolRefSet.Merge(arc.ResOpt.SymbolRefSet);

            // use the traits for linked entity identifiers to record the actual foreign key links
            if (rasb.ResolvedAttributeSet?.Set != null && relInfo.IsByRef)
            {
                foreach (var att in rasb.ResolvedAttributeSet.Set)
                {
                    var reqdTrait = att.ResolvedTraits.Find(resOpt, "is.linkedEntity.identifier");
                    if (reqdTrait == null)
                    {
                        continue;
                    }

                    if (reqdTrait.ParameterValues == null || reqdTrait.ParameterValues.Length == 0)
                    {
                        Logger.Warning(nameof(CdmEntityAttributeDefinition), this.Ctx as ResolveContext, "is.linkedEntity.identifier does not support arguments");
                        continue;
                    }

                    var entReferences = new List<string>();
                    var attReferences = new List<string>();
                    Action<CdmEntityReference, string> addEntityReference = (CdmEntityReference entRef, string nameSpace) =>
                        {
                            var entDef = entRef.FetchObjectDefinition<CdmEntityDefinition>(resOpt);
                            var identifyingTrait = entRef.FetchResolvedTraits(resOpt).Find(resOpt, "is.identifiedBy");
                            if (identifyingTrait != null && entDef != null)
                            {
                                var attRef = identifyingTrait.ParameterValues.FetchParameterValueByName("attribute").Value;
                                string attNamePath = ((CdmObjectReferenceBase)attRef).NamedReference;
                                string attName = attNamePath.Split('/').Last();                                // path should be absolute and without a namespace
                                string relativeEntPath = Ctx.Corpus.Storage.CreateAbsoluteCorpusPath(entDef.AtCorpusPath, entDef.InDocument);
                                if (relativeEntPath.StartsWith($"{nameSpace}:"))
                                {
                                    relativeEntPath = relativeEntPath.Substring(nameSpace.Length + 1);
                                }
                                entReferences.Add(relativeEntPath);
                                attReferences.Add(attName);
                            }
                        };
                    if (relInfo.SelectsOne)
                    {
                        var entPickFrom = (this.Entity as CdmEntityReference).FetchObjectDefinition<CdmEntityDefinition>(resOpt) as CdmEntityDefinition;
                        var attsPick = entPickFrom?.GetAttributeDefinitions()?.Cast<CdmObject>().ToList();
                        if (entPickFrom != null && attsPick != null)
                        {
                            for (int i = 0; i < attsPick.Count; i++)
                            {
                                if (attsPick[i].ObjectType == CdmObjectType.EntityAttributeDef)
                                {
                                    var entAtt = attsPick[i] as CdmEntityAttributeDefinition;
                                    addEntityReference(entAtt.Entity, this.InDocument.Namespace);
                                }
                            }
                        }
                    }
                    else
                    {
                        addEntityReference(this.Entity, this.InDocument.Namespace);
                    }

                    var constantEntity = this.Ctx.Corpus.MakeObject<CdmConstantEntityDefinition>(CdmObjectType.ConstantEntityDef);
                    constantEntity.EntityShape = this.Ctx.Corpus.MakeRef<CdmEntityReference>(CdmObjectType.EntityRef, "entityGroupSet", true);
                    constantEntity.ConstantValues = entReferences.Select((entRef, idx) => new List<string> { entRef, attReferences[idx] }).ToList();
                    var traitParam = this.Ctx.Corpus.MakeRef<CdmEntityReference>(CdmObjectType.EntityRef, constantEntity, false);
                    reqdTrait.ParameterValues.SetParameterValue(resOpt, "entityReferences", traitParam);
                }
            }

            // a 'structured' directive wants to keep all entity attributes together in a group
            if (arc.ResOpt.Directives?.Has("structured") == true)
            {
                ResolvedAttribute raSub = new ResolvedAttribute(rtsThisAtt.ResOpt, rasb.ResolvedAttributeSet, this.Name, (CdmAttributeContext)rasb.ResolvedAttributeSet.AttributeContext);
                if (relInfo.IsArray)
                {
                    // put a resolved trait on this att group, yuck, hope I never need to do this again and then need to make a function for this
                    CdmTraitReference tr = this.Ctx.Corpus.MakeObject<CdmTraitReference>(CdmObjectType.TraitRef, "is.linkedEntity.array", true);
                    var t = tr.FetchObjectDefinition<CdmTraitDefinition>(resOpt);
                    ResolvedTrait rt = new ResolvedTrait(t, null, new List<dynamic>(), new List<bool>());
                    raSub.ResolvedTraits = raSub.ResolvedTraits.Merge(rt, true);
                }
                rasb = new ResolvedAttributeSetBuilder();
                rasb.OwnOne(raSub);
            }

            return rasb;
        }
Пример #9
0
        internal override ResolvedAttributeSetBuilder ConstructResolvedAttributes(ResolveOptions resOpt, CdmAttributeContext under = null)
        {
            // find and cache the complete set of attributes
            // attributes definitions originate from and then get modified by subsequent re-definitions from (in this order):
            // an extended entity, traits applied to extended entity, exhibited traits of main entity, the (datatype or entity) used as an attribute, traits applied to that datatype or entity,
            // the relationsip of the attribute, the attribute definition itself and included attribute groups, dynamic traits applied to the attribute.
            this.Rasb = new ResolvedAttributeSetBuilder();
            this.Rasb.ResolvedAttributeSet.AttributeContext = under;

            if (this.ExtendsEntity != null)
            {
                CdmObjectReference         extRef          = this.ExtendsEntityRef;
                CdmAttributeContext        extendsRefUnder = null;
                AttributeContextParameters acpExtEnt       = null;
                if (under != null)
                {
                    AttributeContextParameters acpExt = new AttributeContextParameters
                    {
                        under         = under,
                        type          = CdmAttributeContextType.EntityReferenceExtends,
                        Name          = "extends",
                        Regarding     = null,
                        IncludeTraits = false
                    };
                    extendsRefUnder = this.Rasb.ResolvedAttributeSet.CreateAttributeContext(resOpt, acpExt);
                    acpExtEnt       = new AttributeContextParameters
                    {
                        under         = extendsRefUnder,
                        type          = CdmAttributeContextType.Entity,
                        Name          = extRef.NamedReference,
                        Regarding     = extRef,
                        IncludeTraits = false
                    };
                }
                // save moniker, extended entity may attach a different moniker that we do not
                // want to pass along to getting this entities attributes
                string oldMoniker = resOpt.FromMoniker;

                this.Rasb.MergeAttributes((this.ExtendsEntityRef as CdmObjectReferenceBase).FetchResolvedAttributes(resOpt, acpExtEnt));

                if (this.ExtendsEntityResolutionGuidance != null)
                {
                    // some guidance was given on how to integrate the base attributes into the set. apply that guidance
                    ResolvedTraitSet rtsBase = this.FetchResolvedTraits(resOpt);

                    // this context object holds all of the info about what needs to happen to resolve these attributes.
                    // make a copy and set defaults if needed
                    CdmAttributeResolutionGuidance resGuide = (CdmAttributeResolutionGuidance)this.ExtendsEntityResolutionGuidance.Copy(resOpt);
                    resGuide.UpdateAttributeDefaults(resGuide.FetchObjectDefinitionName());
                    // holds all the info needed by the resolver code
                    AttributeResolutionContext arc = new AttributeResolutionContext(resOpt, resGuide, rtsBase);

                    this.Rasb.GenerateApplierAttributes(arc, false); // true = apply the prepared traits to new atts
                }

                // reset to the old moniker
                resOpt.FromMoniker = oldMoniker;
            }

            this.Rasb.MarkInherited();
            this.Rasb.ResolvedAttributeSet.AttributeContext = under;

            if (this.Attributes != null)
            {
                int l = this.Attributes.Count;
                for (int i = 0; i < l; i++)
                {
                    dynamic                    att      = this.Attributes.AllItems[i];
                    CdmAttributeContext        attUnder = under;
                    AttributeContextParameters acpAtt   = null;
                    if (under != null)
                    {
                        acpAtt = new AttributeContextParameters
                        {
                            under         = under,
                            type          = CdmAttributeContextType.AttributeDefinition,
                            Name          = att.FetchObjectDefinitionName(),
                            Regarding     = att,
                            IncludeTraits = false
                        };
                    }
                    this.Rasb.MergeAttributes(att.FetchResolvedAttributes(resOpt, acpAtt));
                }
            }
            this.Rasb.MarkOrder();
            this.Rasb.ResolvedAttributeSet.AttributeContext = under;

            // things that need to go away
            this.Rasb.RemoveRequestedAtts();

            return(this.Rasb);
        }