package de.dwslab.risk.gui.jgraphx.actions;

import static de.dwslab.ai.util.Utils.createTempFile;

import java.awt.event.ActionEvent;
import java.io.File;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.ProgressMonitor;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.googlecode.rockit.app.RockItAPI;
import com.googlecode.rockit.app.result.RockItResult;

import de.dwslab.risk.gui.RoCA;

public class RootCauseAnalysisAction extends AbstractAction {

    private static final long serialVersionUID = -8773333133469858955L;
    private static final Logger logger = LogManager.getLogger();

    private RoCA roca;

    public RootCauseAnalysisAction(RoCA roca) {
        this.roca = roca;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        RootCauseAnalysisWorker worker = new RootCauseAnalysisWorker(roca);
        worker.execute();
    }

    private static class RootCauseAnalysisWorker extends SwingWorker<Void, Void> {

        private final RoCA roca;
        private ProgressMonitor monitor;

        public RootCauseAnalysisWorker(RoCA roca) {
            this.roca = roca;
            monitor = new ProgressMonitor(
                    SwingUtilities.getWindowAncestor(roca),
                    "Running Root Cause Analysis...", "Please wait...", 0, 100);
            monitor.setMillisToPopup(0);
            monitor.setMillisToDecideToPopup(0);
        }

        @Override
        protected Void doInBackground() throws Exception {
            // Export the background knwoledge to temporary files
            File mlnFile = createTempFile("mln-", ".mln");
            File evidenceFile = createTempFile("evidence-", ".db");
            roca.getBackgroundKnowledge().exportAsMln(mlnFile.toPath(), evidenceFile.toPath());

            // Extend the MLN for abductive reasoning

            // Run RockIt
            RockItAPI rockit = new RockItAPI("src/main/resources/rockit.properties");
            List<RockItResult> mapState = rockit.doMapState(mlnFile.getAbsolutePath(),
                    evidenceFile.getAbsolutePath());

            // Process the result
            mapState.forEach(m -> System.err.println(m));

            return null;
        }

        @Override
        protected void done() {
            monitor.close();
        }

    }

}

@@ -511,24 +511,98 @@
 tResult;%0D%0A%0D%0A
+import de.dwslab.ai.riskmanagement.abduction.existential.ExistentialApi;%0D%0A
 import de.dw
@@ -2087,16 +2087,385 @@
 asoning%0D
+%0A            ExistentialApi api = new ExistentialApi();%0D%0A            File mlnExtFile = createTempFile(%22mln-%22, %22.mln%22);%0D%0A            File evidenceExtFile = createTempFile(%22evidence-%22, %22.db%22);%0D%0A            api.existenitalApi(mlnFile.getAbsolutePath(), evidenceFile.getAbsolutePath(),%0D%0A                    mlnExtFile.getAbsolutePath(), evidenceExtFile.getAbsolutePath());%0D
 %0A%0D%0A     
@@ -2632,24 +2632,27 @@
 MapState(mln
+Ext
 File.getAbso
@@ -2684,32 +2684,35 @@
         evidence
+Ext
 File.getAbsolute

Extend the MLN for the root cause analysis