Transport Meta example

In this example, we use the Meta function of the Expressions extension to find a form of transport suitable for different types of goods.

Figure: Transport Meta example - The mix


Tip: Get the code from the SPARKL public repository.
Table 1. Transport Meta example - The markup
Markup Explanation
<folder name="Transport_xmp">
  <service name="Sequencer" 
    provision="sequencer"/>
  <field name="goods_type" 
    type="string"/>
  <field name="TRUCK"/>
  <field name="PLANE"/>
  <field name="CARGO"/>
  <mix name="Mix">
    <folder name="Order">
      ...
    </folder>
    <folder name="Management">
    ...
    </folder>
    <folder name="Logistics">
     ...
    </folder>
  </mix>
</folder>
In this mix we use:
  • 5 svc_expr services plus the Sequencer
  • 5 operations on the services:
    • 1 notify
    • 1 request/reply
    • 3 consumes
  • 4 fields, a string and three FLAGs
The services, and the operations on them, are separated into three folders.

The notify is used to kick off a transaction. The transaction ends when one of the three consumes is satisfied.

The request/reply helps the Sequencer collect the fields needed to satisfy the consumes.

<folder name="Order">
  <service name="LocalGoods" 
    provision="expr">
    <prop name="expr.src" 
      content-type="text/x-erlang"><![CDATA[
ServiceName = Meta(name).
    ]]></prop>
  </service>
  <service name="Express" 
    provision="expr">
    <prop name="expr.src" 
      content-type="text/x-erlang"><![CDATA[
ServiceName = Meta(name).
    ]]></prop>
  </service>
  <service name="Overseas" 
    provision="expr">
    <prop name="expr.src" 
      content-type="text/x-erlang"><![CDATA[
ServiceName = Meta(name).
    ]]></prop>
  </service>
  <notify name="SendCargo" 
    service="Sequencer" 
    clients="LocalGoods Express Overseas" 
    fields="goods_type">
    <prop name="expr.bind.out" 
      ServiceName="goods_type"/>
    <prop name="expr.auto" 
      count="1"/>
  </notify>
</folder>
The Order folder contains three services:
  • LocalGoods
  • Express
  • Overseas

All three services have the expr.src property with the same expression.

This expression:

  1. Defines the static variable ServiceName
  2. Uses the Meta function with the name argument to retrieve the name of the given service
  3. Binds the outcome of the Meta function to the ServiceName static variable
The folder contains a single notify operation, SendCargo.

This operation can be triggered by any of the above services.

When fired, SendCargo outputs the field goods_type, which is bound to the ServiceName static variable.

The value of ServiceName always resolves to the name of the service that fires SendCargo.

The expr.auto property makes sure that SendCargo is fired as soon as one of the services trigger it on service start-up.

<folder name="Management">
  <service name="Management" 
    provision="expr"/>
  <request name="ByMeansOf" 
    service="Management" 
    fields="goods_type">
    <prop name="expr.bind.in" 
      Goods="goods_type"/>
    <prop name="expr.src" 
      content-type="text/x-erlang"><![CDATA[
case Goods of
    "LocalGoods" ->
    "Truck";
    "Express" ->
    "Plane";
    "Overseas" ->
    "Cargo"
end.
    ]]></prop>
    <reply name="Truck" 
      fields="TRUCK"/>
    <reply name="Plane" 
      fields="PLANE"/>
    <reply name="Cargo" 
      fields="CARGO"/>
  </request>
</folder>
The Management folder contains an svc_expr service, Management, that has no additional properties.

The ByMeansOf operation is implemented by the Management service.

The ByMeansOf operation uses its properties to:

  1. Bind its input field goods_type to Goods, a static variable
  2. Send one of the three alternative replies based on the value of Goods
In other words, the reply sent by this operation depends on which service fired the SendCargo operation.
  • If SendCargo is fired by the LocalGoods service, the Truck reply is sent
  • If SendCargo is fired by the Express service, the Plane reply is sent
  • If SendCargo is fired by the Overseas service, the Cargo reply is sent
The output fields TRUCK, PLANE and CARGO are all FLAGs.
<folder name="Logistics">
  <service name="Logistics" 
    provision="expr"/>
  <consume name="TruckDriver" 
    service="Logistics" 
    fields="TRUCK"/>
  <consume name="PlaneDriver" 
    service="Logistics" 
    fields="PLANE"/>
  <consume name="ShipCaptain" 
    service="Logistics" 
    fields="CARGO"/>
</folder>
The Logistics folder contains a single service Logistics, and three consume operations implemented by this service.

In any given transaction, only of the consumes can be satisfied.

  • If the ByMeansOf operation sends the Truck reply, the TruckDriver consume is satisfied
  • If the ByMeansOf operation sends the Plane reply, the PlaneDriver consume is satisfied
  • If the ByMeansOf operation sends the Cargo reply, the ShipCaptain consume is satisfied
When either consume is satisfied, the given transaction ends.
Tip: In this example, we did not give any specific markup to the Logistics service or the consumes. The Logistics service could be provisioned using any other SPARKL extension as well.