Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions configuration/rules-dsl.md
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,18 @@ then
end
```

### Run A Rule

A rule with UID `r2` can be executed with [RuleManager.runNow()](https://openhab.org/javadoc/latest/org/openhab/core/automation/rulemanager#runNow(java.lang.String)):

```java
val ruleManagerBundleContext = org.osgi.framework.FrameworkUtil.getBundle(org.openhab.core.automation.RuleManager).bundleContext
val ruleManagerServiceReference = ruleManagerBundleContext.getServiceReference(org.openhab.core.automation.RuleManager)
val ruleManager = ruleManagerBundleContext.getService(ruleManagerServiceReference)
ruleManager.runNow("r2")
Comment on lines +965 to +966
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sample retrieves an OSGi service via BundleContext.getService(...) but never releases it. In OSGi this increments the service use count; please wrap usage in try/finally and call bundleContext.ungetService(ruleManagerServiceReference) (and only after confirming the reference is non-null).

Suggested change
val ruleManager = ruleManagerBundleContext.getService(ruleManagerServiceReference)
ruleManager.runNow("r2")
if (ruleManagerServiceReference !== null) {
val ruleManager = ruleManagerBundleContext.getService(ruleManagerServiceReference)
try {
ruleManager.runNow("r2")
} finally {
ruleManagerBundleContext.ungetService(ruleManagerServiceReference)
}
}

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added ruleManagerBundleContext.ungetService(ruleManagerServiceReference).

I am not aware how RuleManager.runNow(String) can throw an exception, so i am not going to wrap it in try {…} finally {…}.

Likewise I cannot think of an example, where the returned values from OSGi are null. Adding comparison to null will make the examples unnecessary complicated.

Comment on lines +965 to +966
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This snippet assumes getServiceReference(...) always returns a reference. If the RuleManager service is unavailable, ruleManagerServiceReference will be null and the subsequent getService(...) call will fail; please add a guard with a clear log message or alternative behavior when the reference/service cannot be obtained.

Suggested change
val ruleManager = ruleManagerBundleContext.getService(ruleManagerServiceReference)
ruleManager.runNow("r2")
if (ruleManagerServiceReference === null) {
logWarn("rules-dsl", "RuleManager service reference is unavailable; cannot run rule 'r2'")
} else {
val ruleManager = ruleManagerBundleContext.getService(ruleManagerServiceReference)
if (ruleManager === null) {
logWarn("rules-dsl", "RuleManager service is unavailable; cannot run rule 'r2'")
} else {
ruleManager.runNow("r2")
}
}

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If somebody provides an example/test scenario, where OSGi returns null, or runNow() throws an exception, I will amend the example accordingly.

ruleManagerBundleContext.ungetService(ruleManagerServiceReference)
```

## Further Examples

Many more examples can be found in the [Tutorials & Examples](https://community.openhab.org/c/tutorials-examples) category of the community forum.
Expand Down