a simple rule engine for Magnolia CMS
Rules module for Magnolia CMS
The Rules module for Magnolia CMS is a Magnolia module allowing editors to define custom expressions to be evaluated at request time. The goal is to make the user self-sufficient in implementing rules and generating outputs, providing him with a flexible tool for building expressions of variable complexity.
The task of expression composition is made accessible to non-programmers by the availability of an editor with tools like a configurable sub-expression palette, operand pickers and syntax validation.
User Guide
The expression editor
The expression editor is a text area with a toolbar on top. Expressions can be entered directly or using the provided tools.

The explanation for the expression in the screenshot above is:
- now() : gets the current date and time;
- formatDate(now(), 'H'): gets the current hour of day as a string;
- toInt(formatDate(now(), 'H')): gets the current hour of day as a number;
- toInt(formatDate(now(), 'H')) < 12: returns true if the current hour is less than 12.
Toolbar: expression palette

The expression palette tool opens a dialog containing pre-configured expressions organized by category in tab panels. These reusable expressions can be simple functions or more complex compositions. The goal is to provide the user with sets of ready-to-use components, which can be quickly inserted in the editor.
Magnolia users having configuration permissions can add new expressions to the palette.

Toolbar: operator pickers

The operator pickers are dropdown menus that group operators by their category.
Toolbar: validation button

The validation button checks the expression and reports any error occurred during evaluation. In the screenshot below, the string "X" has been passed instead of "H" as pattern parameter to function formatDate: the validation error message explains that "X" is not recognized as a valid pattern component for date formatting.

Developer Guide
Including the tag library
The URI of the ContextMenu tag library is mgnlexpressions.
You can declare it in your JSP Document by adding a XML namespace:
<jsp:root version="2.0" ... xmlns:mexpr="mgnlexpressions">
Evaluating an expression
An expression stored as a property in the paragraph node can be evaluated inside a jsp by using the "evaluate" EL function:
<cms:setNode var="node" />
<c:set var="result" value="${mexpr:evaluate(node.expression, pageContext)}" /> The following code snippets shows how to evaluate an expression when a pageContext is not available:
String expression = NodeDataUtil.getString(node, "expression"); String result = ExpressionsElFunctions.evaluate(expression);
Adding functions
Any public static method of a java class can be made available in expressions: simply add a child node to the "expression-functions" node in the Configuration workspace, specifying the class and method to execute. In the following screenshot method Calendar.getInstance() has been bound to function "now".

For methods with parameters, add a node "parameterTypes" specifying the type of each paramter. The following screenshot declares a function for date formatting that takes two parameters: a Calendar for the date to format and a String for the pattern to use.

Adding expressions to the palette tool
The expression palette is populated with data from the "expression-libraries" configuration node. The following screenshot shows the configuration for a tab panel named "Date":

Each tab panel contains the expressions defined in node "expressions". In the example, tab panel "Date" contains an expression that returns a number corresponding to the current hour of day:

Project info & quick links
-
Latest release: 1.0.0
License: GPLv3 - Download Sourceforge file download area
- Bugs and feature requests OpenmindLAB Jira
- Changelog See what's new in release 1.0.0
- Source code Subversion public url
- Maven The dependency snippet for your pom