public abstract class AbstractRule extends AbstractTransformElement
In the default transformation engine, transforms are containers that traverse the transformation element hierarchy, executing extractors, rules and nested transforms. Extractors effectively walk the source model and rules create or update the target model.
A rule is responsible for converting an object from the source model into an object in the target model. Before a rule is executed, it must accept the execution context which contains properties that define the source object, the target container (within target model). The context also contains transformation specific properties. When the rule is executed, it has the option of determining if the target object already exists and if so, simply perfom an update of that object. Alternately, the rule must create the target object if it does not exist.
There are several methods in this base class that enable this functionality,
which is effectively persisting and looking up a "derived" relationship
between the target object and the source object. The mapTarget()
method enables the rule to save the derived relationship and the
findTarget()
method can use the saved relationships to
locate the existing target object so that it can be updated (not recreated).
Constructor and Description |
---|
AbstractRule()
The default constructor should only be used internally by the
transformation service.
|
AbstractRule(java.lang.String id,
java.lang.String name)
Constructor to create a rule.
|
Modifier and Type | Method and Description |
---|---|
protected abstract java.lang.Object |
createTarget(ITransformContext context)
Create a new target object based on the source in the context.
|
java.lang.Object |
execute(ITransformContext context)
When a rule is executed, it will:
determine whether a target object already exists.
|
protected java.lang.Object |
findTarget(ITransformContext context)
Return a target object if it already exists.
|
boolean |
isSourceConsumed(ITransformContext context)
Determine if the source object is to be consumed by this rule.
|
protected void |
mapTarget(ITransformContext context,
java.lang.Object target)
Create a mapping from the source object to the target object so that it can
be found when findTarget() is called.
|
java.lang.String |
toString() |
protected void |
updateTarget(ITransformContext context,
java.lang.Object target)
When an existing target object is found, this update method is called to refresh
any stale information in the target based on the new context.
|
canAccept, getAcceptCondition, getDescription, getId, getName, getProgressMonitor, setAcceptCondition, setDescription, setId, setName
public AbstractRule()
public AbstractRule(java.lang.String id, java.lang.String name)
id
- A unique identifier for the rule.name
- The name of the rule.protected abstract java.lang.Object createTarget(ITransformContext context) throws java.lang.Exception
context
- the current context of the transformation executionjava.lang.Exception
public java.lang.Object execute(ITransformContext context) throws java.lang.Exception
context
- the current context of the transformation executionjava.lang.Exception
public boolean isSourceConsumed(ITransformContext context)
When a source object is consumed, all sibling rules, extractors and transforms in the same parent transform are skipped (not executed) for the source object. By default, a rule does not consume the source. To change this behaviour, the concrete rule class must override this method.
context
- the current context of the transformation executionprotected java.lang.Object findTarget(ITransformContext context)
context
- the current context of the transformation executionprotected void mapTarget(ITransformContext context, java.lang.Object target)
context
- the current context of the transformation executiontarget
- the target object created by the rule.public java.lang.String toString()
toString
in class java.lang.Object
Object.toString()
protected void updateTarget(ITransformContext context, java.lang.Object target) throws java.lang.Exception
Subclasses can override this method to modify any attributes of an existing target object.
context
- the current context of the transformation executiontarget
- the previously created object (returned by findTarget())java.lang.Exception