MEL逻辑和行为奇怪,操作数的顺序很重要?

时间:2013-10-29 15:17:38

标签: mule mule-el

任何人都可以解释为什么会发生以下行为?为什么使用逻辑AND时操作数的顺序很重要?在mule-ce 3.3.1上运行。

输出如下:

true  
true  
false  
true


<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">

    <flow name="testFlow1" doc:name="testFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="20005" path="test" doc:name="HTTP"/>

        <message-properties-transformer overwrite="true" scope="session" doc:name="Message Properties">
              <add-message-property key="productNumber" value="1" />
        </message-properties-transformer>

        <logger level="INFO" message="#[(sessionVars['productNumber'] == '1' || sessionVars['productNumber'] == '2') and  (sessionVars['quantity'] == null)]" doc:name="Logger"/>
        <!-- true -->

        <logger level="INFO" message="#[(sessionVars['quantity'] == null) and (sessionVars['productNumber'] == '1' || sessionVars['productNumber'] == '2')]" doc:name="Logger"/>
        <!-- true -->

        <message-properties-transformer overwrite="true" scope="session" doc:name="Message Properties">
              <add-message-property key="productNumber" value="3" />
        </message-properties-transformer>

        <logger level="INFO" message="#[(sessionVars['productNumber'] == '1' || sessionVars['productNumber'] == '2') and  (sessionVars['quantity'] == null)]" doc:name="Logger"/>
        <!-- false -->

        <logger level="INFO" message="#[(sessionVars['quantity'] == null) and (sessionVars['productNumber'] == '1' || sessionVars['productNumber'] == '2')]" doc:name="Logger"/>
        <!-- true? it should not be true! -->        
    </flow>
</mule>

2 个答案:

答案 0 :(得分:1)

使用&amp; amp;而不是和。它计算正确的值。更新流程

<logger level="INFO" message="#[(sessionVars['productNumber'] == '1' || sessionVars['productNumber'] == '2') &amp;&amp;  (sessionVars['quantity'] == null)]" doc:name="Logger"/>
    <!-- false -->


<logger level="INFO" message="#[(sessionVars['quantity'] == null) &amp;&amp; (sessionVars['productNumber'] == '1' || sessionVars['productNumber'] == '1')]" doc:name="Logger"/>

答案 1 :(得分:0)

我不知道为什么会这样,但我在3.4.0 CE中有相同的经验。实现正确结果的一种解决方法似乎是使用“&amp;&amp;”,如

#[(sessionVars['quantity'] == null) && (sessionVars['productNumber'] == '1' || sessionVars['productNumber'] == '2')]