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;
020
021import org.aksw.jena_sparql_api.cache.h2.CacheUtilsH2;
022import org.aksw.jena_sparql_api.core.QueryExecutionFactory;
023import org.aksw.jena_sparql_api.model.QueryExecutionFactoryModel;
024import org.dllearner.core.ComponentAnn;
025
026import org.apache.jena.ontology.OntModel;
027import org.apache.jena.ontology.OntModelSpec;
028import org.apache.jena.rdf.model.Model;
029import org.apache.jena.rdf.model.ModelFactory;
030
031@ComponentAnn(name = "Local Endpoint", shortName = "local_sparql", version = 0.9)
032public class LocalModelBasedSparqlEndpointKS extends SparqlEndpointKS {
033        
034        private OntModel model;
035        
036        public LocalModelBasedSparqlEndpointKS() {}
037        
038        /**
039         * Create new Sparql Endpoint based on Jena Ontology
040         * @param model ontology model
041         */
042        public LocalModelBasedSparqlEndpointKS(OntModel model) {
043                this.model = model;
044        }
045        
046        /**
047         * Create new Sparql Endpoint based on Jena model
048         * @param model rdf model
049         * @param reasoningEnabled whether to use Jena RDFS inferencing
050         */
051        public LocalModelBasedSparqlEndpointKS(Model model, boolean reasoningEnabled) {
052                this(model, reasoningEnabled ? OntModelSpec.OWL_MEM_RDFS_INF : OntModelSpec.OWL_MEM);
053        }
054        
055        /**
056         * Create new Sparql Endpoint based on Jena Model and reasoning spec
057         * @param model rdf model
058         * @param reasoning type of reasoning
059         */
060        public LocalModelBasedSparqlEndpointKS(Model model, OntModelSpec reasoning) {
061                this.model = ModelFactory.createOntologyModel(reasoning, model);
062        }
063        
064        /**
065         * Create new Sparql Endpoint based on Jena Model. No reasoning enabled by default.
066         * @param model rdf model
067         */
068        public LocalModelBasedSparqlEndpointKS(Model model) {
069                this(model, false);
070        }
071        
072        /* (non-Javadoc)
073         * @see org.dllearner.kb.SparqlEndpointKS#buildQueryExecutionFactory()
074         */
075        @Override
076        protected QueryExecutionFactory buildQueryExecutionFactory() {
077                QueryExecutionFactory qef = new QueryExecutionFactoryModel(model);
078                
079                // we are working on an in-memory model, but still should enable caching by default
080                qef = CacheUtilsH2.createQueryExecutionFactory(qef, cacheDir, true, cacheTTL);
081                
082                return qef;
083        }
084        
085        public OntModel getModel() {
086                return model;
087        }
088        
089        @Override
090        public boolean isRemote() {
091                return false;
092        }
093        
094        @Override
095        public boolean supportsSPARQL_1_1() {
096                return true;
097        }
098        
099        @Override
100        public String toString() {
101                String out = String.format("%-15s %-25s%n", "Endpoint:", "in-memory model");
102                out += String.format("%-15s %-25s%n", "Profile:", model.getProfile());
103                return out;
104        }
105}