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:
- The Moxie SimulationLogger
- Delegate debugging
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
- If you did not complete the previous section, re-install the delegate module.
- Open the UAVMission.mdzip project from the previous section.
-
Run () the simulation
and observe that the simulation fails with the following exception:
This indicates that one or more of the properties used in the
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 < maxPowerDrawcurrentPowerDraw + powerRequest < maxPowerDraw
guard isnull
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.
-
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. -
In the
PowerSystemDelegate
class, add the following import statement:import com.agi.moxie.api.SimulationLogger;
-
Add the following private field:
private SimulationLogger mSimulationLogger;
-
Add the following dependency injection and field initialization in the constructor:
public PowerSystemDelegate( ...
@InjectByName("simulationLogger") SimulationLogger simulationLogger,
... ) {
mSimulationLogger = simulationLogger;
...
} -
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()); - Save your work and re-install the delegate module.
-
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.
- Close your No Magic modeling tool.
-
Open a Command Prompt (cmd)
and enter
cd C:\Moxie\UAVMissionDelegateModule
, replacing the path to your delegate module directory, as necessary. -
Enter
gradle launchNoMagicWithDebugging
. - Once your modeling tool has loaded, re-open the UAVMission.mdzip project.
-
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 selectcom.nomagic.launcher.Launcher (5005)
. -
In the
PowerSystemDelegate
class, insert a breakpoint in theprovidePower()
method. - Run () the simulation again and wait for it to hit the breakpoint.
-
Use your IDE's debugging features to examine the values of the properties used in the
currentPowerDraw + powerRequest < maxPowerDraw
guard. For example, in IntelliJ, expandthis
>powerRequestProperty
and notice thatmData
isnull
.
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.
-
Add the following code before the logging statements in the
providePower()
method to set the property:powerRequestProperty.setValue(request);
- Save your work and re-install the delegate module.
-
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 >