use of org.eclipse.smarthome.automation.Trigger in project smarthome by eclipse.
the class RuleEngineTest method createTriggers.
private List<Trigger> createTriggers(String type) {
List<Trigger> triggers = new ArrayList<Trigger>();
Configuration configurations = new Configuration();
configurations.put("a", "x");
configurations.put("b", "y");
configurations.put("c", "z");
triggers.add(new Trigger("triggerId", type, configurations));
return triggers;
}
use of org.eclipse.smarthome.automation.Trigger in project smarthome by eclipse.
the class RuleEngineTest method testRuleTriggers.
/**
* test rule triggers
*/
@Test
public void testRuleTriggers() {
RuleEngine ruleEngine = createRuleEngine();
Rule rule1 = createRule();
List<Trigger> triggers = rule1.getTriggers();
ruleEngine.addRule(rule1, true);
RuntimeRule rule1Get = ruleEngine.getRuntimeRule("rule1");
List<Trigger> triggersGet = rule1Get.getTriggers();
Assert.assertNotNull("Null triggers list", triggersGet);
Assert.assertEquals("Empty triggers list", 1, triggersGet.size());
Assert.assertEquals("Returned triggers list should not be a copy", triggersGet, rule1Get.getTriggers());
triggers.add(new Trigger("triggerId2", "typeUID2", null));
// ruleEngine.update will update the RuntimeRule.moduleMap with the new
ruleEngine.updateRule(rule1, true);
// module
RuntimeRule rule2Get = ruleEngine.getRuntimeRule("rule1");
List<Trigger> triggersGet2 = rule2Get.getTriggers();
Assert.assertNotNull("Null triggers list", triggersGet2);
Assert.assertEquals("Trigger was not added to the rule's list of triggers", 2, triggersGet2.size());
Assert.assertEquals("Returned triggers list should not be a copy", triggersGet2, rule2Get.getTriggers());
Assert.assertNotNull("Rule trigger with wrong id is returned: " + triggersGet2, rule2Get.getModule("triggerId2"));
}
use of org.eclipse.smarthome.automation.Trigger in project smarthome by eclipse.
the class RuleEngine method setTriggerOutputs.
/**
* The method updates {@link Output} of the {@link Trigger} with a new triggered data.
*
* @param td new Triggered data.
*/
private void setTriggerOutputs(String ruleUID, TriggerData td) {
Trigger t = td.getTrigger();
updateContext(ruleUID, t.getId(), td.getOutputs());
}
use of org.eclipse.smarthome.automation.Trigger in project smarthome by eclipse.
the class RuleEngine method autoMapConnections.
/**
* The auto mapping tries to link not connected module inputs to output of other modules. The auto mapping will link
* input to output only when following criteria are done: 1) input must not be connected. The auto mapping will not
* overwrite explicit connections done by the user. 2) input tags must be subset of the output tags. 3) condition
* inputs can be connected only to triggers' outputs 4) action outputs can be connected to both conditions and
* actions
* outputs 5) There is only one output, based on previous criteria, where the input can connect to. If more then one
* candidate outputs exists for connection, this is a conflict and the auto mapping leaves the input unconnected.
* Auto
* mapping is always applied when the rule is added or updated. It changes initial value of inputs of conditions and
* actions participating in the rule. If an "auto map" connection has to be removed, the tags of corresponding
* input/output have to be changed.
*
* @param r updated rule
*/
private void autoMapConnections(RuntimeRule r) {
Map<Set<String>, OutputRef> triggerOutputTags = new HashMap<Set<String>, OutputRef>(11);
for (Trigger t : r.getTriggers()) {
TriggerType tt = (TriggerType) mtRegistry.get(t.getTypeUID());
if (tt != null) {
initTagsMap(t.getId(), tt.getOutputs(), triggerOutputTags);
}
}
Map<Set<String>, OutputRef> actionOutputTags = new HashMap<Set<String>, OutputRef>(11);
for (Action a : r.getActions()) {
ActionType at = (ActionType) mtRegistry.get(a.getTypeUID());
if (at != null) {
initTagsMap(a.getId(), at.getOutputs(), actionOutputTags);
}
}
// auto mapping of conditions
if (!triggerOutputTags.isEmpty()) {
for (Condition c : r.getConditions()) {
boolean isConnectionChanged = false;
ConditionType ct = (ConditionType) mtRegistry.get(c.getTypeUID());
if (ct != null) {
Set<Connection> connections = ((RuntimeCondition) c).getConnections();
for (Input input : ct.getInputs()) {
if (isConnected(input, connections)) {
// the input is already connected. Skip it.
continue;
}
if (addAutoMapConnections(input, triggerOutputTags, connections)) {
isConnectionChanged = true;
}
}
if (isConnectionChanged) {
// update condition inputs
connections = ((RuntimeCondition) c).getConnections();
Map<String, String> connectionMap = getConnectionMap(connections);
c.setInputs(connectionMap);
}
}
}
}
// auto mapping of actions
if (!triggerOutputTags.isEmpty() || !actionOutputTags.isEmpty()) {
for (Action a : r.getActions()) {
boolean isConnectionChanged = false;
ActionType at = (ActionType) mtRegistry.get(a.getTypeUID());
if (at != null) {
Set<Connection> connections = ((RuntimeAction) a).getConnections();
for (Input input : at.getInputs()) {
if (isConnected(input, connections)) {
// the input is already connected. Skip it.
continue;
}
if (addAutoMapConnections(input, triggerOutputTags, connections)) {
isConnectionChanged = true;
}
if (addAutoMapConnections(input, actionOutputTags, connections)) {
isConnectionChanged = true;
}
}
if (isConnectionChanged) {
// update condition inputs
connections = ((RuntimeAction) a).getConnections();
Map<String, String> connectionMap = getConnectionMap(connections);
a.setInputs(connectionMap);
}
}
}
}
}
use of org.eclipse.smarthome.automation.Trigger in project smarthome by eclipse.
the class RuleEngine method register.
/**
* This method register the Rule to start working. This is the final step of initialization process where triggers
* received {@link RuleEngineCallback}s object and starts to notify the rule engine when they are triggered. After
* activating all triggers the rule goes into IDLE state
*
* @param rule an initialized rule which has to starts tracking the triggers.
*/
private void register(RuntimeRule rule) {
RuleEngineCallback reCallback = getRuleEngineCallback(rule);
for (Iterator<Trigger> it = rule.getTriggers().iterator(); it.hasNext(); ) {
RuntimeTrigger t = (RuntimeTrigger) it.next();
TriggerHandler triggerHandler = t.getModuleHandler();
triggerHandler.setRuleEngineCallback(reCallback);
}
}
Aggregations