[rules-users] how to get field names from Rule using drools 5.5

jeetendray jeetcyb at gmail.com
Wed Jan 23 13:25:27 EST 2013


Let me explain my requirement again, if its unclear from my prev email.

Ref DRL:

package drools
         import drools.ProposalLight
         rule Proposal bugdet rule
         when
           $obj : ProposalLight(proposalName.equals("Jeet") || proposalId ==
 121)
           then
           $obj.addFailedRule("Proposal bugdet rule");
        end;
 
I have a POJO ProposalLight with properties: proposalName, proposalId....
and more.

I have annotations on these properties in the class. So here's is what I do:

1. Find the properties of POJO in the DRL which construct the condition.
e.g. proposalName and proposalId.
2. Apply logic based on annotation on these properties.
3. do further processing.

For step 1: I used to locate these properties in DRL using Drools 4.xx with
below code snippet. But since few of the classes have been deprecated in
Drools 5.5 hence can;t make use of ORConstriants & LiteralConstraints.


 private Set<KeyValuePair> getRestrictedRules(RuleBase ruleBase) throws
PBAppException {
    Set<KeyValuePair> restrictedRuleCollection = new
HashSet<KeyValuePair>();
    Field alphaConstraintsField = unrestrictOrConstraints();

    if (ruleBase != null) {
      Package[] packages = ruleBase.getPackages();
      if (!DSMGeneralUtil.isArrayEmptyOrNull(packages)) {
        for (Package pack : packages) {
          Rule[] rules = pack.getRules();
          if (!DSMGeneralUtil.isArrayEmptyOrNull(rules)) {
            for (Rule rule : rules) {
              GroupElement groupElement = rule.getLhs();
              List children = groupElement.getChildren();
              if (!DSMGeneralUtil.isCollectionEmptyOrNull(children)) {
                for (Object childrenObject : children) {
                  if (childrenObject instanceof Pattern) {
                    Pattern pattern = (Pattern) childrenObject;
                    List constraints = pattern.getConstraints();
                    if
(!DSMGeneralUtil.isCollectionEmptyOrNull(constraints)) {
                      for (Object constraintObject : constraints) {
                        LiteralConstraint literalConstraint = null;
                        OrConstraint orConstraint = null;
                        // if the rule is AND rule then we will find all the 
                        // conditions as instances of LiteralConstraint.
                        if (constraintObject instanceof LiteralConstraint) {
                          literalConstraint = (LiteralConstraint)
constraintObject;
                          if (addFieldIfApplicable(restrictedRuleCollection,
literalConstraint,
                              rule.getName(), restrictedFieldNameGroup)) {
                            break;
                          }
                          // if the rule is OR rule then we will find all
the 
                          // conditions as instances of OrConstraint.
                        } else if (constraintObject instanceof OrConstraint)
{
                          orConstraint = (OrConstraint) constraintObject;
                          if
(getGeoRestrictedRulesForOrConstraints(restrictedRuleCollection,
                              orConstraint, rule.getName(),
alphaConstraintsField, restrictedFieldNameGroup)) {
                            break;
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    return restrictedRuleCollection;
  }

some code from addFieldIfApplicable method:

    Object fieldExtractorObject = literalConstraint.getFieldExtractor();
    if (fieldExtractorObject instanceof ClassFieldExtractor) {
      ClassFieldExtractor classFieldExtractor =
(ClassFieldExtractor)fieldExtractorObject;
      String fieldName = classFieldExtractor.getFieldName(); // this is what
i need

Thanks,
Jeetendra.




--
View this message in context: http://drools.46999.n3.nabble.com/how-to-get-field-names-from-Rule-using-drools-5-5-tp4021655p4021764.html
Sent from the Drools: User forum mailing list archive at Nabble.com.


More information about the rules-users mailing list