001/**
002 * Copyright (C) 2007 - 2016, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.dllearner.algorithms.schema;
020
021import java.util.HashSet;
022import java.util.Set;
023
024import org.semanticweb.owlapi.model.OWLClass;
025import org.semanticweb.owlapi.model.OWLEntity;
026import org.semanticweb.owlapi.model.OWLIndividual;
027import org.semanticweb.owlapi.model.OWLNamedIndividual;
028import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom;
029import org.semanticweb.owlapi.model.OWLOntology;
030import org.semanticweb.owlapi.model.parameters.Imports;
031import org.semanticweb.owlapi.reasoner.OWLReasoner;
032import org.semanticweb.owlapi.reasoner.structural.StructuralReasonerFactory;
033
034import com.google.common.collect.Sets;
035import com.google.common.collect.Sets.SetView;
036
037/**
038 * 
039 * @author Lorenz Buehmann
040 * @since Oct 21, 2014
041 */
042public class EntityDependencyMatrix<T> {
043        
044        
045        private EntityDependencyMatrix() {
046                
047        }
048        
049        public static void getEntityDependencyMatrix(OWLOntology ontology) {
050                OWLReasoner reasoner = new StructuralReasonerFactory().createNonBufferingReasoner(ontology);
051                
052                // how often are individuals of class A related to individuals of class B
053                Set<OWLClass> classes = ontology.getClassesInSignature(Imports.INCLUDED);
054                for (OWLClass clsA : classes) {
055                        for (OWLClass clsB : classes) {
056                                if(!clsA.equals(clsB)) {
057                                        Set<OWLNamedIndividual> instancesA = reasoner.getInstances(clsA, false).getFlattened();
058                                        Set<OWLNamedIndividual> instancesB = reasoner.getInstances(clsB, false).getFlattened();
059
060                                        // S_1 = { o_i | A(a_i) and there is an p_i(a_i, o_i) in O }
061                                        Set<OWLIndividual> objectsOfInstancesFromA = new HashSet<>();
062                                        for (OWLNamedIndividual ind : instancesA) {
063                                                Set<OWLObjectPropertyAssertionAxiom> axioms = ontology.getObjectPropertyAssertionAxioms(ind);
064                                                for (OWLObjectPropertyAssertionAxiom axiom : axioms) {
065                                                        objectsOfInstancesFromA.add(axiom.getObject());
066                                                }
067                                        }
068
069                                        // S_2 = { o_i | B(b_i) and there is an p_i(b_i, o_i) in O }
070                                        Set<OWLIndividual> objectsOfInstancesFromB = new HashSet<>();
071                                        for (OWLNamedIndividual ind : instancesB) {
072                                                Set<OWLObjectPropertyAssertionAxiom> axioms = ontology.getObjectPropertyAssertionAxioms(ind);
073                                                for (OWLObjectPropertyAssertionAxiom axiom : axioms) {
074                                                        objectsOfInstancesFromB.add(axiom.getObject());
075                                                }
076                                        }
077                                        
078                                        // A -> B
079                                        SetView<OWLIndividual> aToB = Sets.intersection(objectsOfInstancesFromA, instancesB);
080                                        // B -> A
081                                        SetView<OWLIndividual> bToA = Sets.intersection(objectsOfInstancesFromB, instancesA);
082                                        
083                                }
084                        }
085                }
086        }
087        
088        /**
089         * Returns the degree by which entity1 depends on entity2.
090         * Note that this value is not necessarily symmetric, i.e. the degree
091         * entity1 depends on entity2 might be different from what
092         * entity2 depends on entity1.
093         * 
094         * @param entity1 the first entity
095         * @param entity2 the second entity
096         * @return the dependency value
097         */
098        public double getDependency(OWLEntity entity1, OWLEntity entity2){
099                return 0;
100        }
101
102}