Xcos: launch a simulation 59/17459/3
Clément DAVID [Tue, 17 Nov 2015 08:02:05 +0000 (09:02 +0100)]
This commit also manage :
 * `scicos_new("0x2")` for Scilab calls from Java using a Java long
 * DEBUG_LEVEL as a model property

Change-Id: I1226b6ac0fd530304bfcca785ce5ee15f823386b

scilab/modules/scicos/includes/utilities.hxx
scilab/modules/scicos/sci_gateway/cpp/sci_scicos_new.cpp
scilab/modules/scicos/src/cpp/Model_getObjectProperties.cpp
scilab/modules/scicos/src/cpp/Model_setObjectProperties.cpp
scilab/modules/scicos/src/cpp/model/Diagram.hxx
scilab/modules/xcos/src/java/org/scilab/modules/xcos/ObjectProperties.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/actions/StartAction.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/block/actions/BlockParametersAction.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/utils/XcosMessages.java

index 1357d37..6562411 100644 (file)
@@ -105,6 +105,7 @@ enum object_properties_t
     TITLE,              //!< model::Diagram::title file name value
     PATH,               //!< model::Diagram::title file path value
     PROPERTIES,         //!< model::Diagram::tol & tf values
+    DEBUG_LEVEL,        //!< model::Diagram::debug_level value
     DIAGRAM_CONTEXT,    //!< model::Diagram::context value
     VERSION_NUMBER,     //!< model::Diagram::version value
     MAX_OBJECT_PROPERTIES //!< last valid value of the object_properties_t enum
index 3c38012..b17cd9f 100644 (file)
@@ -245,7 +245,7 @@ static ScicosID get(types::GenericType* UIDs, int index)
         {
             wchar_t* str = UIDs->getAs<types::String>()->get(index);
             std::wistringstream iss(str);
-            iss >> ret;
+            iss >> std::hex >> ret;
             break;
         }
         case types::InternalType::ScilabInt64:
