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

it.unife.ml.probowlapi.monitor.ConsoleRendererTimeExplanationProgressMonitor Maven / Gradle / Ivy

Go to download

This package encapsulates OWL API adding tools for managing DISPONTE probabilistic semantics. Used by the reasoner BUNDLE.

The newest version!
/**
 *  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.ml.probowlapi.monitor;

import com.clarkparsia.owlapi.explanation.io.ExplanationRenderer;
import it.unife.ml.probowlapi.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 ConsoleRendererTimeExplanationProgressMonitor
        extends TimeMonitorImpl
        implements BundleRendererExplanationProgressMonitor2 {

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

    public ConsoleRendererTimeExplanationProgressMonitor(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 ConsoleRendererTimeExplanationProgressMonitor(int timeout) {
        this(timeout, new PrintWriter(System.out));
    }

    @Override
    public void foundExplanation(ExplanationGenerator generator, Explanation explanation, Set> allFoundExplanations) {
        setExplanations.add(explanation);
        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(OWLAxiom query) {
        try {
            rend.render(query, 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() {
        pw.flush();
//        if (super.isCancelled()) {
//            try {
//                rend.endRendering();
//            } catch (OWLException | IOException e) {
//                System.err.println("Error rendering explanation: " + e);
//            }
//        }
        return super.isCancelled();
    }

    @Override
    public Set> getFoundExplanations() {
        return setExplanations;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy