Using the Expressions extension

To use svc_expr, provision a service in your mix with this extension. Expressions can be then added as service or operation properties.

Markup

See Table 1 and Table 2 on the properties.

<mix name="Mix">
  <service name="Expr"
    provision="expr">
  <!-- HERE YOU CAN ADD 
        expr.src 
        expr.state 
        expr.init 
        expr.init.[Var] --> 
  </service>

  <request name="MyRequest"
    service="expr"
    fields="myfield1">
  <!-- HERE YOU CAN ADD 
        expr.src --> 
    <reply name="MyReply"
      fields="myField2"/>
  </request>
</mix>

Properties

Table 1. Expressions - Operation properties
Property Description Example
expr.src Use this property to add Erlang expressions either to a service or an operation. The content is rendered as a CDATA section.

Use the built-in Erlang functions put/2 and get/1 to access or manipulate the values carried in the SPARKL fields.

Important: The expressions list must end with a full stop.
Tip: To get syntax highlighting, use the content-type="text/x-erlang" attribute.
<request name="AddUp"
  service="Math"
  fields="n m">
  <prop name="expr.src"
    content-type="text/x-erlang"><![CDATA[
% Add up the field values using a function of the Math service.
Value = AddUpFun(get("n"), get("m")),
                  
% Assign the result to the value field.
put("value", Value),
                  
% Send the Ok reply.
"Ok".
  ]]></prop>
  <reply name="Ok"
    fields="value"/>
</request>
expr.auto Use this property to specify how many times, and how frequently a notify is triggered by the svc_expr service.
Note: If this property is not specified, default values apply.

The following attributes can be used:

count
The total times the notify is triggered.
Possible values are:
"-1"
The default value. The notify is triggered an unlimited number of times.
"0"
The notify is never triggered.
"n"
The notify is triggered an "n" number of times.
interval
The time between each triggering of the notify.
Possible values are:
"30s"
This is the default value. The notify is triggered every 30 seconds.
"n"
The notify is triggered every "n" seconds.
"n<timeunit>"
The notify is triggered every "n" time unit specified. Supported time units, besides seconds, are:
  • milliseconds(ms)
  • minutes(m)
  • hours(h)
<notify 
  name="SomeNotify"
  service="Sequencer"
  clients="SomeExprService">
  <prop 
    name="expr.auto"    
      interval="5s"    
      count="4"/>
</notify>
                
expr.state Use this property to trigger a notify operation when a particular state change occurs.

The state has to be defined on the client service of the notify.

See also the service property expr.state .

Note: Variable names must start with a capital letter.
<!-- OldCount is the last 
known value of Count -->
<notify name="myNotify"  
  service="Sequencer"  
  clients="Expr"  
  fields="TRIGGER">  
  <prop name="expr.state"    
    Count="OldCount"/>  
  <prop name="expr.src"    
    content-type="text/x-erlang"><![CDATA[
Count > 5.  
  ]]></prop>
</notify>
Table 2. Expressions - Service properties
Property Description Example
expr.src Use this property to add Erlang expressions either to a service or an operation. The content is rendered as a CDATA section.
Important: The expressions list must end with a full stop.
Tip: To get syntax highlighting, use the content-type="text/x-erlang" attribute.
<service name="Math"
  provision="expr">
  <prop name="expr.src"
    content-type="text/x-erlang"><![CDATA[
% Provide a simple function for all operations on this service.
AddUpFun = 
  fun(N, M) ->
    N + M
  end.
  ]]></prop>
</service>
expr.state Use this property on a service to define state variables and other state variables that update them.

See also the operation property expr.state.

Note: Variable names must start with a capital letter.
<!-- NewCount updates Count -->
<service name="Expr"  
  provision="expr">  
  <prop name="expr.state"    
    Count="NewCount"/>
</service>
expr.init Use this property to give variables an initial value. Depending on their use, the variables can be state or static variables.
Tip: An expression in expr.src can also initialise a state variable. For example, NewFoo = 0. This leads to a state change, as opposed to using expr.init, with the attribute Foo="0".
<service 
  name="Bartender" 
  provision="expr">
  <prop 
    name="expr.init" 
    GinsServed="0"/>
  <prop 
    name="expr.state" 
    GinsServed="NewGinsServed"/>
</service>
expr.init.[Var] Use this property to contain variables that are longer Erlang terms, such as maps #{...}, and lists [...].

The expr.init property is appended with the name of the variable.

Note: Variable names must start with a capital letter.

Do not use a full stop at the end of the term, like in the case of expressions contained in expr.src.

<service 
  name="Database" 
  provision="expr">
  <prop name="expr.init.Table"
    content-type="text/x-erlang"><![CDATA[
#{
  0 => "Foo",
  1 => "Bar"}
  ]]></prop>
</service>

Lifecycle - Expressions in services

On init
If a service has one or more of the below properties, their content is evaluated at service instance initialisation, as follows:
  • expr.src
    The expressions are executed at instance initialisation.
    If a state variable is updated using an expression of the form NewStateVar = NewValue, a state change occurs which may result in the triggering of a notify operation.
    See above how to use this property.
  • expr.init
    Each capitalized [Var]="term" attribute in the property is used to create an initial value for the [Var] variable. The variable can be a state or static variable.
    See above how to use this property.
  • expr.init.[Var]
    The content of this property is used to create an initial value for the [Var] variable. The variable can be a state or static variable.
    See above how to use this property.
On close
If the last, or returned, value of the expr.src property of the service is a zero-arg function, this function is invoked at instance-down time.
For example, the following shows a service that opens a file when started, and closes it when stopped.
<mix name="Test">
  <field name="value" type="term"/>
  <service name="FileWriter" provision="expr">
    <prop name="expr.src" content-type="text/x-erlang">
      {ok, File} =
        file:open("my_file.txt",
          [read, write]),
 
      fun() ->
        file:close(File)
      end.
    </prop>
  </service>
 
  <consume name="Write" service="FileWriter" fields="value">
    <prop name="expr.src" content-type="text/x-erlang">
      Result =
        file:write(File, get("value")).
    </prop>
  </consume>
</mix>