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.split;
020
021import java.util.ArrayList;
022import java.util.LinkedList;
023import java.util.List;
024import java.util.Map;
025import java.util.Map.Entry;
026import java.util.SortedSet;
027
028import org.dllearner.core.AbstractReasonerComponent;
029import org.dllearner.utilities.OWLAPIUtils;
030import org.joda.time.DateTime;
031import org.joda.time.format.DateTimeFormatter;
032import org.semanticweb.owlapi.model.OWLDataFactory;
033import org.semanticweb.owlapi.model.OWLDataProperty;
034import org.semanticweb.owlapi.model.OWLDatatype;
035import org.semanticweb.owlapi.model.OWLIndividual;
036import org.semanticweb.owlapi.model.OWLLiteral;
037
038/**
039 * A splitter for date time values which simply returns a fixed number of split
040 * values.
041 
042 * @author Lorenz Buehmann
043 *
044 */
045public class DefaultDateTimeValuesSplitter extends AbstractDateTimeValuesSplitter {
046        
047        private int maxNrOfSplits = 10;
048
049        public DefaultDateTimeValuesSplitter(AbstractReasonerComponent reasoner, OWLDataFactory dataFactory, int maxNrOfSplits) {
050                super(reasoner, dataFactory);
051                this.maxNrOfSplits = maxNrOfSplits;
052        }
053
054        /**
055         * @param maxNrOfSplits the maximal number of splits
056         */
057        public void setMaxNrOfSplits(int maxNrOfSplits) {
058                this.maxNrOfSplits = maxNrOfSplits;
059        }
060        
061        /* (non-Javadoc)
062         * @see org.dllearner.utilities.split.AbstractValuesSplitter#computeSplits(org.semanticweb.owlapi.model.OWLDataProperty)
063         */
064        @Override
065        public List<OWLLiteral> computeSplits(OWLDataProperty dp) {
066                List<OWLLiteral> splitLiterals = new ArrayList<>();
067                
068                Map<OWLIndividual, SortedSet<OWLLiteral>> ind2Values = reasoner.getDatatypeMembers(dp);
069                
070                OWLDatatype datatype = reasoner.getDatatype(dp);
071                
072                DateTimeFormatter parser = OWLAPIUtils.dateTimeParsers.get(datatype);
073                DateTimeFormatter formatter = OWLAPIUtils.dateTimeFormatters.get(datatype);
074                
075                List<DateTime> values = new LinkedList<>();
076                for (Entry<OWLIndividual, SortedSet<OWLLiteral>> entry : ind2Values.entrySet()) {
077                        
078                        for (OWLLiteral value : entry.getValue()) {
079                                DateTime dateTime = parser.parseDateTime(value.getLiteral());
080                                values.add(dateTime);
081                        }
082                        
083                }
084                
085                List<DateTime> splitValues = simpleListSplitter(values, maxNrOfSplits);
086                
087                for (DateTime value : splitValues) {
088                        OWLLiteral literal = dataFactory.getOWLLiteral(value.toString(formatter), reasoner.getDatatype(dp));
089                        splitLiterals.add(literal);
090                }
091                
092                return splitLiterals;
093        }
094        
095        private DateTime computeSplitValue(DateTime value1, DateTime value2){
096                return value1;
097        }
098
099        @Override
100        protected <T> T mixTwoValues(T value1, T value2) {
101                return (T)computeSplitValue((DateTime)value1, (DateTime)value2);
102        }
103}