All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.unife.endif.ml.bundle.monitor.ConsoleTimeExplanationProgressMonitor Maven / Gradle / Ivy

/**
 *  This file is part of BUNDLE.
 *
 *  BUNDLE is a probabilistic reasoner for OWL 2 ontologies.
 *
 *  BUNDLE can be used both as module and as standalone.
 *
 *  BUNDLE and all its parts are distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see .
 *
 */
package it.unife.endif.ml.bundle.monitor;

import com.clarkparsia.owlapi.explanation.io.ExplanationRenderer;
import it.unife.endif.ml.bundle.renderer.ManchesterSyntaxExplanationRendererExt;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.semanticweb.owl.explanation.api.Explanation;
import org.semanticweb.owl.explanation.api.ExplanationGenerator;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLException;

/**
 *
 * @author Giuseppe Cota 
 * @param 
 */
public class ConsoleTimeExplanationProgressMonitor
        extends TimeMonitorImpl
        implements BundleRendererExplanationProgressMonitor2 {

//    private ExplanationRenderer rend = new ManchesterSyntaxExplanationRenderer();
    private ExplanationRenderer rend = new ManchesterSyntaxExplanationRendererExt();
    private OWLAxiom axiom;
    private Set> setExplanations;
    private PrintWriter pw;

    public ConsoleTimeExplanationProgressMonitor(int timeout, PrintWriter pw) {
        this.pw = pw;
        setExplanations = new HashSet<>();
        try {
            rend.startRendering(pw);

        } catch (OWLException | IOException e) {
            throw new RuntimeException("Error rendering explanation: " + e);
        }
    }

    public ConsoleTimeExplanationProgressMonitor(int timeout) {
        this(timeout, new PrintWriter(System.out));
    }

    @Override
    public void foundExplanation(ExplanationGenerator generator, Explanation explanation, Set> allFoundExplanations) {
        setExplanations.add(explanation.getAxioms());
        try {
            rend.render(explanation.getEntailment(), Collections.singleton(explanation.getAxioms()));
            pw.flush();
        } catch (IOException | OWLException e) {
            throw new RuntimeException("Error rendering explanation: " + e);
        }

    }

    @Override
    public void foundNoExplanations(Explanation explanation) {
        try {
//            rend.render(axiom, Collections.>emptySet());
            rend.render(explanation.getEntailment(), Collections.>emptySet());
            rend.endRendering();
            rend = null;
        } catch (OWLException e) {
            System.err.println("Error rendering explanation: " + e);
        } catch (IOException e) {
            System.err.println("Error rendering explanation: " + e);
        }
    }

    @Override
    public boolean isCancelled() {
        if (super.isCancelled()) {
            try {
                pw.flush();
                rend.endRendering();
            } catch (OWLException | IOException e) {
                System.err.println("Error rendering explanation: " + e);
            }
        }
        return super.isCancelled();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy