Logging Messages and Debugging Delegates

Overview

As when writing any custom code, when writing your delegates you will likely want to have them log messages and to debug them in your Integrated Development Environment (IDE). Moxie makes both of these tasks more convenient by providing a built-in simulation logger and by configuring your No Magic modeling tool for debugging for you. In this section, you will debug an exception using both log messages and your Java IDE's debugger.

This section covers the following concepts:

mSimulationLogger.info("currentPowerDraw=" + currentPowerDrawProperty.getValue());
mSimulationLogger.info("powerRequest=" + powerRequestProperty.getValue());
mSimulationLogger.info("maxPowerDraw=" + maxPowerDrawProperty.getValue());

Figure A1: Logging property values in a delegate

Prerequisites

Prerequisite Description
Moxie Installation You must have installed Moxie and have the prerequisites for developing delegates for Moxie.
Tutorial Project You must start this section with the UAV Mission simulation project and delegate module project from the previous section. If you did not complete the previous section, you can use the files from the Moxie installation: \documentation\tutorialFiles\03\DelegateWireUp, but you may still need to configure the delegate module for your environment.
Recommended Reading Before completing this section, you may want to read the following help topic:

Instructions

Examine the error log

  1. If you did not complete the previous section, re-install the delegate module.
  2. Open the UAVMission.mdzip project from the previous section.
  3. Run () the simulation and observe that the simulation fails with the following exception:
    ERROR: java.lang.Exception:
    com.agi.moxie.uml.infrastructure.expressions.JexlExpressionParser.parseBooleanExpression@1:20
    + error caused by null operand within guard source text:
    currentPowerDraw + powerRequest < maxPowerDraw
    This indicates that one or more of the properties used in the currentPowerDraw + powerRequest < maxPowerDraw guard is null when the guard is evaluated.

Log the property values

After you include the SimulationLogger dependency from the analysis tool controller in your delegates, you can use it to log messages to the simulation log file and the simulation console.

  1. In the PowerSystem State Machine diagram, notice that the providePower() operation executes immediately before the guard that is throwing the exception. Unlike the guard, this operation has corresponding delegate code in which you can log the values of the properties used in the guard.
  2. In the PowerSystemDelegate class, add the following import statement:
    import com.agi.moxie.api.SimulationLogger;
  3. Add the following private field:
    private SimulationLogger mSimulationLogger;
  4. Add the following dependency injection and field initialization in the constructor:
    public PowerSystemDelegate( ...
            @InjectByName("simulationLogger") SimulationLogger simulationLogger,
             ... ) {
        mSimulationLogger = simulationLogger;
         ...
    }
  5. Add the following logging statements for the SysML property values in the providePower() method:
    mSimulationLogger.info("currentPowerDraw=" + currentPowerDrawProperty.getValue());
    mSimulationLogger.info("powerRequest=" + powerRequestProperty.getValue());
    mSimulationLogger.info("maxPowerDraw=" + maxPowerDrawProperty.getValue());
  6. Save your work and re-install the delegate module.
  7. Run () the simulation again and observe that the simulation now reports the following information before the exception:
    INFO: currentPowerDraw=3.0
    INFO: powerRequest=null
    INFO: maxPowerDraw=100.0

Debug the property values

If you are using a Java IDE with debugging capabilities, such as IntelliJ IDEA or Eclipse, you can attach its debugger to a simulation to take advantage of all of the debugging features that your IDE provides, like setting breakpoints and examining property values during the simulation. You can use the same gradle.build script that you are using to build and install your delegates to also configure your No Magic modeling tool for debugging.

If you are not using an IDE with a debugger, you can just read through this subsection or skip it entirely.

  1. Close your No Magic modeling tool.
  2. Open a Command Prompt (cmd) and enter cd C:\Moxie\UAVMissionDelegateModule, replacing the path to your delegate module directory, as necessary.
  3. Enter gradle launchNoMagicWithDebugging.
  4. Once your modeling tool has loaded, re-open the UAVMission.mdzip project.
  5. In your IDE, attach the debugger to the com.nomagic.launcher.Launcher (5005) process. For example, in IntelliJ, select Run > Attach to Process... and then select com.nomagic.launcher.Launcher (5005).
  6. In the PowerSystemDelegate class, insert a breakpoint in the providePower() method.
  7. Run () the simulation again and wait for it to hit the breakpoint.
  8. Use your IDE's debugging features to examine the values of the properties used in the currentPowerDraw + powerRequest < maxPowerDraw guard. For example, in IntelliJ, expand this > powerRequestProperty and notice that mData is null.

Set the null property value

In the SysML model, the computer requests that the power system providePower() for specific subsystems, so the power system should store the value that the computer requests in the powerRequest property.

  1. Add the following code before the logging statements in the providePower() method to set the property:
    powerRequestProperty.setValue(request);
  2. Save your work and re-install the delegate module.
  3. Run () the simulation again and observe that the simulation no longer throws the exception for null operands in the guard and that it completes successfully.

Next Section >