Page 1 of 1

Strange bug(?) in CheckConditions sub in Events module

Posted: Sun Sep 26, 2010 5:33 pm
by dali
I found a strange bug in CheckConditions that I can't figure out.

I have an event with a TimeNow trigger. The events have two conditions, "Season = fall" OR "Season = winter". Since it's now fall, the action of the event should trigger but it doesn't.
However, if I change the condition to "Season = fall" AND "Season = winter", the action runs.

I have looked through the CheckConditions sub several times, but I can't figure out what's wrong.

/Dali

Debug from the OR-version:

Code: Select all

2010/09/26 17:24:00 [Events] 1a. Running TimeNow query 'SELECT * FROM events, triggers WHERE events.trigger1 = triggers.id AND triggers.type = 1 AND events.enabled AND triggers.param1 = 17 AND triggers.param2 = 24'
2010/09/26 17:24:00 [Events] 1b. Got 1 result(s).
2010/09/26 17:24:00 [Events] 2c. TimeNow trigger on event id 11 named 'Tänd fönsterbelysning på morgonen' with trigger condition 'Hour = 17 and Minute = 24'
2010/09/26 17:24:00 [Events] 2c. Check condition 'fall = fall' = True
2010/09/26 17:24:00 [Events] 2d. Condition with id 4 returned True
2010/09/26 17:24:00 [Events] 2c. Check condition 'fall = winter' = False
2010/09/26 17:24:00 [Events] 2d. Condition with id 3 returned False
2010/09/26 17:24:00 [Events] 2e. Event id 11 named 'Tänd fönsterbelysning på morgonen' has has failed CheckConditions, discarding.
Debug from the AND-version:

Code: Select all

2010/09/26 17:26:00 [Events] 1a. Running TimeNow query 'SELECT * FROM events, triggers WHERE events.trigger1 = triggers.id AND triggers.type = 1 AND events.enabled AND triggers.param1 = 17 AND triggers.param2 = 26'
2010/09/26 17:26:00 [Events] 1b. Got 1 result(s).
2010/09/26 17:26:00 [Events] 2c. TimeNow trigger on event id 11 named 'Tänd fönsterbelysning på morgonen' with trigger condition 'Hour = 17 and Minute = 26'
2010/09/26 17:26:00 [Events] 2c. Check condition 'fall = fall' = True
2010/09/26 17:26:00 [Events] 2d. Condition with id 4 returned True
2010/09/26 17:26:00 [Events] 2c. Check condition 'fall = winter' = False
2010/09/26 17:26:00 [Events] 2d. Condition with id 3 returned False
2010/09/26 17:26:00 [Events] 2e. Event id 11 named 'Tänd fönsterbelysning på morgonen' is validated, running action(s).
2010/09/26 17:26:00 [Events] 3a. Running action with id 8 for Event with id 11
2010/09/26 17:26:00 20609F00FF
2010/09/26 17:26:02 ACK=> 37
2010/09/26 17:26:02 [Events] 1a. Running DeviceChange query 'SELECT * FROM events, triggers WHERE events.trigger1 = triggers.id AND triggers.type = 3 AND events.enabled AND triggers.param1 = 38 AND triggers.param2 = Value'
2010/09/26 17:26:02 [Events] 1b. No result.
2010/09/26 17:26:02 [Events] 3b. Action with id 8 named 'Tänd fönsterbelysning' executed!

Re: Strange bug(?) in CheckConditions sub in Events module

Posted: Sun Sep 26, 2010 6:26 pm
by hhg
Hi Dali

It must be in this part of the code, as you probably already is looking at:

Events.module line 235++:

Code: Select all

      ' check the OR and AND clause
      IF bCondition1 = FALSE AND IF bCondition2 = FALSE THEN
        bCondition = FALSE
      ELSE IF sOperand == "AND" THEN
        IF bCondition1 = FALSE OR bCondition2 = FALSE THEN
          bCondition = FALSE
        ENDIF
      ENDIF
Try change the first line to:

Code: Select all

 IF bCondition1 = FALSE AND bCondition2 = FALSE THEN
I think the second IF in that line is causing the ELSE part to match the wrong IF, so remove that - But I'm not sure, pretty new to Gambas.

Regards
hhg

Re: Strange bug(?) in CheckConditions sub in Events module

Posted: Sun Sep 26, 2010 8:07 pm
by dali
I also thought that part looked a bit strange, but removing the second IF makes no difference...

Re: Strange bug(?) in CheckConditions sub in Events module

Posted: Sun Sep 26, 2010 11:19 pm
by hhg
No, your rigth, it's not the second if.
I've reproduced your scenario, and the problem is the dubbel == in this line:

Code: Select all

ELSE IF sOperand == "AND" THEN
Change that to a single =:

Code: Select all

ELSE IF sOperand = "AND" THEN
and it works, at least in my test case :wink:

Regards
hhg

Re: Strange bug(?) in CheckConditions sub in Events module

Posted: Mon Sep 27, 2010 1:01 pm
by RDNZL
I had this coded and tested...

Code: Select all

