How to use Math Unit

From WebRatio WebML Wiki

Jump to: navigation, search


Introduction

The Math Unit is an Utility Unit designed to parse and evaluate mathematical expressions. A mathematical expression is composed by variables and operators and can be either statically defined at modeling time or dynamically provided through an input parameter at run-time. The Math Unit replaces the variables with their actual values provided through input parameters, evaluates the expression, and finally propagates the result as an output parameter. This Unit can be used as an Operation Unit and as a Content Unit, therefore can be positioned either outside or inside the page.

How to use the Math Unit

In order to correctly use a Math Unit it's necessary to provide a Default Expression and the Variables sub-elements.

The Default Expression is the expression calculated when a dynamical expression is not passed at execution time through the Expression input parameter. In this expression, the names of the operands must be the same of the variables defined as sub-elements of the Math Unit. The result of the evaluated expression will be provided as an output parameter called Result.

Let's make a simple example of the use of Math Unit. Here it's shown how to build a web application which permits to make addition between two operands. Here's the Web Model that realize the example, the Entry unit has two fields where the operands of our expression will be inserted by the user and a link which provides the inserted values to the Math unit. The Math Unit will evaluate the default expression with the values passed.

Image:mathUnit.png

In the Math Unit properties panel it's possible to define the default expression and its result type.

File:Properties.png

In order to add the necessary input parameters for the evaluation of the expression, right click on the Math Unit and then select "Add Variable". According to the Default Expression, it's necessary to add the "firstOperand" and "secondOperand" variables. Remember that the variables must have the same names of the operands of the Default Expression.

File:AddVariable.png File:Outline.png

Now it's necessary to set the coupling of the link between the Entry Unit and the Math Unit and of the link between the Math Unit and the Multi Message Unit, which will show on the browser the result of the math operation. The values contained in the fields of the Entry Unit must be passed to the input parameters of the Math Unit. The result calculated by the Math Unit must be passed to the Multi Message Unit in order to show it in the page.


It's even possible to define an expression that will be evaluate at run-time. The Math Unit indeed can receive, through the Expression input parameter, a string representing an expression that will be parsed and evaluated at run-time. Just click on the input link of the Math Unit and set the input parameter with the desired expression.

File:Coupling23.png


This will be the result of our model:


File:ResultModel.png


The supported operators are

  • + : Addition
  • - : Subtraction
  • * : Multiplication

  • / : Division
  • % : Modulus
  • <= : Less or Equal
  • < : Less than
  • >= : Greater or Equal
  • > : Greater than
  • = : Equal
  • != : Not Equal
  • and : Boolean And
  • or : Boolean Or
  • not : Boolean Not


The supported functions are:


  • cos : Cosine
  • sin : Sine
  • tan : Tangent
  • acos : Arc Cosine
  • asin : Arc Sine
  • atan : Arc Tangent
  • sqrt : Square Root
  • sqr : Square
  • log : Natural Logarithm
  • exp : Exponential
  • abs : Absolute Value
  • neg :Negate
  • rnd :Random Number (between 0 and its argument)



Related articles:
Category Difficulty Refers
Context Parameters Web Model Beginner Get Unit
Set Unit
Reset Unit
Create Unit Web Model Beginner Create Unit
Create a "Hello world" application Web Model Beginner Site View
WebML
WebRatio
Create a "Simple data centric" web application Web Model Beginner Site View
WebML
WebRatio
Custom Locations and Master pages Web Model Beginner Master Page
Custom Location
… further results












Did you find this article useful? Please rate it!

Rating: 5.0/5 (1 vote cast)

Personal tools