Example Rule
As a simple example, let’s say we have a rule
getUserAttribute that gets and returns a specified attribute – for example
fullname – of a user identified by her
accountId:
| <Rule name="getUserAttribute"> |
| <RuleArgument name="context" /> |
| <RuleArgument name="accountId" /> |
| <RuleArgument name="attrName" /> |
| <invoke name="getObjectIfExists"> |
| <invoke name="getAttribute"> |
Executing Rules from Java
Let’s say this rule is stored in a configuration object of
wsType EndUserLibrary whose name is
MyCorp-UserRuleLibrary.
That is, the full name to our rule is
MyCorp-UserRuleLibrary:getUserAttribute.
It takes only a few lines to execute this rule from Java. Let’s first create a
Util class holding our method:
| package com.mycorp.util.XpressUtil; |
| import com.waveset.expression.ExState; |
| import com.waveset.object.LighthouseContext; |
| import com.waveset.object.Rule; |
| import com.waveset.util.WavesetException; |
| public class XpressUtil { |
| public static Object executeRule(LighthouseContext context, |
| String ruleName, Map args) throws WavesetException { |
| Rule rule = Rule.findRule(context.getCache(), ruleName); |
| ExState state = new ExState(); |
| return rule.eval(state, args); |
Now we can execute our XPress rule from Java:
| import java.util.HashMap; |
| import com.waveset.session.Session; |
| import com.waveset.session.SessionFactory; |
| import com.waveset.util.EncryptedData; |
| import com.mycorp.util.XpressUtil; |
| EncryptedData encryptedPassword = new EncryptedData("configurator"); |
| Session wsSess = SessionFactory.getSession("Configurator", encryptedPassword); |
| String ruleName = "MyCorp-UserRuleLibrary:getUserAttribute"; |
| Map args = new HashMap(); |
| args.put("context", wsSess); |
| args.put("accountId", "Configurator"); |
| args.put("attrName", "fullname"); |
| Object result = XpressUtil.executeRule( wsSess, ruleName, args ); | | |
No comments:
Post a Comment