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.unife.edge;
007
008import java.math.BigDecimal;
009import java.util.ArrayList;
010import java.util.Collections;
011import java.util.List;
012import java.util.Set;
013import org.apache.log4j.Logger;
014import org.dllearner.core.ComponentAnn;
015import org.dllearner.core.ComponentInitException;
016import org.dllearner.core.probabilistic.unife.ParameterLearningException;
017import org.dllearner.learningproblems.ClassLearningProblem;
018import org.semanticweb.owlapi.model.OWLAxiom;
019//import unife.edge.EDGE;
020
021/**
022 * This class is a wrapper for EDGE algorithm. This algorithm uses BUNDLE for
023 * probabilistic reasoning. This class stores an instantiation of EDGE and
024 * invokes its methods in order to compute the parameters.
025 *
026 * @author Giuseppe Cota <giuseppe.cota@unife.it>, Riccardo Zese
027 * <riccardo.zese@unife.it>
028 */
029@ComponentAnn(name = "EDGE", shortName = "edge", version = 1.0)
030public class EDGE extends AbstractEDGE {
031
032    private static Logger logger
033            = Logger.getLogger(EDGE.class.getName());
034
035    private boolean fullyInitialized = false;
036
037    public EDGE() {
038        edge = new unife.edge.EDGE();
039    }
040
041    public EDGE(ClassLearningProblem lp, Set<OWLAxiom> targetAxioms) {
042        super(lp, targetAxioms);
043        edge = new unife.edge.EDGE();
044    }
045
046    @Override
047    public BigDecimal getParameter(OWLAxiom ax) {
048        BigDecimal parameter = super.getParameter(ax);
049        if (parameter == null) {
050            String msg = "the given axiom: " + ax.getAxiomWithoutAnnotations() + " is not probabilistic or does not exist";
051            logger.warn(msg);
052        }
053        return parameter;
054    }
055
056    @Override
057    public void init() throws ComponentInitException {
058        if (edge == null) {
059            String msg = "Underlying EDGE class not instantiated";
060            logger.error(msg);
061            throw new ComponentInitException(msg);
062        }
063        logger.debug("Initializing EDGE");
064        fullyInitialized = false;
065        super.init();
066
067    }
068
069    @Override
070    public void start() {
071        isRunning = true;
072        stop = false;
073
074        try {
075            if (!fullyInitialized) {
076                List<OWLAxiom> positiveExamplesList = new ArrayList<>(positiveExampleAxioms);
077                if (maxPositiveExamples > 0) {
078                    logger.debug("max number of positive examples to set: " + maxPositiveExamples);
079                    //List positiveIndividualsList = new ArrayList(positiveIndividuals);
080                    Collections.shuffle(positiveExamplesList);
081                    if (maxPositiveExamples < positiveExamplesList.size()) {
082                        positiveExamplesList = positiveExamplesList.subList(0, maxPositiveExamples);
083                    }
084                }
085                edge.setPositiveExamples(positiveExamplesList);
086                
087                List<OWLAxiom> negativeExamplesList = new ArrayList<>(negativeExampleAxioms);
088                if (maxNegativeExamples > 0) {
089                    logger.debug("max number of negative examples to set: " + maxNegativeExamples);
090                    Collections.shuffle(negativeExamplesList);
091                    if (maxNegativeExamples < negativeExamplesList.size()) {
092                        negativeExamplesList = negativeExamplesList.subList(0, maxNegativeExamples);
093                    }
094                }
095                edge.setNegativeExamples(negativeExamplesList);
096                edge.init();
097                fullyInitialized = true;
098            }
099            results = edge.computeLearning();
100        } catch (Exception ex) {
101            logger.error(ex.getMessage(), ex);
102            throw new ParameterLearningException(ex);
103        }
104        isRunning = false;
105    }
106    
107}