[MVEL]how to intercept all ASTNode without annotation?
by kapokfly
To be able to intercept against each node, I have to manually update the
expression String to register some interceptors like below, is there any
convenient API can make this enabled without adding such interceptors?
public void testMyInterceptors() {
// Avoid MVEL OOM issue: http://jira.codehaus.org/browse/MVEL-252
// This also disables JIT compilre
// At this time, MVEL does not support per instance configuration of the
JIT due to performance constraints.
OptimizerFactory.setDefaultOptimizer(OptimizerFactory.SAFE_REFLECTIVE);
Interceptor testLeftInterceptor = new Interceptor() {
public int doBefore(ASTNode node,
VariableResolverFactory factory) {
System.out.println("BEFORE left Node: " + node.getName());
System.out.println("BEFORE left Node: node.getClass(): " +
node.getClass());
//node.setAsLiteral();
//node.setLiteralValue("Ivan");
System.out.println("BEFORE left Node: " + node.getLiteralValue());
//node.setAccessor(new MetaDataAccessor());
return 0;
}
public int doAfter(Object val,
ASTNode node,
VariableResolverFactory factory) {
System.out.println("AFTER left Node: " + node.getName());
System.out.println("AFTER left Node: node.getLiteralValue(): " +
node.getLiteralValue());
return 0;
}
};
Interceptor testRightInterceptor = new Interceptor() {
public int doBefore(ASTNode node,
VariableResolverFactory factory) {
System.out.println("BEFORE right Node: " + node.getName());
System.out.println("BEFORE right Node: node.getClass(): " +
node.getClass());
return 0;
}
public int doAfter(Object val,
ASTNode node,
VariableResolverFactory factory) {
System.out.println("AFTER right Node: " + node.getName());
return 0;
}
};
Map<String, Interceptor> interceptors = new HashMap<String,
Interceptor>();
interceptors.put("testLeft", testLeftInterceptor);
interceptors.put("testRight", testRightInterceptor);
String expression = "(@testLeft emailMessage.from == @testRight 'Mark 1'
|| @testLeft myBean.var == 1)";
//compileExpression(expression, null, interceptors);
Map<String, Object> contextObjects = new HashMap<String, Object>();
EmailMessage emailMessage = new EmailMessage();
emailMessage.setFrom("Mark");
contextObjects.put("emailMessage", emailMessage);
MyBean myBean = new MyBean();
myBean.setVar(1);
contextObjects.put("myBean", myBean);
boolean evaluationResult =
executeExpression(compileExpression(expression, null, interceptors),
contextObjects, Boolean.class);
System.out.println(evaluationResult);
}
-----
Ivan, your Panda, forever
--
View this message in context: http://drools.46999.n3.nabble.com/MVEL-how-to-intercept-all-ASTNode-witho...
Sent from the Drools: User forum mailing list archive at Nabble.com.
14 years, 3 months
[MVEL]Getting Null Context Object with MVEL Inteceptor
by kapokfly
public void testMyInterceptors() {
// Avoid MVEL OOM issue: http://jira.codehaus.org/browse/MVEL-252
// This also disables JIT compilre
// At this time, MVEL does not support per instance configuration of the
JIT due to performance constraints.
OptimizerFactory.setDefaultOptimizer(OptimizerFactory.SAFE_REFLECTIVE);
Interceptor testLeftInterceptor = new Interceptor() {
public int doBefore(ASTNode node,
VariableResolverFactory factory) {
System.out.println("BEFORE left Node: " + node.getName());
System.out.println("BEFORE left Node: node.getClass(): " +
node.getClass());
//node.setAsLiteral();
//node.setLiteralValue("Ivan");
System.out.println("BEFORE left Node: " + node.getLiteralValue());
node.setAccessor(new MetaDataAccessor());
return 0;
}
public int doAfter(Object val,
ASTNode node,
VariableResolverFactory factory) {
System.out.println("AFTER left Node: " + node.getName());
System.out.println("AFTER left Node: node.getLiteralValue(): " +
node.getLiteralValue());
return 0;
}
};
Interceptor testRightInterceptor = new Interceptor() {
public int doBefore(ASTNode node,
VariableResolverFactory factory) {
System.out.println("BEFORE right Node: " + node.getName());
System.out.println("BEFORE right Node: node.getClass(): " +
node.getClass());
return 0;
}
public int doAfter(Object val,
ASTNode node,
VariableResolverFactory factory) {
System.out.println("AFTER right Node: " + node.getName());
return 0;
}
};
Map<String, Interceptor> interceptors = new HashMap<String,
Interceptor>();
interceptors.put("testLeft", testLeftInterceptor);
interceptors.put("testRight", testRightInterceptor);
String expression = "(@testLeft emailMessage.from == @testRight 'Mark'
|| @testLeft myBean.var == 1)";
//compileExpression(expression, null, interceptors);
Map<String, Object> contextObjects = new HashMap<String, Object>();
EmailMessage emailMessage = new EmailMessage();
emailMessage.setFrom("Mark");
contextObjects.put("emailMessage", emailMessage);
MyBean myBean = new MyBean();
myBean.setVar(1);
contextObjects.put("myBean", myBean);
boolean evaluationResult =
executeExpression(compileExpression(expression, null, interceptors),
contextObjects, Boolean.class);
System.out.println(evaluationResult);
}
public class MetaDataAccessor implements Accessor{
public Object getValue(Object ctx, Object elCtx, VariableResolverFactory
variableFactory) {
return null; //To change body of implemented methods use File |
Settings | File Templates.
}
public Object setValue(Object ctx, Object elCtx, VariableResolverFactory
variableFactory, Object value) {
return null; //To change body of implemented methods use File |
Settings | File Templates.
}
public Class getKnownEgressType() {
return null; //To change body of implemented methods use File |
Settings | File Templates.
}
}
When the MetaDataAccessor's geValue was invoked, the ctx and elCtx were
Null, however I am expecting the correct expression and context object
presents?
What is wrong with my codes?
I would expect the value reduce process can be done by my accessor in some
cases but apparently it is not.
-----
Ivan, your Panda, forever
--
View this message in context: http://drools.46999.n3.nabble.com/MVEL-Getting-Null-Context-Object-with-M...
Sent from the Drools: User forum mailing list archive at Nabble.com.
14 years, 3 months
MVEL expression
by Femke De Backere
Hi,
The following expression keeps returning the error [112,32]: [ERR 103]
Line 112:32 no viable alternative at input '+' in rule "RuleFlow-Split-
masterproef.ruleflows.shortsedation-18-22" in pattern Patient:
Patient(sedation.ultiva+0.02 > 0.1)
with sedation.ultiva returning a double
What am I doing wrong?
Thx,
Femke
14 years, 3 months
[MVEL]user typed in free format expression and advanced property/expression handler integration issue
by kapokfly
I have been trying and evaluating MVEL in past few days and in general, I
love this small kids but with 1 doubt needs ask for suggestions here.
The key scenario is, we have an UI to allow users type in free format
expression and we have a couple of custom property/variable even expression
handler classes needs to be integrated with.
Example sub-expression:
1) person.name.isVisiable --> Please note, the isVisisble is not a java
property of name (it is name field's meta data which requires a quite
different resolver other than how we resolve the person and how we resolve
to name field).
2) person.name --> normal property resolver.
Now with the user typed in expression:
(person.name.isVisiable == true) && (person.name == "Joe Wang"), what is
the best way to utilize MVEL to be able to parse/compile and execute this
expression?
the first thing the API needs to know is whether it is a meta field
evaluation or a normal java property evaluation, there is no such clue on
user typed in expression unless we can parse the expression and scan its
full text like 'person.name.isVisiable'
The thing I am currently looking at is @Interceptor or custom function, and
looking for a way to be able to translate the expression into an easier
format like (@meta person.name.isVisiable == true) && (person.name == "Joe
Wang").
Is there any API in MVEL exists can make such transformation easier? Or
maybe we can use a hidden field on UI to append @meta tag when user types
in?
Once 1st question is satisfied, the next one should be how we should proceed
@meta interceptor with the ASTNode passed in.
Looking forward to your comments.
Thanks.
-----
Ivan, your Panda, forever
--
View this message in context: http://drools.46999.n3.nabble.com/MVEL-user-typed-in-free-format-expressi...
Sent from the Drools: User forum mailing list archive at Nabble.com.
14 years, 3 months
display all rules name.
by srinivasasanda
Hi All,
Thanks in advance, Please clear this problem.
Please sort out my problem. I am able to display the package
names(Specification and GeoArea) but i want to display no. of rules names
which are available under each package.
(Under Specification package i have 2 rules & GeoArea package i have 2
rules)
I am getting the output Rule names as HashCode.
Specification
org.drools.definitions.rule.impl.RuleImpl@e483a8f
org.drools.definitions.rule.impl.RuleImpl@7d897ca2
GeoArea
org.drools.definitions.rule.impl.RuleImpl@9b82c6e5
org.drools.definitions.rule.impl.RuleImpl@5e6f671d
My Code is below:
KnowledgeBuilder kbuilder1 = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder1.add(ResourceFactory.newUrlResource("http://localhost:8082/guvnor-5.2.0.Final-jboss-as-5.1/org.drools.guvnor.G..."),ResourceType.PKG);
kbuilder1.add(ResourceFactory.newUrlResource("http://localhost:8082/guvnor-5.2.0.Final-jboss-as-5.1/org.drools.guvnor.G..."),ResourceType.PKG);
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder1.getKnowledgePackages());
for( KnowledgePackage kp : kbase.getKnowledgePackages() ){
System.out.println("------------------->"+kp.getName());
Collection c=kp.getRules();
Iterator it=c.iterator();
while(it.hasNext())
{
Object ruleName=it.next();
System.out.println("-------------------->"+ruleName);
}
Regards,
Srinivasa
--
View this message in context: http://drools.46999.n3.nabble.com/display-all-rules-name-tp3553740p355374...
Sent from the Drools: User forum mailing list archive at Nabble.com.
14 years, 3 months
facts custom sort
by Syargey
Hello all,
I experienced a problem I can't solve.
After rules firing I got a number of facts of certain type.
I would like to get a collection of facts sorted by custom field with a
query.
Drools guide proposes to sort facts by a rule like:
rule "Rule 03"
when
$number : Number( )
not Number( intValue < $number.intValue )
then
System.out.println("Number found with value: "+$number.intValue() );
retract( $number );
end
But it doesn't suit me:
1) I can't retract facts from session
2) I need to get sorted collection with a query which doesn't have 'then'
part.
Is there any method to get custom sorted collection with a query?
--
View this message in context: http://drools.46999.n3.nabble.com/facts-custom-sort-tp3538410p3538410.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
14 years, 3 months
Guvnor Versioning - eclipse plug-in
by Ronald Albury
JBoss Developer Studio 4.1.1.GA
Guvnor 5.2.0.Final
Tomcat 7.0.23
Guvnor: Created categories and packages. Imported Pojo jar file
Eclipse: Created drl rules. Added to Guvnor via plug-in
Guvnor: Confirmed drl rules showed up. Everything validated and verified
Eclipse: Modified drl rule. Update via plug-in and discovered that update
meant that I got a new copy from Guvnor and overwrote my changes ....
rats-changing the name for that command would be nice.
Eclipse: Modified drl rule again. Commit via plug-in.
Guvnor: The drl changes didn't show up when I opened the rule. Checked
Version History - it showed three versions (the third being the one with my
changes), but indicated that Current Version is 2. If I 'view' version 3 I
see my changes.
I tried re-validating and re-verifying rule. I tried saving rule (didn't
work). I tried re-validating and rebuilding the package.
I give up ... how do I get Version 3 to be the 'Current Version'.
14 years, 3 months