add eventdescriptor and paramdescriptor classes to be used with Rules

This commit is contained in:
Michael Zanetti 2014-04-25 00:45:31 +02:00
parent 932e813217
commit d4053297c8
16 changed files with 406 additions and 106 deletions

View File

@ -412,7 +412,6 @@ void DeviceManager::loadConfiguredDevices()
settings.beginGroup(paramNameString);
Param param(paramNameString.remove(QRegExp("Param-")));
param.setValue(settings.value("value"));
param.setOperand((Param::OperandType)settings.value("operand").toInt());
params.append(param);
settings.endGroup();
}
@ -439,7 +438,6 @@ void DeviceManager::storeConfiguredDevices()
foreach (const Param &param, device->params()) {
settings.beginGroup("Param-" + param.name());
settings.setValue("value", param.value());
settings.setValue("operand", param.operand());
settings.endGroup();
}
settings.endGroup();

View File

@ -19,9 +19,11 @@ SOURCES += plugin/device.cpp \
types/statetype.cpp \
types/eventtype.cpp \
types/event.cpp \
types/eventdescriptor.cpp \
types/vendor.cpp \
types/paramtype.cpp \
types/param.cpp
types/param.cpp \
types/paramdescriptor.cpp \
HEADERS += plugin/device.h \
plugin/deviceclass.h \
@ -36,8 +38,10 @@ HEADERS += plugin/device.h \
types/statetype.h \
types/eventtype.h \
types/event.h \
types/eventdescriptor.h \
types/vendor.h \
types/typeutils.h \
types/paramtype.h \
types/param.h
types/param.h \
types/paramdescriptor.h

View File

@ -0,0 +1,152 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* This file is part of guh. *
* *
* Guh is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, version 2 of the License. *
* *
* Guh is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with guh. If not, see <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*!
\class EventDescriptor
\brief Describes a certain \l{Event}.
\ingroup types
\inmodule libguh
An EventDescriptor describes an \l{Event} in order to match it with a \l{Rule}.
\sa Event, Rule
*/
#include "eventdescriptor.h"
/*! Constructs an EventDescriptor describing an Event.
*/
EventDescriptor::EventDescriptor(const EventTypeId &eventTypeId, const DeviceId &deviceId, const QList<ParamDescriptor> &paramDescriptors):
m_eventTypeId(eventTypeId),
m_deviceId(deviceId),
m_paramDescriptors(paramDescriptors)
{
}
/*! Returns the id of the \l{EventType} which describes this Event.*/
EventTypeId EventDescriptor::eventTypeId() const
{
return m_eventTypeId;
}
/*! Returns the id of the \l{Device} associated with this Event.*/
DeviceId EventDescriptor::deviceId() const
{
return m_deviceId;
}
/*! Returns the parameters of this Event.*/
QList<ParamDescriptor> EventDescriptor::paramDescriptors() const
{
return m_paramDescriptors;
}
/*! Set the parameters of this Event to \a params.*/
void EventDescriptor::setParamDescriptors(const QList<ParamDescriptor> &paramDescriptors)
{
m_paramDescriptors = paramDescriptors;
}
ParamDescriptor EventDescriptor::paramDescriptor(const QString &paramDescriptorName) const
{
foreach (const ParamDescriptor &paramDescriptor, m_paramDescriptors) {
if (paramDescriptor.name() == paramDescriptorName) {
return paramDescriptor;
}
}
return ParamDescriptor(QString());
}
/*! Compare this Event to the Event given by \a other.
Events are equal (returns true) if eventTypeId, deviceId and params match. */
bool EventDescriptor::operator ==(const EventDescriptor &other) const
{
bool paramsMatch = true;
foreach (const ParamDescriptor &otherParamDescriptor, other.paramDescriptors()) {
ParamDescriptor paramDescriptor = this->paramDescriptor(otherParamDescriptor.name());
if (!paramDescriptor.isValid() || paramDescriptor.value() != otherParamDescriptor.value()) {
paramsMatch = false;
break;
}
}
return m_eventTypeId == other.eventTypeId()
&& m_deviceId == other.deviceId()
&& paramsMatch;
}
bool EventDescriptor::operator ==(const Event &event) const
{
if (m_eventTypeId != event.eventTypeId() || m_deviceId != event.deviceId()) {
return false;
}
foreach (const ParamDescriptor &paramDescriptor, m_paramDescriptors) {
switch (paramDescriptor.operand()) {
case ParamDescriptor::OperandTypeEquals:
if (event.param(paramDescriptor.name()).value() != paramDescriptor.value()) {
return false;
}
break;
case ParamDescriptor::OperandTypeNotEquals:
if (event.param(paramDescriptor.name()).value() == paramDescriptor.value()) {
return false;
}
break;
case ParamDescriptor::OperandTypeGreater:
if (event.param(paramDescriptor.name()).value() <= paramDescriptor.value()) {
return false;
}
break;
case ParamDescriptor::OperandTypeGreaterOrEqual:
if (event.param(paramDescriptor.name()).value() < paramDescriptor.value()) {
return false;
}
break;
case ParamDescriptor::OperandTypeLess:
if (event.param(paramDescriptor.name()).value() >= paramDescriptor.value()) {
return false;
}
break;
case ParamDescriptor::OperandTypeLessOrEqual:
if (event.param(paramDescriptor.name()).value() < paramDescriptor.value()) {
return false;
}
break;
}
}
return true;
}
QDebug operator<<(QDebug dbg, const EventDescriptor &eventDescriptor)
{
dbg.nospace() << "EventDescriptor(EventTypeId: " << eventDescriptor.eventTypeId().toString() << ", DeviceId" << eventDescriptor.deviceId() << ")";
return dbg.space();
}
QDebug operator<<(QDebug dbg, const QList<EventDescriptor> &eventDescriptors)
{
dbg.nospace() << "EventDescriptorList (count:" << eventDescriptors.count() << ")";
for (int i = 0; i < eventDescriptors.count(); i++ ) {
dbg.nospace() << " " << i << ": " << eventDescriptors.at(i);
}
return dbg.space();
}

