drools条件编译错误

时间:2014-02-16 09:07:56

标签: drools

I am getting compilations errors in drools.  I am velocity file for rule string. 
I am not familiar with drools.  Please help me in resolving the following.
I am getting compilation error while loading the rules:

Error while creating rule package - [927,27]: unknown:927:27 Unexpected token '"Y1PC"'[927,35]: unknown:927:35 mismatched token: [@2072,12451:12451='>',<77>,927:35]; 
expecting type RIGHT_PAREN[927,61]: unknown:927:61 mismatched token: [@2078,12477:12482='"Y0CC"',<20>,927:61]; expecting type RIGHT_PAREN[928,3]: 
unknown:928:3 Unexpected token 'departureTime'[928,25]: unknown:928:25 Unexpected token 'departureTime' 

以下是vm代码:

#macro(generateDepartureMinTimeCond $condition)
&& departureTime >= $condition.minVal && departureTime <= $condition.maxVal     
#end

#macro(generatePaxVarCond $condition)
#set( $Q = '"' )
&& $mapValues: variableValues
Map(this["Y1PC"] > this["Y0CC"]) from $mapValues
##flight : Flight()
##&& getValue(flight.getVariableValues(),${condition.leftPaxCls.abbr}${condition.leftPaxVariable.paxVariableCode}) ${condition.operator} getValue(flight.getVariableValues(),${condition.rightPaxCls.abbr}${condition.rightPaxVariable.paxVariableCode})
##&& variableValue[${Q}${condition.leftPaxCls.abbr}${condition.leftPaxVariable.paxVariableCode}${Q}]${condition.operator} variableValue[${Q}${condition.rightPaxCls.abbr}${condition.rightPaxVariable.paxVariableCode}${Q}]
#end

#macro(generateConditionText $condition)
#elseif($condition.conditionType.id.longValue() == $COND_DEP_MINUS_TIME)
#generateDepartureMinTimeCond($condition)
#elseif($condition.conditionType.id.longValue() == $COND_PAX_VAR)
#generatePaxVarCond($condition)
#end    
#end

#macro(generateConditionsText $rule $p)

