use of org.eclipse.smarthome.automation.type.TriggerType 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.type.TriggerType in project smarthome by eclipse.
the class ModuleTypeRegistryImpl method getTriggers.
@Override
public Collection<TriggerType> getTriggers(String... tags) {
Collection<ModuleType> moduleTypes = getByTags(tags);
Collection<TriggerType> triggerTypes = new ArrayList<TriggerType>();
for (ModuleType mt : moduleTypes) {
if (mt instanceof TriggerType) {
triggerTypes.add((TriggerType) mt);
}
}
return triggerTypes;
}
use of org.eclipse.smarthome.automation.type.TriggerType in project smarthome by eclipse.
the class ModuleTypeRegistryImpl method getTriggers.
@Override
public Collection<TriggerType> getTriggers(Locale locale, String... tags) {
Collection<ModuleType> moduleTypes = getByTags(locale, tags);
Collection<TriggerType> triggerTypes = new ArrayList<TriggerType>();
for (ModuleType mt : moduleTypes) {
if (mt instanceof TriggerType) {
triggerTypes.add((TriggerType) mt);
}
}
return triggerTypes;
}
use of org.eclipse.smarthome.automation.type.TriggerType in project smarthome by eclipse.
the class ModuleTypeRegistryImpl method createCopy.
private ModuleType createCopy(ModuleType mType) {
if (mType == null) {
return null;
}
ModuleType result;
if (mType instanceof CompositeTriggerType) {
CompositeTriggerType m = (CompositeTriggerType) mType;
result = new CompositeTriggerType(mType.getUID(), mType.getConfigurationDescriptions(), mType.getLabel(), mType.getDescription(), mType.getTags(), mType.getVisibility(), m.getOutputs(), copyTriggers(m.getChildren()));
} else if (mType instanceof TriggerType) {
TriggerType m = (TriggerType) mType;
result = new TriggerType(mType.getUID(), mType.getConfigurationDescriptions(), mType.getLabel(), mType.getDescription(), mType.getTags(), mType.getVisibility(), m.getOutputs());
} else if (mType instanceof CompositeConditionType) {
CompositeConditionType m = (CompositeConditionType) mType;
result = new CompositeConditionType(mType.getUID(), mType.getConfigurationDescriptions(), mType.getLabel(), mType.getDescription(), mType.getTags(), mType.getVisibility(), m.getInputs(), copyConditions(m.getChildren()));
} else if (mType instanceof ConditionType) {
ConditionType m = (ConditionType) mType;
result = new ConditionType(mType.getUID(), mType.getConfigurationDescriptions(), mType.getLabel(), mType.getDescription(), mType.getTags(), mType.getVisibility(), m.getInputs());
} else if (mType instanceof CompositeActionType) {
CompositeActionType m = (CompositeActionType) mType;
result = new CompositeActionType(mType.getUID(), mType.getConfigurationDescriptions(), mType.getLabel(), mType.getDescription(), mType.getTags(), mType.getVisibility(), m.getInputs(), m.getOutputs(), copyActions(m.getChildren()));
} else if (mType instanceof ActionType) {
ActionType m = (ActionType) mType;
result = new ActionType(mType.getUID(), mType.getConfigurationDescriptions(), mType.getLabel(), mType.getDescription(), mType.getTags(), mType.getVisibility(), m.getInputs(), m.getOutputs());
} else {
throw new IllegalArgumentException("Invalid template type:" + mType);
}
return result;
}
use of org.eclipse.smarthome.automation.type.TriggerType in project smarthome by eclipse.
the class ConnectionValidator method checkConnection.
/**
* The method validates the connection between outputs of list of triggers to the action's or condition's input. It
* checks if the input is unconnected and compatibility of data types of the input and connected output. Throws
* exception if they are incompatible.
*
* @param connection that should be validated
* @param input that should be validated
* @param triggers is a list with triggers of the rule on which the action belongs
* @throws IllegalArgumentException when validation fails.
*/
private static void checkConnection(Connection connection, Input input, List<Trigger> triggers) {
Map<String, Trigger> triggersMap = new HashMap<String, Trigger>();
for (Trigger trigger : triggers) {
triggersMap.put(trigger.getId(), trigger);
}
String moduleId = connection.getOuputModuleId();
String msg = " Invalid Connection \"" + connection.getInputName() + "\" : ";
if (moduleId != null) {
Trigger trigger = triggersMap.get(moduleId);
if (trigger == null) {
throw new IllegalArgumentException(msg + " Trigger with ID \"" + moduleId + "\" does not exist!");
}
String triggerTypeUID = trigger.getTypeUID();
TriggerType triggerType = (TriggerType) mtRegistry.get(triggerTypeUID);
if (triggerType == null) {
throw new IllegalArgumentException(msg + " Trigger Type with UID \"" + triggerTypeUID + "\" does not exist!");
}
checkCompatibility(msg, connection, input, triggerType.getOutputs());
}
}
Aggregations