' check the optional conditions 1 and 2
PRIVATE SUB CheckConditions(iCondition1 AS Integer, iCondition2 AS Integer, sOperand AS String) AS Boolean

  DIM bCondition, bCondition1, bCondition2 AS Boolean

  ' assume that all conditions are met
  bCondition = FALSE

  IF iCondition1 THEN
      bCondition1 = CheckSingleCondition(iCondition1)
      IF iCondition2 THEN
        bCondition2 = CheckSingleCondition(iCondition2)
      ELSE
        bCondition2 = bCondition1
      ENDIF

      ' check the OR and AND clause
      SELECT CASE UCase(sOperand)
        CASE "OR"
          IF Main.bEventsDebug THEN Main.WriteDebugLog(("[Events] 2e. CheckConditions checking iCondition1 id ") & iCondition1 & " OR " & (" iCondition2 id ") & iCondition2)
          IF bCondition1 OR IF bCondition2 THEN bCondition = TRUE
        CASE "AND"
          IF Main.bEventsDebug THEN Main.WriteDebugLog(("[Events] 2e. CheckConditions checking iCondition1 id ") & iCondition1 & " AND " & (" iCondition2 id ") & iCondition2)
          IF bCondition1 AND IF bCondition2 THEN bCondition = TRUE
      END SELECT

  ENDIF
  RETURN bCondition

END
The fact that IF sString == "AND" fails looks like a Gambas bug to me, reported it.
It should do a case unsensitive test... see here...
http://gambasdoc.org/help/cat/stringop

Re: Strange bug(?) in CheckConditions sub in Events module

Posted: Mon Sep 27, 2010 2:08 pm
by dali
Ron, doesn't that code (and partly the old code) assume that an Operand always exist? That won't be the case if only one condition exist in the event, or am I missing something?

/Dali

Re: Strange bug(?) in CheckConditions sub in Events module

Posted: Mon Sep 27, 2010 3:20 pm
by RDNZL
Good point.

If it's not set then condition2 is also not set so there is only one condition and bCondition2 will be the same as bCondition1.
The event editor takes care of having operand if there is condition2 defined, but have to check if this is still true if an existing condition2 is removed...

Re: Strange bug(?) in CheckConditions sub in Events module

Posted: Mon Sep 27, 2010 3:38 pm
by dali
What I was aiming at is that the CheckConditions sub takes care of setting bCondition2 = bCondition1 (as you said above) if iCondition2 isn't set. But there's no code to check if sOperand is set, which should be fine for present code (that's not working due to a possible Gambas bug) but not the code with the SELECT CASE statement.
I guess something like this needs to be added:

Code: Select all

IF NOT sOperand THEN
  sOperand = "OR"
ENDIF

Re: Strange bug(?) in CheckConditions sub in Events module

Posted: Mon Sep 27, 2010 4:08 pm
by RDNZL
Last version.... :roll:

Code: Select all

' check the optional conditions 1 and 2
PRIVATE SUB CheckConditions(iCondition1 AS Integer, iCondition2 AS Integer, sOperand AS String) AS Boolean

  DIM bCondition1, bCondition2 AS Boolean

  IF iCondition1 THEN
      bCondition1 = CheckSingleCondition(iCondition1)
      IF iCondition2 THEN
        bCondition2 = CheckSingleCondition(iCondition2)
      ELSE
        sOperand = ""
      ENDIF

      ' check the OR and AND clause
      SELECT CASE UCase(sOperand)
        CASE "OR", ""
          IF Main.bEventsDebug THEN Main.WriteDebugLog(("[Events] 2e. CheckConditions checking iCondition1 id ") & iCondition1 & " OR " & (" iCondition2 id ") & iCondition2)
          IF bCondition1 OR IF bCondition2 THEN RETURN TRUE
        CASE "AND"
          IF Main.bEventsDebug THEN Main.WriteDebugLog(("[Events] 2e. CheckConditions checking iCondition1 id ") & iCondition1 & " AND " & (" iCondition2 id ") & iCondition2)
          IF bCondition1 AND IF bCondition2 THEN RETURN TRUE
        CASE ELSE
      END SELECT

  ELSE ' no conditions defined so return true
    RETURN TRUE
  ENDIF
  RETURN FALSE

END

Re: Strange bug(?) in CheckConditions sub in Events module

Posted: Mon Sep 27, 2010 11:41 pm
by hhg
I see
I didn't know of the case insentive string comparison operator

A small test in the console window:

Code: Select all

? "AND" == "AND"
False
? "AND" == "and"
False
? "AND" = "AND"
True
? "AND" = "and"
False
Well, I agree, it's not working the way I would expect....

/hhg

Re: Strange bug(?) in CheckConditions sub in Events module

Posted: Tue Sep 28, 2010 8:19 am
by RDNZL
Revision: 3245
http://gambas.svn.sourceforge.net/gamba ... 5&view=rev
Author: gambas
Date: 2010-09-28 04:58:29 +0000 (Tue, 28 Sep 2010)

Log Message:
-----------
[INTERPRETER]
* BUG: The == operator works correctly now.

Modified Paths:
--------------
gambas/branches/2.0/main/gbx/gbx_subr_test.c
The Gambas bug was fixed this morning, so my original code was not the problem after all. :wink:

Re: Strange bug(?) in CheckConditions sub in Events module

Posted: Tue Sep 28, 2010 12:16 pm
by j.hoekstra
Latest gambas evaluates it like this:

Code: Select all

? "AND" == "AND"
True
? "AND" == "and"
True
? "AND" = "AND"
True
? "AND" = "and"
False

Re: Strange bug(?) in CheckConditions sub in Events module

Posted: Wed Sep 29, 2010 11:00 am
by dali
Yes, the conditions work as expected after an upgrade of Gambas.