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