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 org.dllearner.core.AbstractReasonerComponent;
022import org.dllearner.utilities.OWLAPIUtils;
023import org.semanticweb.owlapi.model.OWLDataFactory;
024import org.semanticweb.owlapi.model.OWLDataProperty;
025import org.semanticweb.owlapi.model.OWLDatatype;
026
027import java.math.BigDecimal;
028import java.math.RoundingMode;
029import java.util.Set;
030
031/**
032 * @author Lorenz Buehmann
033 *
034 */
035public abstract class AbstractNumericValuesSplitter extends AbstractValuesSplitter{
036
037        public AbstractNumericValuesSplitter(AbstractReasonerComponent reasoner, OWLDataFactory dataFactory) {
038                super(reasoner, dataFactory);
039        }
040
041        /* (non-Javadoc)
042         * @see org.dllearner.utilities.split.AbstractValuesSplitter#getDataProperties()
043         */
044        @Override
045        public Set<OWLDataProperty> getDataProperties() {
046                return reasoner.getNumericDataProperties();
047        }
048        
049        /* (non-Javadoc)
050         * @see org.dllearner.utilities.split.AbstractValuesSplitter#getDatatypes()
051         */
052        @Override
053        public Set<OWLDatatype> getDatatypes() {
054                return OWLAPIUtils.numericDatatypes;
055        }
056
057        @Override
058        protected <T> T mixTwoValues(T value1, T value2) {
059                return avg(value1, value2);
060        }
061
062        @SuppressWarnings("UnnecessaryUnboxing")
063        private <T> T avg(T number1, T number2) {
064                T avg = null;
065                if (number1 instanceof Integer && number2 instanceof Integer){
066                        avg = (T) Integer.valueOf(((Integer) number1 + (Integer) number2) / 2);
067                } else if (number1 instanceof Short && number2 instanceof Short){
068                        avg = (T) Short.valueOf((short) (((Short) number1 + (Short) number2) / 2));
069                } else if (number1 instanceof Byte && number2 instanceof Byte){
070                        avg = (T) Byte.valueOf((byte) (((Byte) number1 + (Byte) number2) / 2));
071                } else if (number1 instanceof Long && number2 instanceof Long){
072                        avg = (T) Long.valueOf(((Long) number1 + (Long) number2) / 2);
073                } else if (number1 instanceof Double && number2 instanceof Double) {
074                        avg = (T) Double.valueOf((BigDecimal.valueOf(((Double)number1).doubleValue()).add(
075                                        BigDecimal.valueOf(((Double)number2).doubleValue())).divide(BigDecimal.valueOf(2), RoundingMode.HALF_DOWN)).doubleValue());
076                } else if(number1 instanceof Float && number2 instanceof Float) {
077                        avg = (T) Float.valueOf(
078                                        (BigDecimal.valueOf(((Float)number1).floatValue()).
079                        add(BigDecimal.valueOf(((Float)number2).floatValue())).divide(
080                                        BigDecimal.valueOf(2d), RoundingMode.HALF_DOWN)).floatValue());
081                }
082                return avg;
083        }
084
085}