- First we want to have more convenient access to the resource guids.
- To accomplish this, we need to check a setting in the project settings of
ch.actifsource.tutorial.refactoring
- Right-click on the project and select
Properties
- Check in the actifsource settings whether the option “generate javamodel” is set
- If the “generate javamodel” option is not set, this can be set or add a the ExportWithoutStatements buildconfig to the
src-gen
target folder for exporting java classes for the SimpleService packages
- This will generate a class for each package containing a constant for each resource.
-If you cannot use the buildconfig ExportWithoutStatements, check in the Built-in Dependencies tab whether the JAVAMODEL
is available. If not, add it via the Add Builtin button.
- Implement the refactor method
-
The refactor method has only two parameters an IModifiable and a list of packages. The modifiable provides the context to access the actifsource resources. The package list contains the actifsource packages selected by the user when starting the refactoring. How the packages selection is interpreted is up to the implementer.
-
In general you need to use the two classes Select and Update. These are facades providing a convenient way to select and update resource information in a context. To get information about the available methods, open the class and a have a look at the javadoc comments on how to use them.
-
The actifsource API works with statements and resources. A Statement is a triple connecting two resources (subject, object) through a property (predicate). The subject is the resource whose type (class) defines the predicate (property). The object is an instance of properties range. This is almost the same you see in the resource editor. In our example Patient is an instance of Service Person refers the Call throw the call defined in the Service class. For example you will get a statement Person (subject), call (predicate), Create (object) because Person refers to Create through the call relation. This is different from the diagram editor where subject and object are represented by their type.
-
The old model looked like this
and the new model looks like this now
The only thing the refactoring has to do is adding a CallGroup into each Service and move the Call into the group.
- Select all Service instances in packages posted to the refactorer
- The instanceWithPackage method takes an ISelectable and the GUID of a class. The result is an Iterable providing all instances reachable through the selectable. Since each IModifiable is also an ISelectable, you can use the IModifiable passed to the refactorer.
- Create a new CallGroup using the Update-Facade and add it to the Service-Instance
- The Update-Facade always requires an IModifiable to work with. Using the createAndInitializeResource method you can create a new instance of a type in the specific package. If you have a named resource, it is recommended to use the overload taking a name, in this case
group
. The last parameter defines the default values for the attributes and relations when creating the resource and can be left out. For each property not found in the defaultValue map the default defined in the model will be used.
- Move the Call to the group
- This time the method is located on the RefactorUtil. The reason for this is that the Update-Facade only provides simple modification methods and no selects. The moveStatements method uses methods from both the Select and Update method and is more complex. The moveStatements method take the IModifiable, a Property, the source and the target. It is simply often used when moving a Property from one class to another.
- By using the cut and paste in ResourceEditor, actifsource automatically detected that you have moved the Property call