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.kb.extraction;
020
021import java.util.ArrayList;
022import java.util.HashSet;
023import java.util.List;
024import java.util.Set;
025import java.util.SortedSet;
026import java.util.TreeSet;
027
028import org.dllearner.kb.aquisitors.RDFBlankNode;
029import org.dllearner.kb.aquisitors.TupleAquisitor;
030import org.dllearner.kb.manipulator.Manipulator;
031import org.dllearner.utilities.datastructures.RDFNodeTuple;
032import org.dllearner.utilities.owl.OWLVocabulary;
033import org.semanticweb.owlapi.model.IRI;
034import org.semanticweb.owlapi.model.OWLAnnotation;
035import org.semanticweb.owlapi.model.OWLAxiom;
036import org.semanticweb.owlapi.model.OWLClass;
037import org.semanticweb.owlapi.model.OWLClassExpression;
038import org.semanticweb.owlapi.model.OWLDataFactory;
039import org.semanticweb.owlapi.model.OWLObjectProperty;
040
041
042
043/**
044 * Property node, has connection to a and b part
045 * 
046 * @author Sebastian Hellmann
047 * 
048 */
049
050public class ObjectPropertyNode extends PropertyNode {
051
052        
053        // specialtypes like owl:symmetricproperty
054        private SortedSet<String> specialTypes = new TreeSet<>();
055        private SortedSet<RDFNodeTuple> propertyInformation = new TreeSet<>();
056        private List<BlankNode> blankNodes = new ArrayList<>();
057
058        public ObjectPropertyNode(String propertyURI, Node a, Node b) {
059                super(propertyURI, a, b);               
060        }
061
062        // Property Nodes are normally not expanded,
063        // this function is never called
064        @Override
065        public List<Node> expand(TupleAquisitor tupelAquisitor, Manipulator manipulator) {
066                return null;
067        }
068
069        // gets the types for properties recursively
070        @Override
071        public List<BlankNode> expandProperties(TupleAquisitor tupelAquisitor, Manipulator manipulator, boolean dissolveBlankNodes) {
072                List<BlankNode> ret = new ArrayList<>();
073                ret.addAll(b.expandProperties(tupelAquisitor, manipulator, dissolveBlankNodes));
074                SortedSet<RDFNodeTuple> newTypes = tupelAquisitor.getTupelForResource(uri);
075                for (RDFNodeTuple tuple : newTypes) {
076                        try {
077                                
078                                if (tuple.a.toString().equals(OWLVocabulary.RDF_TYPE)) {
079                                        if(!tuple.b.toString().equals(OWLVocabulary.OWL_OBJECTPROPERTY)){
080                                                specialTypes.add(tuple.b.toString());
081                                        }
082                                }else if(tuple.b.isAnon()){
083                                                                        
084                                        if(dissolveBlankNodes){
085                                                RDFBlankNode n = (RDFBlankNode) tuple.b;
086                                                BlankNode tmp = new BlankNode( n, tuple.a.toString()); 
087                                                //add it to the graph
088                                                blankNodes.add(tmp);
089                                                ret.add( tmp);
090                                        }
091                                        
092                                }else{
093                                        
094                                        propertyInformation.add(tuple);
095                                        
096                                }
097                        } catch (Exception e) {
098                                tail("expand properties:  with tuple: "+ tuple);
099                                e.printStackTrace();
100                        }
101                        
102                }
103                return ret;
104                
105
106        }
107        
108        @Override
109        public SortedSet<String> toNTriple() {
110                SortedSet<String> s = new TreeSet<>();
111                s.add(getNTripleForm()+"<" + OWLVocabulary.RDF_TYPE + "><"
112                                + OWLVocabulary.OWL_OBJECTPROPERTY + ">.");
113                for (String one : specialTypes) {
114                        s.add(getNTripleForm()+"<" + OWLVocabulary.RDF_TYPE + "><"
115                                        + one + ">.");
116                }
117                
118                for (RDFNodeTuple one : propertyInformation) {
119                        s.add(one.getNTriple(uri));
120                }
121
122                return s;
123        }
124        
125        @Override
126        public void toOWLOntology( OWLAPIOntologyCollector owlAPIOntologyCollector){
127                //FIXME Property information
128
129                OWLDataFactory factory =  owlAPIOntologyCollector.getFactory();
130                OWLObjectProperty me =factory.getOWLObjectProperty(getIRI());
131        
132                for (RDFNodeTuple one : propertyInformation) {
133                        
134                        
135                        if(one.aPartContains(OWLVocabulary.RDFS_range)){
136                                OWLClass c = factory.getOWLClass(IRI.create(one.b.toString()));
137                                owlAPIOntologyCollector.addAxiom(factory.getOWLObjectPropertyRangeAxiom(me, c));
138                        }else if(one.aPartContains(OWLVocabulary.RDFS_domain)){
139                                OWLClass c = factory.getOWLClass(IRI.create(one.b.toString()));
140                                owlAPIOntologyCollector.addAxiom(factory.getOWLObjectPropertyDomainAxiom(me, c));
141                        }else if(one.aPartContains(OWLVocabulary.RDFS_SUB_PROPERTY_OF)){
142                                OWLObjectProperty p = factory.getOWLObjectProperty(IRI.create(one.b.toString()));
143                                owlAPIOntologyCollector.addAxiom(factory.getOWLSubObjectPropertyOfAxiom(me, p));
144                        }else if(one.aPartContains(OWLVocabulary.OWL_inverseOf)){
145                                OWLObjectProperty p = factory.getOWLObjectProperty(IRI.create(one.b.toString()));
146                                owlAPIOntologyCollector.addAxiom(factory.getOWLInverseObjectPropertiesAxiom(me, p));                            
147                        }else if(one.aPartContains(OWLVocabulary.OWL_equivalentProperty)){
148                                OWLObjectProperty p = factory.getOWLObjectProperty(IRI.create(one.b.toString()));
149                                Set<OWLObjectProperty> tmp = new HashSet<>();
150                                tmp.add(me);tmp.add(p);
151                                owlAPIOntologyCollector.addAxiom(factory.getOWLEquivalentObjectPropertiesAxiom(tmp));
152                                
153                        }else if(one.a.toString().equals(OWLVocabulary.RDFS_LABEL)){
154                                OWLAnnotation annoLabel = factory.getOWLAnnotation(factory.getRDFSLabel(), factory.getOWLLiteral(one.b.toString()));
155                                OWLAxiom ax = factory.getOWLAnnotationAssertionAxiom(me.getIRI(), annoLabel);
156                                owlAPIOntologyCollector.addAxiom(ax);
157                        }else if(one.b.isLiteral()){
158                                // XXX comments
159                        }
160                        else {
161                                tail("conversion to ontology: property information: " + one);
162                        }
163                        
164                }
165                
166                for (String one : specialTypes) {
167
168                        switch (one) {
169                                case OWLVocabulary.OWL_FunctionalProperty:
170                                        owlAPIOntologyCollector.addAxiom(factory.getOWLFunctionalObjectPropertyAxiom(me));
171                                        break;
172                                case OWLVocabulary.OWL_InverseFunctionalProperty:
173                                        owlAPIOntologyCollector.addAxiom(factory.getOWLInverseFunctionalObjectPropertyAxiom(me));
174                                        break;
175                                case OWLVocabulary.OWL_TransitiveProperty:
176                                        owlAPIOntologyCollector.addAxiom(factory.getOWLTransitiveObjectPropertyAxiom(me));
177                                        break;
178                                case OWLVocabulary.OWL_SymmetricProperty:
179                                        owlAPIOntologyCollector.addAxiom(factory.getOWLSymmetricObjectPropertyAxiom(me));
180                                        break;
181                                default:
182                                        tail("conversion to ontology: special types: " + one);
183                                        break;
184                        }
185                }
186                for (BlankNode bn : blankNodes) {
187                        OWLClassExpression target = bn.getAnonymousClass(owlAPIOntologyCollector);
188                        if(bn.getInBoundEdge().equals(OWLVocabulary.RDFS_range)){
189                                owlAPIOntologyCollector.addAxiom(factory.getOWLObjectPropertyRangeAxiom(me, target));
190                        }else if(bn.getInBoundEdge().equals(OWLVocabulary.RDFS_domain)){
191                                owlAPIOntologyCollector.addAxiom(factory.getOWLObjectPropertyDomainAxiom(me, target));
192                        }
193                        //System.out.println(bn.getAnonymousClass(owlAPIOntologyCollector).toString());
194                }
195        }
196        
197
198        
199}