From 8c42e44c983922dca7dde35ce1f7b27fb053f612 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 23 Jul 2016 20:09:13 +0300 Subject: [PATCH] Completed action result caching. Fixed goals parallelism. --- .../actions/ReadNumassStorageAction.java | 9 +-- .../models/TransmissionInterpolator.java | 2 +- .../java/inr/numass/tasks/PrepareTask.java | 2 +- .../java/inr/numass/test/TestLazyData.java | 41 ---------- .../workbench/NumassWorkbenchController.java | 79 +++++++++++++------ .../main/resources/fxml/NumassWorkbench.fxml | 13 +-- .../numass/viewer/MainViewerController.java | 8 +- .../inr/numass/viewer/MspViewController.java | 2 +- .../viewer/NumassLoaderTreeBuilder.java | 2 +- .../viewer/NumassLoaderViewComponent.java | 2 +- 10 files changed, 72 insertions(+), 88 deletions(-) delete mode 100644 numass-main/src/main/java/inr/numass/test/TestLazyData.java diff --git a/numass-main/src/main/java/inr/numass/actions/ReadNumassStorageAction.java b/numass-main/src/main/java/inr/numass/actions/ReadNumassStorageAction.java index d1d29ba9..30343620 100644 --- a/numass-main/src/main/java/inr/numass/actions/ReadNumassStorageAction.java +++ b/numass-main/src/main/java/inr/numass/actions/ReadNumassStorageAction.java @@ -6,13 +6,12 @@ package inr.numass.actions; import hep.dataforge.actions.GenericAction; -import hep.dataforge.work.Work; -import hep.dataforge.work.WorkManager.Callback; +import hep.dataforge.computation.Work; +import hep.dataforge.computation.WorkManager.Callback; import hep.dataforge.data.Data; import hep.dataforge.data.DataFilter; import hep.dataforge.data.DataNode; import hep.dataforge.data.DataSet; -import hep.dataforge.data.StaticData; import hep.dataforge.description.TypedActionDef; import hep.dataforge.description.ValueDef; import hep.dataforge.meta.Meta; @@ -53,7 +52,7 @@ public class ReadNumassStorageAction extends GenericAction { Loader loader = pair.getValue(); if (loader instanceof NumassData) { NumassDataLoader nd = (NumassDataLoader) loader; - Data datum = new StaticData<>(nd); + Data datum = Data.buildStatic(nd); if (filter.acceptData(pair.getKey(), datum)) { boolean accept = true; if (forwardOnly || reverseOnly) { @@ -71,7 +70,7 @@ public class ReadNumassStorageAction extends GenericAction { if (actionMeta.getBoolean("loadLegacy", false)) { logger().info("Loading legacy files"); storage.legacyFiles().forEach(nd -> { - Data datum = new StaticData<>(nd); + Data datum = Data.buildStatic(nd); if (filter.acceptData(nd.getName(), datum)) { builder.putData("legacy." + nd.getName(), datum); } diff --git a/numass-main/src/main/java/inr/numass/models/TransmissionInterpolator.java b/numass-main/src/main/java/inr/numass/models/TransmissionInterpolator.java index 2de9affb..e6b1b19e 100644 --- a/numass-main/src/main/java/inr/numass/models/TransmissionInterpolator.java +++ b/numass-main/src/main/java/inr/numass/models/TransmissionInterpolator.java @@ -51,7 +51,7 @@ public class TransmissionInterpolator implements UnivariateFunction { public static TransmissionInterpolator fromAction(Context context, Meta actionAnnotation, String xName, String yName, int nSmooth, double w, double border) throws InterruptedException { DataNode node = ActionUtils.runConfig(context, actionAnnotation); - PointSource data = node.getData().getNow(); + PointSource data = node.getData().get(); return new TransmissionInterpolator(data, xName, yName, nSmooth, w, border); } diff --git a/numass-main/src/main/java/inr/numass/tasks/PrepareTask.java b/numass-main/src/main/java/inr/numass/tasks/PrepareTask.java index ee21eec5..df933fa6 100644 --- a/numass-main/src/main/java/inr/numass/tasks/PrepareTask.java +++ b/numass-main/src/main/java/inr/numass/tasks/PrepareTask.java @@ -7,7 +7,7 @@ package inr.numass.tasks; import hep.dataforge.actions.Action; import hep.dataforge.context.Context; -import hep.dataforge.work.WorkManager; +import hep.dataforge.computation.WorkManager; import hep.dataforge.data.DataNode; import hep.dataforge.data.DataTree; import hep.dataforge.meta.Meta; diff --git a/numass-main/src/main/java/inr/numass/test/TestLazyData.java b/numass-main/src/main/java/inr/numass/test/TestLazyData.java deleted file mode 100644 index 59f1d8ba..00000000 --- a/numass-main/src/main/java/inr/numass/test/TestLazyData.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package inr.numass.test; - -import hep.dataforge.actions.ActionController; -import java.util.concurrent.CompletableFuture; - -/** - * - * @author Alexander Nozik - */ -public class TestLazyData { - - /** - * @param args the command line arguments - */ - public static void main(String[] args) { - ActionController lock = new ActionController(); - CompletableFuture future = lock.hold(() -> { - System.out.println(" I am initialized"); - return "abcd"; - }); - future = future.thenCompose((String res) -> CompletableFuture.supplyAsync(() -> { - System.out.println(" I am capitalized"); - return res.toUpperCase(); - })); - future = future.handleAsync((res, err) -> { - System.out.println(" I am handled"); - return "handled " + res; - }); - System.out.println("Releasing hold"); - lock.release(); - -// dat1.getInFuture().thenRunAsync(() -> System.out.println(" I am finished")); -// dat1.getInFuture().thenAcceptAsync((res) -> System.out.println(" I am finished")); - } - -} diff --git a/numass-main/src/main/java/inr/numass/workbench/NumassWorkbenchController.java b/numass-main/src/main/java/inr/numass/workbench/NumassWorkbenchController.java index 3b19fc97..b2be8299 100644 --- a/numass-main/src/main/java/inr/numass/workbench/NumassWorkbenchController.java +++ b/numass-main/src/main/java/inr/numass/workbench/NumassWorkbenchController.java @@ -20,7 +20,7 @@ import hep.dataforge.fx.ConsoleFragment; import hep.dataforge.fx.FXDataOutputPane; import hep.dataforge.fx.FXReportListener; import hep.dataforge.fx.configuration.MetaEditor; -import hep.dataforge.fx.process.ProcessManagerFragment; +import hep.dataforge.fx.work.WorkManagerFragment; import hep.dataforge.io.IOManager; import hep.dataforge.io.MetaFileReader; import hep.dataforge.meta.ConfigChangeListener; @@ -45,13 +45,15 @@ import java.util.ResourceBundle; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; import javafx.application.Platform; +import javafx.beans.property.BooleanProperty; +import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.Node; import javafx.scene.control.Accordion; import javafx.scene.control.Alert; -import javafx.scene.control.Button; import javafx.scene.control.Tab; import javafx.scene.control.TabPane; import javafx.scene.control.TitledPane; @@ -78,10 +80,12 @@ public class NumassWorkbenchController implements Initializable, StagePaneHolder Map stages = new ConcurrentHashMap<>(); - ProcessManagerFragment processWindow; + WorkManagerFragment processWindow; FXDataOutputPane logPane; + BooleanProperty isRunning = new SimpleBooleanProperty(false); + @FXML private StatusBar statusBar; @FXML @@ -91,7 +95,7 @@ public class NumassWorkbenchController implements Initializable, StagePaneHolder @FXML private Tab logTab; @FXML - private Button runButton; + private ToggleButton runButton; @FXML private ToggleButton consoleButton; @FXML @@ -121,14 +125,23 @@ public class NumassWorkbenchController implements Initializable, StagePaneHolder consoleWindow.addRootLogHandler(); consoleWindow.hookStd(); - processWindow = new ProcessManagerFragment(); + processWindow = new WorkManagerFragment(); processWindow.bindTo(processButton); + isRunning.addListener((ObservableValue observable, Boolean oldValue, Boolean newValue) -> { + runButton.setSelected(newValue); + if (newValue) { + runButton.setText("Stop"); + } else { + runButton.setText("Run"); + } + }); + } public Context getContext() { if (context == null) { - return GlobalContext.instance(); + throw new RuntimeException("Context not defined"); } else { return context; } @@ -158,7 +171,6 @@ public class NumassWorkbenchController implements Initializable, StagePaneHolder context.setIO(new WorkbenchIOManager(new NumassIO(), this)); buildContextPane(); context.getReport().addReportListener(new FXReportListener(logPane)); - // display plots iside workbench PlotsPlugin.buildFrom(context).setPlotHolderDelegate(this); @@ -322,9 +334,13 @@ public class NumassWorkbenchController implements Initializable, StagePaneHolder @FXML private void onRunButtonClick(ActionEvent event) { - if (getContext() != null && !actionsConfig.isEmpty()) { - statusBar.setText("Starting action execution"); - runActions(); + if (!isRunning.get()) { + if (context != null && !actionsConfig.isEmpty()) { + statusBar.setText("Starting action execution"); + runActions(); + } + } else { + this.context.workManager().shutdown(); } } @@ -341,27 +357,42 @@ public class NumassWorkbenchController implements Initializable, StagePaneHolder clearAllStages(); // processWindow.show(); new Thread(() -> { + + Platform.runLater(() -> { + isRunning.set(true); + statusBar.setProgress(-1); + }); + DataNode data = new FileDataFactory().build(getContext(), getDataConfiguration()); - Platform.runLater(() -> statusBar.setProgress(-1)); try { ActionUtils.runAction(getContext(), data, getActionConfiguration()).compute(); Platform.runLater(() -> statusBar.setText("Execution complete")); } catch (Exception ex) { - GlobalContext.instance().getLogger().error("Exception while executing action chain", ex); - Platform.runLater(() -> { - //printing stack trace to the default output + if (ex instanceof java.util.concurrent.CancellationException) { + //cach cancelation exception + GlobalContext.instance().getLogger().info("Interrupted by user"); + Platform.runLater(() -> { + statusBar.setText("Execution interrupted by user"); + }); + } else { + //cach all other exceptions + GlobalContext.instance().getLogger().error("Exception while executing action chain", ex); ex.printStackTrace(System.err); -// ex.printStackTrace();//?? - statusBar.setText("Execution failed"); - Alert alert = new Alert(Alert.AlertType.ERROR); - alert.setTitle("Exception!"); - alert.setHeaderText("Action execution failure"); - alert.setContentText(ex.getMessage()); - alert.show(); - - }); + Platform.runLater(() -> { + //printing stack trace to the default output + statusBar.setText("Execution failed"); + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Exception!"); + alert.setHeaderText("Action execution failure"); + alert.setContentText(ex.getMessage()); + alert.show(); + }); + } } finally { - Platform.runLater(() -> statusBar.setProgress(0)); + Platform.runLater(() -> { + isRunning.set(false); + statusBar.setProgress(0); + }); } }, "actions").start(); diff --git a/numass-main/src/main/resources/fxml/NumassWorkbench.fxml b/numass-main/src/main/resources/fxml/NumassWorkbench.fxml index 14bb7411..e92e7e88 100644 --- a/numass-main/src/main/resources/fxml/NumassWorkbench.fxml +++ b/numass-main/src/main/resources/fxml/NumassWorkbench.fxml @@ -9,14 +9,13 @@ - + - - + @@ -44,14 +43,10 @@ - + + diff --git a/numass-viewer/src/main/java/inr/numass/viewer/MainViewerController.java b/numass-viewer/src/main/java/inr/numass/viewer/MainViewerController.java index b67bbeba..dac79f6d 100644 --- a/numass-viewer/src/main/java/inr/numass/viewer/MainViewerController.java +++ b/numass-viewer/src/main/java/inr/numass/viewer/MainViewerController.java @@ -17,10 +17,10 @@ package inr.numass.viewer; import hep.dataforge.context.Context; import hep.dataforge.context.GlobalContext; -import hep.dataforge.work.WorkManager; +import hep.dataforge.computation.WorkManager; import hep.dataforge.exceptions.StorageException; import hep.dataforge.fx.ConsoleFragment; -import hep.dataforge.fx.process.ProcessManagerFragment; +import hep.dataforge.fx.work.WorkManagerFragment; import inr.numass.NumassProperties; import inr.numass.storage.NumassData; import inr.numass.storage.NumassStorage; @@ -66,7 +66,7 @@ public class MainViewerController implements Initializable { } // private ConsoleFragment consoleFragment; -// private ProcessManagerFragment processFragment = ProcessManagerFragment.attachToContext(GlobalContext.instance()); +// private WorkManagerFragment processFragment = WorkManagerFragment.attachToContext(GlobalContext.instance()); @FXML private ToggleButton consoleButton; @FXML @@ -115,7 +115,7 @@ public class MainViewerController implements Initializable { ConsoleFragment consoleFragment = new ConsoleFragment(); consoleFragment.hookStd(); consoleFragment.bindTo(consoleButton); - ProcessManagerFragment.attachToContext(GlobalContext.instance()).bindTo(processManagerButton); + WorkManagerFragment.attachToContext(GlobalContext.instance()).bindTo(processManagerButton); } @FXML diff --git a/numass-viewer/src/main/java/inr/numass/viewer/MspViewController.java b/numass-viewer/src/main/java/inr/numass/viewer/MspViewController.java index 84b2d1d1..da915ea0 100644 --- a/numass-viewer/src/main/java/inr/numass/viewer/MspViewController.java +++ b/numass-viewer/src/main/java/inr/numass/viewer/MspViewController.java @@ -21,7 +21,7 @@ package inr.numass.viewer; * and open the template in the editor. */ import hep.dataforge.context.Context; -import hep.dataforge.work.WorkManager; +import hep.dataforge.computation.WorkManager; import hep.dataforge.exceptions.StorageException; import hep.dataforge.plots.PlotUtils; import hep.dataforge.plots.data.DynamicPlottable; diff --git a/numass-viewer/src/main/java/inr/numass/viewer/NumassLoaderTreeBuilder.java b/numass-viewer/src/main/java/inr/numass/viewer/NumassLoaderTreeBuilder.java index d3b2dac7..c7f61ae0 100644 --- a/numass-viewer/src/main/java/inr/numass/viewer/NumassLoaderTreeBuilder.java +++ b/numass-viewer/src/main/java/inr/numass/viewer/NumassLoaderTreeBuilder.java @@ -15,7 +15,7 @@ */ package inr.numass.viewer; -import hep.dataforge.work.WorkManager; +import hep.dataforge.computation.WorkManager; import hep.dataforge.exceptions.StorageException; import inr.numass.storage.NumassData; import inr.numass.storage.NumassStorage; diff --git a/numass-viewer/src/main/java/inr/numass/viewer/NumassLoaderViewComponent.java b/numass-viewer/src/main/java/inr/numass/viewer/NumassLoaderViewComponent.java index 6c808a75..f0e1c377 100644 --- a/numass-viewer/src/main/java/inr/numass/viewer/NumassLoaderViewComponent.java +++ b/numass-viewer/src/main/java/inr/numass/viewer/NumassLoaderViewComponent.java @@ -21,7 +21,7 @@ package inr.numass.viewer; * and open the template in the editor. */ import hep.dataforge.context.Context; -import hep.dataforge.work.WorkManager; +import hep.dataforge.computation.WorkManager; import hep.dataforge.io.ColumnedDataWriter; import hep.dataforge.meta.Meta; import hep.dataforge.meta.MetaBuilder;