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