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
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 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:
|
<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
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> |
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
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
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 |
<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.
-
- 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.
-
- 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.
-
- 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.