index 0053790..8bd036b 100644 (file)
@@ -111,8 +111,12 @@ bool Model::getObjectProperty(ScicosID uid, kind_t k, object_properties_t p, int
     }
     else if (k == DIAGRAM)
     {
+        model::Diagram* o = static_cast<model::Diagram*>(baseObject);
         switch (p)
         {
+            case DEBUG_LEVEL:
+                o->getDebugLevel(v);
+                return true;
             default:
                 break;
         }
index 1da9f55..7039d23 100644 (file)
@@ -109,8 +109,11 @@ update_status_t Model::setObjectProperty(ScicosID uid, kind_t k, object_properti
     }
     else if (k == DIAGRAM)
     {
+        model::Diagram* o = static_cast<model::Diagram*>(baseObject);
         switch (p)
         {
+            case DEBUG_LEVEL:
+                return o->setDebugLevel(v);
             default:
                 break;
         }
index 9619ddd..26c61ee 100644 (file)
@@ -69,13 +69,13 @@ struct SimulationConfig
 class Diagram: public BaseObject
 {
 public:
-    Diagram() : BaseObject(DIAGRAM), m_title("Untitled"), m_path(), m_properties(), m_context(), m_children(), m_version()
+    Diagram() : BaseObject(DIAGRAM), m_title("Untitled"), m_path(), m_properties(), m_debugLevel(), m_context(), m_children(), m_version()
     {
         m_color = { -1, 1};
     }
 
     Diagram(const Diagram& o) : BaseObject(DIAGRAM), m_title(o.m_title), m_path(o.m_path), m_color(o.m_color), m_properties(o.m_properties),
-        m_context(o.m_context), m_children(o.m_children), m_version(o.m_version) {};
+        m_debugLevel(o.m_debugLevel), m_context(o.m_context), m_children(o.m_children), m_version(o.m_version) {};
     ~Diagram() = default;
 
 private:
@@ -145,6 +145,22 @@ private:
         return SUCCESS;
     }
 
+    void getDebugLevel(int& data) const
+    {
+        data = m_debugLevel;
+    }
+
+    update_status_t setDebugLevel(const int& data)
+    {
+        if (data == m_debugLevel)
+        {
+            return NO_CHANGES;
+        }
+
+        m_debugLevel = data;
+        return SUCCESS;
+    }
+
     void getTitle(std::string& data) const
     {
         data = m_title;
@@ -214,6 +230,7 @@ private:
     std::string m_path;
     std::vector<int> m_color;
     SimulationConfig m_properties;
+    int m_debugLevel;
     std::vector<std::string> m_context;
 
     std::vector<ScicosID> m_children;
index 9df94ff..7b1aa5d 100644 (file)
@@ -9,62 +9,63 @@
 package org.scilab.modules.xcos;
 
 public enum ObjectProperties {
-  PARENT_DIAGRAM,
-  PARENT_BLOCK,
-  GEOMETRY,
-  DESCRIPTION,
-  FONT,
-  FONT_SIZE,
-  RELATED_TO,
-  INTERFACE_FUNCTION,
-  SIM_FUNCTION_NAME,
-  SIM_FUNCTION_API,
-  SIM_SCHEDULE,
-  SIM_BLOCKTYPE,
-  SIM_DEP_UT,
-  ANGLE,
-  EXPRS,
-  INPUTS,
-  OUTPUTS,
-  EVENT_INPUTS,
-  EVENT_OUTPUTS,
-  STATE,
-  DSTATE,
-  ODSTATE,
-  NZCROSS,
-  NMODE,
-  RPAR,
-  IPAR,
-  OPAR,
-  EQUATIONS,
-  UID,
-  CHILDREN,
-  PORT_REFERENCE,
-  STYLE,
-  LABEL,
-  DESTINATION_PORT,
-  SOURCE_PORT,
-  CONTROL_POINTS,
-  THICK,
-  COLOR,
-  KIND,
-  FROM,
-  TO,
-  DATATYPE,
-  DATATYPE_ROWS,
-  DATATYPE_COLS,
-  DATATYPE_TYPE,
-  FIRING,
-  SOURCE_BLOCK,
-  PORT_KIND,
-  IMPLICIT,
-  PORT_NUMBER,
-  CONNECTED_SIGNALS,
-  TITLE,
-  PATH,
-  PROPERTIES,
-  DIAGRAM_CONTEXT,
-  VERSION_NUMBER,
-  MAX_OBJECT_PROPERTIES;
+    PARENT_DIAGRAM,
+    PARENT_BLOCK,
+    GEOMETRY,
+    DESCRIPTION,
+    FONT,
+    FONT_SIZE,
+    RELATED_TO,
+    INTERFACE_FUNCTION,
+    SIM_FUNCTION_NAME,
+    SIM_FUNCTION_API,
+    SIM_SCHEDULE,
+    SIM_BLOCKTYPE,
+    SIM_DEP_UT,
+    ANGLE,
+    EXPRS,
+    INPUTS,
+    OUTPUTS,
+    EVENT_INPUTS,
+    EVENT_OUTPUTS,
+    STATE,
+    DSTATE,
+    ODSTATE,
+    NZCROSS,
+    NMODE,
+    RPAR,
+    IPAR,
+    OPAR,
+    EQUATIONS,
+    UID,
+    CHILDREN,
+    PORT_REFERENCE,
+    STYLE,
+    LABEL,
+    DESTINATION_PORT,
+    SOURCE_PORT,
+    CONTROL_POINTS,
+    THICK,
+    COLOR,
+    KIND,
+    FROM,
+    TO,
+    DATATYPE,
+    DATATYPE_ROWS,
+    DATATYPE_COLS,
+    DATATYPE_TYPE,
+    FIRING,
+    SOURCE_BLOCK,
+    PORT_KIND,
+    IMPLICIT,
+    PORT_NUMBER,
+    CONNECTED_SIGNALS,
+    TITLE,
+    PATH,
+    PROPERTIES,
+    DEBUG_LEVEL,
+    DIAGRAM_CONTEXT,
+    VERSION_NUMBER,
+    MAX_OBJECT_PROPERTIES;
 }
 
index 47ef267..1b41233 100644 (file)
 package org.scilab.modules.xcos.actions;
 
 import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.util.logging.Logger;
 
 import javax.swing.JButton;
+import javax.swing.Timer;
 
+import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.InterpreterException;
+import org.scilab.modules.graph.ScilabComponent;
 import org.scilab.modules.graph.ScilabGraph;
 import org.scilab.modules.graph.actions.base.GraphActionManager;
 import org.scilab.modules.graph.actions.base.OneBlockDependantAction;
 import org.scilab.modules.gui.menuitem.MenuItem;
+import org.scilab.modules.xcos.JavaController;
+import org.scilab.modules.xcos.ObjectProperties;
 import org.scilab.modules.xcos.graph.XcosDiagram;
 import org.scilab.modules.xcos.utils.XcosMessages;
 
+import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.asynchronousScilabExec;
+import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.buildCall;
+
 /**
  * Start the simulation
  */
@@ -39,6 +49,9 @@ public final class StartAction extends OneBlockDependantAction {
     /** Accelerator key for the action */
     public static final int ACCELERATOR_KEY = 0;
 
+    private int counter;
+    private Timer displayTimer;
+
     /**
      * @param scilabGraph
      *            graph
@@ -46,6 +59,15 @@ public final class StartAction extends OneBlockDependantAction {
     public StartAction(ScilabGraph scilabGraph) {
         super(scilabGraph);
         setEnabled(GraphActionManager.getEnable(StartAction.class));
+
+        char[] msg = (XcosMessages.SIMULATION_IN_PROGRESS + XcosMessages.DOTS).toCharArray();
+        final int minimalMsgLen = XcosMessages.SIMULATION_IN_PROGRESS.length();
+
+        counter = 0;
+        displayTimer = new Timer(1000, e -> {
+            counter = (counter + 1) % (XcosMessages.DOTS.length() + 1);
+            ((XcosDiagram) scilabGraph).info(new String(msg, 0, minimalMsgLen + counter));
+        });
     }
 
     /**
@@ -76,101 +98,80 @@ public final class StartAction extends OneBlockDependantAction {
     @Override
     public void actionPerformed(ActionEvent e) {
         final XcosDiagram graph = (XcosDiagram) getGraph(e);
+
+        // action disabled when the cell is edited
+        final ScilabComponent comp = ((ScilabComponent) graph.getAsComponent());
+        if (comp.isEditing()) {
+            return;
+        }
+
+        updateUI(true);
+        displayTimer.start();
+
+        final String cmd = createSimulationCommand(graph);
+        final ActionListener action = new ActionListener() {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                displayTimer.stop();
+                updateUI(false);
+            }
+        };
+
+        try {
+            asynchronousScilabExec(action, cmd);
+        } catch (InterpreterException e1) {
+            final Logger log = Logger.getLogger(StartAction.class.getName());
+            log.warning(e1.getMessage());
+        }
+    }
+
+    /**
+     * Create the command String
+     *
+     * @param diagram
+     *            the working diagram
+     * @return the command string
+     */
+    private String createSimulationCommand(final XcosDiagram diagram) {
         String cmd;
-        //FIXME: implement the compilation then simulation
-        //        // action disabled when the cell is edited
-        //        final ScilabComponent comp = ((ScilabComponent) graph.getAsComponent());
-        //        if (comp.isEditing()) {
-        //            return;
-        //        }
-        //
-        //        final ScilabDirectHandler handler = ScilabDirectHandler.acquire();
-        //        if (handler == null) {
-        //            return;
-        //        }
-        //
-        //        updateUI(true);
-        //
-        //        try {
-        //            cmd = createSimulationCommand(graph, handler);
-        //        } catch (IOException ex) {
-        //            Logger.getLogger(StartAction.class.getName()).severe(ex.toString());
-        //            updateUI(false);
-        //
-        //            handler.release();
-        //            return;
-        //        }
-        //
-        //        final ActionListener action = new ActionListener() {
-        //            @Override
-        //            public void actionPerformed(ActionEvent e) {
-        //                updateUI(false);
-        //
-        //                handler.release();
-        //            }
-        //        };
-        //
-        //        try {
-        //            asynchronousScilabExec(action, cmd);
-        //        } catch (InterpreterException e1) {
-        //            e1.printStackTrace();
-        //            updateUI(false);
-        //
-        //            handler.release();
-        //        }
+        final StringBuilder command = new StringBuilder();
+
+        /*
+         * Log compilation info
+         */
+        final Logger log = Logger.getLogger(StartAction.class.getName());
+        log.finest("start simulation");
+
+        JavaController controller = new JavaController();
+        int[] debugLevel = new int[1];
+        controller.getObjectProperty(diagram.getUID(), diagram.getKind(), ObjectProperties.DEBUG_LEVEL, debugLevel);
+        command.append(buildCall("scicos_debug", debugLevel[0])).append("; ");
+
+        /*
+         * Export the schema on `scs_m` and simulate
+         */
+        command.append("scs_m = scicos_new(\"0x").append(Long.toHexString(diagram.getUID())).append("\"); ");
+        command.append("xcos_simulate(scs_m, 4); ");
+
+        cmd = command.toString();
+        return cmd;
     }
 
-    //    /**
-    //     * Create the command String
-    //     *
-    //     * @param diagram
-    //     *            the working diagram
-    //     * @param handler
-    //     *            the handler use to communicate with Scilab
-    //     * @return the command string
-    //     * @throws IOException
-    //     *             when temporary files must not be created.
-    //     */
-    //    private String createSimulationCommand(final XcosDiagram diagram, final ScilabDirectHandler handler) throws IOException {
-    //        String cmd;
-    //        final StringBuilder command = new StringBuilder();
-    //
-    //        /*
-    //         * Log compilation info
-    //         */
-    //        final Logger log = Logger.getLogger(StartAction.class.getName());
-    //        log.finest("start simulation");
-    //
-    //        /*
-    //         * Import a valid scs_m structure into Scilab
-    //         */
-    //        handler.writeDiagram(diagram);
-    //        command.append(buildCall("scicos_debug", diagram.getScicosParameters().getDebugLevel()));
-    //
-    //        /*
-    //         * Simulate
-    //         */
-    //        command.append("xcos_simulate(scs_m, 4); ");
-    //
-    //        cmd = command.toString();
-    //        return cmd;
-    //    }
-    //
-    //    /**
-    //     * Update the UI depending on the action selected or not
-    //     *
-    //     * @param started
-    //     *            the started status
-    //     */
-    //    public void updateUI(boolean started) {
-    //        GraphActionManager.setEnable(StartAction.class, !started);
-    //        GraphActionManager.setEnable(StopAction.class, false);
-    //        ((XcosDiagram) getGraph(null)).setReadOnly(started);
-    //
-    //        if (started) {
-    //            ((XcosDiagram) getGraph(null)).info(XcosMessages.SIMULATION_IN_PROGRESS);
-    //        } else {
-    //            ((XcosDiagram) getGraph(null)).info(XcosMessages.EMPTY_INFO);
-    //        }
-    //    }
+    /**
+     * Update the UI depending on the action selected or not
+     *
+     * @param started
+     *            the started status
+     */
+    public void updateUI(boolean started) {
+        GraphActionManager.setEnable(StartAction.class, !started);
+        GraphActionManager.setEnable(StopAction.class, false);
+        ((XcosDiagram) getGraph(null)).setReadOnly(started);
+
+        if (started) {
+            ((XcosDiagram) getGraph(null)).info(XcosMessages.SIMULATION_IN_PROGRESS);
+        } else {
+            ((XcosDiagram) getGraph(null)).info(XcosMessages.EMPTY_INFO);
+        }
+    }
 }
index 171f8f8..651e55d 100644 (file)
@@ -160,7 +160,7 @@ public class BlockParametersAction extends VertexSelectionDependantAction {
                     }
 
                     block.setLocked(true);
-                    String blk = buildCall("scicos_new", Long.toString(cell.getUID()));
+                    String blk = buildCall("scicos_new", Long.toHexString(cell.getUID()));
                     String xcosBlockInterface = buildCall("xcosBlockInterface", interfaceFunction[0].toCharArray(), "set", blk.toCharArray(), ScilabDirectHandler.CONTEXT.toCharArray());
 
                     asynchronousScilabExec(action, "xcosCellCreated", xcosBlockInterface.toCharArray());
index ad6896d..b13c356 100644 (file)
@@ -235,7 +235,7 @@ public final class XcosMessages {
     public static final String LOADING_USER_DEFINE = Messages.gettext("Loading user defined palettes") + DOTS;
     public static final String GENERATING_C_CODE = Messages.gettext("Generating C Code for SuperBlock") + DOTS;
     public static final String ERROR_GENERATING_C_CODE = Messages.gettext("A SuperBlock must be selected to generate code");
-    public static final String SIMULATION_IN_PROGRESS = Messages.gettext("Simulation in progress") + DOTS;
+    public static final String SIMULATION_IN_PROGRESS = Messages.gettext("Simulation in progress");
     public static final String COMPILATION_IN_PROGRESS = Messages.gettext("Compilation in progress, results will be stored in the \'scicos_cpr\' variable")
             + DOTS;
     public static final String GENERATE_SUPERBLOCK = Messages.gettext("Generate SuperBlock, please wait") + DOTS;