private static void FixEnumerationSelect(SchemaModel model, DomainStructure namespaces) { // predefined type is defined as a select between two enumerations. We implement selects as interfaces and enumerations // in C# can not implement interfaces var transportElement = model.Get <EntityDefinition>(e => e.Name == "IfcTransportElement").First(); var transportElementType = model.Get <EntityDefinition>(e => e.Name == "IfcTransportElementType").First(); var predefinedType = transportElement.Attributes.First(a => a.Name == "PredefinedType") as ExplicitAttribute; var typePredefinedType = transportElementType.Attributes.First(a => a.Name == "PredefinedType") as ExplicitAttribute; var select = predefinedType.Domain as SelectType; var enums = select.Selections.OfType <EnumerationType>().ToList(); var ns = namespaces.Domains.FirstOrDefault(d => d.Types.Any(t => t == enums[0].Name)); var singleEnum = model.New <EnumerationType>(transportElement.ParentSchema, e => { e.Name = "IfcTransportElementTypeEnum"; e.PersistanceName = e.Name; e.TypeId = model.LastTypeId + 1; e.Elements = new List <ExpressId>( enums.SelectMany(en => en.Elements).Distinct() ); }); predefinedType.Domain = singleEnum; typePredefinedType.Domain = singleEnum; ns.Types.Add(singleEnum.Name); // remove select and enumerations not used model.Remove(select); enums.ForEach(e => model.Remove(e)); }
private static void EnhanceNullStyleInIfc(SchemaModel model, DomainStructure structure) { var nullStyle = model.Get <EnumerationType>(n => n.Name == "IfcNullStyle").FirstOrDefault(); if (nullStyle == null) { return; } nullStyle.Name = "IfcNullStyleEnum"; nullStyle.PersistanceName = "IfcNullStyleEnum"; var defType = model.New <DefinedType>(nullStyle.ParentSchema, d => { d.Name = "IfcNullStyle"; d.PersistanceName = "IfcNullStyle"; d.Domain = nullStyle; }); var selects = model.Get <SelectType>(s => s.Selections.Contains(nullStyle)); foreach (var @select in selects) { select.Selections.Remove(nullStyle); select.Selections.Add(defType); } //adjust namespace var domain = structure.GetDomainForType("IfcNullStyle"); domain.Types.Add("IfcNullStyleEnum"); }
/// <summary> /// Use this method before you generate the source code to keep type IDs consistent even when you /// move the content in the EXPRESS file. When you rename an entity type in EXPRESS this will make /// sure that new Entity will have new ID unless you modify the ID file manually. /// </summary> /// <param name="model">Schema model</param> /// <param name="directory">Target directory</param> /// <returns></returns> private static int SetTypeNumbers(SchemaModel model, string directory) { var max = 0; var types = model.Get <NamedType>().ToList(); const string extension = "_TYPE_IDS.csv"; var ids = new Dictionary <string, int>(); var file = model.FirstSchema.Name + extension; var source = Path.Combine(directory, file); if (File.Exists(source)) { var data = File.ReadAllText(source); var kvps = data.Trim().Split('\n'); foreach (var vals in kvps.Select(kvp => kvp.Split(','))) { ids.Add(vals[0], int.Parse(vals[1])); } } //reset latest values foreach (var type in types.ToList()) { if (!ids.TryGetValue(type.PersistanceName, out int id)) { continue; } type.TypeId = id; max = Math.Max(max, id); types.Remove(type); } //set new values to the new types foreach (var type in types) { type.TypeId = ++max; } using (var o = File.CreateText(source)) { //save for the next processing foreach (var type in model.Get <NamedType>()) { o.Write("{0},{1}\n", type.PersistanceName, type.TypeId); } o.Close(); } return(max); }
private static void WriteElementTypes(SchemaModel schema, TextWriter w) { var element = schema.Get <EntityDefinition>(e => e.Name == "IfcElement").FirstOrDefault(); if (element == null) { throw new Exception(); } var elements = element.AllSubTypes.ToList(); elements.Add(element); foreach (var definition in elements) { //try to get just from name (IfcWall - IfcWallType) var type = GetCorrespondingType(definition); if (type != null) { w.WriteLine("{0},{1},{2}", definition.Name, definition.Instantiable ? "Concrete" : "Abstract", type); continue; } //no type match found w.WriteLine("{0},{1},{2}", definition.Name, definition.Instantiable ? "Concrete" : "Abstract", "no-type"); } }
private static void MoveEnumsToInterfaces(DomainStructure domains, SchemaModel model, string dir, string prj) { const string iName = "Interfaces"; var enums = model.Get <EnumerationType>(); var iDomain = domains.Domains.FirstOrDefault(d => d.Name == iName); if (iDomain == null) { iDomain = new Domain { Name = iName, Types = new List <string>() }; domains.Domains.Add(iDomain); } foreach (var enumeration in enums) { var enumName = enumeration.PersistanceName; var domain = domains.GetDomainForType(enumName); if (domain != null) { //remove from where it is in the documentation domain.Types.Remove(enumName); //remove old files if generated before var path = Path.Combine(dir, prj, domain.Name, enumName + ".cs"); if (File.Exists(path)) { File.Delete(path); } } //add to interfaces namespace iDomain.Types.Add(enumName); } }
public static bool GenerateSchema(GeneratorSettings settings, SchemaModel schema) { if (!Directory.Exists(settings.OutputPath)) { Directory.CreateDirectory(settings.OutputPath); } // make sure IDs are stable over regenerations SetTypeNumbers(schema, settings.OutputPath); //set schema IDs for this generation session settings.SchemasIds = schema.Schemas.Select(s => s.Identification); //set namespaces settings.Namespace = settings.OutputPath; if (!settings.Namespace.StartsWith("Xbim.")) { settings.Namespace = "Xbim." + settings.Namespace; } settings.InfrastructureNamespace = @"Xbim.Common"; var templates = new List <ICodeTemplate>(); templates.AddRange( schema.Get <DefinedType>().Select(type => new DefinedTypeTemplate(settings, type))); templates.AddRange(schema.Get <SelectType>().Select(type => new SelectTypeTemplate(settings, type))); templates.AddRange(schema.Get <EntityDefinition>().Select(type => new EntityInterfaceTemplate(settings, type))); templates.AddRange( schema.Get <EnumerationType>().Select(type => new EnumerationTemplate(settings, type))); // entity factory for this schema and any extensions templates.AddRange( schema.Schemas.Select(s => new EntityFactoryTemplate(settings, s))); //inner model infrastructure templates.Add(new ItemSetTemplate(settings)); templates.Add(new OptionalItemSetTemplate(settings)); //templates.ForEach(t => ProcessTemplate(t, settings.Namespace)); Parallel.ForEach(templates, tmpl => ProcessTemplate(tmpl, settings.Namespace)); return(true); }
private static void WriteElementTypesHierarchy(SchemaModel schema, TextWriter w) { var elementType = schema.Get <EntityDefinition>(e => e.Name == "IfcElementType").FirstOrDefault(); if (elementType == null) { throw new Exception(); } w.WriteLine("IfcElementType,"); WriteHierarchy(elementType, w); }
private EntityDefinition TryHandleBuildingElementMatch(EntityDefinition source, SchemaModel targetSchema) { if (string.Equals(source.Name, "IfcBuildingElement", StringComparison.OrdinalIgnoreCase)) { return(targetSchema.Get <EntityDefinition>(e => string.Equals(e.Name, "IfcBuiltElement", StringComparison.OrdinalIgnoreCase)) .FirstOrDefault()); } if (string.Equals(source.Name, "IfcBuildingElementType", StringComparison.OrdinalIgnoreCase)) { return(targetSchema.Get <EntityDefinition>(e => string.Equals(e.Name, "IfcBuiltElementType", StringComparison.OrdinalIgnoreCase)) .FirstOrDefault()); } if (string.Equals(source.Name, "IfcBuiltElement", StringComparison.OrdinalIgnoreCase)) { return(targetSchema.Get <EntityDefinition>(e => string.Equals(e.Name, "IfcBuildingElement", StringComparison.OrdinalIgnoreCase)) .FirstOrDefault()); } if (string.Equals(source.Name, "IfcBuiltElementType", StringComparison.OrdinalIgnoreCase)) { return(targetSchema.Get <EntityDefinition>(e => string.Equals(e.Name, "IfcBuildingElementType", StringComparison.OrdinalIgnoreCase)) .FirstOrDefault()); } return(null); }
/// <summary> /// Constructor will try to find matching definition from target schema /// </summary> /// <param name="source">Entity definition</param> /// <param name="targetSchema">Target schema where target entity definition should be found</param> public EntityDefinitionMatch(EntityDefinition source, SchemaModel targetSchema) { MatchName = string.Format("{0}To{1}", source.ParentSchema.Name, targetSchema.FirstSchema.Name); Source = source; //default state unless we find a match MatchType = EntityMatchType.NotFound; AttributeMatches = Enumerable.Empty <ExplicitAttributeMatch>(); //try to find identity var identity = targetSchema.Get <EntityDefinition>( e => string.Compare(source.Name, e.Name, StringComparison.InvariantCultureIgnoreCase) == 0).FirstOrDefault(); if (identity == null) { identity = TryHandleBuildingElementMatch(source, targetSchema); } if (identity != null) { Target = identity; var attrMatches = ExplicitAttributeMatch.FindMatches(Source, Target).ToList(); AttributeMatches = attrMatches; MatchType = attrMatches.Any(am => am.MatchType != AttributeMatchType.Identity)? EntityMatchType.Changed : EntityMatchType.Identity; if (!HasAllExplicitAttributes()) { throw new Exception(); } return; } //var superTypeMatch = GetSupertypeMatch(source, targetSchema); //if (superTypeMatch != null) //{ // Target = superTypeMatch; // MatchType = EntityMatchType.Changed; // AttributeMatches = ExplicitAttributeMatch.FindMatches(Source, Target).ToList(); // if (!HasAllExplicitAttributes()) // throw new Exception(); //} }
private static IEnumerable <Tuple <SelectType, SelectType> > GetSelectsToImplement(SchemaModel schema, SchemaModel remote, IEnumerable <EntityDefinitionMatch> matches) { var definitionMatches = matches as IList <EntityDefinitionMatch> ?? matches.ToList(); var targets = remote.Get <SelectType>().ToList(); foreach (var source in schema.Get <SelectType>()) { var target = targets.FirstOrDefault(t => t.Name == source.Name); if (target == null) { continue; } if (EntityInterfaceImplementation.IsSelectCompatible(source, target, definitionMatches)) { yield return(new Tuple <SelectType, SelectType>(source, target)); } } }
private static EntityDefinition GetSupertypeMatch(EntityDefinition entity, SchemaModel targetSchema) { while (true) { if (entity.Supertypes == null || !entity.Supertypes.Any()) { return(null); } var supertype = entity.Supertypes.First(); var candidate = targetSchema.Get <EntityDefinition>( e => string.Compare(supertype.Name, e.Name, StringComparison.InvariantCultureIgnoreCase) == 0).FirstOrDefault(); if (candidate != null) { return(candidate); } entity = supertype; } }
private static string Schema(SchemaModel model) { return(string.Join(", ", model.Get <SchemaDefinition>().Select(s => s.Name))); }
public static IEnumerable <EntityDefinitionMatch> GetMatches(SchemaModel a, SchemaModel b) { return(a.Get <EntityDefinition>().Select(entityDefinition => new EntityDefinitionMatch(entityDefinition, b))); }