Exemplo n.º 1
0
        public static IDictionary <object, ILookup <IPropertyExpression, ISubClassOf> > Validate(
            this IOntology ontology,
            IDictionary <object, ISet <IClass> > classifications
            )
        {
            var evaluator = new ClassMembershipEvaluator(
                ontology,
                classifications);

            return((
                       from keyValuePair in classifications
                       let individual = keyValuePair.Key
                                        from classExpression in keyValuePair.Value
                                        from subClassOf in ontology.Get <ISubClassOf>()
                                        from annotation in subClassOf.Annotations
                                        where
                                        subClassOf.SubClassExpression == classExpression &&
                                        subClassOf.SuperClassExpression is IPropertyRestriction &&
                                        annotation.Property.Iri == "Validation.Restriction" &&
                                        !subClassOf.SuperClassExpression.Evaluate(
                           evaluator,
                           individual)
                                        group subClassOf by individual into errorsGroupedByIndividual
                                        select errorsGroupedByIndividual
                       ).ToDictionary(
                       group => group.Key,
                       group => group.ToLookup(
                           g => ((IPropertyRestriction)g.SuperClassExpression).PropertyExpression)));
        }
        private bool AreEqual(
            object lhs,
            object rhs
            )
        {
            var commonClassExpressions = Classify(lhs);

            commonClassExpressions.IntersectWith(Classify(rhs));
            var hasKeys =
                from commonClassExpression in commonClassExpressions
                join hasKey in _ontology.Get <IHasKey>() on commonClassExpression equals hasKey.ClassExpression
                select hasKey;

            return
                (hasKeys.Any() &&
                 hasKeys.All(
                     hasKey => hasKey.DataPropertyExpressions.All(
                         dataPropertyExpression => AreEqual(
                             dataPropertyExpression,
                             lhs,
                             rhs))));
        }
        public ClassMembershipEvaluator(
            IOntology ontology,
            IDictionary <object, ISet <IClass> > classifications
            )
        {
            _ontology        = ontology;
            _classifications = classifications;
            _classes         = _ontology.Get <IClass>().ToDictionary(
                @class => @class.Iri);
            _classAssertions = _ontology.Get <IClassAssertion>().ToLookup(
                classAssertion => classAssertion.NamedIndividual,
                classAssertion => classAssertion.ClassExpression);
            _objectPropertyAssertions = _ontology.Get <IObjectPropertyAssertion>().ToLookup(
                objectPropertyAssertion => objectPropertyAssertion.SourceIndividual);
            _dataPropertyAssertions = _ontology.Get <IDataPropertyAssertion>().ToLookup(
                dataPropertyAssertion => dataPropertyAssertion.SourceIndividual);
            _superClassExpressions = _ontology.Get <ISubClassOf>().ToLookup(
                subClassOf => subClassOf.SubClassExpression,
                subClassOf => subClassOf.SuperClassExpression);
            _subClassExpressions = _ontology.Get <ISubClassOf>().ToLookup(
                subClassOf => subClassOf.SuperClassExpression,
                subClassOf => subClassOf.SubClassExpression);
            _classDefinitions = (
                from @class in _ontology.Get <IClass>()
                from equivalentClasses in _ontology.Get <IEquivalentClasses>()
                where equivalentClasses.ClassExpressions.Contains(@class)
                from classExpression in equivalentClasses.ClassExpressions
                where !(classExpression is IClass)
                group classExpression by @class into classExpressionsGroupedbyClass
                select
                (
                    Class: classExpressionsGroupedbyClass.Key,
                    ClassExpression: classExpressionsGroupedbyClass.First()
                )).ToDictionary(
                definition => definition.Class,
                definition => definition.ClassExpression);

            var disjointPairs = (
                from disjointClasses in _ontology.Get <IDisjointClasses>()
                from classExpression1 in disjointClasses.ClassExpressions
                from classExpression2 in disjointClasses.ClassExpressions
                where classExpression1 != classExpression2
                select
                (
                    ClassExpression1: classExpression1,
                    ClassExpression2: classExpression2
                )).ToList();

            for (var index = 0; index < disjointPairs.Count; ++index)
            {
                var(classExpression1, classExpression2) = disjointPairs[index];
                _subClassExpressions[classExpression2].ForEach(
                    subclassExpression => disjointPairs.Add(
                        (
                            ClassExpression1: classExpression1,
                            ClassExpression2: subclassExpression
                        )));
            }

            _disjointClassExpressions = disjointPairs.ToLookup(
                disjointPair => disjointPair.ClassExpression1,
                disjointPair => disjointPair.ClassExpression2);

            HashSet <IClass> adjacent = null;
            var empty         = new HashSet <IClass>();
            var adjacencyList = _ontology.Get <IClass>()
                                .ToDictionary(
                @class => @class,
                @class => empty);

            var classVisitor = new ClassExpressionNavigator(new ClassVisitor(@class => adjacent.Add(@class)));

            _classDefinitions.ForEach(
                pair =>
            {
                adjacent = new HashSet <IClass>();
                pair.Value.Accept(classVisitor);
                adjacencyList[pair.Key] = adjacent;
            });

            _definedClasses = (
                from @class in adjacencyList.TopologicalSort()
                join definition in _classDefinitions on @class equals definition.Key
                select definition.Key
                ).ToList();
        }