Getting Started
Imperius with Java binding (JavaSPL)
JavaSPL extends SPL to interact with Java objects directly.
Prerequisites
JavaSPL requires ANTLRv2.7.7 and Java 142 to be installed on your system.
Download JavaSPL
Go to the download page and download the latest binary distribution. Unzip the contents to a directory on your file system.
Running Samples
JavaSPL ships with a set of simple policies to help you get started. To run all policies do the following:
- Make sure an environment variables ANTLR_HOME and JAVA_HOME are set
- <JAVA_SPL_HOME>/samples/simple/runsamples (.bat or .sh)
- To run a single policy do the following: <JAVA_SPL_HOME>/samples/simple/runsamples (.bat or .sh) < policyname >
The sample program will try to delete - create - run each policy under the <JAVA_SPL_HOME>/samples/simple/policies folder. Note: If this is the first time you have run this program then an exception will be thrown while deleting the policy as it may not exist.
My First SPL Policy
Anchor Class
Every SPL policy needs to import atleast one anchor class. The anchor class encapsulates the state of system being managed using SPL policies. Properties and methods of instances of this class are referenced within the SPL policy during evaluation of conditions and actions.
We will use TestElement.java as our Anchor class for our policy
package org.apache.imperius.javaspl.samples.simplepolicies;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class TestElement {
public String ElementName;
public String s;
public char c;
public double r64;
public float r32;
public Calendar d;
public long s64;
public int s32;
public short s16;
public byte s8;
public boolean b;
public String childProperty;
public int testMethod(int a, int b) {
return a + b;
}
public TestElement() {
ElementName = "Element1";
s="string5";
c='d';
r64=54.0;
r32=54.0f;
d= new GregorianCalendar();
s64=-530;
s32=-530;
s16=-50;
s8=-10;
b=false;
childProperty="child property5";
}
}
A Simple Policy
The premise of this policy is simple if the instance property s32 of the instance imported by the policy equals -530 then reset it to 3
/*Import statement declares the classes and instances to be used within the policy. */
Import Class org.apache.imperius.javaspl.samples.simplepolicies.TestElement:te;
/* strategy statement is used to specify what needs to be done when multiple sub policies exist */
Strategy Execute_All_Applicable;
/* policy section */
Policy
{
/* declaration section is used to declare constants and methods */
Declaration
{
const = -530;
}
/* The "if" portion of the policy */
Condition
{
/* condition expression must be boolean */
te.s32 == const
}
/* The "then" portion of the policy. It specifies what actions need to be taken*/
Decision
{
/* Set operator is used to set instance property of the imported object*/
Set(te.s32 = 3)
}
}:1; /* specifies the priority of the policy */
Deploy and Run the policy
Now we are ready to test our new policy. To run the policy do the following
- Save the policy above as simple.spl under <JAVA_SPL_HOME>/resources/policies
- Run <JAVA_SPL_HOME>/runsamples simple
Notice that the condition evaluated to true because the value of property s32 was indeed -530

