Strange bug(?) in CheckConditions sub in Events module

Forum about Domotiga Open Source Home Automation for Linux.

Moderator: RDNZL

Post Reply
dali
Starting Member
Starting Member
Posts: 46
Joined: Mon Nov 09, 2009 10:02 am
Location: Habo, Sweden
Contact:

Strange bug(?) in CheckConditions sub in Events module

Post 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!
hhg
Starting Member
Starting Member
Posts: 20
Joined: Sun Jul 18, 2010 6:45 pm
Location: Denmark

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

Post 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
dali
Starting Member
Starting Member
Posts: 46
Joined: Mon Nov 09, 2009 10:02 am
Location: Habo, Sweden
Contact:

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

Post by dali »

I also thought that part looked a bit strange, but removing the second IF makes no difference...
hhg
Starting Member
Starting Member
Posts: 20
Joined: Sun Jul 18, 2010 6:45 pm
Location: Denmark

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

Post 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
User avatar
RDNZL
Forum Moderator
Forum Moderator
Posts: 1008
Joined: Sun Sep 24, 2006 1:45 pm
Location: Dordrecht, The Netherlands
Contact:

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

Post 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
Regards, Ron.
dali
Starting Member
Starting Member
Posts: 46
Joined: Mon Nov 09, 2009 10:02 am
Location: Habo, Sweden
Contact:

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

Post 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
User avatar
RDNZL
Forum Moderator
Forum Moderator
Posts: 1008
Joined: Sun Sep 24, 2006 1:45 pm
Location: Dordrecht, The Netherlands
Contact:

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

Post 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...
Regards, Ron.
dali
Starting Member
Starting Member
Posts: 46
Joined: Mon Nov 09, 2009 10:02 am
Location: Habo, Sweden
Contact:

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

Post 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
User avatar
RDNZL
Forum Moderator
Forum Moderator
Posts: 1008
Joined: Sun Sep 24, 2006 1:45 pm
Location: Dordrecht, The Netherlands
Contact:

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

Post 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
Regards, Ron.
hhg
Starting Member
Starting Member
Posts: 20
Joined: Sun Jul 18, 2010 6:45 pm
Location: Denmark

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

Post 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
User avatar
RDNZL
Forum Moderator
Forum Moderator
Posts: 1008
Joined: Sun Sep 24, 2006 1:45 pm
Location: Dordrecht, The Netherlands
Contact:

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

Post 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:
Regards, Ron.
j.hoekstra
Member
Member
Posts: 255
Joined: Thu Jun 25, 2009 2:09 pm
Location: Enschede, The Netherlands

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

Post by j.hoekstra »

Latest gambas evaluates it like this:

Code: Select all

? "AND" == "AND"
True
? "AND" == "and"
True
? "AND" = "AND"
True
? "AND" = "and"
False
dali
Starting Member
Starting Member
Posts: 46
Joined: Mon Nov 09, 2009 10:02 am
Location: Habo, Sweden
Contact:

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

Post by dali »

Yes, the conditions work as expected after an upgrade of Gambas.
Post Reply

Return to “DomotiGa Forum”