internal override void ConstructResolvedTraits(ResolvedTraitSetBuilder rtsb, ResolveOptions resOpt) { // base traits then add any elevated from attributes then add things exhibited by the att. CdmObjectReference baseClass = this.ExtendsEntityRef; if (baseClass != null) { // merge in all from base class rtsb.MergeTraits((baseClass as CdmObjectReferenceBase).FetchResolvedTraits(resOpt)); } if (this.Attributes != null) { ResolvedTraitSet rtsElevated = new ResolvedTraitSet(resOpt); for (int i = 0; i < this.Attributes.Count; i++) { dynamic att = this.Attributes.AllItems[i]; dynamic rtsAtt = att.FetchResolvedTraits(resOpt); if (rtsAtt?.HasElevated == true) { rtsElevated = rtsElevated.MergeSet(rtsAtt, true); } } rtsb.MergeTraits(rtsElevated); } this.ConstructResolvedTraitsDef(null, rtsb, resOpt); }
internal override void ConstructResolvedTraits(ResolvedTraitSetBuilder rtsb, ResolveOptions resOpt) { CdmObjectDefinition objDef = this.FetchObjectDefinition <CdmObjectDefinition>(resOpt); if (objDef != null) { ResolvedTraitSet rtsInh = (objDef as CdmObjectDefinitionBase).FetchResolvedTraits(resOpt); if (rtsInh != null) { rtsInh = rtsInh.DeepCopy(); } rtsb.TakeReference(rtsInh); } else { string defName = this.FetchObjectDefinitionName(); Logger.Warning(defName, this.Ctx, $"unable to resolve an object from the reference '{defName}'"); } if (this.AppliedTraits != null) { foreach (CdmTraitReference at in this.AppliedTraits) { rtsb.MergeTraits(at.FetchResolvedTraits(resOpt)); } } }
/// <summary> /// Projections require a new resolved attribute to be created multiple times /// This function allows us to create new resolved attributes based on a input attribute /// </summary> /// <param name="projCtx"></param> /// <param name="attrCtxUnder"></param> /// <param name="targetAttr"></param> /// <param name="overrideDefaultName"></param> /// <param name="addedSimpleRefTraits"></param> /// <returns></returns> internal static ResolvedAttribute CreateNewResolvedAttribute( ProjectionContext projCtx, CdmAttributeContext attrCtxUnder, CdmAttribute targetAttr, string overrideDefaultName = null, List <string> addedSimpleRefTraits = null) { ResolvedAttribute newResAttr = new ResolvedAttribute( projCtx.ProjectionDirective.ResOpt, targetAttr, !string.IsNullOrWhiteSpace(overrideDefaultName) ? overrideDefaultName : targetAttr.GetName(), attrCtxUnder); if (addedSimpleRefTraits != null) { foreach (string trait in addedSimpleRefTraits) { if (targetAttr.AppliedTraits.Item(trait) == null) { targetAttr.AppliedTraits.Add(trait, true); } } } ResolvedTraitSet resTraitSet = targetAttr.FetchResolvedTraits(projCtx.ProjectionDirective.ResOpt); // Create deep a copy of traits to avoid conflicts in case of parameters if (resTraitSet != null) { newResAttr.ResolvedTraits = resTraitSet.DeepCopy(); } return(newResAttr); }
/// Returns a map from parameter names to the final argument values for a trait reference. /// Values come (in this order) from base trait defaults, then default overrides on inheritence, /// then values supplied on this reference. public Dictionary <string, dynamic> FetchFinalArgumentValues(ResolveOptions resOpt) { Dictionary <string, dynamic> finalArgs = new Dictionary <string, dynamic>(); // get resolved traits does all the work, just clean up the answers ResolvedTraitSet rts = this.FetchResolvedTraits(resOpt); if (rts == null) { return(null); } // there is only one resolved trait ResolvedTrait rt = rts.First; if (rt.ParameterValues != null && rt.ParameterValues.Length > 0) { int l = rt.ParameterValues.Length; for (int i = 0; i < l; i++) { var p = rt.ParameterValues.FetchParameterAtIndex(i); dynamic v = rt.ParameterValues.FetchValue(i); string name = p.Name; if (name == null) { name = i.ToString(); } finalArgs.Add(name, v); } } return(finalArgs); }
internal static CdmAttributeContext CreateChildUnder(ResolveOptions resOpt, AttributeContextParameters acp) { if (acp == null) { return(null); } if (acp.type == CdmAttributeContextType.PassThrough) { return(acp.under); } // this flag makes sure we hold on to any resolved object refs when things get copied resOpt.SaveResolutionsOnCopy = true; CdmObjectReference definition = null; ResolvedTraitSet rtsApplied = null; // get a simple reference to definition object to avoid getting the traits that might be part of this ref // included in the link to the definition. if (acp.Regarding != null) { // make a portable reference. this MUST be fixed up when the context node lands in the final document definition = (acp.Regarding as CdmObjectBase).CreatePortableReference(resOpt); // now get the traits applied at this reference (applied only, not the ones that are part of the definition of the object) // and make them the traits for this context if (acp.IncludeTraits) { rtsApplied = (acp.Regarding as CdmObjectBase).FetchResolvedTraits(resOpt); } } CdmAttributeContext underChild = acp.under.Ctx.Corpus.MakeObject <CdmAttributeContext>(CdmObjectType.AttributeContextDef, acp.Name); // need context to make this a 'live' object underChild.Ctx = acp.under.Ctx; underChild.InDocument = acp.under.InDocument; underChild.Type = acp.type; underChild.Definition = definition; // add traits if there are any if (rtsApplied?.Set != null) { rtsApplied.Set.ForEach(rt => { var traitRef = CdmObjectBase.ResolvedTraitToTraitRef(resOpt, rt); underChild.ExhibitsTraits.Add(traitRef); }); } // add to parent underChild.SetParent(resOpt, acp.under); if (resOpt.MapOldCtxToNewCtx != null) { resOpt.MapOldCtxToNewCtx[underChild] = underChild; // so we can find every node, not only the replaced ones } return(underChild); }
internal static CdmAttributeContext CreateChildUnder(ResolveOptions resOpt, AttributeContextParameters acp) { if (acp == null) { return(null); } if (acp.type == CdmAttributeContextType.PassThrough) { return(acp.under as CdmAttributeContext); } // this flag makes sure we hold on to any resolved object refs when things get copied ResolveOptions resOptCopy = CdmObjectBase.CopyResolveOptions(resOpt); resOptCopy.SaveResolutionsOnCopy = true; CdmObjectReference definition = null; ResolvedTraitSet rtsApplied = null; // get a simple reference to definition object to avoid getting the traits that might be part of this ref // included in the link to the definition. if (acp.Regarding != null) { definition = acp.Regarding.CreateSimpleReference(resOptCopy); definition.InDocument = acp.under.InDocument; // ref is in same doc as context // now get the traits applied at this reference (applied only, not the ones that are part of the definition of the object) // and make them the traits for this context if (acp.IncludeTraits) { rtsApplied = (acp.Regarding as CdmObjectBase).FetchResolvedTraits(resOptCopy); } } CdmAttributeContext underChild = acp.under.Ctx.Corpus.MakeObject <CdmAttributeContext>(CdmObjectType.AttributeContextDef, acp.Name); // need context to make this a 'live' object underChild.Ctx = acp.under.Ctx; underChild.InDocument = (acp.under as CdmAttributeContext).InDocument; underChild.Type = acp.type; underChild.Definition = definition; // add traits if there are any if (rtsApplied?.Set != null) { rtsApplied.Set.ForEach(rt => { var traitRef = CdmObjectBase.ResolvedTraitToTraitRef(resOptCopy, rt); underChild.ExhibitsTraits.Add(traitRef); }); } // add to parent underChild.SetParent(resOptCopy, acp.under as CdmAttributeContext); return(underChild); }
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); }
/// <summary> /// Get a resolved trait set which contains new resolved traits with placement for wild characters if it's applicable. /// </summary> /// <param name="projCtx">The current projection context.</param> /// <param name="currentPAS">The current attribute state set.</param> /// <returns></returns> private ResolvedTraitSet ResolvedNewTraits(ProjectionContext projCtx, ProjectionAttributeState currentPAS) { ResolvedTraitSet resolvedTraitSet = new ResolvedTraitSet(projCtx.ProjectionDirective.ResOpt); string projectionOwnerName = projCtx.ProjectionDirective.OriginalSourceAttributeName ?? ""; foreach (var traitRef in this.TraitsToAdd) { var traitRefCopy = traitRef.FetchResolvedTraits(projCtx.ProjectionDirective.ResOpt).DeepCopy(); ReplaceWildcardCharacters(projCtx.ProjectionDirective.ResOpt, traitRefCopy, projectionOwnerName, currentPAS); resolvedTraitSet = resolvedTraitSet.MergeSet(traitRefCopy); } return(resolvedTraitSet); }
/// <summary> /// Remove traits from the new resolved attribute. /// </summary> /// <param name="resOpt">The resolve options.</param> /// <param name="newResAttr">The new resolved attribute.</param> private void RemoveTraitsInNewAttribute(ResolveOptions resOpt, ResolvedAttribute newResAttr) { HashSet <string> traitNamesToRemove = new HashSet <string>(); if (this.TraitsToRemove != null) { foreach (var traitRef in this.TraitsToRemove) { ResolvedTraitSet resolvedTraitSet = traitRef.FetchResolvedTraits(resOpt).DeepCopy(); resolvedTraitSet.Set.ForEach(rt => traitNamesToRemove.Add(rt.TraitName)); } traitNamesToRemove.ToList().ForEach(traitName => newResAttr.ResolvedTraits.Remove(resOpt, traitName)); } }
/// Returns a map from parameter names to the final argument values for a trait reference. /// Values come (in this order) from base trait defaults, then default overrides on inheritence, /// then values supplied on this reference. public Dictionary <string, dynamic> FetchFinalArgumentValues(ResolveOptions resOpt) { Dictionary <string, dynamic> finalArgs = new Dictionary <string, dynamic>(); // get resolved traits does all the work, just clean up the answers ResolvedTraitSet rts = this.FetchResolvedTraits(resOpt); if (rts == null || rts.Size != 1) { // well didn't get the traits. maybe imports are missing or maybe things are just not defined yet. // this function will try to fake up some answers then from the arguments that are set on this reference only if (this.Arguments != null && this.Arguments.Count > 0) { int unNamedCount = 0; foreach (var arg in this.Arguments) { // if no arg name given, use the position in the list. string argName = arg.Name; if (string.IsNullOrWhiteSpace(argName)) { argName = unNamedCount.ToString(); } finalArgs.Add(argName, arg.Value); unNamedCount++; } return(finalArgs); } return(null); } // there is only one resolved trait ResolvedTrait rt = rts.First; if (rt?.ParameterValues != null && rt.ParameterValues.Length > 0) { int l = rt.ParameterValues.Length; for (int i = 0; i < l; i++) { var p = rt.ParameterValues.FetchParameterAtIndex(i); dynamic v = rt.ParameterValues.FetchValue(i); string name = p.Name; if (name == null) { name = i.ToString(); } finalArgs.Add(name, v); } } return(finalArgs); }
internal override void ConstructResolvedTraits(ResolvedTraitSetBuilder rtsb, ResolveOptions resOpt) { // get only the elevated traits from attribute first, then add in all traits from this definition if (this.Members != null) { ResolvedTraitSet rtsElevated = new ResolvedTraitSet(resOpt); for (int i = 0; i < this.Members.Count; i++) { dynamic att = this.Members.AllItems[i]; ResolvedTraitSet rtsAtt = att.FetchResolvedTraits(resOpt); if (rtsAtt?.HasElevated == true) { rtsElevated = rtsElevated.MergeSet(rtsAtt, true); } } rtsb.MergeTraits(rtsElevated); } this.ConstructResolvedTraitsDef(null, rtsb, resOpt); }
/// <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)); }
/// <summary> /// Replace wild characters in the arguments if argumentsContainWildcards is true. /// </summary> /// <param name="resOpt">The resolve options.</param> /// <param name="resolvedTraitSet">The current attribute state set.</param> /// <param name="projectionOwnerName">The attribute name of projection owner (only available when the owner is an entity attribute or type attribute).</param> /// <param name="currentPAS">The attribute state.</param> private void ReplaceWildcardCharacters(ResolveOptions resOpt, ResolvedTraitSet resolvedTraitSet, string projectionOwnerName, ProjectionAttributeState currentPAS) { if (this.ArgumentsContainWildcards.HasValue && this.ArgumentsContainWildcards.Value == true) { foreach (ResolvedTrait resolvedTrait in resolvedTraitSet.Set) { var parameterValueSet = resolvedTrait.ParameterValues; for (int i = 0; i < parameterValueSet.Length; ++i) { var value = parameterValueSet.FetchValue(i); if (value is string v) { var newVal = ReplaceWildcardCharacters(v, projectionOwnerName, currentPAS); if (newVal != value) { parameterValueSet.SetParameterValue(resOpt, parameterValueSet.FetchParameterAtIndex(i).GetName(), newVal); } } } } } }
/// <summary> /// Creates a resolved copy of the entity. /// </summary> public async Task <CdmEntityDefinition> CreateResolvedEntityAsync(string newEntName, ResolveOptions resOpt = null, CdmFolderDefinition folder = null, string newDocName = null) { if (resOpt == null) { resOpt = new ResolveOptions(this, this.Ctx.Corpus.DefaultResolutionDirectives); } if (resOpt.WrtDoc == null) { Logger.Error(nameof(CdmEntityDefinition), this.Ctx as ResolveContext, $"No WRT document was supplied.", nameof(CreateResolvedEntityAsync)); return(null); } if (string.IsNullOrEmpty(newEntName)) { Logger.Error(nameof(CdmEntityDefinition), this.Ctx as ResolveContext, $"No Entity Name provided.", nameof(CreateResolvedEntityAsync)); return(null); } // if the wrtDoc needs to be indexed (like it was just modified) then do that first if (!await resOpt.WrtDoc.IndexIfNeeded(resOpt, true)) { Logger.Error(nameof(CdmEntityDefinition), this.Ctx as ResolveContext, $"Couldn't index source document.", nameof(CreateResolvedEntityAsync)); return(null); } if (folder == null) { folder = this.InDocument.Folder; } string fileName = (string.IsNullOrEmpty(newDocName)) ? $"{newEntName}.cdm.json" : newDocName; string origDoc = this.InDocument.AtCorpusPath; // Don't overwite the source document string targetAtCorpusPath = $"{this.Ctx.Corpus.Storage.CreateAbsoluteCorpusPath(folder.AtCorpusPath, folder)}{fileName}"; if (StringUtils.EqualsWithIgnoreCase(targetAtCorpusPath, origDoc)) { Logger.Error(nameof(CdmEntityDefinition), this.Ctx as ResolveContext, $"Attempting to replace source entity's document '{targetAtCorpusPath}'", nameof(CreateResolvedEntityAsync)); return(null); } // make the top level attribute context for this entity // for this whole section where we generate the attribute context tree and get resolved attributes // set the flag that keeps all of the parent changes and document dirty from from happening bool wasResolving = this.Ctx.Corpus.isCurrentlyResolving; this.Ctx.Corpus.isCurrentlyResolving = true; string entName = newEntName; ResolveContext ctx = this.Ctx as ResolveContext; CdmAttributeContext attCtxEnt = ctx.Corpus.MakeObject <CdmAttributeContext>(CdmObjectType.AttributeContextDef, entName, true); attCtxEnt.Ctx = ctx; attCtxEnt.InDocument = this.InDocument; // cheating a bit to put the paths in the right place AttributeContextParameters acp = new AttributeContextParameters { under = attCtxEnt, type = CdmAttributeContextType.AttributeGroup, Name = "attributeContext" }; CdmAttributeContext attCtxAC = CdmAttributeContext.CreateChildUnder(resOpt, acp); AttributeContextParameters acpEnt = new AttributeContextParameters { under = attCtxAC, type = CdmAttributeContextType.Entity, Name = entName, Regarding = ctx.Corpus.MakeObject <CdmObject>(CdmObjectType.EntityRef, this.GetName(), true) }; // use this whenever we need to keep references pointing at things that were already found. used when 'fixing' references by localizing to a new document ResolveOptions resOptCopy = CdmObjectBase.CopyResolveOptions(resOpt); resOptCopy.SaveResolutionsOnCopy = true; // resolve attributes with this context. the end result is that each resolved attribute // points to the level of the context where it was created var ras = this.FetchResolvedAttributes(resOptCopy, acpEnt); if (ras == null) { this.resolvingAttributes = false; return(null); } // create a new copy of the attribute context for this entity HashSet <CdmAttributeContext> allAttCtx = new HashSet <CdmAttributeContext>(); CdmAttributeContext newNode = attCtxEnt.CopyNode(resOpt) as CdmAttributeContext; attCtxEnt = attCtxEnt.CopyAttributeContextTree(resOpt, newNode, ras, allAttCtx, "resolvedFrom"); CdmAttributeContext attCtx = (attCtxEnt.Contents[0] as CdmAttributeContext).Contents[0] as CdmAttributeContext; this.Ctx.Corpus.isCurrentlyResolving = wasResolving; // make a new document in given folder if provided or the same folder as the source entity folder.Documents.Remove(fileName); CdmDocumentDefinition docRes = folder.Documents.Add(fileName); // add a import of the source document origDoc = this.Ctx.Corpus.Storage.CreateRelativeCorpusPath(origDoc, docRes); // just in case we missed the prefix docRes.Imports.Add(origDoc, "resolvedFrom"); // make the empty entity CdmEntityDefinition entResolved = docRes.Definitions.Add(entName); // set the context to the copy of the tree. fix the docs on the context nodes entResolved.AttributeContext = attCtx; attCtx.Visit($"{entName}/attributeContext/", new VisitCallback { Invoke = (obj, path) => { obj.InDocument = docRes; return(false); } }, null); // add the traits of the entity ResolvedTraitSet rtsEnt = this.FetchResolvedTraits(resOpt); rtsEnt.Set.ForEach(rt => { var traitRef = CdmObjectBase.ResolvedTraitToTraitRef(resOptCopy, rt); (entResolved as CdmObjectDefinition).ExhibitsTraits.Add(traitRef); }); // the attributes have been named, shaped, etc for this entity so now it is safe to go and // make each attribute context level point at these final versions of attributes IDictionary <string, int> attPath2Order = new Dictionary <string, int>(); Action <ResolvedAttributeSet, string> pointContextAtResolvedAtts = null; pointContextAtResolvedAtts = (rasSub, path) => { rasSub.Set.ForEach(ra => { List <CdmAttributeContext> raCtxInEnt = new List <CdmAttributeContext>(); HashSet <CdmAttributeContext> raCtxSet = null; rasSub.Ra2attCtxSet.TryGetValue(ra, out raCtxSet); // find the correct attCtx for this copy, intersect these two sets // (iterate over the shortest list) if (allAttCtx.Count < raCtxSet.Count) { foreach (CdmAttributeContext currAttCtx in allAttCtx) { if (raCtxSet.Contains(currAttCtx)) { raCtxInEnt.Add(currAttCtx); } } } else { foreach (CdmAttributeContext currAttCtx in raCtxSet) { if (allAttCtx.Contains(currAttCtx)) { raCtxInEnt.Add(currAttCtx); } } } foreach (CdmAttributeContext raCtx in raCtxInEnt) { var refs = raCtx.Contents; // there might be more than one explanation for where and attribute came from when things get merges as they do // this won't work when I add the structured attributes to avoid name collisions string attRefPath = path + ra.ResolvedName; if ((ra.Target as CdmAttribute)?.GetType().GetMethod("GetObjectType") != null) { if (!attPath2Order.ContainsKey(attRefPath)) { var attRef = this.Ctx.Corpus.MakeObject <CdmObjectReferenceBase>(CdmObjectType.AttributeRef, attRefPath, true); // only need one explanation for this path to the insert order attPath2Order.Add(attRef.NamedReference, ra.InsertOrder); refs.Add(attRef); } } else { attRefPath += "/members/"; pointContextAtResolvedAtts(ra.Target as ResolvedAttributeSet, attRefPath); } } }); }; pointContextAtResolvedAtts(ras, entName + "/hasAttributes/"); // generated attribute structures may end up with 0 attributes after that. prune them Func <CdmObject, bool, bool> CleanSubGroup = null; CleanSubGroup = (subItem, underGenerated) => { if (subItem.ObjectType == CdmObjectType.AttributeRef) { return(true); // not empty } CdmAttributeContext ac = subItem as CdmAttributeContext; if (ac.Type == CdmAttributeContextType.GeneratedSet) { underGenerated = true; } if (ac.Contents == null || ac.Contents.Count == 0) { return(false); // empty } // look at all children, make a set to remove List <CdmAttributeContext> toRemove = new List <CdmAttributeContext>(); foreach (var subSub in ac.Contents) { if (CleanSubGroup(subSub, underGenerated) == false) { bool potentialTarget = underGenerated; if (potentialTarget == false) { // cast is safe because we returned false meaning empty and not a attribute ref // so is this the set holder itself? potentialTarget = (subSub as CdmAttributeContext).Type == CdmAttributeContextType.GeneratedSet; } if (potentialTarget) { toRemove.Add(subSub as CdmAttributeContext); } } } foreach (var toDie in toRemove) { ac.Contents.Remove(toDie); } return(ac.Contents.Count != 0); }; CleanSubGroup(attCtx, false); Func <CdmAttributeContext, int?> orderContents = null; // create an all-up ordering of attributes at the leaves of this tree based on insert order // sort the attributes in each context by their creation order and mix that with the other sub-contexts that have been sorted Func <CdmObject, int?> getOrderNum = (item) => { if (item.ObjectType == CdmObjectType.AttributeContextDef && orderContents != null) { return(orderContents(item as CdmAttributeContext)); } else if (item.ObjectType == CdmObjectType.AttributeRef) { string attName = (item as CdmAttributeReference).NamedReference; int o = attPath2Order[attName]; return(o); } else { return(-1); // put the mystery item on top. } }; orderContents = (CdmAttributeContext under) => { if (under.LowestOrder == null) { under.LowestOrder = -1; // used for group with nothing but traits if (under.Contents.Count == 1) { under.LowestOrder = getOrderNum(under.Contents[0]); } else { under.Contents.AllItems.Sort((l, r) => { int lNum = -1; int rNum = -1; int?aux; aux = getOrderNum(l); if (aux != null) { lNum = (int)aux; } aux = getOrderNum(r); if (aux != null) { rNum = (int)aux; } if (lNum != -1 && (under.LowestOrder == -1 || lNum < under.LowestOrder)) { under.LowestOrder = lNum; } if (rNum != -1 && (under.LowestOrder == -1 || rNum < under.LowestOrder)) { under.LowestOrder = rNum; } return(lNum - rNum); }); } } return(under.LowestOrder); }; orderContents((CdmAttributeContext)attCtx); // resolved attributes can gain traits that are applied to an entity when referenced // since these traits are described in the context, it is redundant and messy to list them in the attribute // so, remove them. create and cache a set of names to look for per context // there is actually a hierarchy to all attributes from the base entity should have all traits applied independently of the // sub-context they come from. Same is true of attribute entities. so do this recursively top down var ctx2traitNames = new Dictionary <CdmAttributeContext, HashSet <string> >(); Action <CdmAttributeContext, HashSet <string> > collectContextTraits = null; collectContextTraits = (subAttCtx, inheritedTraitNames) => { var traitNamesHere = new HashSet <string>(inheritedTraitNames); var traitsHere = subAttCtx.ExhibitsTraits; if (traitsHere != null) { foreach (var tat in traitsHere) { traitNamesHere.Add(tat.NamedReference); } } ctx2traitNames.Add(subAttCtx, traitNamesHere); subAttCtx.Contents.AllItems.ForEach((cr) => { if (cr.ObjectType == CdmObjectType.AttributeContextDef) { // do this for all types? collectContextTraits(cr as CdmAttributeContext, traitNamesHere); } }); }; collectContextTraits(attCtx, new HashSet <string>()); // add the attributes, put them in attribute groups if structure needed IDictionary <ResolvedAttribute, string> resAtt2RefPath = new Dictionary <ResolvedAttribute, string>(); Action <ResolvedAttributeSet, dynamic, string> addAttributes = null; addAttributes = (rasSub, container, path) => { rasSub.Set.ForEach(ra => { string attPath = path + ra.ResolvedName; // use the path of the context associated with this attribute to find the new context that matches on path HashSet <CdmAttributeContext> raCtxSet = null; rasSub.Ra2attCtxSet.TryGetValue(ra, out raCtxSet); CdmAttributeContext raCtx = null; // find the correct attCtx for this copy // (interate over the shortest list) if (allAttCtx.Count < raCtxSet.Count) { foreach (CdmAttributeContext currAttCtx in allAttCtx) { if (raCtxSet.Contains(currAttCtx)) { raCtx = currAttCtx; break; } } } else { foreach (CdmAttributeContext currAttCtx in raCtxSet) { if (allAttCtx.Contains(currAttCtx)) { raCtx = currAttCtx; break; } } } if (ra.Target is ResolvedAttributeSet) { // this is a set of attributes. // make an attribute group to hold them CdmAttributeGroupDefinition attGrp = this.Ctx.Corpus.MakeObject <CdmAttributeGroupDefinition>(CdmObjectType.AttributeGroupDef, ra.ResolvedName, false); attGrp.AttributeContext = this.Ctx.Corpus.MakeObject <CdmAttributeContextReference>(CdmObjectType.AttributeContextRef, raCtx.AtCorpusPath, true); // take any traits from the set and make them look like traits exhibited by the group HashSet <string> avoidSet = ctx2traitNames[raCtx]; ResolvedTraitSet rtsAtt = ra.ResolvedTraits; rtsAtt.Set.ForEach(rt => { if (rt.Trait.Ugly != true) { // don't mention your ugly traits if (avoidSet?.Contains(rt.TraitName) != true) { // avoid the ones from the context var traitRef = CdmObjectBase.ResolvedTraitToTraitRef(resOptCopy, rt); (attGrp as CdmObjectDefinitionBase).ExhibitsTraits.Add(traitRef); } } }); // wrap it in a reference and then recurse with this as the new container CdmAttributeGroupReference attGrpRef = this.Ctx.Corpus.MakeObject <CdmAttributeGroupReference>(CdmObjectType.AttributeGroupRef, null, false); attGrpRef.ExplicitReference = attGrp; container.AddAttributeDef(attGrpRef); // isn't this where ... addAttributes(ra.Target as ResolvedAttributeSet, attGrp, attPath + "/members/"); } else { CdmTypeAttributeDefinition att = this.Ctx.Corpus.MakeObject <CdmTypeAttributeDefinition>(CdmObjectType.TypeAttributeDef, ra.ResolvedName, false); att.AttributeContext = this.Ctx.Corpus.MakeObject <CdmAttributeContextReference>(CdmObjectType.AttributeContextRef, raCtx.AtCorpusPath, true); HashSet <string> avoidSet = ctx2traitNames[raCtx]; ResolvedTraitSet rtsAtt = ra.ResolvedTraits; rtsAtt.Set.ForEach(rt => { if (rt.Trait.Ugly != true) { // don't mention your ugly traits if (avoidSet?.Contains(rt.TraitName) != true) { // avoid the ones from the context var traitRef = CdmObjectBase.ResolvedTraitToTraitRef(resOptCopy, rt); ((CdmTypeAttributeDefinition)att).AppliedTraits.Add(traitRef); } } }); // none of the dataformat traits have the bit set that will make them turn into a property // this is intentional so that the format traits make it into the resolved object // but, we still want a guess as the data format, so get it and set it. var impliedDataFormat = att.DataFormat; if (impliedDataFormat != CdmDataFormat.Unknown) { att.DataFormat = impliedDataFormat; } container.AddAttributeDef(att); resAtt2RefPath[ra] = attPath; } }); }; addAttributes(ras, entResolved, entName + "/hasAttributes/"); // any resolved traits that hold arguments with attribute refs should get 'fixed' here Action <CdmTraitReference, string, bool> replaceTraitAttRef = (tr, entityHint, isAttributeContext) => { if (tr.Arguments != null) { foreach (CdmArgumentDefinition arg in tr.Arguments.AllItems) { dynamic v = arg.UnResolvedValue != null ? arg.UnResolvedValue : arg.Value; // is this an attribute reference? if (v != null && (v as CdmObject)?.ObjectType == CdmObjectType.AttributeRef) { // only try this if the reference has no path to it (only happens with intra-entity att refs) var attRef = v as CdmAttributeReference; if (!string.IsNullOrEmpty(attRef.NamedReference) && attRef.NamedReference.IndexOf('/') == -1) { if (arg.UnResolvedValue == null) { arg.UnResolvedValue = arg.Value; } // give a promise that can be worked out later. assumption is that the attribute must come from this entity. var newAttRef = this.Ctx.Corpus.MakeRef <CdmAttributeReference>(CdmObjectType.AttributeRef, entityHint + "/(resolvedAttributes)/" + attRef.NamedReference, true); // inDocument is not propagated during resolution, so set it here newAttRef.InDocument = arg.InDocument; arg.Value = newAttRef; } } } } }; // fix entity traits if (entResolved.ExhibitsTraits != null) { foreach (var et in entResolved.ExhibitsTraits) { replaceTraitAttRef(et, newEntName, false); } } // fix context traits Action <CdmAttributeContext, string> fixContextTraits = null; fixContextTraits = (subAttCtx, entityHint) => { var traitsHere = subAttCtx.ExhibitsTraits; if (traitsHere != null) { foreach (var tr in traitsHere) { replaceTraitAttRef(tr, entityHint, true); } } subAttCtx.Contents.AllItems.ForEach((cr) => { if (cr.ObjectType == CdmObjectType.AttributeContextDef) { // if this is a new entity context, get the name to pass along CdmAttributeContext subSubAttCtx = (CdmAttributeContext)cr; string subEntityHint = entityHint; if (subSubAttCtx.Type == CdmAttributeContextType.Entity) { subEntityHint = subSubAttCtx.Definition.NamedReference; } // do this for all types fixContextTraits(subSubAttCtx, subEntityHint); } }); }; fixContextTraits(attCtx, newEntName); // and the attribute traits var entAttributes = entResolved.Attributes; if (entAttributes != null) { foreach (var entAtt in entAttributes) { CdmTraitCollection attTraits = entAtt.AppliedTraits; if (attTraits != null) { foreach (var tr in attTraits) { replaceTraitAttRef(tr, newEntName, false); } } } } // we are about to put this content created in the context of various documents (like references to attributes from base entities, etc.) // into one specific document. all of the borrowed refs need to work. so, re-write all string references to work from this new document // the catch-22 is that the new document needs these fixes done before it can be used to make these fixes. // the fix needs to happen in the middle of the refresh // trigger the document to refresh current content into the resolved OM attCtx.Parent = null; // remove the fake parent that made the paths work ResolveOptions resOptNew = CdmObjectBase.CopyResolveOptions(resOpt); resOptNew.LocalizeReferencesFor = docRes; resOptNew.WrtDoc = docRes; if (!await docRes.RefreshAsync(resOptNew)) { Logger.Error(nameof(CdmEntityDefinition), this.Ctx as ResolveContext, $"Failed to index the resolved document.", nameof(CreateResolvedEntityAsync)); return(null); } // get a fresh ref entResolved = docRes.FetchObjectFromDocumentPath(entName, resOptNew) as CdmEntityDefinition; this.Ctx.Corpus.resEntMap[this.AtCorpusPath] = entResolved.AtCorpusPath; return(entResolved); }
internal override ResolvedTraitSet FetchResolvedTraits(ResolveOptions resOpt = null) { if (resOpt == null) { resOpt = new ResolveOptions(this, this.Ctx.Corpus.DefaultResolutionDirectives); } const string kind = "rtsb"; ResolveContext ctx = this.Ctx as ResolveContext; // this may happen 0, 1 or 2 times. so make it fast CdmTraitDefinition baseTrait = null; ResolvedTraitSet baseRts = null; List <dynamic> baseValues = null; System.Action GetBaseInfo = () => { if (this.ExtendsTrait != null) { baseTrait = this.ExtendsTrait.FetchObjectDefinition <CdmTraitDefinition>(resOpt); if (baseTrait != null) { baseRts = this.ExtendsTrait.FetchResolvedTraits(resOpt); if (baseRts?.Size == 1) { ParameterValueSet basePv = baseRts.Get(baseTrait)?.ParameterValues; if (basePv != null) { baseValues = basePv.Values; } } } } }; // see if one is already cached // if this trait has parameters, then the base trait found through the reference might be a different reference // because trait references are unique per argument value set. so use the base as a part of the cache tag // since it is expensive to figure out the extra tag, cache that too! if (this.BaseIsKnownToHaveParameters == null) { GetBaseInfo(); // is a cache tag needed? then make one this.BaseIsKnownToHaveParameters = false; if (baseValues?.Count > 0) { this.BaseIsKnownToHaveParameters = true; } } string cacheTagExtra = ""; if (this.BaseIsKnownToHaveParameters == true) { cacheTagExtra = this.ExtendsTrait.Id.ToString(); } string cacheTag = ctx.Corpus.CreateDefinitionCacheTag(resOpt, this, kind, cacheTagExtra); dynamic rtsResultDynamic = null; if (cacheTag != null) { ctx.Cache.TryGetValue(cacheTag, out rtsResultDynamic); } ResolvedTraitSet rtsResult = rtsResultDynamic as ResolvedTraitSet; // store the previous reference symbol set, we will need to add it with // children found from the constructResolvedTraits call SymbolSet currSymbolRefSet = resOpt.SymbolRefSet; if (currSymbolRefSet == null) { currSymbolRefSet = new SymbolSet(); } resOpt.SymbolRefSet = new SymbolSet(); // if not, then make one and save it if (rtsResult == null) { GetBaseInfo(); if (baseTrait != null) { // get the resolution of the base class and use the values as a starting point for this trait's values if (!this.HasSetFlags) { // inherit these flags if (this.Elevated == null) { this.Elevated = baseTrait.Elevated; } if (this.Ugly == null) { this.Ugly = baseTrait.Ugly; } if (this.AssociatedProperties == null) { this.AssociatedProperties = baseTrait.AssociatedProperties; } } } this.HasSetFlags = true; ParameterCollection pc = this.FetchAllParameters(resOpt); List <dynamic> av = new List <dynamic>(); List <bool> wasSet = new List <bool>(); this.ThisIsKnownToHaveParameters = pc.Sequence.Count > 0; for (int i = 0; i < pc.Sequence.Count; i++) { // either use the default value or (higher precidence) the value taken from the base reference dynamic value = (pc.Sequence[i] as CdmParameterDefinition).DefaultValue; dynamic baseValue = null; if (baseValues != null && i < baseValues.Count) { baseValue = baseValues[i]; if (baseValue != null) { value = baseValue; } } av.Add(value); wasSet.Add(false); } // save it ResolvedTrait resTrait = new ResolvedTrait(this, pc, av, wasSet); rtsResult = new ResolvedTraitSet(resOpt); rtsResult.Merge(resTrait, false); // register set of possible symbols ctx.Corpus.RegisterDefinitionReferenceSymbols(this.FetchObjectDefinition <CdmObjectDefinitionBase>(resOpt), kind, resOpt.SymbolRefSet); // get the new cache tag now that we have the list of docs cacheTag = ctx.Corpus.CreateDefinitionCacheTag(resOpt, this, kind, cacheTagExtra); if (!string.IsNullOrWhiteSpace(cacheTag)) { ctx.Cache[cacheTag] = rtsResult; } } else { // cache found // get the SymbolSet for this cached object string key = CdmCorpusDefinition.CreateCacheKeyFromObject(this, kind); ((CdmCorpusDefinition)ctx.Corpus).DefinitionReferenceSymbols.TryGetValue(key, out SymbolSet tempSymbolRefSet); resOpt.SymbolRefSet = tempSymbolRefSet; } // merge child document set with current currSymbolRefSet.Merge(resOpt.SymbolRefSet); resOpt.SymbolRefSet = currSymbolRefSet; return(rtsResult); }
internal override ResolvedTraitSet FetchResolvedTraits(ResolveOptions resOpt = null) { if (resOpt == null) { resOpt = new ResolveOptions(this, this.Ctx.Corpus.DefaultResolutionDirectives); } const string kind = "rtsb"; var ctx = this.Ctx as ResolveContext; // get referenced trait var trait = this.FetchObjectDefinition <CdmTraitDefinition>(resOpt); ResolvedTraitSet rtsTrait = null; if (trait == null) { return(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 cacheByPath = true; if (trait.ThisIsKnownToHaveParameters != null) { cacheByPath = !((bool)trait.ThisIsKnownToHaveParameters); } string cacheTag = ctx.Corpus.CreateDefinitionCacheTag(resOpt, this, kind, "", cacheByPath, trait.AtCorpusPath); 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 argument in this.Arguments) { paramFound = param.ResolveParameter(iArg, argument.Name); argument.ResolvedParameter = paramFound; aValue = argument.Value; aValue = ctx.Corpus.ConstTypeCheck(resOpt, this.InDocument, paramFound, aValue); argument.Value = aValue; iArg++; } } } if (this.Arguments != null) { foreach (CdmArgumentDefinition a in this.Arguments) { rtsResult.SetParameterValueFromArgument(trait, a); } } } // register set of possible symbols ctx.Corpus.RegisterDefinitionReferenceSymbols(this.FetchObjectDefinition <CdmObjectDefinition>(resOpt), kind, resOpt.SymbolRefSet); // get the new cache tag now that we have the list of symbols cacheTag = ctx.Corpus.CreateDefinitionCacheTag(resOpt, this, kind, "", cacheByPath, trait.AtCorpusPath); 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); 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); }
private RelationshipInfo GetRelationshipInfo(ResolveOptions resOpt, AttributeResolutionContext arc) { ResolvedTraitSet rts = null; bool noMaxDepth = false; bool hasRef = false; bool isByRef = false; bool isArray = false; bool selectsOne = false; int? nextDepth = null; bool maxDepthExceeded = false; if (arc != null && arc.ResGuide != null) { if (arc.ResGuide.entityByReference != null && arc.ResGuide.entityByReference.allowReference == true) { hasRef = true; } if (arc.ResOpt.Directives != null) { noMaxDepth = arc.ResOpt.Directives.Has("noMaxDepth"); // based on directives if (hasRef) { isByRef = arc.ResOpt.Directives.Has("referenceOnly"); } selectsOne = arc.ResOpt.Directives.Has("selectOne"); isArray = arc.ResOpt.Directives.Has("isArray"); } // figure out the depth for the next level int?oldDepth = resOpt.RelationshipDepth; nextDepth = oldDepth; // if this is a 'selectone', then skip counting this entity in the depth, else count it if (!selectsOne) { // if already a ref, who cares? if (!isByRef) { if (nextDepth == null) { nextDepth = 1; } else { nextDepth++; } // max comes from settings but may not be set int maxDepth = 2; if (hasRef && arc.ResGuide.entityByReference.referenceOnlyAfterDepth != null) { maxDepth = (int)arc.ResGuide.entityByReference.referenceOnlyAfterDepth; } if (noMaxDepth) { maxDepth = 32; // no max? really? what if we loop forever? if you need more than 32 nested entities, then you should buy a different metadata description system. } if (nextDepth > maxDepth) { // don't do it isByRef = true; maxDepthExceeded = true; } } } } return(new RelationshipInfo { Rts = rts, IsByRef = isByRef, IsArray = isArray, SelectsOne = selectsOne, NextDepth = nextDepth, MaxDepthExceeded = maxDepthExceeded }); }
internal override ResolvedTraitSet FetchResolvedTraits(ResolveOptions resOpt = null) { if (resOpt == null) { resOpt = new ResolveOptions(this, this.Ctx.Corpus.DefaultResolutionDirectives); } const string kind = "rtsb"; var ctx = this.Ctx as ResolveContext; // get referenced trait var trait = this.FetchObjectDefinition <CdmTraitDefinition>(resOpt); ResolvedTraitSet rtsTrait = null; if (trait == null) { return(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); } ResolvedTraitSet rtsResult = null; // store the previous reference symbol set, we will need to add it with // children found from the constructResolvedTraits call SymbolSet currSymRefSet = resOpt.SymbolRefSet ?? new SymbolSet(); resOpt.SymbolRefSet = new SymbolSet(); // 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); int argumentIndex = 0; foreach (CdmArgumentDefinition argument in this.Arguments) { CdmParameterDefinition paramFound = param.ResolveParameter(argumentIndex, argument.Name); argument.ResolvedParameter = paramFound; argument.Value = paramFound.ConstTypeCheck(resOpt, this.InDocument, argument.Value); argumentIndex++; } } foreach (CdmArgumentDefinition argument in this.Arguments) { rtsResult.SetParameterValueFromArgument(trait, argument); } } // register set of possible symbols ctx.Corpus.RegisterDefinitionReferenceSymbols(this.FetchObjectDefinition <CdmObjectDefinition>(resOpt), kind, resOpt.SymbolRefSet); // merge child document set with current currSymRefSet.Merge(resOpt.SymbolRefSet); resOpt.SymbolRefSet = currSymRefSet; return(rtsResult); }
internal override ResolvedTraitSet FetchResolvedTraits(ResolveOptions resOpt = null) { if (resOpt == null) { resOpt = new ResolveOptions(this); } if (this.NamedReference != null && this.AppliedTraits == null) { const string kind = "rts"; ResolveContext ctx = this.Ctx as ResolveContext; string objDefName = this.FetchObjectDefinitionName(); string cacheTag = ((CdmCorpusDefinition)ctx.Corpus).CreateDefinitionCacheTag(resOpt, this, kind, "", true); dynamic rtsResultDynamic = null; if (cacheTag != null) { ctx.Cache.TryGetValue(cacheTag, out rtsResultDynamic); } ResolvedTraitSet rtsResult = rtsResultDynamic as ResolvedTraitSet; // store the previous document 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 (rtsResult == null) { CdmObjectDefinition objDef = this.FetchObjectDefinition <CdmObjectDefinition>(resOpt); if (objDef != null) { rtsResult = (objDef as CdmObjectDefinitionBase).FetchResolvedTraits(resOpt); if (rtsResult != null) { rtsResult = rtsResult.DeepCopy(); } // register set of possible docs ((CdmCorpusDefinition)ctx.Corpus).RegisterDefinitionReferenceSymbols(objDef, kind, resOpt.SymbolRefSet); // get the new cache tag now that we have the list of docs cacheTag = ((CdmCorpusDefinition)ctx.Corpus).CreateDefinitionCacheTag(resOpt, this, kind, "", true); 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); } else { return(base.FetchResolvedTraits(resOpt)); } }
private RelationshipInfo GetRelationshipInfo(ResolveOptions resOpt, AttributeResolutionContext arc) { ResolvedTraitSet rts = null; bool hasRef = false; bool isByRef = false; bool isArray = false; bool selectsOne = false; int? nextDepth = null; bool maxDepthExceeded = false; if (arc != null && arc.ResGuide != null) { if (arc.ResGuide.entityByReference != null && arc.ResGuide.entityByReference.allowReference == true) hasRef = true; if (arc.ResOpt.Directives != null) { // based on directives if (hasRef) isByRef = arc.ResOpt.Directives.Has("referenceOnly"); selectsOne = arc.ResOpt.Directives.Has("selectOne"); isArray = arc.ResOpt.Directives.Has("isArray"); } // figure out the depth for the next level int? oldDepth = resOpt.RelationshipDepth; nextDepth = oldDepth; // if this is a 'selectone', then skip counting this entity in the depth, else count it if (!selectsOne) { // if already a ref, who cares? if (!isByRef) { if (nextDepth == null) nextDepth = 1; else nextDepth++; // max comes from settings but may not be set int maxDepth = 2; if (hasRef && arc.ResGuide.entityByReference.referenceOnlyAfterDepth != null) maxDepth = (int)arc.ResGuide.entityByReference.referenceOnlyAfterDepth; if (nextDepth > maxDepth) { // don't do it isByRef = true; maxDepthExceeded = true; } } } } return new RelationshipInfo { Rts = rts, IsByRef = isByRef, IsArray = isArray, SelectsOne = selectsOne, NextDepth = nextDepth, MaxDepthExceeded = maxDepthExceeded }; }
// 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; }
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); }
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); }