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.List;
023import java.util.SortedSet;
024import java.util.TreeSet;
025
026import org.dllearner.kb.aquisitors.RDFBlankNode;
027import org.dllearner.kb.aquisitors.TupleAquisitor;
028import org.dllearner.kb.manipulator.Manipulator;
029import org.dllearner.utilities.datastructures.RDFNodeTuple;
030import org.dllearner.utilities.owl.OWLVocabulary;
031import org.semanticweb.owlapi.model.IRI;
032import org.semanticweb.owlapi.model.OWLAxiom;
033import org.semanticweb.owlapi.model.OWLClass;
034import org.semanticweb.owlapi.model.OWLClassExpression;
035import org.semanticweb.owlapi.model.OWLDataFactory;
036import org.semanticweb.owlapi.model.OWLDataProperty;
037import org.semanticweb.owlapi.model.OWLDataRange;
038
039/**
040 * Property node, has connection to a and b part
041 * 
042 * @author Sebastian Hellmann
043 * 
044 */
045
046public class DatatypePropertyNode extends PropertyNode {
047
048        //       specialtypes like owl:symmetricproperty
049        private SortedSet<String> specialTypes = new TreeSet<>();
050        private SortedSet<RDFNodeTuple> propertyInformation = new TreeSet<>();
051        private List<BlankNode> blankNodes = new ArrayList<>();
052        
053        public DatatypePropertyNode(String uri, Node a, LiteralNode b) {
054                super(uri, a, b);
055        }
056
057        // Property Nodes are normally not expanded,
058        // this function is never called
059        @Override
060        public List<Node> expand(TupleAquisitor tupelAquisitor, Manipulator manipulator) {
061                return null;
062        }
063
064        // gets the types for properties recursively
065        @Override
066        public List<BlankNode>  expandProperties(TupleAquisitor tupelAquisitor, Manipulator manipulator, boolean dissolveBlankNodes) {
067                List<BlankNode> ret = new ArrayList<>();
068                //ret.addAll(b.expandProperties(tupelAquisitor, manipulator));
069                SortedSet<RDFNodeTuple> newTypes = tupelAquisitor.getTupelForResource(uri);
070                
071                for (RDFNodeTuple tuple : newTypes) {
072                        try {
073                                if (tuple.a.toString().equals(OWLVocabulary.RDF_TYPE)) {
074                                        if(!tuple.b.toString().equals(OWLVocabulary.OWL_DATATYPPROPERTY)){
075                                                specialTypes.add(tuple.b.toString());
076                                        }
077                                }else if(tuple.b.isAnon()){
078                                                                        
079                                        if(dissolveBlankNodes){
080                                                RDFBlankNode n = (RDFBlankNode) tuple.b;
081                                                BlankNode tmp = new BlankNode( n, tuple.a.toString()); 
082                                                //add it to the graph
083                                                blankNodes.add(tmp);
084                                                ret.add( tmp);
085                                        }
086                                        
087                                }else{
088                                        
089                                        propertyInformation.add(tuple);
090                                        
091                                }
092                        } catch (Exception e) {
093                                logger.warn("resource "+uri+" with "+ tuple);
094                                e.printStackTrace();
095                        }
096                        
097                }
098                return ret;
099        }
100        
101        @Override
102        public LiteralNode getBPart(){
103                return (LiteralNode)b;
104        }
105        
106        public String getNTripleFormOfB() {
107                return b.getNTripleForm();
108        }
109
110        @Override
111        public SortedSet<String> toNTriple() {
112                SortedSet<String> s = new TreeSet<>();
113                s.add(getNTripleForm()+"<" + OWLVocabulary.RDF_TYPE + "><"
114                                + OWLVocabulary.OWL_DATATYPPROPERTY + ">.");
115
116                return s;
117        }
118        
119        @Override
120        public void toOWLOntology( OWLAPIOntologyCollector owlAPIOntologyCollector){
121                
122
123                OWLDataFactory factory =  owlAPIOntologyCollector.getFactory();
124                OWLDataProperty me =factory.getOWLDataProperty(getIRI());
125        
126                for (RDFNodeTuple one : propertyInformation) {
127                        
128                        
129                        if(one.aPartContains(OWLVocabulary.RDFS_range)){
130                                //System.out.println(me + one.b.toString());
131                                OWLDataRange o = factory.getOWLDatatype(IRI.create(one.b.toString()));
132                                OWLAxiom ax = factory.getOWLDataPropertyRangeAxiom(me, o);
133                                owlAPIOntologyCollector.addAxiom(ax);
134                                //XXX implement
135                                //OWLClass c = factory.getOWLClass(URI.create(one.b.toString()));
136                                //owlAPIOntologyCollector.addAxiom(factory.getOWLDataPropertyRangeAxiom(propery, owlDataRange)(me, c));
137                        }else if(one.aPartContains(OWLVocabulary.RDFS_domain)){
138                                OWLClass c = factory.getOWLClass(IRI.create(one.b.toString()));
139                                owlAPIOntologyCollector.addAxiom(factory.getOWLDataPropertyDomainAxiom(me, c));
140                        }
141                }
142                
143                
144                for (BlankNode bn : blankNodes) {
145                        OWLClassExpression target = bn.getAnonymousClass(owlAPIOntologyCollector);
146                        if(bn.getInBoundEdge().equals(OWLVocabulary.RDFS_range)){
147                        
148                                //XXX implement
149                                //owlAPIOntologyCollector.addAxiom(factory.getOWLObjectPropertyRangeAxiom(me, target));
150                        }else if(bn.getInBoundEdge().equals(OWLVocabulary.RDFS_domain)){
151                                owlAPIOntologyCollector.addAxiom(factory.getOWLDataPropertyDomainAxiom(me, target));
152                                
153                        }
154                        //System.out.println(bn.getAnonymousClass(owlAPIOntologyCollector).toString());
155                }
156                
157                
158        }
159        
160        
161
162}