Exit rule with return false

@rlkoshak @sjka do you know of a valid keyword or would it make sense to define an exception for the validator? The command return false seems to do what it is supposed to, without exception or warning.

I don’t have any practical experience with the new validator yet so can’t speak to whether this would still be allowed, but I believe break, contrary to what one would expect, actually exits the rule, not just the current context. But I learned that way back on 1.7, that behavior may be different now. And even then that was me trying to use a break in a lambda, not the main part of the rule.

I personally always structure my rules so there is only one exit (it’s the coding standard at my first job and it stuck) so I don’t really have much experience with return statements in rules.

The error implies to me that the rule is treated as a function with no return value. I would try return void to see if that makes the parsers and the code happier. Otherwise living with the warnings or restructuring the roles may be required.

How about simply writing

if (something) return

Iirc it should work just like in Java.

I know what you mean and agree that this is often a good idea. Let me give just one example I ran into the other day.

rule "Entrance door monitoring"
when
    Item Entrance_Door changed to OPEN
then
    if (Night_State.state == ON) {
        // 30 lines of wild code
    }
end

Alternative:

    if (Night_State.state != ON) break or return
    // 30 lines of wild code

Of course in reality this situation can become worse if multiple conditions or multiple steps are involved. I’m sure you can think of similar ones :wink: If the alternative is a better solution is pure personal preference I’d say.


@sjka

  • if (something) return false - Validator: “Void functions cannot return a value.”
  • if (something) return void - Validator: “Void functions cannot return a value.”
  • if (something) return - Validator: “Void functions cannot return a value.”
  • if (something) break - Validator is please but the rule will yield an error: “An error occurred during the script execution: The name ‘break’ cannot be resolved to an item or type.”
1 Like

works like a charm. No error in the designer, and no error when loading the file.

By the way: the validation has not changed at all with the OH 2.1 release. The only new thing is that files which are so broken that the parser cannot make sense of it fully will be ignored completely. Before, those parts that the parser read successfully (including any bogus it came up with) were used anyway.

1 Like

Hello,

works like a charm. No error in the designer, and no error when loading the file.

Yes, but there is an error during executing the rule. I tried this before. So this return without any parameter doesn´t work.

An error occured during the script execution: Unhandled parameter types: [null, org.eclipse.xtext.xbase.interpreter.im
pl.DefaultEvaluationContext@cffbf8, org.eclipse.xtext.util.CancelIndicator$1@9e19d9]

Urgh! Now I finally got the problem. Sorry, it took me a time to realize…
That’s indeed not nice! It looks like an ancient bug in the interpreter to me.
Could you please open an issue in https://github.com/eclipse/smarthome for it?

Done

1 Like

Thanks! In the meantime I had a look at the code and it on the first glance it looks like it would be pretty easy to fix. Let’s see how it goes…

2 Likes

I installed the last Snapshot and there are some changes.
Now the command
return
doesn´t cause an error during the rule execution any longer, but it also doesn´t stop the execution as it should.

Really? that’s not what I experienced (would need to test again) but that’s actually worse.

That’s what I also experienced and tried to explain with my last post.

@Sebastian_Zimmermann did you open an issue?

@Sebastian_Zimmermann did you open an issue?

Yes, did that 5 posts before…

I mean on GitHub… you know, where the actual change happens :smiley:

Yes, I did. I wrote the notification regarding the GitHub issue post in this forum topic.

Are we talking about the same thing? I’m looking for the issue ticket you’ve opened at https://github.com/eclipse/smarthome/issues -> could you please link it here? I want to stay informed about the progress.

https://github.com/eclipse/smarthome/issues/3831

Here we are…

Ah, already got closed, there’s the reason I didn’t find it :slight_smile:

Hmm.
Just installed the latest Snapshot some minutes ago.
The expression
return
doesn´t stop the execution of the code.
The issue is closed, but maybe the solution wasn´t tested?