Streamlining Development with the Java Delegate Code Generator

Overview

Moxie provides a Java delegate code generator that can generate delegate interfaces for all of the blocks in a SysML model, class stubs for the blocks, and a delegate provider that registers the delegate module with the Moxie Engine at runtime. These generated files include everything that the Moxie Engine needs in order to connect the delegates to the SysML model, allowing you to focus on the methods that need custom implementations. In this section, you will generate delegates for the UAV mission and install them for Moxie to use.

This section covers the following concepts:

Figure A1: Java delegate code generation

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 delegate module project from the previous section. If you did not complete the previous section, you can use the delegate module project from the Moxie installation: \documentation\tutorialFiles\03\DelegateModules\UAVMissionDelegateModule, but you may still need to configure the delegate module for your environment.

You also need the UAV Mission simulation project included alongside the delegate module project: \documentation\tutorialFiles\03\DelegateModules\UAVMission.mdzip

Recommended Reading Before completing this section, you may want to read the following help topics:

Instructions

Generate the delegates

You can use the Java delegate code generator to generate interfaces and class stubs for the blocks in your SysML model, as well as a delegate provider that registers your delegate module with the Moxie Engine at runtime. By including the @AutoDelegateImplementation annotation on these interfaces and making the classes abstract, you will only need to implement the methods that need custom functionality. The Moxie Engine will auto-implement the rest at runtime.

  1. In the UAVMissionDelegateModule directory from the previous section, create a new directory named src.
  2. Open the UAVMission.mdzip project specified in the prerequisites for this section.
  3. Select Moxie > Generate Java Code (or press CtrlAltShiftG).
  4. In the Containment Tree, select the Structure package to include all of the blocks that it contains in the delegate code generation.
  5. In the Java delegate code generator, notice the Currently selected element from Containment Tree text box is now set to Structure.
  6. Set the Save Java code to the following location text box to the new src directory.
  7. Enter com.agi.moxie.tutorial.uavmission in the Base Java package for Java code text box.
  8. Select the Include @AutoDelegateImplementation on Interfaces checkbox.
  9. Select the Generate Class Implementations for Generated Interfaces checkbox.
  10. Leave the default Class Prefix and Class Suffix to help differentiate between the class and interface names.
  11. Select the Generate Abstract Classes checkbox.
  12. Select the Generate Delegate Provider checkbox.
  13. Enter UAVMission in the Name of Delegate Module text box.

    Figure B1: The Java delegate code generator

  14. Click Generate, then Ok, and then close the Java delegate code generator.
  15. You should now see the generated interfaces, classes, and delegate provider in the UAVMissionDelegateModule\src\com\agi\moxie\tutorial\uavmission directory and its subdirectories.

Examine the delegate provider

In the com.agi.moxie.tutorial.uavmission.UAVMissionDelegateProvider class:

  • Notice the list of interfaces and classes returned by provideDescriptor(). If you add more delegates to the project later, you will need to add them to this list.
  • Notice the analysis tool controller returned by provideDescriptor(). The code generator sets this to the BuiltInAnalysisToolController, but you will replace this with the StkController when connecting to STK.

Examine the interfaces

For example, in the com.agi.moxie.tutorial.uavmission.structure.UAV interface:

  • Notice the @DelegateFor("Structure::UAV") annotation. This informs the Moxie Engine which SysML block the Java interface corresponds to.
    • Only one class or interface may use a @DelegateFor annotation for any given block.
  • Notice the @AutoDelegateImplementation annotation. This instructs the Moxie Engine to implement missing SysML properties and operations at runtime.
    • For example, the processFlightPlan() SysML operation is only used as a call event, so its Java implementation does not need to do anything. The Moxie Engine will auto-implement the processFlightPlan() Java method with an empty body in memory at runtime.

Examine the classes

For example, in the com.agi.moxie.tutorial.uavmission.structure.impl.UAVDelegate abstract class:

  • Notice the @AutoDelegateImplementation annotation. This does the same thing here as it does in the interface, so you can remove it from either the class or the interface if you want, but it does not hurt to leave it in both.
  • Notice the @DefaultDelegate annotation. This informs the Moxie Engine which class to use as the default implementation of the SysML block specified by a @DelegateFor annotation.
    • A @DefaultDelegate annotation is required when multiple classes derive from the same class or interface that uses a @DelegateFor annotation. Only one @DefaultDelegate annotation may be used in that hierarchy.
    • If you have multiple SysML instance specifications that need to be implemented by different Java classes, then you need to use the MoxieJavaDelegate stereotype to specify which class to use for each instance specification.
  • Notice the BasicProperty and ListProperty Java fields for each of the UAV's SysML properties. These support change tracking of the SysML property values during the simulation.
  • Notice the @InjectByName("timeProvider") annotation in the constructor parameters. This instructs the Moxie Engine to inject the TimeProvider dependency from the analysis tool controller into the constructor so that it can be used within the delegate.
  • Notice the @OptionalInjectBySlot annotations in the constructor parameters. These instruct the Moxie Engine to inject their values from the SysML instance specification slots into the constructor so that they can be used within the delegate.
    • The @InjectBySlot annotation does the same thing, but does not allow null values.

Update the Service Provider Interface (SPI)

The Moxie Engine uses the SPI to find the delegate provider at runtime.

  1. In the UAVMissionDelegateModule directory, open resources\META-INF\services\com.agi.moxie.spi.DelegateProvider in a text editor.
  2. Enter com.agi.moxie.tutorial.uavmission.UAVMissionDelegateProvider and then save and close the file.

Re-install the delegate module

  1. Open a Command Prompt (cmd) and enter cd C:\Moxie\UAVMissionDelegateModule, replacing the path to your delegate module directory, as necessary.
  2. Enter gradle clean install to clean, assemble, and re-install the delegate module. After the build completes, confirm that there were no errors.

Run the delegate availability report

Moxie provides a delegate availability report that lists all of the delegates that are currently installed and available to the simulation and the SysML blocks that they correspond to. This list includes the delegates that are part of the Moxie installation itself.

  1. In your No Magic modeling tool, select Moxie > Delegate Availability Report.
  2. Notice the list of delegate providers includes the UAVMissionDelegateProvider.
  3. Notice the list of delegate classes includes both the Java interface and class for each of the SysML blocks in your project.

Next Section >