flight : Flight(eval(fireNextPriority==true), categoryCount==$category.paxCatgySeq, eval(!firedRules.contains(Integer.valueOf($rule.id))) && airlineId == $ruleSet.carrier.id && (departureDate >= $ruleSet.effDate.getTime()) && (departureDate <= $ruleSet.expDate.getTime()) && (departureDate >= $rule.effDate.getTime()) && (departureDate <= $rule.expDate.getTime())
&& eval($p == $rule.priority)  
#foreach($condition in $rule.paxConditions)#generateConditionText($condition)#end)
#end


#macro(generateActionRollover $action)
        Integer oldValueFrom;
        Integer oldValueTo;
        Integer newValueFrom;
        Integer newValueTo;
        Integer oldPCVarFrom;
        Integer oldPCVarTo;
        Integer newPCVarFrom;
        Integer newPCVarTo;
#foreach($rollover in $action.actionRolloverSet)
        #if (!$rollover.varCode)
        #set ($fromString = "$rollover.fromCabinClass.abbr$roVariable")
        #set ($toString = "$rollover.toCabinClass.abbr$roVariable")
        #set ($compareTo = "$rollover.fromCabinClass.abbr$capVariable")
        oldValueFrom = flight.getVariableValue("$fromString");
        oldValueTo = flight.getVariableValue("$toString");
        flight.rollOverVariableValue("$fromString", "$toString", "$compareTo");
        newValueFrom = flight.getVariableValue("$fromString");
        newValueTo = flight.getVariableValue("$toString");

        System.out.println("generateActionRollover 1 valuefrom :"+ oldValueFrom +" -- "+ newValueFrom  +" -- "+ $fromString);
        System.out.println("generateActionRollover 1 valueto :"+ oldValueTo +" -- "+  newValueTo  +" -- "+ $toString);

        ruleLog.addLogEntry(flight.getSoFltLegId(),flight.getFlightLegId(), new Long($ruleSet.id), new Long($category.id), new Long($action.paxAdjRule.id), "$rollover.fromCabinClass.abbr", "$roVariable", oldValueFrom, newValueFrom, flight.getPaxCountString());
        ruleLog.addLogEntry(flight.getSoFltLegId(),flight.getFlightLegId(), new Long($ruleSet.id), new Long($category.id), new Long($action.paxAdjRule.id), "$rollover.toCabinClass.abbr", "$roVariable", oldValueTo, newValueTo, flight.getPaxCountString());
        #else
        #set ($fromString = "$rollover.fromCabinClass.abbr$rollover.varCode")
        #set ($fromPaxString = "$rollover.fromCabinClass.abbr$roVariable")
        #set ($toString = "$rollover.toCabinClass.abbr$roVariable")
        #set ($compareToCC = "$rollover.toCabinClass.abbr$capVariable")
        #set ($compareToPC = "$rollover.toCabinClass.abbr$roVariable")
        oldValueFrom = flight.getVariableValue("$fromString");
        oldValueTo = flight.getVariableValue("$toString");
        oldPCVarFrom = flight.getVariableValue("$fromPaxString");
        oldPCVarTo = flight.getVariableValue("$toString");
        flight.rollOverVariableValue("$fromString", "$fromPaxString", "$toString", "$compareToCC", "$compareToPC");
        newValueFrom = flight.getVariableValue("$fromString");
        newValueTo = flight.getVariableValue("$toString");
        newPCVarFrom = flight.getVariableValue("$fromPaxString");
        newPCVarTo = flight.getVariableValue("$toString");


        #end
#end
#end

#macro(generateRuleText $rule $p)
rule "$category.paxCatgyCode - $rule.ruleCode"
salience -$rule.priority$rule.sortSeq
agenda-group  "$rule.paxRuleSet.paxRuleSetCode - $category.paxCatgyCode"
auto-focus true


    when
        #generateConditionsText($rule $p)
    then
        System.out.println("Firing rule: $category.paxCatgyCode - $rule.ruleCode");


        flight.setStatus(true);
        flight.setPriority($p);

        flight.addToFiredRules($rule.id);

        update(flight);

        #set ($maxPrio = "-$rule.priority$rule.sortSeq");
        ## \#System.out.println("maxPrio : " + $maxPrio );

#foreach($action in $rule.actions)
#if ($category.catgyType.id.longValue() == $CT_ROLLOVER)
#generateActionRollover($action)
#end

#end
end
#end


#foreach($rule in $rulesWithPriorities)

#set ($lastRuleId = $sortedRulesInCatgMap.get($rule.priority))

#if($rule.isEnabled.intValue() == 1)
#generateRuleText($rule $p)
#end
#set ($maxSequ = $rule.sortSeq)
#end ## \# foreach rule


    rule "Default Rule to clear agenda $category.paxCatgyCode - $p" 
    salience #generatePrio("$maxSequ" $p)
    agenda-group "$ruleSet.paxRuleSetCode - $category.paxCatgyCode"
    auto-focus true

         when           
             $flight : Flight(eval(fireNextPriority==true),categoryCount==$category.paxCatgySeq, eval(priority==$p), eval(status==true))

         then 
            System.out.println("Firing rule: Default Rule to clear agenda $category.paxCatgyCode - Priority $p " );

                    $flight.setFireNextPriority(false);
                    $flight.setStatus(false);

                    ## \#flight.setExecuteFinalRuleForCatg(false);


                    update($flight);

                    drools.getWorkingMemory().clearAgendaGroup("$ruleSet.paxRuleSetCode - $category.paxCatgyCode");             

    end


#end ## \# foreach p



#end





#macro (calcPriority $value)
#set($prio = ($value.trim()))
#set ($Integer = 0)
#set ($intValue = ($Integer.parseInt($prio) - 1))$intValue#end

#macro (generatePrio $value $p)
#set ($Integer = 0)
#set ($priValue = ($Integer.parseInt($value) + 1))
-$p$priValue #end

Object Flight code:

public class Flight {


    Long departureTime;


    // Variable Details
    Map<String, Integer> variableValues = new HashMap<String, Integer>();


    public Long getDepartureTime() {
        return departureTime;
    }

    public void setDepartureTime(Long departureTime) {
        this.departureTime = departureTime;
    }


    public Map<String, Integer> getVariableValues() {
        return variableValues;
    }

    public void setVariableValues(Map<String, Integer> variableValues) {
        this.variableValues = variableValues;
    }

    /**
     * Add an entry to the variable-count map for the given variable with the given count.
     * If an entry already exists, update the count.
     * @param variableCode
     * @param count
     */
    public void addVariableValue(String variableCode, Integer count){
        if (count < 0) count = 0;
        this.variableValues.put(variableCode, count);
    }



    /**
     * Return the currently stored count for the variable code
     * @param variableCode
     * @return
     */
    public Integer getVariableValue(String variableCode){
        Integer value = this.variableValues.get(variableCode);
        return value == null? 0 : value;
    }   



}

以上vm文件的生成规则字符串。规则用于生成在对象中使用的map的keya,并且键显示为意外的令牌:

规则&#34; PRL - PRL001&#34; 显着性-11 议程小组&#34; PARS_LIVE - PRL&#34; 自动对焦真实

flight : Flight(eval(fireNextPriority==true), categoryCount==3, eval(!firedRules.contains(Integer.valueOf(23537))) && airlineId == 1189321015 && (departureDate >= 1263925800000) && (departureDate <= 4102338600000) && (departureDate >= 1263925800000) && (departureDate <= 4102338600000)
&& eval(1 == 1)  
&& flight.getVariableValue("Y1PC") > flight.getVariableValue("Y0CC")
&& departureTime >= 6 && departureTime <= 8     
     )
    then
        System.out.println("Firing rule: PRL - PRL001");


        flight.setStatus(true);
        flight.setPriority(1);

        flight.addToFiredRules(23537);

        update(flight);

        Integer oldValueFrom;
        Integer oldValueTo;
        Integer newValueFrom;
        Integer newValueTo;
        Integer oldPCVarFrom;
        Integer oldPCVarTo;
        Integer newPCVarFrom;
        Integer newPCVarTo;
        oldValueFrom = flight.getVariableValue("Y1PC");
        oldValueTo = flight.getVariableValue("C1PC");
        flight.rollOverVariableValue("Y1PC", "C1PC", "Y1CC");
        newValueFrom = flight.getVariableValue("Y1PC");
        newValueTo = flight.getVariableValue("C1PC");

end

1 个答案:

答案 0 :(得分:1)

这部分条件:

flight.getVariableValue("Y1PC") > flight.getVariableValue("Y0CC")
必须将

写为eval表达式,因为Drools希望任何getX()调用都是没有参数的getter调用。

eval( flight.getVariableValue("Y1PC") > flight.getVariableValue("Y0CC") )

直接访问Map variableValues可能更简单,例如

variableValues["Y1PC"] > variableValues["Y0CC"]