View File

@ -0,0 +1,54 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* This file is part of guh. *
* *
* Guh is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, version 2 of the License. *
* *
* Guh is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with guh. If not, see <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef EVENTDESCRIPTOR_H
#define EVENTDESCRIPTOR_H
#include "typeutils.h"
#include "paramdescriptor.h"
#include "event.h"
#include <QString>
#include <QVariantList>
#include <QDebug>
class EventDescriptor
{
public:
EventDescriptor(const EventTypeId &eventTypeId, const DeviceId &deviceId, const QList<ParamDescriptor> &paramDescriptors = QList<ParamDescriptor>());
EventTypeId eventTypeId() const;
DeviceId deviceId() const;
QList<ParamDescriptor> paramDescriptors() const;
void setParamDescriptors(const QList<ParamDescriptor> &paramDescriptors);
ParamDescriptor paramDescriptor(const QString &paramDescriptorName) const;
bool operator ==(const EventDescriptor &other) const;
bool operator ==(const Event &event) const;
private:
EventTypeId m_eventTypeId;
DeviceId m_deviceId;
QList<ParamDescriptor> m_paramDescriptors;
};
QDebug operator<<(QDebug dbg, const EventDescriptor &eventDescriptor);
QDebug operator<<(QDebug dbg, const QList<EventDescriptor> &eventDescriptors);
#endif // EVENTDESCRIPTOR_H

View File

