Filtering subroutines

You can use Erlang expressions to conditionalize the firing of subroutines. For example, a consume operation has several notify subroutines, some of which only fire if certain conditions are met.

If the subr.spec property of a subroutine notify or solicit operation contains Erlang expressions, this content is used as a predicate to determine whether the operation fires for a given call.

Note: This is similar to the way notifies on an svc_expr service are treated. See Notify triggers.

The operation only fires if the predicate returns true. This result is coerced to a boolean, so all the following results mean the same:

  • <<"true">>
  • "true"
  • 1
  • true
Table 1. Filtered subroutines example
Example Description
<folder name="SubrFilters">
  <service name="Subr" provision="subr" />
  <mix name="Master">
    <field name="master" type="string"/>
    <consume name="Master"
      fields="master" 
      service="Subr">
      <prop name="subr.spec" 
        function="SubrFun"/>
    </consume>
  </mix>
  <mix name="Slave1">
    <field name="slave1" type="string"/>
    <notify name="Slave1" 
      clients="Subr" 
      fields="slave1"  
      service="Sequencer">
      <prop name="params"><![CDATA[
master:slave1
      ]]></prop>
      <prop name="subr.spec" 
        function="SubrFun"><![CDATA[
get("master") == "foo".
      ]]></prop>
    </notify>
  </mix>
  <mix name="Slave2">
    <field name="slave2" type="string"/>
    <notify name="Slave2" 
      clients="Subr" 
      fields="slave2" 
      service="Sequencer">
      <prop name="params"><![CDATA[
master:slave2
      ]]></prop>
      <prop name="subr.spec" 
        function="SubrFun"><![CDATA[
get("master") == "bar".
      ]]></prop>
    </notify>
  </mix>
  <mix name="Slave3">
    <field name="slave3" type="string"/>
    <notify name="Slave3" 
      clients="Subr" 
      fields="slave3"  
      service="Sequencer">
      <prop name="params"><![CDATA[
master:slave3
      ]]></prop>
      <prop name="subr.spec"
        function="SubrFun"/>
    </notify>
  </mix>
</folder>
The fields of the operations were all bound to the same parameter, master.

The Master consume has the following three notify subroutines:

  • Slave1, which fires only if the value of master is "foo"
  • Slave2, which fires only if the value of master is "bar"
  • Slave3, which fires every time the consume is implemented

If a subroutine has no predicate, it is the same as having a predicate whose result is always true.

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

Important: Target the parameter - not the field - names to access the values.