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.algorithms.pattern;
020
021import org.aksw.jena_sparql_api.core.QueryExecutionFactory;
022import org.aksw.jena_sparql_api.pagination.core.PaginationUtils;
023import org.apache.jena.query.ParameterizedSparqlString;
024import org.apache.jena.query.Query;
025import org.apache.jena.rdf.model.Model;
026import org.apache.jena.rdf.model.ModelFactory;
027import org.dllearner.kb.SparqlEndpointKS;
028import org.semanticweb.owlapi.model.OWLClass;
029
030import java.util.concurrent.TimeUnit;
031
032/**
033 * @author Lorenz Buehmann
034 *
035 */
036public class TimeBasedFragmentExtractor implements FragmentExtractor{
037        
038        public static final FragmentExtractionStrategy extractionStrategy = FragmentExtractionStrategy.TIME;
039        private SparqlEndpointKS ks;
040        private QueryExecutionFactory qef;
041        
042        private long maxExecutionTimeInMilliseconds;
043        private long startTime;
044        
045        public TimeBasedFragmentExtractor(SparqlEndpointKS ks, int maxExecutionTimeInMilliseconds, TimeUnit timeUnit) {
046                this.ks = ks;
047                this.maxExecutionTimeInMilliseconds = timeUnit.toMillis(maxExecutionTimeInMilliseconds);
048
049                qef = ks.getQueryExecutionFactory();
050        }
051
052        /* (non-Javadoc)
053         * @see org.dllearner.algorithms.pattern.FragmentExtractor#extractFragment(org.dllearner.core.owl.NamedClass)
054         */
055        @Override
056        public Model extractFragment(OWLClass cls, int maxFragmentDepth) {
057                startTime = System.currentTimeMillis();
058                Model fragment = ModelFactory.createDefaultModel();
059                
060                Query query = buildConstructQuery(cls, maxFragmentDepth);
061                
062                long pageSize = PaginationUtils.adjustPageSize(qef, 10000);
063                query.setLimit(pageSize);
064                int offset = 0;
065                while(getRemainingRuntime() > 0){
066                        query.setOffset(offset);System.out.println(query);
067                        Model model = qef.createQueryExecution(query).execConstruct();
068                        fragment.add(model);
069                        offset += pageSize;
070                }
071                return fragment;
072        }
073        
074        private Query buildConstructQuery(OWLClass cls, int depth){
075                StringBuilder sb = new StringBuilder();
076                int maxVarCnt = 0;
077                sb.append("CONSTRUCT {\n");
078                sb.append("?s").append("?p0 ").append("?o0").append(".\n");
079                for(int i = 1; i < depth-1; i++){
080                        sb.append("?o").append(i-1).append(" ").append("?p").append(i).append(" ").append("?o").append(i).append(".\n");
081                        maxVarCnt++;
082                }
083                sb.append("?o").append(maxVarCnt).append(" a ?type.\n");
084                sb.append("}\n");
085                sb.append("WHERE {\n");
086                sb.append("?s a ?cls.");
087                sb.append("?s").append("?p0 ").append("?o0").append(".\n");
088                for(int i = 1; i < depth-1; i++){
089                        sb.append("OPTIONAL{\n");
090                        sb.append("?o").append(i-1).append(" ").append("?p").append(i).append(" ").append("?o").append(i).append(".\n");
091                }
092                sb.append("OPTIONAL{?o").append(maxVarCnt).append(" a ?type}.\n");
093                for(int i = 1; i < depth-1; i++){
094                        sb.append("}");
095                }
096                
097                sb.append("}\n");
098                ParameterizedSparqlString template = new ParameterizedSparqlString(sb.toString());
099                template.setIri("cls", cls.toStringID());
100                return template.asQuery();
101        }
102        
103        private long getRemainingRuntime(){
104                return maxExecutionTimeInMilliseconds - (System.currentTimeMillis() - startTime);
105        }
106
107}