User Tools

Site Tools


creating_custom_functions

Creating custom functions

By creating custom-written functions, Ecolego can easily be extended. Custom functions are written in Java.

The typical scenario is to write functions that perform a computation of some sorts, but there is nothing that stops the user from creating functions that

  • Calls an executable, for instance a legacy model.
  • Read or writes data to a file
  • Pulls or pushes data to a network server
  • Displays simulation progress in a seperate window.

Creating a user-defined function

Functions are added from the projects window. Expand the project that you wish to add a function to, and right-click the “Functions” node. Select Add Function from the pop-up menu.

Editing a user-defined function

To edit a function, right-click the function in the projects window and select Edit from the popup-menu.

The edit window has three tabs:

Properties

This page lets you name the function and select the number of parameter arguments the function shall take. Changing these properties will also change the source code for the function which is displayed in the second tab.

Source

The grey area of the text box contains auto-generated code that you are not allowed to change. Press the Test button to see if the function is valid - meaning that it will compile. See the section below on how to write functions.

Library

Lets you add source and library files that are to be used by the function. Valid file types are .java, .class or .jar

Writing user-defined functions

The recommended approach is to keep the function as short as possible and delegate functionality to other classes that are included as libraries. Editing the function directly in the Source page is cumbersome compared to writing Java code in a professional programming tool such as Eclipse or Netbeans.

Structure

The user-defined function must extend a class defined in Ecolego called AbstractUserDefinedFunction. The class has the abstract method function() which takes a vararg argument of doubles and returns a single double.

AbstractUserDefinedFunction also has some callback functions that can be overridden to handle cases like simulationStarted(), as well as some functions that return information about the simulation like getIteration().

AbstractUserDefinedFunction function(arg … double):double This must be overridden and returns a single scalar value - typically a value calculated from the given arguments. isProbabilistic():boolean Returns true if the number of iterations to run is greater than 1 getIteration():int Returns the current iteration simulationStarted() Callback function which is called when the simulation starts simulationEnded() Callback function which is called when the simulation ends iterationStarted() Callback function which is called when a new iteration starts

Using functions

After a function has been added it is accessible from the Expression editing tool just like the built-in functions like abs() or max().

See also

creating_custom_functions.txt · Last modified: 2019/11/18 13:34 (external edit)