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.utilities.owl;
020
021import java.util.HashSet;
022import java.util.Set;
023
024import org.apache.jena.rdf.model.Model;
025import org.apache.jena.rdf.model.Property;
026import org.apache.jena.rdf.model.RDFNode;
027import org.apache.jena.rdf.model.Statement;
028import org.apache.jena.rdf.model.StmtIterator;
029import org.apache.jena.vocabulary.OWL;
030import org.apache.jena.vocabulary.RDF;
031import org.apache.jena.vocabulary.RDFS;
032
033public class OWLEntityTypeAdder {
034
035        /**
036         * Infers the type of predicates p_i by analyzing the object of the triples using p_i and adds the
037         * entity type assertion to the model, i.e. for a data property dp <dp a owl:DatatypeProperty>
038         * will be added.
039         * @param model the model
040         */
041        public static void addEntityTypes(Model model){
042                StmtIterator iterator = model.listStatements();
043                Set<Property> objectPropertyPredicates = new HashSet<>();
044                Set<Property> dataPropertyPredicates = new HashSet<>();
045                while(iterator.hasNext()){
046                        Statement st = iterator.next();
047                        Property predicate = st.getPredicate();
048                        if(!predicate.getURI().startsWith(RDF.getURI()) && !predicate.getURI().startsWith(RDFS.getURI()) 
049                                        && !predicate.getURI().startsWith(OWL.getURI())){
050                                RDFNode object = st.getObject();
051                                if(object.isLiteral()){
052                                        dataPropertyPredicates.add(predicate);
053                                } else if(object.isResource()){
054                                        objectPropertyPredicates.add(predicate);
055                                }
056                        }
057                }
058                iterator.close();
059                for (Property property : dataPropertyPredicates) {
060                        if(!objectPropertyPredicates.contains(property)){
061                                model.add(property, RDF.type, OWL.DatatypeProperty);
062                        }
063                }
064                for (Property property : objectPropertyPredicates) {
065                        if(!dataPropertyPredicates.contains(property)){
066                                model.add(property, RDF.type, OWL.ObjectProperty);
067                        }
068                }
069        }
070
071}