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:
- The Moxie delegate code generator
- Moxie delegate auto-implementation
- Moxie delegate providers
- Moxie delegate annotations
- Moxie delegate properties
- The Moxie delegate availability report
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.
- In the UAVMissionDelegateModule directory from the previous section, create a new directory named src.
- Open the UAVMission.mdzip project specified in the prerequisites for this section.
- Select Moxie > Generate Java Code (or press ).
- In the Containment Tree, select the Structure package to include all of the blocks that it contains in the delegate code generation.
- In the Java delegate code generator, notice the Currently selected element from Containment Tree text box is now set to Structure.
- Set the Save Java code to the following location text box to the new src directory.
-
Enter
com.agi.moxie.tutorial.uavmission
in the Base Java package for Java code text box. - Select the Include @AutoDelegateImplementation on Interfaces checkbox.
- Select the Generate Class Implementations for Generated Interfaces checkbox.
- Leave the default Class Prefix and Class Suffix to help differentiate between the class and interface names.
- Select the Generate Abstract Classes checkbox.
- Select the Generate Delegate Provider checkbox.
-
Enter
UAVMission
in the Name of Delegate Module text box.Figure B1: The Java delegate code generator
- Click , then , and then close the Java delegate code generator.
- 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 theBuiltInAnalysisToolController
, but you will replace this with theStkController
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.
-
Only one class or interface may use a
-
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 theprocessFlightPlan()
Java method with an empty body in memory at runtime.
-
For example, the
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.
-
A
-
Notice the
BasicProperty
andListProperty
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 theTimeProvider
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 allownull
values.
-
The
Update the Service Provider Interface (SPI)
The Moxie Engine uses the SPI to find the delegate provider at runtime.
- In the UAVMissionDelegateModule directory, open resources\META-INF\services\com.agi.moxie.spi.DelegateProvider in a text editor.
-
Enter
com.agi.moxie.tutorial.uavmission.UAVMissionDelegateProvider
and then save and close the file.
Re-install the delegate module
-
Open a Command Prompt (cmd)
and enter
cd C:\Moxie\UAVMissionDelegateModule
, replacing the path to your delegate module directory, as necessary. -
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.
- In your No Magic modeling tool, select Moxie > Delegate Availability Report.
-
Notice the list of delegate providers includes the
UAVMissionDelegateProvider
. - Notice the list of delegate classes includes both the Java interface and class for each of the SysML blocks in your project.
Next Section >