public static IEnumerable GetRelationships()

in OWL2DTDL/DotNetRdfExtensions.cs [337:425]


        public static IEnumerable<Relationship> GetRelationships(this OntologyClass cls)
        {
            List<Relationship> relationships = new List<Relationship>();

            // Start w/ rdfs:domain declarations. At this time we only consider no-range (i.e.,
            // range is owl:Thing) or named singleton ranges
            IEnumerable<OntologyProperty> rdfsDomainProperties = cls.IsDomainOf.Where(
                property =>
                    property.Ranges.Count() == 0 ||
                    (property.Ranges.Count() == 1 && (property.Ranges.First().IsNamed() || property.Ranges.First().IsDatatype())));
            foreach (OntologyProperty property in rdfsDomainProperties)
            {
                Relationship newRelationship;
                if (property.Ranges.Count() == 0)
                {
                    OntologyGraph oGraph = cls.Graph as OntologyGraph;
                    OntologyClass target;
                    if (property.IsObjectProperty())
                    {
                        target = oGraph.CreateOntologyClass(VocabularyHelper.OWL.Thing);
                    }
                    else
                    {
                        target = oGraph.CreateOntologyClass(VocabularyHelper.RDFS.Literal);
                    }
                    newRelationship = new Relationship(property, target);
                }
                else
                {
                    OntologyClass range = property.Ranges.First();
                    newRelationship = new Relationship(property, range);
                }

                if (property.IsFunctional())
                {
                    newRelationship.ExactCount = 1;
                }

                relationships.Add(newRelationship);
            }

            // Continue w/ OWL restrictions on the class
            IEnumerable<OntologyRestriction> ontologyRestrictions = cls.DirectSuperClasses
                .Where(superClass => superClass.IsRestriction())
                .Select(superClass => new OntologyRestriction(superClass));
            foreach (OntologyRestriction ontologyRestriction in ontologyRestrictions)
            {


                OntologyProperty restrictionProperty = ontologyRestriction.OnProperty;
                OntologyClass restrictionClass = ontologyRestriction.OnClass;
                if (restrictionProperty.IsNamed() && (restrictionClass.IsNamed() || restrictionClass.IsDatatype()))
                {
                    Relationship newRelationship = new Relationship(restrictionProperty, restrictionClass);

                    int min = ontologyRestriction.MinimumCardinality;
                    int exactly = ontologyRestriction.ExactCardinality;
                    int max = ontologyRestriction.MaximumCardinality;

                    if (min != 0)
                        newRelationship.MinimumCount = min;
                    if (exactly != 0)
                        newRelationship.ExactCount = exactly;
                    if (max != 0)
                        newRelationship.MaximumCount = max;

                    relationships.Add(newRelationship);
                }
            }

            // Iterate over the gathered list of Relationships and narrow down to the most specific ones, using a Dictionary for lookup and the MergeWith() method for in-place narrowing
            Dictionary<OntologyProperty, Relationship> relationshipsDict = new Dictionary<OntologyProperty, Relationship>(new OntologyResourceComparer());
            foreach (Relationship relationship in relationships)
            {
                OntologyProperty property = relationship.Property;
                OntologyResource target = relationship.Target;
                // If we already have this property listed in the dictionary, first narrow down the relationship by combining it with the old copy
                if (relationshipsDict.ContainsKey(property))
                {
                    Relationship oldRelationship = relationshipsDict[property];
                    relationship.MergeWith(oldRelationship);
                }
                // Put relationship in the dictionary
                relationshipsDict[property] = relationship;
            }

            // Return the values
            return relationshipsDict.Values;
        }