View Full Version : Logic Engine
Hi,
I am confused about Once and IF, I just want to know, does a program run on certain interval, or an action can trigger running a program, for example, when I turn on a bedroom it will trigger my function, procedure
Once = Once 'something' is on then do this. So it is only run once every time the state has changed.
If = If 'something' is on then do that. 'If' is checked / run every loop, from memory up to 4 times per second.
For more information read the logic.pdf - c:\clipsal\piced\maunuals
4.22.2 Once Statement
The format of the Once statement is :
once condition then
statement;
The Once statement is similar to the IF THEN statement, except that the statement is only
executed on the scan when the condition first becomes true. The condition needs to go false, then
true again for the statement to be executed again as shown in the diagram below :
Note that there can not be an "else" clause as there can with an if statement.
If the condition is true when the Logic Engine first runs, the statement will not be executed. The
condition needs to change from false to true in order for the statement to be executed.
Examples
If you want to switch on a lamp when the lounge light goes on, you could do :
if GetLightingState("Lounge") then
SetLightingState("Lamp", ON);
but the statement switching on the lamp would be executed every time the Module was run,
resulting in the lamp being switched on repeatedly while the Lounge light was on. This is obviously
not a good thing to do.
A better approach is to do :
once GetLightingState("Lounge") then
SetLightingState("Lamp", ON);
In this case, the statement switching on the lamp would be executed only when the Lounge light
was first switched on. Until the Lounge light is switched off, then on again, the lamp will not be
switched on again.
4.22.3 When to use if and once
The if and once statements do have different purposes, and are usually not interchangeable.
The once statement is used when you want an action when a condition first becomes true. The if
statement is used when you want an action every scan when the condition is true.
Each time the once statement is evaluated, the Logic Engine looks at the state of the condition. If
the condition is TRUE and it was FALSE the last time it was executed then it will execute the
statement. The consequences of this are that there are several situations where a once statement
may not do what you may expect :
· A once statement will never execute on the first scan, even if the condition is true (because the
previous state is unknown).
· A once statement should never be used inside a loop (repeat, while, for) - see below
· A once statement should never be used inside an if or once statement - see below
· A once statement should never be used inside a Function or Procedure
PSC, thank you for your replay. I am reading the manual, but being a computer programmer, the (Once) wasn't clear, in PC programming, most of the time the code runs when the user click a button, or doing something. for Logic engine, I couldn't understand wether the Once statement, look like as if you say when the user press the button on touch screen to turn the light on then do what I asked you to do. While (IF) is running continually on time interval.
Hope I am right!
Darren
29 May 07, 12:55 PM
in PC programming, most of the time the code runs when the user click a button, or doing something.
You are referring to event driven code. The logic engine is procedural code (which is the way all code was in the "good old days"). There is a section in the logic help file called "for users with programming skills" which briefly introduces the differences between the logic engine and other programming languages.
The logic runs every 200ms. When the logic first starts, it runs the Initialisation code. From then on, each time the logic is run (each scan), the code in each of the Modules is run in the order in which they appear in the Logic Tree.
The code in each Module is run from the top line of code to the bottom line of code each scan. The only exceptions to this are :
1. Disabled Modules : when a Module has been disabled, it does not get run until it has been enabled again. This does not affect the operation of other Modules.
2. When a Module is paused : When a module is paused due to a Delay Procedure or a WaitUntil Procedure, the module will stop at that point of the code until the delay is complete. When the delay is complete, the code will continue again. A delay in one Module does not affect any other Modules.
3. The ExitModule Procedure causes all of the rest of the code in the Module to be skipped. This does not affect any other Modules.
Thank you Darren for you explanation, its clear now for me.
vBulletin® v3.7.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.