/// <summary> /// Check whether an entity (string) is a supertype of another entity /// </summary> /// <param name="context">the IFC version in context for the check</param> /// <param name="superTypeName">the supertype name</param> /// <param name="subTypeName">the subtype name</param> /// <param name="strict">whether the supertype is strictly supertype. Set to false if it "supertype == subtype" is acceptable</param> /// <returns>true if it is supertype</returns> static public bool IsSuperTypeOf(string context, string superTypeName, string subTypeName, bool strict = true) { IfcSchemaEntityTree ifcEntitySchemaTree = GetEntityDictFor(context); //var ifcEntitySchemaTree = IfcSchemaEntityTree.GetEntityDictFor(context); if (ifcEntitySchemaTree == null || ifcEntitySchemaTree.IfcEntityDict == null || ifcEntitySchemaTree.IfcEntityDict.Count == 0) { throw new Exception("Unable to locate IFC Schema xsd file! Make sure the relevant xsd " + context + " exists."); } IfcSchemaEntityNode theNode = ifcEntitySchemaTree.Find(superTypeName); if (theNode != null) { if (strict) { return(theNode.IsSuperTypeOf(subTypeName)); } else { return(theNode.Name.Equals(subTypeName, StringComparison.InvariantCultureIgnoreCase) || theNode.IsSuperTypeOf(subTypeName)); } } return(false); }
/// <summary> /// Find whether an entity is already created before /// </summary> /// <param name="entityName">the entity in concern</param> /// <returns>the entity node in the tree</returns> public IfcSchemaEntityNode Find(string entityName) { IfcSchemaEntityNode res = null; IfcEntityDict.TryGetValue(entityName, out res); return(res); }
/// <summary> /// Test whether the supertTypeName is the valid supertype of this entity /// </summary> /// <param name="superTypeName">the name of the potential supertype</param> /// <returns>true: is the valid supertype</returns> public bool IsSubTypeOf(string superTypeName, bool strict = true) { bool res = false; IfcSchemaEntityNode node = this; while (node.superType != null) { if (strict) { if (superTypeName.Equals(node.superType.Name, StringComparison.InvariantCultureIgnoreCase)) { return(true); } } else { if (superTypeName.Equals(node.superType.Name, StringComparison.InvariantCultureIgnoreCase) || superTypeName.Equals(node.Name, StringComparison.InvariantCultureIgnoreCase)) { return(true); } } node = node.superType; } return(res); }
/// <summary> /// Create the class with the information about the parent (supertype) /// </summary> /// <param name="nodeName">the entity name</param> /// <param name="parentNode">the supertype entity name</param> /// <param name="abstractEntity">optional: whether the entity is an abstract type (default is false)</param> public IfcSchemaEntityNode(string nodeName, IfcSchemaEntityNode parentNode, bool abstractEntity = false) { Name = nodeName; isAbstract = abstractEntity; if (parentNode != null) { superType = parentNode; } }
/// <summary> /// Check whether an entity is a supertype of another entity /// </summary> /// <param name="superTypeName">candidate of the supertype entity name</param> /// <param name="subTypeName">candidate of the subtype entity name</param> /// <returns>true: if the the superTypeName is the supertype of subtTypeName</returns> static public bool IsSuperTypeOf(string superTypeName, string subTypeName) { IfcSchemaEntityNode theNode = Find(superTypeName); if (theNode != null) { return(theNode.IsSuperTypeOf(subTypeName)); } return(false); }
/// <summary> /// Add the subtype node into this node /// </summary> /// <param name="childNode">the subtype entity node</param> public void AddChildNode(IfcSchemaEntityNode childNode) { if (childNode != null) { if (subType == null) { subType = new List <IfcSchemaEntityNode>(); } subType.Add(childNode); } }
/// <summary> /// Create the class with the information about the parent (supertype) /// </summary> /// <param name="nodeName">the entity name</param> /// <param name="parentNode">the supertype entity name</param> /// <param name="abstractEntity">optional: whether the entity is an abstract type (default is false)</param> public IfcSchemaEntityNode(string nodeName, IfcSchemaEntityNode parentNode, string predefTypeEnum, bool abstractEntity = false) { Name = nodeName; isAbstract = abstractEntity; if (parentNode != null) { superType = parentNode; } if (predefTypeEnum != null) { PredefinedType = predefTypeEnum; } }
/// <summary> /// Get all the supertype line of this entity /// </summary> /// <returns>the list of supertype following the level order</returns> public IList <IfcSchemaEntityNode> GetAllAncestors() { List <IfcSchemaEntityNode> res = new List <IfcSchemaEntityNode>(); IfcSchemaEntityNode node = this; while (node.superType != null) { res.Add(superType); node = superType; } return(res); }
/// <summary> /// Set the supertype node into this node /// </summary> /// <param name="parentNode">the supertype entity node</param> public void SetParentNode(IfcSchemaEntityNode parentNode) { if (parentNode == null) { throw new System.Exception("parentNode cannot be null!"); } if (superType == null) { if (parentNode != null) { superType = parentNode; } } }
/// <summary> /// Find a Non ABS supertype entity from the input type name /// </summary> /// <param name="context">the IFC schema context</param> /// <param name="typeName">the type name</param> /// <returns>the non-abs supertype instance node</returns> static public IfcSchemaEntityNode FindNonAbsInstanceSuperType(string context, string typeName) { IfcSchemaEntityTree ifcEntitySchemaTree = GetEntityDictFor(context); IfcSchemaEntityNode res = null; // Note: Implementer's agreement #CV-2x3-166 changes IfcSpaceHeaterType from IfcEnergyConversionDevice to IfcFlowTerminal. if (context.Equals(Ifc2x3Schema, StringComparison.InvariantCultureIgnoreCase) && typeName.Equals("IfcSpaceHeaterType", StringComparison.InvariantCultureIgnoreCase)) { res = ifcEntitySchemaTree.Find("IfcFlowTerminal"); if (res.isAbstract) { return(null); } return(res); } string theTypeName = typeName.Substring(typeName.Length - 4, 4).Equals("Type", StringComparison.CurrentCultureIgnoreCase) ? typeName : typeName + "Type"; IfcSchemaEntityNode entNode = ifcEntitySchemaTree.Find(theTypeName); if (entNode != null) { while (true) { res = entNode.GetParent(); // no more parent node to get if (res == null) { break; } entNode = ifcEntitySchemaTree.Find(res.Name.Substring(0, res.Name.Length - 4)); if (entNode != null && !entNode.isAbstract) { res = entNode; break; } else { entNode = res; // put back the Type Node } } } return(res); }
static public IList <IfcSchemaEntityNode> FindAllSuperTypes(string context, string entityName, params string[] stopNode) { IfcSchemaEntityTree ifcEntitySchemaTree = GetEntityDictFor(context); IList <IfcSchemaEntityNode> res = new List <IfcSchemaEntityNode>(); IfcSchemaEntityNode entNode = ifcEntitySchemaTree.Find(entityName); if (entNode != null) { // return the list when it reaches the stop node foreach (string stopCond in stopNode) { if (entNode.Name.Equals(stopCond, StringComparison.InvariantCultureIgnoreCase)) { return(res); } } bool continueSearch = true; while (continueSearch) { entNode = entNode.GetParent(); // no more parent node to get if (entNode == null) { break; } // Stop the search when it reaches the stop node foreach (string stopCond in stopNode) { if (entNode.Name.Equals(stopCond, StringComparison.InvariantCultureIgnoreCase)) { continueSearch = false; break; } } if (entNode != null) { res.Add(entNode); } } } return(res); }
/// <summary> /// Test whether the supertTypeName is the valid supertype of this entity /// </summary> /// <param name="superTypeName">the name of the potential supertype</param> /// <returns>true: is the valid supertype</returns> public bool IsSubTypeOf(string superTypeName) { bool res = false; IfcSchemaEntityNode node = this; while (node.superType != null) { if (string.Compare(superTypeName, node.superType.Name) == 0) { res = true; break; } node = node.superType; } return(res); }
/// <summary> /// Check whether an entity is a supertype of another entity /// </summary> /// <param name="superTypeName">candidate of the supertype entity name</param> /// <param name="subTypeName">candidate of the subtype entity name</param> /// <returns>true: if the the superTypeName is the supertype of subtTypeName</returns> static public bool IsSuperTypeOf(string superTypeName, string subTypeName, bool strict = true) { IfcSchemaEntityNode theNode = Find(superTypeName); if (theNode != null) { if (strict) { return(theNode.IsSuperTypeOf(subTypeName)); } else { return(theNode.Name.Equals(subTypeName, StringComparison.InvariantCultureIgnoreCase) || theNode.IsSuperTypeOf(subTypeName)); } } return(false); }
/// <summary> /// Find a Non-Abstract Super Type in the current IFC Schema /// </summary> /// <param name="context">the IFC schema context</param> /// <param name="typeName">the entity name</param> /// <param name="stopNode">optional list of entity name(s) to stop the search</param> /// <returns>the appropriate node or null</returns> static public IfcSchemaEntityNode FindNonAbsSuperType(string context, string entityName, params string[] stopNode) { IfcSchemaEntityTree ifcEntitySchemaTree = GetEntityDictFor(context); IfcSchemaEntityNode res = null; IfcSchemaEntityNode entNode = ifcEntitySchemaTree.Find(entityName); if (entNode != null) { foreach (string stopCond in stopNode) { if (entNode.Name.Equals(stopCond, StringComparison.InvariantCultureIgnoreCase)) { return(res); } } while (true) { entNode = entNode.GetParent(); // no more parent node to get if (entNode == null) { break; } foreach (string stopCond in stopNode) { if (entNode.Name.Equals(stopCond, StringComparison.InvariantCultureIgnoreCase)) { break; } } if (entNode != null && !entNode.isAbstract) { res = entNode; break; } } } return(res); }
/// <summary> /// Print the branch starting from this entity node. The print is formatted using tab indentation to represent the hierarchical level /// </summary> /// <param name="level">the level number</param> /// <returns>the tree structure of the banch in a string</returns> public string PrintBranch(int level = 0) { string res = string.Empty; // Print itself first and then followed by each subtypes IfcSchemaEntityNode node = this; string abs = string.Empty; if (node.isAbstract) { abs = " (ABS)"; } res += "\n"; for (int i = 0; i < level; ++i) { res += "\t"; } res += node.Name + abs; if (node.subType == null) { return(res); } foreach (IfcSchemaEntityNode sub in node.subType) { for (int i = 0; i < level; ++i) { res += "\t"; } string br = sub.PrintBranch(level + 1); if (!string.IsNullOrWhiteSpace(br)) { res += br; } } return(res); }
/// <summary> /// Check whether an entityName is found in this entity and its subtypes /// </summary> /// <param name="entityName">the entity name to check</param> /// <returns>true: the entityName is found in this entity or t**s subtype</returns> public bool CheckChildNode(string entityName) { IfcSchemaEntityNode node = this; if (string.Compare(node.Name, entityName, ignoreCase: true) == 0) { return(true); } if (node.subType == null) { return(false); } foreach (IfcSchemaEntityNode sub in node.subType) { if (sub.CheckChildNode(entityName)) { return(true); } } return(false); }
/// <summary> /// Get the entities in the branch starting of this entity node in a set /// </summary> /// <returns>a set that contains all the subtype entity names</returns> public HashSet <string> GetBranch() { HashSet <string> resSet = new HashSet <string>(); IfcSchemaEntityNode node = this; resSet.Add(node.Name); if (node.subType == null) { return(resSet); } foreach (IfcSchemaEntityNode sub in node.subType) { HashSet <string> br = sub.GetBranch(); if (br.Count > 0) { resSet.UnionWith(br); } } return(resSet); }
/// <summary> /// Add a new node into the tree /// </summary> /// <param name="entityName">the entity name</param> /// <param name="parentNodeName">the name of the supertype entity</param> public void Add(string entityName, string parentNodeName, string predefTypeEnum, bool isAbstract = false) { if (string.IsNullOrEmpty(entityName)) { return; } // We will skip the entityname or its parent name that does not start with Ifc (except Entity) if (string.Compare(entityName, 0, "Ifc", 0, 3, ignoreCase: true) != 0 || (string.Compare(parentNodeName, 0, "Ifc", 0, 3, ignoreCase: true) != 0 && string.Compare(parentNodeName, "Entity", ignoreCase: true) != 0)) { return; } IfcSchemaEntityNode parentNode = null; if (!string.IsNullOrEmpty(parentNodeName)) { // skip if the parent name does not start with Ifc if (string.Compare(parentNodeName, 0, "Ifc", 0, 3, ignoreCase: true) == 0) { if (!IfcEntityDict.TryGetValue(parentNodeName, out parentNode)) { // Parent node does not exist yet, create parentNode = new IfcSchemaEntityNode(parentNodeName); IfcEntityDict.Add(parentNodeName, parentNode); TheTree.Add(parentNode); // Add first into the rootNodes because the parent is null at this stage, we will remove it later is not the case } } } IfcSchemaEntityNode entityNode; if (!IfcEntityDict.TryGetValue(entityName, out entityNode)) { if (parentNode != null) { entityNode = new IfcSchemaEntityNode(entityName, parentNode, predefTypeEnum, abstractEntity: isAbstract); parentNode.AddChildNode(entityNode); } else { entityNode = new IfcSchemaEntityNode(entityName, abstractEntity: isAbstract); // Add into the set of root nodes when parent is null/no parent TheTree.Add(entityNode); } IfcEntityDict.Add(entityName, entityNode); } else { // Update the node's isAbstract property and the parent node (if any) entityNode.isAbstract = isAbstract; if (parentNode != null) { entityNode.SetParentNode(parentNode); if (TheTree.Contains(entityNode)) { TheTree.Remove(entityNode); } parentNode.AddChildNode(entityNode); } } }