Examples

Bruce Kissinger

Related

Wiki: Home

Discussion

  • The syntax to run a file containing WEE rules is:

    wee:run(filename).                                  
    or                  
    wee:run(filename, [context variables]).
    

    Note that these commands must be entered inside the Erlang comand line shell.

     

  • Loop Example

    In this example, a variable named counter is defined and assigned the initial value of 1. An endpoint is also defined that displays the current date/time. The control flow consists of three activities.

    1. An activity statement calls an endpoint that displays the current time.
    2. A cycle statement defines the begining of a loop which will execute while the counter value is less than 10000.
    3. An activity statement increments the value of the counter variable
    4. When the cycle completes, the timer endpoint is called again
    %
    % Test performance of large loops
    %
    endpoint :simpletimer => erl://simpletimer:display
    context :counter => 1
    
    controlflow do
    
        % call the timer endpoint to display the current time
        activity :starttime, :call, simpletimer
        end
    
        % main loop
        cycle (@counter < 10000) do
            activity :increment, :manipulate do
                @counter += 1
            end
        end
    
        % call the timer endpoint to display the current time
        activity :endtime, :call, simpletimer
        end
    end
    

    Parallel Activities

    In this example, three HTTP endpoints are defined. A parallel activity is specified and the ":wait" clause tells the engine to wait until all parallel branches have completed.

    endpoint :endpoint1 => http://localhost/endpoint1
    endpoint :endpoint2 => http://localhost/endpoint2
    endpoint :endpoint3 => http://localhost/endpoint3
    
    controlflow do
    
        parallel :wait do
    
            % operates in a seperate thread -- this should complete first
            parallel branch do
                activity :branch1a1, :call, endpoint1
                end
    
                activity :branch1a2, :call, endpoint2
                end
            end
    
            % operates in a seperate thread - this should complete last because 
            % endpoint3 takes a long time to execute
            parallel branch do
                activity :branch2a1, :call, endpoint3
                end
            end
    
        end
    
        % this should not be run until both parallel branches have completed
        activity :trace, :manipulate do
            show runtime_context
        end
    
    end
    

    Note that the statement show runtime_context causes the values of any context variables to be displayed.


    Web Service Example

    In this example, a Google currency conversion service is called. The control flow consists of three activities.

    • First, the query for the URL is constructed by several manipulate activities.
    • Next, the web service is invoked and the query is passed as parameter. The query results are copied to a variable named "returnValue".
    • Finally, some processing of the returned values would be performed.
    % 
    % Demonstrate currency conversion using a Google service
    %
    
    endpoint :currencyConversion => http://www.google.com/ig/calculator
    
    context :amount => '100'
    context :fromCurrencyCode => 'USD'
    context :toCurrencyCode => 'INR'
    context :urlQuery => nil
    context :returnValue => nil
    
    controlflow do
        %
        % build the URL query string which should look like
        %   hl=en&q=100USD=?INR
        %
        activity :build1, :manipulate do
            @urlQuery = 'hl_en&q=' + @amount
        end
        activity :build2, :manipulate do
            @urlQuery = @urlQuery + @fromCurrencyCode
        end
        activity :build3, :manipulate do
            @urlQuery = @urlQuery + '=?'
        end
        activity :build3, :manipulate do
            @urlQuery = @urlQuery + @toCurrencyCode
        end
    
        % call the currency conversion service
        activity :getConversion, :call, currencyConversion, @urlQuery do |results| 
            @returnValue = results
        end
    
        % do something with the currency return results
    end
    

    Note that the statement show runtime_context causes the values of any context variables to be displayed.


    Executing an operating system command Example

    In this example, the command "ls" is executed to display the contents of a directory. Two examples are shown, one that executes the endpoint with no parameters and the second example with a paramater. If this were running on a Windows-based operating sytem, instead of "ls" you would run "dir". Note that any operating system command including a batch file can be executed.

    • First, an endpoint named "cmdls" is defined. Note that the endpoint URL has the prefix "cmd://".
    • Next, two context variables are defined for storing the directory contents. Another context variable is defined to hold a parameter that will be passed.
    • Next, the endpoint is executed and the results stored in a variable named "contents".
    • Next, the endpoint is executed with a parameter of "/tmp" and the results stored in a variable named "contents2".
    • Finally, the value of all context variables is displayed.

    %
    % Test execution of an operating system command
    %
    endpoint :cmdls=> cmd://ls
    context :contents => "NA"
    context :contents2 => "NA"
    context :parameter => "/tmp"

    controlflow do

    % call the cmdls endpoint to display the contents of the current directory 
    % this example uses no parameters but stores the results in a variable named "contents"
    activity :command, :call, cmdls do |results|
    @contents = results
    end
    
    % call the cmdls endpoint to display the contents of the current directory and pass a parameter
    % this example stores the results in a variable named "contents2"
    activity :command, :call, cmdls, @parameter do |results|
    @contents2 = results
    end
    
    % display the runtime context variables 
    activity :trace, :manipulate do
    show runtime_context
    end
    

    end

     
    Last edit: Bruce Kissinger 2012-11-07