001    /***************************************************************************/
002    /*  Copyright (C) 2010-2011, Sebastian Hellmann                            */
003    /*  Note: If you need parts of NLP2RDF in another licence due to licence   */
004    /*  incompatibility, please mail hellmann@informatik.uni-leipzig.de        */
005    /*                                                                         */
006    /*  This file is part of NLP2RDF.                                          */
007    /*                                                                         */
008    /*  NLP2RDF is free software; you can redistribute it and/or modify        */
009    /*  it under the terms of the GNU General Public License as published by   */
010    /*  the Free Software Foundation; either version 3 of the License, or      */
011    /*  (at your option) any later version.                                    */
012    /*                                                                         */
013    /*  NLP2RDF is distributed in the hope that it will be useful,             */
014    /*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
015    /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the           */
016    /*  GNU General Public License for more details.                           */
017    /*                                                                         */
018    /*  You should have received a copy of the GNU General Public License      */
019    /*  along with this program. If not, see <http://www.gnu.org/licenses/>.   */
020    /***************************************************************************/
021    
022    package eu.lod2.nlp2rdf.schema.tools;
023    
024    import java.util.Map;
025    import java.util.HashMap;
026    
027    import com.hp.hpl.jena.ontology.Individual;
028    import com.hp.hpl.jena.ontology.OntModel;
029    import com.hp.hpl.jena.ontology.Ontology;
030    import com.hp.hpl.jena.ontology.OntDocumentManager;
031    
032    import org.apache.commons.logging.Log;
033    import org.apache.commons.logging.LogFactory;
034    
035    /**
036     * Factory
037     */
038    public class Factory {
039    
040            private static Log log = LogFactory.getLog(Factory.class);
041    
042            private static OntModel defaultModel = null;
043    
044            private static Map<java.lang.String, java.lang.String> uri2Type;
045    
046            static {
047                    uri2Type = new HashMap<java.lang.String, java.lang.String>();
048                    uri2Type.put("http://www.w3.org/2002/07/owl#Thing", "eu.lod2.nlp2rdf.schema.IThing");
049                    uri2Type.put("http://nlp2rdf.lod2.eu/schema/sso/StopWord", "eu.lod2.nlp2rdf.schema.sso.IStopWord");
050                    uri2Type.put("http://nlp2rdf.lod2.eu/schema/error/Error", "eu.lod2.nlp2rdf.schema.error.IError");
051                    uri2Type.put("http://nlp2rdf.lod2.eu/schema/sso/Word", "eu.lod2.nlp2rdf.schema.sso.IWord");
052                    uri2Type.put("http://nlp2rdf.lod2.eu/schema/sso/Phrase", "eu.lod2.nlp2rdf.schema.sso.IPhrase");
053                    uri2Type.put("http://nlp2rdf.lod2.eu/schema/string/OffsetBasedString", "eu.lod2.nlp2rdf.schema.str.IOffsetBasedString");
054                    uri2Type.put("http://nlp2rdf.lod2.eu/schema/string/Document", "eu.lod2.nlp2rdf.schema.str.IDocument");
055                    uri2Type.put("http://nlp2rdf.lod2.eu/schema/sso/Sentence", "eu.lod2.nlp2rdf.schema.sso.ISentence");
056                    uri2Type.put("http://nlp2rdf.lod2.eu/schema/string/String", "eu.lod2.nlp2rdf.schema.str.IString");
057                    uri2Type.put("http://nlp2rdf.lod2.eu/schema/topic/Topic", "eu.lod2.nlp2rdf.schema.topic.ITopic");
058                    uri2Type.put("http://nlp2rdf.lod2.eu/schema/string/ContextHashBasedString", "eu.lod2.nlp2rdf.schema.str.IContextHashBasedString");
059            }
060    
061            /**
062             * Sets the default ontModel that may be used by ontology wrapper classes when no explicit ontModel argument is provided
063             */
064            public static void setDefaultModel(OntModel defaultModel) {
065                    Factory.defaultModel = defaultModel;
066            }
067    
068            /**
069             * Returns the default ontModel set with setDefaultModel()
070             */
071            public static OntModel getDefaultModel() {
072                    if (defaultModel == null) {
073                            throw new RuntimeException("No default OntModel was provided to eu.lod2.nlp2rdf.schema.tools.Factory");
074                    }
075                    return defaultModel;
076            }
077    
078            /**
079             * Returns the interface name for a given OWL Class
080             */
081            public static java.lang.String getJavaInterfaceName(java.lang.String uri) {
082                    return uri2Type.get(uri);
083            }
084    
085            /**
086             * Returns true if there is a java interface for the
087             * given OWL Class
088             */
089            public static boolean hasJavaType(java.lang.String uri) {
090                    return uri2Type.containsKey(uri);
091            }
092    
093            /**
094             * Deletes the individual with URI from the OntModel
095             */
096            public static boolean deleteInstance(java.lang.String uri, OntModel ontModel) {
097                    Individual individual = ontModel.getIndividual(uri);
098                    if (individual != null) {
099                            individual.remove();
100                            return true;
101                    }
102                    log.warn("Could not remove non existing instance " + uri + " from model");
103                    return false;
104            }
105    
106            /**
107             * Same as deleteInstance, but works with the default OntModel
108             * @see Factory#deleteInstance(java.lang.String, OntModel)
109             */
110            public static boolean deleteInstance(java.lang.String uri) {
111                    return deleteInstance(uri, getDefaultModel());
112            }
113    
114            /**
115             * Registers all custom classes with jena
116             */
117            public static void registerCustomClasses() {
118                    log.info("Registering custom classes with jena");
119                    eu.lod2.nlp2rdf.schema.Thing.register();
120                    eu.lod2.nlp2rdf.schema.sso.StopWord.register();
121                    eu.lod2.nlp2rdf.schema.error.Error.register();
122                    eu.lod2.nlp2rdf.schema.sso.Word.register();
123                    eu.lod2.nlp2rdf.schema.sso.Phrase.register();
124                    eu.lod2.nlp2rdf.schema.str.OffsetBasedString.register();
125                    eu.lod2.nlp2rdf.schema.str.Document.register();
126                    eu.lod2.nlp2rdf.schema.sso.Sentence.register();
127                    eu.lod2.nlp2rdf.schema.str.String.register();
128                    eu.lod2.nlp2rdf.schema.topic.Topic.register();
129                    eu.lod2.nlp2rdf.schema.str.ContextHashBasedString.register();
130            }
131    
132            /**
133             * Adds imports statements to an ontology and adds
134             * imported subModels to a model.
135             *
136             * Currently, this uses the namespace URI without trailing '#' or ':'
137             * as location.
138             */
139            public static void registerImports(Ontology ontology, OntModel ontModel) {
140                    log.info("Adding import statements to the model");
141                    OntDocumentManager odm = OntDocumentManager.getInstance();
142                    log.debug("Adding import http://nlp2rdf.lod2.eu/schema/string/ to the model");
143                    odm.loadImport(ontModel, "http://nlp2rdf.lod2.eu/schema/string/");
144                    ontology.addImport(ontModel.createResource("http://nlp2rdf.lod2.eu/schema/string/"));
145                    log.debug("Adding import file:/home/sebastian/svn/idea/nlp2rdf/ontologies/sso/sso-v1.0.ttl to the model");
146                    odm.loadImport(ontModel, "file:/home/sebastian/svn/idea/nlp2rdf/ontologies/sso/sso-v1.0.ttl");
147                    ontology.addImport(ontModel.createResource("file:/home/sebastian/svn/idea/nlp2rdf/ontologies/sso/sso-v1.0.ttl"));
148                    log.debug("Adding import http://nlp2rdf.lod2.eu/schema/error/ to the model");
149                    odm.loadImport(ontModel, "http://nlp2rdf.lod2.eu/schema/error/");
150                    ontology.addImport(ontModel.createResource("http://nlp2rdf.lod2.eu/schema/error/"));
151                    log.debug("Adding import http://nlp2rdf.lod2.eu/schema/sso/ to the model");
152                    odm.loadImport(ontModel, "http://nlp2rdf.lod2.eu/schema/sso/");
153                    ontology.addImport(ontModel.createResource("http://nlp2rdf.lod2.eu/schema/sso/"));
154                    log.debug("Adding import file:/home/sebastian/svn/idea/nlp2rdf/ontologies/topic/topic-v0.8.ttl to the model");
155                    odm.loadImport(ontModel, "file:/home/sebastian/svn/idea/nlp2rdf/ontologies/topic/topic-v0.8.ttl");
156                    ontology.addImport(ontModel.createResource("file:/home/sebastian/svn/idea/nlp2rdf/ontologies/topic/topic-v0.8.ttl"));
157                    log.debug("Adding import file:/home/sebastian/svn/idea/nlp2rdf/ontologies/string/string-v1.0.ttl to the model");
158                    odm.loadImport(ontModel, "file:/home/sebastian/svn/idea/nlp2rdf/ontologies/string/string-v1.0.ttl");
159                    ontology.addImport(ontModel.createResource("file:/home/sebastian/svn/idea/nlp2rdf/ontologies/string/string-v1.0.ttl"));
160                    log.debug("Adding import http://nlp2rdf.lod2.eu/schema/topic/ to the model");
161                    odm.loadImport(ontModel, "http://nlp2rdf.lod2.eu/schema/topic/");
162                    ontology.addImport(ontModel.createResource("http://nlp2rdf.lod2.eu/schema/topic/"));
163                    log.debug("Adding import http://ns.aksw.org/scms/ to the model");
164                    odm.loadImport(ontModel, "http://ns.aksw.org/scms/");
165                    ontology.addImport(ontModel.createResource("http://ns.aksw.org/scms/"));
166                    log.debug("Adding import http://www.w3.org/XML/1998/namespace to the model");
167                    odm.loadImport(ontModel, "http://www.w3.org/XML/1998/namespace");
168                    ontology.addImport(ontModel.createResource("http://www.w3.org/XML/1998/namespace"));
169                    log.debug("Adding import file:/home/sebastian/svn/idea/nlp2rdf/ontologies/error/error-v0.1.ttl to the model");
170                    odm.loadImport(ontModel, "file:/home/sebastian/svn/idea/nlp2rdf/ontologies/error/error-v0.1.ttl");
171                    ontology.addImport(ontModel.createResource("file:/home/sebastian/svn/idea/nlp2rdf/ontologies/error/error-v0.1.ttl"));
172            }
173    
174            /**
175             * Same as registerImports, but works with the default OntModel
176             * @see Factory#registerImports(Ontology, OntModel)
177             */
178            public void registerImports(Ontology ontology) {
179                    registerImports(ontology, getDefaultModel());
180            }
181    }