/// <summary> /// Decorates a tactical data with the chosen metadata type if any. /// </summary> /// <param name="pToDecorate"></param> public MetadataSetType Decorate(IMetadatable pToDecorate) { if (pToDecorate == null) { return(null); } // Reset anyway. pToDecorate.Metadata = null; string lUserTypeName = pToDecorate.UserType; if (string.IsNullOrEmpty(lUserTypeName)) { this.Decorate(pToDecorate, null); return(null); } Type lTacticalDataType = pToDecorate.GetType(); Dictionary <string, MetadataSetType> lTypesById; if (sTacticalMetadataTypes.TryGetValue(lTacticalDataType.Name, out lTypesById)) { MetadataSetType lType; if (lTypesById.TryGetValue(lUserTypeName, out lType)) { // Recursion over nested type(s) this.Decorate(pToDecorate, lType); return(lType); } } return(null); }
/// <summary> /// Initializes a new instance of the <see cref="RoboCup.AtHome.CommandGenerator.Token"/> class. /// </summary> /// <param name="key">The original substring in the taks prototype string.</param> /// <param name="value">The replacement object for the wildcard represented by this Token.</param> /// <param name="metadata">Additional metadata to add (e.g. from the grammar /// or the taks prototype string).</param> public Token(string key, INameable value, IEnumerable <string> metadata) { this.key = key; this.value = value; this.metadata = new List <string>(); IMetadatable imvalue = value as IMetadatable; if (imvalue != null) { this.metadata.AddRange(imvalue.Metadata); } if (metadata != null) { this.metadata.AddRange(metadata); } }
/// <summary> /// Recursive fonction to decorate metadatable with metadata type(s). /// </summary> /// <param name="pToDecorate"></param> /// <param name="pType"></param> private void Decorate(IMetadatable pToDecorate, MetadataSetType pType) { // Cleaning metadata. pToDecorate.Metadata = null; // Cleaning the old type. MetadataSetType lOldType = pToDecorate.Type as MetadataSetType; if (lOldType != null) { foreach (MetadataSetType lNestedType in lOldType.NestedTypes) { object lPropertyValue = pToDecorate.GetPropertyValue(lNestedType.TargetType); if (lPropertyValue == null) { continue; } if (lPropertyValue is IEnumerable) // List of metadatable?? { IEnumerable lList = lPropertyValue as IEnumerable; foreach (object lItem in lList) { IMetadatable lMetadatable = lItem as IMetadatable; if (lMetadatable != null) { this.Decorate(lMetadatable, null); } } } else if (lPropertyValue is IMetadatable) // Metadatable. { IMetadatable lMetadatable = lPropertyValue as IMetadatable; this.Decorate(lMetadatable, null); } } } pToDecorate.Type = pType; // Applying the new type. if (pType != null) { foreach (MetadataSetType lNestedType in pType.NestedTypes) { object lPropertyValue = pToDecorate.GetPropertyValue(lNestedType.TargetType); if (lPropertyValue == null) { continue; } if (lPropertyValue is IEnumerable) // List of metadatable?? { IEnumerable lList = lPropertyValue as IEnumerable; foreach (object lItem in lList) { IMetadatable lMetadatable = lItem as IMetadatable; if (lMetadatable != null) { // Recurse for sub nested. this.Decorate(lMetadatable, lNestedType); } } } else if (lPropertyValue is IMetadatable) // Metadatable. { IMetadatable lMetadatable = lPropertyValue as IMetadatable; // Recurse for sub nested. this.Decorate(lMetadatable, lNestedType); } } } }