@ -22,8 +22,7 @@
Param::Param(const QString &name, const QVariant &value):
m_name (name),
m_value(value),
m_operand(OperandTypeEquals)
m_value(value)
{
}
@ -47,16 +46,6 @@ void Param::setValue(const QVariant &value)
m_value = value;
}
Param::OperandType Param::operand() const
{
return m_operand;
}
void Param::setOperand(Param::OperandType operand)
{
m_operand = operand;
}
bool Param::isValid() const
{
return !m_name.isEmpty() && m_value.isValid();

View File

@ -25,15 +25,6 @@
class Param
{
public:
enum OperandType {
OperandTypeEquals,
OperandTypeNotEquals,
OperandTypeLess,
OperandTypeGreater,
OperandTypeLessThan,
OperandTypeGreaterThan
};
Param(const QString &name, const QVariant &value = QVariant());
QString name() const;
@ -42,15 +33,11 @@ public:
QVariant value() const;
void setValue(const QVariant &value);
OperandType operand() const;
void setOperand(OperandType operand);
bool isValid() const;
private:
QString m_name;
QVariant m_value;
OperandType m_operand;
};
QDebug operator<<(QDebug dbg, const Param &param);

View File

@ -0,0 +1,18 @@
#include "paramdescriptor.h"
ParamDescriptor::ParamDescriptor(const QString &name, const QVariant &value):
Param(name, value),
m_operand(OperandTypeEquals)
{
}
ParamDescriptor::OperandType ParamDescriptor::operand() const
{
return m_operand;
}
void ParamDescriptor::setOperand(ParamDescriptor::OperandType operand)
{
m_operand = operand;
}

View File

@ -0,0 +1,26 @@
#ifndef PARAMDESCRIPTOR_H
#define PARAMDESCRIPTOR_H
#include "param.h"
class ParamDescriptor : public Param
{
public:
enum OperandType {
OperandTypeEquals,
OperandTypeNotEquals,
OperandTypeLess,
OperandTypeGreater,
OperandTypeLessOrEqual,
OperandTypeGreaterOrEqual
};
ParamDescriptor(const QString &name, const QVariant &value = QVariant());
OperandType operand() const;
void setOperand(OperandType operand);
private:
OperandType m_operand;
};
#endif // PARAMDESCRIPTOR_H

View File

@ -35,10 +35,12 @@ QVariantList JsonTypes::s_operandTypes;
QVariantMap JsonTypes::s_paramType;
QVariantMap JsonTypes::s_param;
QVariantMap JsonTypes::s_paramDescriptor;
QVariantMap JsonTypes::s_stateType;
QVariantMap JsonTypes::s_state;
QVariantMap JsonTypes::s_eventType;
QVariantMap JsonTypes::s_event;
QVariantMap JsonTypes::s_eventDescriptor;
QVariantMap JsonTypes::s_actionType;
QVariantMap JsonTypes::s_action;
QVariantMap JsonTypes::s_plugin;
@ -67,7 +69,11 @@ void JsonTypes::init()
// Param
s_param.insert("name", "string");
s_param.insert("value", basicTypesRef());
s_param.insert("operand", operandTypesRef());
// ParamDescriptor
s_paramDescriptor.insert("name", "string");
s_paramDescriptor.insert("value", basicTypesRef());
s_paramDescriptor.insert("operand", operandTypesRef());
// StateType
s_stateType.insert("id", "uuid");
@ -90,6 +96,11 @@ void JsonTypes::init()
s_event.insert("deviceId", "uuid");
s_event.insert("o:params", QVariantList() << paramRef());
// EventDescriptor
s_eventDescriptor.insert("eventTypeId", "uuid");
s_eventDescriptor.insert("deviceId", "uuid");
s_eventDescriptor.insert("o:paramDescriptors", QVariantList() << paramDescriptorRef());
// ActionType
s_actionType.insert("id", "uuid");
s_actionType.insert("name", "string");
@ -130,9 +141,10 @@ void JsonTypes::init()
s_deviceDescriptor.insert("title", "string");
s_deviceDescriptor.insert("description", "string");
// Rule
s_rule.insert("id", "uuid");
s_rule.insert("ruleType", ruleTypesRef());
s_rule.insert("events", QVariantList() << eventRef());
s_rule.insert("eventDescriptors", QVariantList() << eventDescriptorRef());
s_rule.insert("actions", QVariantList() << actionRef());
s_rule.insert("states", QVariantList() << stateRef());
@ -153,14 +165,16 @@ QVariantMap JsonTypes::allTypes()
allTypes.insert("SetupMethodType", setupMethodTypes());
allTypes.insert("OperandType", operandTypes());
allTypes.insert("StateType", stateTypeDescription());
allTypes.insert("Event", eventDescription());
allTypes.insert("EventType", eventTypeDescription());
allTypes.insert("EventDescriptor", eventDescriptorDescription());
allTypes.insert("ActionType", actionTypeDescription());
allTypes.insert("Vendor", vendorDescription());
allTypes.insert("DeviceClass", deviceClassDescription());
allTypes.insert("Plugin", pluginDescription());
allTypes.insert("Param", paramDescription());
allTypes.insert("ParamDescriptor", paramDescriptorDescription());
allTypes.insert("State", stateDescription());
allTypes.insert("Event", eventDescription());
allTypes.insert("Device", deviceDescription());
allTypes.insert("DeviceDescriptor", deviceDescriptorDescription());
allTypes.insert("Action", actionDescription());
@ -191,6 +205,19 @@ QVariantMap JsonTypes::packEvent(const Event &event)
return variant;
}
QVariantMap JsonTypes::packEventDescriptor(const EventDescriptor &eventDescriptor)
{
QVariantMap variant;
variant.insert("eventTypeId", eventDescriptor.eventTypeId());
variant.insert("deviceId", eventDescriptor.deviceId());
QVariantList params;
foreach (const ParamDescriptor &paramDescriptor, eventDescriptor.paramDescriptors()) {
params.append(packParamDescriptor(paramDescriptor));
}
variant.insert("params", params);
return variant;
}
QVariantMap JsonTypes::packActionType(const ActionType &actionType)
{
QVariantMap variantMap;
@ -226,9 +253,16 @@ QVariantMap JsonTypes::packStateType(const StateType &stateType)
QVariantMap JsonTypes::packParam(const Param &param)
{
QVariantMap variantMap;
variantMap.insert("name", param.name());
variantMap.insert("value", param.value());
variantMap.insert("operand", s_operandTypes.at(param.operand()));
variantMap.insert(param.name(), param.value());
return variantMap;
}
QVariantMap JsonTypes::packParamDescriptor(const ParamDescriptor &paramDescriptor)
{
QVariantMap variantMap;
variantMap.insert("name", paramDescriptor.name());
variantMap.insert("value", paramDescriptor.value());
variantMap.insert("operand", s_operandTypes.at(paramDescriptor.operand()));
return variantMap;
}
@ -322,11 +356,11 @@ QVariantMap JsonTypes::packRule(const Rule &rule)
{
QVariantMap ruleMap;
ruleMap.insert("id", rule.id());
QVariantList eventList;
foreach (const Event &event, rule.events()) {
eventList.append(JsonTypes::packEvent(event));
QVariantList eventDescriptorList;
foreach (const EventDescriptor &eventDescriptor, rule.eventDescriptors()) {
eventDescriptorList.append(JsonTypes::packEventDescriptor(eventDescriptor));
}
ruleMap.insert("events", eventList);
ruleMap.insert("eventDescriptors", eventDescriptorList);
ruleMap.insert("ruleType", s_ruleTypes.at(rule.ruleType()));
@ -343,22 +377,7 @@ QVariantMap JsonTypes::packRule(const Rule &rule)
Param JsonTypes::unpackParam(const QVariantMap &paramMap)
{
Param param(paramMap.value("name").toString(), paramMap.value("value"));
QString operandString = paramMap.value("operand").toString();
if (operandString == "OperandTypeEquals") {
param.setOperand(Param::OperandTypeEquals);
} else if (operandString == "OperandTypeNotEquals") {
param.setOperand(Param::OperandTypeNotEquals);
} else if (operandString == "OperandTypeLess") {
param.setOperand(Param::OperandTypeLess);
} else if (operandString == "OperandTypeGreater") {
param.setOperand(Param::OperandTypeGreater);
} else if (operandString == "OperandTypeLessThan") {
param.setOperand(Param::OperandTypeLessThan);
} else if (operandString == "OperandTypeGreaterThan") {
param.setOperand(Param::OperandTypeGreaterThan);
}
return param;
return Param(paramMap.value("name").toString(), paramMap.value("value"));
}
QList<Param> JsonTypes::unpackParams(const QVariantList &paramList)
@ -370,6 +389,35 @@ QList<Param> JsonTypes::unpackParams(const QVariantList &paramList)
return params;
}
ParamDescriptor JsonTypes::unpackParamDescriptor(const QVariantMap &paramMap)
{
ParamDescriptor param(paramMap.value("name").toString(), paramMap.value("value"));
QString operandString = paramMap.value("operand").toString();
if (operandString == "OperandTypeEquals") {
param.setOperand(ParamDescriptor::OperandTypeEquals);
} else if (operandString == "OperandTypeNotEquals") {
param.setOperand(ParamDescriptor::OperandTypeNotEquals);
} else if (operandString == "OperandTypeLess") {
param.setOperand(ParamDescriptor::OperandTypeLess);
} else if (operandString == "OperandTypeGreater") {
param.setOperand(ParamDescriptor::OperandTypeGreater);
} else if (operandString == "OperandTypeLessOrEqual") {
param.setOperand(ParamDescriptor::OperandTypeLessOrEqual);
} else if (operandString == "OperandTypeGreaterOrEqual") {
param.setOperand(ParamDescriptor::OperandTypeGreaterOrEqual);
}
return param;
}
QList<ParamDescriptor> JsonTypes::unpackParamDescriptors(const QVariantList &paramList)
{
QList<ParamDescriptor> params;
foreach (const QVariant &paramVariant, paramList) {
params.append(unpackParamDescriptor(paramVariant.toMap()));
}
return params;
}
QPair<bool, QString> JsonTypes::validateMap(const QVariantMap &templateMap, const QVariantMap &map)
{
s_lastError.clear();
@ -471,10 +519,8 @@ QPair<bool, QString> JsonTypes::validateVariant(const QVariant &templateVariant,
return result;
}
} else if (refName == paramRef()) {
QPair<bool, QString> result = validateMap(paramDescription(), variant.toMap());
if (!result.first) {
qDebug() << "Param not valid";
return result;
if (!variant.canConvert(QVariant::Map)) {
report(false, "Param not valid. Should be a map.");
}
} else if (refName == deviceRef()) {
QPair<bool, QString> result = validateMap(deviceDescription(), variant.toMap());

View File

@ -27,6 +27,7 @@
#include "types/action.h"
#include "types/actiontype.h"
#include "types/paramtype.h"
#include "types/paramdescriptor.h"
#include <QObject>
@ -70,10 +71,12 @@ public:
DECLARE_TYPE(operandTypes, "OperandType")
DECLARE_OBJECT(paramType, "ParamType")
DECLARE_OBJECT(param, "Param")
DECLARE_OBJECT(paramDescriptor, "ParamDescriptor")
DECLARE_OBJECT(stateType, "StateType")
DECLARE_OBJECT(state, "State")
DECLARE_OBJECT(eventType, "EventType")
DECLARE_OBJECT(event, "Event")
DECLARE_OBJECT(eventDescriptor, "EventDescriptor")
DECLARE_OBJECT(actionType, "ActionType")
DECLARE_OBJECT(action, "Action")
DECLARE_OBJECT(plugin, "Plugin")
@ -85,11 +88,13 @@ public:
static QVariantMap packEventType(const EventType &eventType);
static QVariantMap packEvent(const Event &event);
static QVariantMap packEventDescriptor(const EventDescriptor &event);
static QVariantMap packActionType(const ActionType &actionType);
static QVariantMap packAction(const Action &action);
static QVariantMap packStateType(const StateType &stateType);
static QVariantMap packParam(const Param &param);
static QVariantMap packParamType(const ParamType &paramType);
static QVariantMap packParamDescriptor(const ParamDescriptor &paramDescriptor);
static QVariantMap packVendor(const Vendor &vendor);
static QVariantMap packDeviceClass(const DeviceClass &deviceClass);
static QVariantMap packPlugin(DevicePlugin *plugin);
@ -99,6 +104,8 @@ public:
static Param unpackParam(const QVariantMap &paramMap);
static QList<Param> unpackParams(const QVariantList &paramList);
static ParamDescriptor unpackParamDescriptor(const QVariantMap &paramDescriptorMap);
static QList<ParamDescriptor> unpackParamDescriptors(const QVariantList &paramDescriptorList);
static QPair<bool, QString> validateMap(const QVariantMap &templateMap, const QVariantMap &map);
static QPair<bool, QString> validateProperty(const QVariant &templateValue, const QVariant &value);

View File

@ -39,7 +39,7 @@ RulesHandler::RulesHandler(QObject *parent) :
params.clear(); returns.clear();
setDescription("AddRule", "Add a rule");
params.insert("event", JsonTypes::eventRef());
params.insert("eventDescriptor", JsonTypes::eventDescriptorRef());
QVariantList actions;
actions.append(JsonTypes::actionRef());
params.insert("actions", actions);
@ -82,8 +82,8 @@ JsonReply* RulesHandler::AddRule(const QVariantMap &params)
EventTypeId eventTypeId(eventMap.value("eventTypeId").toString());
DeviceId eventDeviceId(eventMap.value("deviceId").toString());
QList<Param> eventParams = JsonTypes::unpackParams(eventMap.value("params").toList());
Event event(eventTypeId, eventDeviceId, eventParams);
QList<ParamDescriptor> eventParams = JsonTypes::unpackParamDescriptors(eventMap.value("paramDescriptors").toList());
EventDescriptor eventDescriptor(eventTypeId, eventDeviceId, eventParams);
QList<Action> actions;
QVariantList actionList = params.value("actions").toList();
@ -102,7 +102,7 @@ JsonReply* RulesHandler::AddRule(const QVariantMap &params)
return createReply(returns);
}
switch(GuhCore::instance()->ruleEngine()->addRule(event, actions)) {
switch(GuhCore::instance()->ruleEngine()->addRule(eventDescriptor, actions)) {
case RuleEngine::RuleErrorNoError:
returns.insert("success", true);
returns.insert("errorMessage", "");

View File

@ -23,13 +23,13 @@
\ingroup rules
\inmodule server
A Rule is always evented by an \l{Event}, has \l{State}{States}
A Rule is always triggered by an \l{EventDescriptor}, has \l{State}{States}
to be compared and \l{Action}{Actions} to be executed. Additionally a
Rule is either of type \l{Rule::RuleTypeAll} or \l{Rule::RuleTypeAny}
which determines if all or any of the \l{State}{States} must be matching
in order for the \l{Action}{Actions} to be executed.
\sa Event, State, Action
\sa EventDescriptor, State, Action
*/
/*! \enum Rule::RuleType
@ -48,11 +48,11 @@
#include <QDebug>
/*! Constructs a Rule with the given \a id, \a event, \a states and \a actions. The ruleType will default to
/*! Constructs a Rule with the given \a id, \a eventDescriptor, \a states and \a actions. The ruleType will default to
\l{Rule::RuleTypeAll}.*/
Rule::Rule(const QUuid &id, const Event &event, const QList<State> &states, const QList<Action> &actions):
Rule::Rule(const QUuid &id, const EventDescriptor &eventDescriptor, const QList<State> &states, const QList<Action> &actions):
m_id(id),
m_events(QList<Event>() << event),
m_eventDescriptors(QList<EventDescriptor>() << eventDescriptor),
m_states(states),
m_actions(actions),
m_ruleType(RuleTypeAll)
@ -65,10 +65,10 @@ QUuid Rule::id() const
return m_id;
}
/*! Returns the \l{Event} that events this Rule.*/
QList<Event> Rule::events() const
/*! Returns the \l{EventDescriptor} for this Rule.*/
QList<EventDescriptor> Rule::eventDescriptors() const
{
return m_events;
return m_eventDescriptors;
}
/*! Returns the \l{State}{States} that need to be matching in order for this to Rule apply. */
@ -77,7 +77,7 @@ QList<State> Rule::states() const
return m_states;
}
/*! Returns the \l{Action}{Actions} to be executed when this Rule is eventd and states match. */
/*! Returns the \l{Action}{Actions} to be executed when this Rule is matched and states match. */
QList<Action> Rule::actions() const
{
return m_actions;

View File

@ -21,7 +21,7 @@
#include "types/state.h"
#include "types/action.h"
#include "types/event.h"
#include "types/eventdescriptor.h"
#include "stateevaluator.h"
#include <QUuid>
@ -34,10 +34,10 @@ public:
RuleTypeAny
};
Rule(const QUuid &id, const Event &event, const QList<State> &states, const QList<Action> &actions);
Rule(const QUuid &id, const EventDescriptor &eventDescriptor, const QList<State> &states, const QList<Action> &actions);
QUuid id() const;
QList<Event> events() const;
QList<EventDescriptor> eventDescriptors() const;
QList<State> states() const;
QList<Action> actions() const;
@ -46,7 +46,7 @@ public:
private:
QUuid m_id;
QList<Event> m_events;
QList<EventDescriptor> m_eventDescriptors;
QList<State> m_states;
StateEvaluator stateEvaluator;
QList<Action> m_actions;

View File

@ -25,9 +25,9 @@
\inmodule server
You can add, remove and update rules and query the engine for actions to be executed
for a given \l{Event}.
for a given \l{Event} described by an \l{EventDescriptor}.
\sa Event, Rule, Action
\sa Event, EventDescriptor, Rule, Action
*/
/*! \fn void RuleEngine::ruleAdded(const QUuid &ruleId)
@ -51,6 +51,8 @@
*/
#include "ruleengine.h"
#include "types/paramdescriptor.h"
#include "types/eventdescriptor.h"
#include "guhcore.h"
@ -80,17 +82,17 @@ RuleEngine::RuleEngine(QObject *parent) :
settings.beginGroup("event");
EventTypeId eventTypeId(settings.value("eventTypeId").toString());
DeviceId deviceId(settings.value("deviceId").toString());
QList<Param> params;
QList<ParamDescriptor> params;
foreach (QString groupName, settings.childGroups()) {
if (groupName.startsWith("Param-")) {
if (groupName.startsWith("ParamDescriptor-")) {
settings.beginGroup(groupName);
Param param(groupName.remove(QRegExp("^Param-")), settings.value("value"));
param.setOperand((Param::OperandType)settings.value("operand").toInt());
params.append(param);
ParamDescriptor paramDescriptor(groupName.remove(QRegExp("^ParamDescriptor-")), settings.value("value"));
paramDescriptor.setOperand((ParamDescriptor::OperandType)settings.value("operand").toInt());
params.append(paramDescriptor);
settings.endGroup();
}
}
Event event(eventTypeId, deviceId, params);
EventDescriptor eventDescriptor(eventTypeId, deviceId, params);
settings.endGroup();
settings.beginGroup("states");
@ -114,7 +116,6 @@ RuleEngine::RuleEngine(QObject *parent) :
if (paramNameString.startsWith("Param-")) {
settings.beginGroup(paramNameString);
Param param(paramNameString.remove(QRegExp("^Param-")), settings.value("value"));
param.setOperand((Param::OperandType)settings.value("operand").toInt());
params.append(param);
settings.endGroup();
}
@ -128,7 +129,7 @@ RuleEngine::RuleEngine(QObject *parent) :
settings.endGroup();
Rule rule = Rule(QUuid(idString), event, states, actions);
Rule rule = Rule(QUuid(idString), eventDescriptor, states, actions);
m_rules.append(rule);
}
@ -143,8 +144,8 @@ QList<Action> RuleEngine::evaluateEvent(const Event &event)
qDebug() << "got event:" << event;
QList<Action> actions;
for (int i = 0; i < m_rules.count(); ++i) {
qDebug() << "evaluating rule" << i << m_rules.at(i).events();
if (m_rules.at(i).events().contains(event)) {
qDebug() << "evaluating rule" << i << m_rules.at(i).eventDescriptors();
if (containsEvent(m_rules.at(i), event)) {
bool statesMatching = true;
qDebug() << "checking states:" << m_rules.at(i).states();
foreach (const State &state, m_rules.at(i).states()) {
@ -172,17 +173,17 @@ QList<Action> RuleEngine::evaluateEvent(const Event &event)
/*! Add a new \l{Rule} with the given \a event and \a actions to the engine.
For convenience, this creates a Rule without any \l{State} comparison. */
RuleEngine::RuleError RuleEngine::addRule(const Event &event, const QList<Action> &actions)
RuleEngine::RuleError RuleEngine::addRule(const EventDescriptor &eventDescriptor, const QList<Action> &actions)
{
return addRule(event, QList<State>(), actions);
return addRule(eventDescriptor, QList<State>(), actions);
}
/*! Add a new \l{Rule} with the given \a event, \a states and \a actions to the engine. */
RuleEngine::RuleError RuleEngine::addRule(const Event &event, const QList<State> &states, const QList<Action> &actions)
RuleEngine::RuleError RuleEngine::addRule(const EventDescriptor &eventDescriptor, const QList<State> &states, const QList<Action> &actions)
{
Device *device = GuhCore::instance()->deviceManager()->findConfiguredDevice(event.deviceId());
Device *device = GuhCore::instance()->deviceManager()->findConfiguredDevice(eventDescriptor.deviceId());
if (!device) {
qWarning() << "Cannot create rule. No configured device for eventTypeId" << event.eventTypeId();
qWarning() << "Cannot create rule. No configured device for eventTypeId" << eventDescriptor.eventTypeId();
return RuleErrorDeviceNotFound;
}
DeviceClass deviceClass = GuhCore::instance()->deviceManager()->findDeviceClass(device->deviceClassId());
@ -190,28 +191,28 @@ RuleEngine::RuleError RuleEngine::addRule(const Event &event, const QList<State>
bool eventTypeFound = false;
foreach (const EventType &eventType, deviceClass.events()) {
if (eventType.id() == event.eventTypeId()) {
if (eventType.id() == eventDescriptor.eventTypeId()) {
eventTypeFound = true;
}
}
if (!eventTypeFound) {
qWarning() << "Cannot create rule. Device " + device->name() + " has no event type:" << event.eventTypeId();
qWarning() << "Cannot create rule. Device " + device->name() + " has no event type:" << eventDescriptor.eventTypeId();
return RuleErrorEventTypeNotFound;
}
Rule rule = Rule(QUuid::createUuid(), event, states, actions);
Rule rule = Rule(QUuid::createUuid(), eventDescriptor, states, actions);
m_rules.append(rule);
emit ruleAdded(rule.id());
QSettings settings(m_settingsFile);
settings.beginGroup(rule.id().toString());
settings.beginGroup("event");
settings.setValue("eventTypeId", event.eventTypeId());
settings.setValue("deviceId", event.deviceId());
foreach (const Param &param, event.params()) {
settings.beginGroup("Param-" + param.name());
settings.setValue("value", param.value());
settings.setValue("operand", param.operand());
settings.setValue("eventTypeId", eventDescriptor.eventTypeId());
settings.setValue("deviceId", eventDescriptor.deviceId());
foreach (const ParamDescriptor &paramDescriptor, eventDescriptor.paramDescriptors()) {
settings.beginGroup("ParamDescriptor-" + paramDescriptor.name());
settings.setValue("value", paramDescriptor.value());
settings.setValue("operand", paramDescriptor.operand());
settings.endGroup();
}
@ -274,3 +275,13 @@ RuleEngine::RuleError RuleEngine::removeRule(const QUuid &ruleId)
}
return RuleErrorRuleNotFound;
}
bool RuleEngine::containsEvent(const Rule &rule, const Event &event)
{
foreach (const EventDescriptor &eventDescriptor, rule.eventDescriptors()) {
if (eventDescriptor == event) {
return true;
}
}
return false;
}

View File

@ -41,8 +41,8 @@ public:
QList<Action> evaluateEvent(const Event &event);
RuleError addRule(const Event &event, const QList<Action> &actions);
RuleError addRule(const Event &event, const QList<State> &states, const QList<Action> &actions);
RuleError addRule(const EventDescriptor &eventDescriptor, const QList<Action> &actions);
RuleError addRule(const EventDescriptor &eventDescriptor, const QList<State> &states, const QList<Action> &actions);
QList<Rule> rules() const;
RuleError removeRule(const QUuid &ruleId);
@ -51,6 +51,9 @@ signals:
void ruleAdded(const QUuid &ruleId);
void ruleRemoved(const QUuid &ruleId);
private:
bool containsEvent(const Rule &rule, const Event &event);
private:
QString m_settingsFile;
QList<Rule> m_rules;

View File

@ -566,8 +566,13 @@ void TestJSONRPC::storedDevices()
foreach (const QVariant device, response.toMap().value("params").toMap().value("devices").toList()) {
qDebug() << "found stored device" << device;
if (DeviceId(device.toMap().value("id").toString()) == addedDeviceId) {
qDebug() << "found added device" << device.toMap().value("params").toList().first();
QCOMPARE(device.toMap().value("params").toMap(), deviceParams);
// foreach (const QVariant &paramVariant, device.toMap().value("params").toList()) {
// if ()
// }
qDebug() << "found added device" << device.toMap().value("params").toList().first().toMap();
qDebug() << "expected deviceParams:" << deviceParams;
QCOMPARE(device.toMap().value("params").toList().first().toMap(), deviceParams);
}
}