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.

<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 
      expr.bind.in 
      expr.bind.out --> 
  <reply name="MyReply"
    fields="myField2"/>
</request>

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.
Important: The expressions list must end with a full stop.
Tip: To get syntax highlighting, use the content-type="text/x-erlang" attribute.
<prop name="expr.src"
  content-type="text/x-erlang"><![CDATA[
case IsLocked of  
  true ->    
    Error = "Already locked",    
    "Error";   

  false ->    
    NewIsLocked = true,    
    "Ok"
end.
]]></prop>
expr.bind.in Use this property to bind the input fields of an operation to variables.
Note: Variable names must start with a capital letter.
<prop name="expr.bind.in"    
Var="input"/>
expr.bind.out Use this property to bind the output fields of an operation to variables.
Note: Variable names must start with a capital letter.
<prop name="expr.bind.out"    
Var="output"/>
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.
<prop name="expr.src"
  content-type="text/x-erlang"><![CDATA[
case IsLocked of  
  true ->    
    Error = "Already locked",    
    "Error";   

  false ->    
    NewIsLocked = true,    
    "Ok"
end.
]]></prop>
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.bind.in" Value="value"/>
    <prop name="expr.src" content-type="text/x-erlang">
      Result =
        file:write(File, Value).
    </prop>
  </consume>
</mix>