IfcDoc.FormatOWL.FormatEntityFull C# (CSharp) Method

FormatEntityFull() public method

public FormatEntityFull ( DocEntity docEntity, DocObject>.Dictionary map, bool>.Dictionary included, bool fullListing ) : string
docEntity DocEntity
map DocObject>.Dictionary
included bool>.Dictionary
fullListing bool
return string
        public string FormatEntityFull(DocEntity docEntity, Dictionary<string, DocObject> map, Dictionary<DocObject, bool> included, bool fullListing)
        {
            // entity
            StringBuilder sb = new StringBuilder();
            // object properties
            StringBuilder sbProps = new StringBuilder();
            // lists
            StringBuilder sbLists = new StringBuilder();

            sb.AppendLine("ifc:" + docEntity.Name);

            // superclass
            if (docEntity.BaseDefinition != null)
            {
                DocEntity super = map[docEntity.BaseDefinition] as DocEntity;
                if (super != null)
                {
                    sb.AppendLine("\trdfs:subClassOf \tifc:" + super.Name + " ;");

                    // disjoint
                    if (subTypesOfEntity.ContainsKey(super.Name))
                    {
                        string tmp = "";
                        foreach (string subtype in subTypesOfEntity[super.Name])
                        {
                            if (subtype != docEntity.Name)
                            {
                                if (tmp.Length > 0)
                                    tmp += ", ";
                                tmp += ToOwlClass(subtype) + " ";
                            }
                        }
                        if (tmp.Length > 0)
                            sb.AppendLine("\towl:disjointWith " + tmp + ";");
                    }
                }
            }

            // abstract class
            if (docEntity.IsAbstract())
            {
                sb.AppendLine("\trdfs:subClassOf ");
                sb.AppendLine("\t\t[ ");
                sb.AppendLine("\t\t\trdf:type owl:Class ;");
                sb.Append("\t\t\towl:unionOf ( ");
                if (subTypesOfEntity.ContainsKey(docEntity.Name))
                {
                    foreach (string subtype in subTypesOfEntity[docEntity.Name])
                    {
                        sb.Append(ToOwlClass(subtype) + " ");
                    }
                }
                sb.Append(")");
                sb.AppendLine();
                sb.AppendLine("\t\t] ;");
            }

            // attributes -> create properties and restrictions
            foreach (DocAttribute docAttr in docEntity.Attributes)
            {
                // check if Attr must be skipped (1) - not included attribute
                if (included != null)
                    if (!included.ContainsKey(docAttr))
                        continue;

                string propname = "";
                string propfullname = "";
                string invpropname = "";
                string invpropfullname = "";

                string targetString = docAttr.DefinedType;
                DocEntity targetEntity = null;
                if (map.ContainsKey(targetString))
                {
                    targetEntity = map[targetString] as DocEntity;
                }

                // check if Attr must be skipped (2) - DERIVE attribute
                if (docAttr.Derived != null)
                    continue;

                // check if Attr must be skipped (3) - not manageable INVERSE attribute
                invpropname = docAttr.Inverse;
                if (invpropname != null)
                {
                    // check if there are problems with inverse and in this case skip the attribute!
                    // 1) the inverse is an inverse of two or more properties
                    // 2) a list/array is involved as range in the property or its inverse

                    // 1)
                    var key = new Tuple<string, string>(docAttr.Inverse, targetString);
                    if (attribInverses.ContainsKey(key))
                        if (attribInverses[key] > 1)
                            continue;

                    // 2.a)
                    if (docAttr.GetAggregation() == DocAggregationEnum.LIST || docAttr.GetAggregation() == DocAggregationEnum.ARRAY)
                        continue;
                    // 2.b)
                    if (targetEntity != null)
                    {
                        bool toBeSkipped = false;
                        foreach (DocAttribute docAttrInv in targetEntity.Attributes)
                        {
                            if (docAttrInv.Name == invpropname)
                                if (docAttrInv.GetAggregation() == DocAggregationEnum.LIST || docAttrInv.GetAggregation() == DocAggregationEnum.ARRAY)
                                {
                                    toBeSkipped = true;
                                    break;
                                }
                        }
                        if (toBeSkipped)
                            continue;
                    }
                }

                // set actual target
                string actualTargetString = ToOwlClass(targetString);
                if (docAttr.GetAggregation() == DocAggregationEnum.LIST || docAttr.GetAggregation() == DocAggregationEnum.ARRAY)
                {
                    string newlistdef = createListClass(actualTargetString);
                    actualTargetString = actualTargetString + "_List";
                    if (newlistdef.Length > 0)
                        sbLists.Append(newlistdef);

                    DocAttribute docAggregate = docAttr.AggregationAttribute;
                    while (docAggregate != null)
                    {
                        newlistdef = createListClass(actualTargetString);
                        actualTargetString = actualTargetString + "_List";
                        if (newlistdef.Length > 0) sbLists.Append(newlistdef);

                        docAggregate = docAggregate.AggregationAttribute;
                    }
                }

                // create property
                propname = docAttr.Name;
                propfullname = propname + "_" + docEntity.Name;
                if (propfullname.Length > 0) propfullname = char.ToLower(propfullname[0]) + propfullname.Substring(1);
                propfullname = "ifc:" + propfullname;
                sbProps.AppendLine(propfullname);
                sbProps.Append("\trdf:type \towl:ObjectProperty ");
                // functional
                if (docAttr.GetAggregation() == DocAggregationEnum.NONE ||
                   docAttr.GetAggregation() == DocAggregationEnum.LIST ||
                   docAttr.GetAggregation() == DocAggregationEnum.ARRAY ||
                   (docAttr.GetAggregation() == DocAggregationEnum.SET && docAttr.GetAggregationNestingUpper() == 1))
                {
                    sbProps.Append(", owl:FunctionalProperty ");
                }
                sbProps.Append(";");
                sbProps.AppendLine();
                // inverse
                if (invpropname != null && targetEntity != null)
                {
                    invpropfullname = invpropname + "_" + targetEntity.Name;
                    if (invpropfullname.Length > 0) invpropfullname = char.ToLower(invpropfullname[0]) + invpropfullname.Substring(1);
                    invpropfullname = "ifc:" + invpropfullname;
                    sbProps.AppendLine("\towl:inverseOf \t" + invpropfullname + " ;");
                }
                // domain
                sbProps.AppendLine("\trdfs:domain " + ToOwlClass(docEntity.Name) + " ;");
                // range
                sbProps.AppendLine("\trdfs:range " + actualTargetString + " ;");
                // label
                sbProps.AppendLine("\trdfs:label  \"" + propname + "\" .");
                sbProps.AppendLine();

                // create restrictions
                {

                    // only
                    sb.AppendLine("\trdfs:subClassOf ");
                    sb.AppendLine("\t\t[ ");
                    sb.AppendLine("\t\t\trdf:type owl:Restriction ;");
                    sb.AppendLine("\t\t\towl:allValuesFrom " + actualTargetString + " ;");
                    sb.AppendLine("\t\t\towl:onProperty " + propfullname);
                    sb.AppendLine("\t\t] ;");

                    if (docAttr.GetAggregation() == DocAggregationEnum.NONE ||
                       docAttr.GetAggregation() == DocAggregationEnum.LIST ||
                       docAttr.GetAggregation() == DocAggregationEnum.ARRAY)
                    {
                        sb.AppendLine("\t" + "rdfs:subClassOf ");
                        sb.AppendLine("\t\t" + "[");
                        sb.AppendLine("\t\t\t" + "rdf:type owl:Restriction ;");
                        if (docAttr.IsOptional)
                            sb.AppendLine("\t\t\t" + "owl:maxQualifiedCardinality \"" + 1 + "\"^^xsd:nonNegativeInteger ;");
                        else
                            sb.AppendLine("\t\t\t" + "owl:qualifiedCardinality \"" + 1 + "\"^^xsd:nonNegativeInteger ;");
                        sb.AppendLine("\t\t\towl:onProperty " + propfullname + " ;");
                        sb.AppendLine("\t\t\t" + "owl:onClass " + actualTargetString);
                        sb.AppendLine("\t\t] ;");
                    }

                    if (docAttr.GetAggregation() == DocAggregationEnum.SET)
                    {
                        int mincard = 0;
                        if (docAttr.AggregationLower != null) mincard = Int32.Parse(docAttr.AggregationLower);
                        int maxcard = 0;
                        if (String.IsNullOrEmpty(docAttr.AggregationUpper) || !Int32.TryParse(docAttr.AggregationUpper, out maxcard))
                            maxcard = 0;

                        if (docAttr.IsOptional)
                            mincard = 0;

                        if (mincard == maxcard && mincard > 0)
                        {
                            sb.AppendLine("\t" + "rdfs:subClassOf ");
                            sb.AppendLine("\t\t" + "[");
                            sb.AppendLine("\t\t\t" + "rdf:type owl:Restriction ;");
                            sb.AppendLine("\t\t\t" + "owl:qualifiedCardinality \"" + mincard + "\"^^xsd:nonNegativeInteger ;");
                            sb.AppendLine("\t\t\towl:onProperty " + propfullname + " ;");
                            sb.AppendLine("\t\t\t" + "owl:onClass " + actualTargetString);
                            sb.AppendLine("\t\t] ;");
                        }
                        else
                        {
                            if (mincard > 0)
                            {
                                sb.AppendLine("\t" + "rdfs:subClassOf ");
                                sb.AppendLine("\t\t" + "[");
                                sb.AppendLine("\t\t\t" + "rdf:type owl:Restriction ;");
                                sb.AppendLine("\t\t\t" + "owl:minQualifiedCardinality \"" + mincard + "\"^^xsd:nonNegativeInteger ;");
                                sb.AppendLine("\t\t\towl:onProperty " + propfullname + " ;");
                                sb.AppendLine("\t\t\t" + "owl:onClass " + actualTargetString);
                                sb.AppendLine("\t\t] ;");
                            }
                            if (maxcard > 0)
                            {
                                sb.AppendLine("\t" + "rdfs:subClassOf ");
                                sb.AppendLine("\t\t" + "[");
                                sb.AppendLine("\t\t\t" + "rdf:type owl:Restriction ;");
                                sb.AppendLine("\t\t\t" + "owl:maxQualifiedCardinality \"" + maxcard + "\"^^xsd:nonNegativeInteger ;");
                                sb.AppendLine("\t\t\towl:onProperty " + propfullname + " ;");
                                sb.AppendLine("\t\t\t" + "owl:onClass " + actualTargetString);
                                sb.AppendLine("\t\t] ;");
                            }
                        }

                    }

                    if (docAttr.GetAggregation() == DocAggregationEnum.LIST ||
                       docAttr.GetAggregation() == DocAggregationEnum.ARRAY)
                    {

                        int mincard = 0;
                        if (docAttr.AggregationLower != null) mincard = Int32.Parse(docAttr.AggregationLower);
                        int maxcard = 0;
                        if (String.IsNullOrEmpty(docAttr.AggregationUpper) || !Int32.TryParse(docAttr.AggregationUpper, out maxcard))
                            maxcard = 0;

                        if (docAttr.GetAggregation() == DocAggregationEnum.ARRAY)
                        {
                            mincard = maxcard - mincard + 1;
                            maxcard = mincard;
                        }

                        if (mincard >= 1)
                        {
                            string cards = "";
                            cards += WriteMinCardRestr(actualTargetString, propfullname, mincard, true);
                            cards += " ;";
                            sb.AppendLine(cards);
                        }

                        if (maxcard > 1)
                        {
                            string cards = "";
                            string emptyListTgt = actualTargetString.Substring(0, actualTargetString.Length - 4) + "EmptyList";
                            cards += WriteMaxCardRestr(emptyListTgt, propfullname, maxcard, true);
                            cards += " ;";
                            sb.AppendLine(cards);
                        }
                    }
                }
            }

            sb.AppendLine("\trdf:type \towl:Class .");
            sb.AppendLine();
            if (fullListing)
            {
                sb.Append(sbProps.ToString());
                sb.Append(sbLists.ToString());
            }

            return sb.ToString();
        }