001/*
002 * To change this license header, choose License Headers in Project Properties.
003 * To change this template file, choose Tools | Templates
004 * and open the template in the editor.
005 */
006package org.dllearner.algorithms.probabilistic.parameter.distributed.unife.edge;
007
008import java.math.BigDecimal;
009import java.util.ArrayList;
010import java.util.Collections;
011import java.util.List;
012import org.apache.log4j.Logger;
013import org.dllearner.core.ComponentAnn;
014import org.dllearner.core.ComponentInitException;
015import org.dllearner.core.config.ConfigOption;
016import org.dllearner.core.probabilistic.unife.ParameterLearningException;
017import org.dllearner.core.probabilistic.distributed.unife.AbstractEDGEDistributed;
018import org.semanticweb.owlapi.model.OWLAxiom;
019import unife.bundle.logging.BundleLoggerFactory;
020import unife.edge.EDGEMPIDynamic;
021
022/**
023 *
024 * @author Giuseppe Cota <giuseppe.cota@unife.it>, Riccardo Zese
025 * <riccardo.zese@unife.it>
026 */
027@ComponentAnn(name = "EDGEDistributedDynamic", shortName = "edgedynamic", version = 1.0)
028public class EDGEDistributedDynamic extends AbstractEDGEDistributed {
029
030    private static Logger logger
031            = Logger.getLogger(EDGEDistributedDynamic.class.getName(), new BundleLoggerFactory());
032
033    @ConfigOption(defaultValue = "1", description = "number of example for chunk")
034    private int chunkDim = 1;
035
036    @ConfigOption(description = " max number of concurrent threads which send examples to the slaves", defaultValue = "#processors - 1")
037    private int maxSenderThreads;
038
039    private boolean fullyInitialized = false;
040
041    public EDGEDistributedDynamic() {
042        edge = new EDGEMPIDynamic();
043    }
044
045    @Override
046    public void init() throws ComponentInitException {
047        fullyInitialized = false;
048        super.init();
049        if (maxSenderThreads == 0) {
050            maxSenderThreads = Runtime.getRuntime().availableProcessors() - 1;
051            if (maxSenderThreads == 0) {
052                maxSenderThreads = 1;
053            }
054        }
055    }
056
057    @Override
058    public void start() {
059        isRunning = true;
060        stop = false;
061
062        try {
063            EDGEMPIDynamic edgeDyn = (EDGEMPIDynamic) edge;
064            if (!fullyInitialized) {
065                List<OWLAxiom> positiveExamplesList = new ArrayList<>(positiveExampleAxioms);
066                if (maxPositiveExamples > 0) {
067                    logger.debug("max positive examples set: " + maxPositiveExamples);
068                    //List positiveIndividualsList = new ArrayList(positiveIndividuals);
069                    Collections.shuffle(positiveExamplesList);
070                    if (maxPositiveExamples < positiveExamplesList.size()) {
071                        positiveExamplesList = positiveExamplesList.subList(0, maxPositiveExamples);
072                    }
073                }
074                List<OWLAxiom> negativeExamplesList = new ArrayList<>(negativeExampleAxioms);
075                if (maxNegativeExamples > 0) {
076                    logger.debug("max negative examples set: " + maxNegativeExamples);
077                    Collections.shuffle(negativeExamplesList);
078                    if (maxNegativeExamples < negativeExamplesList.size()) {
079                        negativeExamplesList = negativeExamplesList.subList(0, maxNegativeExamples);
080                    }
081                }
082                edge.setPositiveExamples(positiveExamplesList);
083                edge.setNegativeExamples(negativeExamplesList);
084                edgeDyn.setChunkDim(chunkDim);
085                edgeDyn.setMaxSenderThreads(getMaxSenderThreads());
086                edgeDyn.setMaxSenderThreads(maxSenderThreads);
087                edgeDyn.setComm(comm);
088                fullyInitialized = true;
089                logger.debug("qui");
090            }
091            logger.debug("qui");
092            //logger.debug(PelletOptions.USE_TRACING);
093            results = edgeDyn.computeLearning();
094            //logger.debug(PelletOptions.USE_TRACING);
095//            logger.info("Log-Likelihood " + results.getLL());
096//            Map<String, Long> timers = results.getTimers();
097        } catch (Exception ex) {
098            logger.error(ex.getMessage(), ex);
099            throw new ParameterLearningException(ex);
100        }
101        isRunning = false;
102    }
103
104    /**
105     * @return the chunkDim
106     */
107    public int getChunkDim() {
108        return chunkDim;
109    }
110
111    /**
112     * @param chunkDim the chunkDim to set
113     */
114    public void setChunkDim(int chunkDim) {
115        this.chunkDim = chunkDim;
116    }
117
118    /**
119     * @return the maxSenderThreads
120     */
121    public int getMaxSenderThreads() {
122        return maxSenderThreads;
123    }
124
125    /**
126     * @param maxSenderThreads the maxSenderThreads to set
127     */
128    public void setMaxSenderThreads(int maxSenderThreads) {
129        this.maxSenderThreads = maxSenderThreads;
130    }
131
132//    @Override
133//    public void reset() {
134//        super.reset();
135//        isRunning = false;
136//        stop = false;
137//    }
138
139    @Override
140    public BigDecimal getParameter(OWLAxiom ax) {
141        BigDecimal parameter = super.getParameter(ax);
142        if (parameter == null) {
143            String msg = "the given axiom: " + ax.getAxiomWithoutAnnotations() + " is not probabilistic or does not exist";
144            logger.warn(msg);
145        }
146        return parameter;
147    }
148}