From 21f656e6ee00ca05f5593aa80fd7a52f2e419b02 Mon Sep 17 00:00:00 2001 From: Tobias Moebert <moebert@uni-potsdam.de> Date: Fri, 17 Apr 2015 15:50:33 +0200 Subject: [PATCH] + added Interpretation class - [AbstractList] removed counter - [Aggregator] interpreters - [Aggregator] addInterpreter() and getInterpreters() - [Aggregator] getInterpretedData() - [AttributeTypeList] getItem(_identifier) - [AttributeValueList] getAttributeValue(_key) - [ParameterList] getIdentifier() - [Interpreter] - getInterpretedData() + [AttributeType] hasInputParameter() + [AttributeTypeList] removeAttributeType(_attributeType, _allOccurences) + [AttributeTypeList] hasAttributesWithInputParameters() + [AttributeTypeList] getAttributesWithInputParameters() + [AttributeValueList] containsAttributeType(_attributeType) + [AttributeValueList] getValueObjectForAttributeType(_attributeType) + [AttributeValueList] removeAttributeValuesWithAttributeType(_attributeType) + [AttributeValueList] updateValue(_attributeValue) + [ParameterList] hasInputParameter() + [Interpreter] hasOutAttributesWithInputParameters() + [Interpreter] getOutAttributesWithInputParameters() # [Aggregator] uses Interpretation objects to save referenced interpreters # [Aggregator] can handle parameters with values that change at runtime # [AttributeType] equals(_attributeType) doesn't rely on identifier anymore # [AttributeType] virtual public toString() -> public getIdentifier() # [AttributeTypeList] public put(_attributeType) -> public put(_attributeType, _multipleInstances) # [AttributeValueList] contains(_items) -> contains(_attributeValue) # [Parameter] getIdentifier() -> toString() # [Interpreter] callInterpreter(_dataToInterpret, _function) -> callInterpreter(_inAttributeValues, _outAttributeValues, _function) --- dist/contactJS.js | 695 ++++++++------------ js/config.js | 1 + js/modules/abstractList.js | 22 +- js/modules/aggregator/aggregator.js | 172 ++--- js/modules/aggregator/interpretation.js | 22 + js/modules/aggregator/storage/storage.js | 29 +- js/modules/attribute/attributeType.js | 33 +- js/modules/attribute/attributeTypeList.js | 99 ++- js/modules/attribute/attributeValue.js | 9 +- js/modules/attribute/attributeValueList.js | 114 ++-- js/modules/attribute/parameter.js | 30 +- js/modules/attribute/parameterList.js | 100 +-- js/modules/interpreter/interpreter.js | 45 +- js/modules/interpreter/interpreterResult.js | 18 +- js/modules/widget/widget.js | 4 +- 15 files changed, 582 insertions(+), 811 deletions(-) create mode 100644 js/modules/aggregator/interpretation.js diff --git a/dist/contactJS.js b/dist/contactJS.js index 8054183..f680ef6 100644 --- a/dist/contactJS.js +++ b/dist/contactJS.js @@ -521,14 +521,7 @@ define('abstractList',[ 'easejs' ], function(easejs) { */ var AbstractList = AbstractClass('AbstractList', { - /** - * @alias counter - * @protected - * @type {int} - * @memberof AbstractList# - * @desc Number of Items. - */ - 'protected counter' : 0, + /** * @alias items * @protected @@ -644,7 +637,7 @@ define('abstractList',[ 'easejs' ], function(easejs) { * @returns {Array} */ 'public getKeys' : function() { - var listKeys = new Array(); + var listKeys = []; for ( var key in this.items) { listKeys.push(key); } @@ -660,11 +653,7 @@ define('abstractList',[ 'easejs' ], function(easejs) { * @returns {Array} */ 'virtual public getItems' : function() { - var listValues = new Array(); - for ( var key in this.items) { - listValues.push(this.items[key]); - } - return listValues; + return this.items; }, /** @@ -676,7 +665,7 @@ define('abstractList',[ 'easejs' ], function(easejs) { * @returns {int} */ 'public size' : function() { - return this.counter; + return this.items.length; }, /** @@ -687,7 +676,7 @@ define('abstractList',[ 'easejs' ], function(easejs) { * @returns {boolean} */ 'public isEmpty' : function() { - return this.counter == 0; + return this.size() == 0; }, /** @@ -698,7 +687,6 @@ define('abstractList',[ 'easejs' ], function(easejs) { */ 'public clear' : function() { this.items = []; - this.counter = 0; } }); @@ -828,45 +816,31 @@ define('parameter',['easejs'], * @returns {boolean} */ 'public equals' : function(_parameter) { + var ignoreValue = false; if(Class.isA(Parameter, _parameter)){ - if(_parameter.getIdentifier() == this.getIdentifier()){ - return true; + if (_parameter.getValue() == "PV_INPUT" || this.getValue() == "PV_INPUT") { + return this.getKey() == _parameter.getKey(); + } else { + return this.getKey() == _parameter.getKey() && this.getValue() == _parameter.getValue(); } } return false; }, - /** - * Returns a string that describes the parameter. - * - * @public - * @override - * @alias toString - * @memberof Parameter# - * @returns {string} - */ - 'public toString': function() { - return this.getIdentifier(); - }, - /** * Returns an identifier that uniquely describes the parameter. * The identifier can be used to compare two parameters. * Format: [ParameterName:ParameterValue] * * @public - * @alias getIdentifier + * @alias toString * @memberof Parameter# * @returns {String} * @example [CP_UNIT:KILOMETERS] */ - 'public getIdentifier': function() { - if (this.value != "PV_INPUT") { - return "["+this.key+":"+this.value+"]"; - } else { - return "["+this.key+"]"; - } + 'public toString': function() { + return "["+this.key+":"+this.value+"]"; } }); @@ -920,18 +894,10 @@ define('parameterList',[ 'easejs', 'abstractList', 'parameter' ], * @returns {ParameterList} */ 'public withItems' : function(_parameterList) { - var list = []; if (_parameterList instanceof Array) { - list = _parameterList; + this.items = _parameterList; } else if (Class.isA(ParameterList, _parameterList)) { - list = _parameterList.getItems(); - } - for ( var i in list) { - var parameter = list[i]; - if (Class.isA(Parameter, parameter)) { - this.items[parameter.getKey()] = parameter.getValue(); - this.counter++; - } + this.items = _parameterList.getItems(); } return this; }, @@ -946,11 +912,9 @@ define('parameterList',[ 'easejs', 'abstractList', 'parameter' ], */ 'public put' : function(_parameter) { if (Class.isA(Parameter, _parameter)) { - - if (!(this.containsKey(_parameter.getKey()))) { - this.counter++; + if (!(this.contains(_parameter))) { + this.items.push(_parameter); } - this.items[_parameter.getKey()] = _parameter.getValue(); } }, @@ -969,14 +933,8 @@ define('parameterList',[ 'easejs', 'abstractList', 'parameter' ], } else if (Class.isA(ParameterList, _parameterList)) { list = _parameterList.getItems(); } - for ( var i in list) { - var parameter = list[i]; - if (Class.isA(Parameter, parameter)) { - if (!(this.containsKey(parameter.getKey()))) { - this.counter++; - } - this.items[parameter.getKey()] = parameter.getValue(); - } + for (var i in list) { + this.put(list[i]); } }, @@ -986,16 +944,16 @@ define('parameterList',[ 'easejs', 'abstractList', 'parameter' ], * @public * @alias contains * @memberof ParameterList# - * @param {Parameter} - * _item Parameter that should be - * verified + * @param {Parameter} _item Parameter that should be verified * @returns {boolean} */ 'public contains' : function(_item) { if (Class.isA(Parameter, _item)) { - var tmp = this.getItem(_item.getKey()); - if (tmp === _item.getValue()) { - return true; + for (var index in this.items) { + var tmp = this.items[index]; + if (tmp.equals(_item)) { + return true; + } } } return false; @@ -1007,37 +965,20 @@ define('parameterList',[ 'easejs', 'abstractList', 'parameter' ], * @public * @alias equals * @memberof ParameterList# - * @param {ParameterList} _list ParameterList that should be compared + * @param {ParameterList} _parameterList ParameterList that should be compared * @returns {boolean} */ 'public equals' : function(_parameterList) { if (Class.isA(ParameterList, _parameterList) && _parameterList.size() == this.size()) { - for (var index in _parameterList.getItems()) { - var theParameter = _parameterList.getItems()[index]; - if (!this.contains(theParameter)) return false; - } + for (var index in _parameterList.getItems()) { + var theParameter = _parameterList.getItems()[index]; + if (!this.contains(theParameter)) return false; + } return true; } return false; }, - /** - * Returns all items as parameter objects. - * @public - * @alias getItems - * @memberof ParameterList# - * @returns {Array<Parameter>} - */ - 'override public getItems' : function() { - var parameters = []; - for (var key in this.items) { - var parameter = new Parameter().withKey(key) - .withValue(this.items[key]); - parameters.push(parameter); - } - return parameters; - }, - /** * Returns the objects of the list as JSON objects. * @@ -1049,34 +990,19 @@ define('parameterList',[ 'easejs', 'abstractList', 'parameter' ], 'public getItemsAsJson': function() { var parameters = {}; for (var key in this.items) { - parameters[key] = this.items[key]; + var theParameter = this.items[key]; + parameters[theParameter.getKey()] = theParameter.getValue(); } return parameters; }, - /** - * Returns an identifier of all the parameters in the list. - * The identifier can be used to compare two parameter lists. <br/> - * Format: [FirstParameterName:FirstParameterValue][SecondParameterName:SecondParameterValue]… - * - * @public - * @alias getIdentifier - * @memberof ParameterList# - * @returns {String} - * @example [CP_TARGET_LATITUDE:52][CP_TARGET_LONGITUDE:13][CP_UNIT:KILOMETERS] - */ - 'public getIdentifier': function() { - var identifier = ""; - for (var key in this.items) { - var value = this.items[key]; - if (value != "PV_INPUT") { - identifier += "["+key+":"+value+"]"; - } else { - identifier += "["+key+"]"; - } - } - return identifier; - } + 'public hasInputParameter': function() { + for (var index in this.items) { + var theParameter = this.items[index]; + if (theParameter.getValue() == "PV_INPUT") return true; + } + return false; + } }); return ParameterList; @@ -1290,26 +1216,18 @@ define('attributeType',['easejs', * @param {AttributeType} _attributeType AttributeType that should be compared * @returns {boolean} */ - 'virtual public equals' : function(_attributeType) { - if(Class.isA(AttributeType, _attributeType)){ - if (this.getIdentifier() == _attributeType.getIdentifier()) { - return true; - } + 'virtual public equals' : function(_attributeType) { + if (Class.isA(AttributeType, _attributeType)) { + if (this.getName() == _attributeType.getName() && + this.getType() == _attributeType.getType() && + this.getParameters().equals(_attributeType.getParameters())) + return true; } return false; }, - /** - * Returns a string that describes the attribute type. - * - * @virtual - * @public - * @alias toString - * @memberof AttributeType# - * @returns {String} - */ - 'virtual public toString': function() { - return this.getIdentifier(); + 'public hasInputParameter': function() { + return this.hasParameters() && this.parameterList.hasInputParameter(); }, /** @@ -1318,16 +1236,19 @@ define('attributeType',['easejs', * Format: (AttributeName:AttributeType)#[FirstParameterName:FirstParameterValue]… * * @public - * @alias getIdentifier + * @alias toString * @memberof AttributeType# * @returns {String} * @example (CI_USER_LOCATION_DISTANCE:FLOAT)#[CP_TARGET_LATITUDE:52][CP_TARGET_LONGITUDE:13][CP_UNIT:KILOMETERS] */ - 'public getIdentifier': function() { + 'virtual public toString': function() { var identifier = "("+this.name+":"+this.type+")"; if (this.hasParameters()) { identifier += "#"; - identifier += this.parameterList.getIdentifier(); + for (var index in this.parameterList.getItems()) { + var theParameter = this.parameterList.getItems()[index]; + identifier += theParameter.toString(); + } } return identifier; } @@ -1461,9 +1382,8 @@ define('attributeValue',[ 'easejs', 'attributeType' ], function(easejs, Attribut */ 'override public equals' : function(_attributeValue) { if (Class.isA(AttributeValue, _attributeValue)) { - if (this.__super(_attributeValue.getAttributeType()) - && _attributeValue.getValue() == this - .getValue()) { + if (_attributeValue.getAttributeType().equals(this.getAttributeType()) && + _attributeValue.getValue() == this.getValue()) { return true; } } @@ -1496,7 +1416,7 @@ define('attributeValue',[ 'easejs', 'attributeType' ], function(easejs, Attribut 'public buildFromAttributeType' : function(_attributeType) { if (Class.isA(AttributeType, _attributeType)) { return new AttributeValue().withName(_attributeType.getName()) - .withType(_attributeType.getType()).withParameters(_attributeType.getParameters()).withValue('undefined'); + .withType(_attributeType.getType()).withParameters(_attributeType.getParameters()).withValue('NO_VALUE'); } return null; }, @@ -1511,7 +1431,7 @@ define('attributeValue',[ 'easejs', 'attributeType' ], function(easejs, Attribut * @returns {string} */ 'override public toString': function() { - return this.getIdentifier()+":"+this.getValue(); + return this.__super().toString()+":"+this.getValue(); } }); @@ -1535,14 +1455,7 @@ define('attributeTypeList',[ 'easejs', 'abstractList', 'attributeType', 'paramet * @requires AttributeType */ var AttributeTypeList = Class('AttributeTypeList').extend(AbstractList, { - /** - * @alias counter - * @protected - * @type {integer} - * @memberof AttributeTypeList# - * @desc Number of items. - */ - 'protected counter' : 0, + /** * @alias items * @protected @@ -1550,7 +1463,7 @@ define('attributeTypeList',[ 'easejs', 'abstractList', 'attributeType', 'paramet * @memberof AttributeTypeList# * @desc ItemList */ - 'protected items' : {}, + 'protected items' : [], /** * Builder for item list. @@ -1562,19 +1475,13 @@ define('attributeTypeList',[ 'easejs', 'abstractList', 'attributeType', 'paramet * @returns {AttributeTypeList} */ 'public withItems' : function(_attributeTypeList) { - var list = {}; + var list = []; if (_attributeTypeList instanceof Array) { list = _attributeTypeList; } else if (Class.isA(AttributeTypeList, _attributeTypeList)) { list = _attributeTypeList.getItems(); } - for ( var i in list) { - var attributeType = list[i]; - if (Class.isA(AttributeType, attributeType)) { - this.items[attributeType.getIdentifier()] = attributeType; - this.counter++; - } - } + this.items = list; return this; }, @@ -1586,12 +1493,12 @@ define('attributeTypeList',[ 'easejs', 'abstractList', 'attributeType', 'paramet * @memberof AttributeTypeList# * @param {AttributeType} _attributeType AttributeType */ - 'public put' : function(_attributeType) { + 'public put' : function(_attributeType, _multipleInstances) { + var _multipleInstances = typeof _multipleInstances == "undefined" ? false : _multipleInstances; if (Class.isA(AttributeType, _attributeType)) { - if (!(this.containsKey(_attributeType.getIdentifier()))) { - this.counter++; + if (_multipleInstances || !(this.contains(_attributeType))) { + this.items.push(_attributeType); } - this.items[_attributeType.getIdentifier()] = _attributeType; } }, @@ -1612,13 +1519,7 @@ define('attributeTypeList',[ 'easejs', 'abstractList', 'attributeType', 'paramet list = _attributeTypeList.getItems(); } for ( var i in list) { - var attributeType = list[i]; - if (Class.isA(AttributeType, attributeType)) { - if (!(this.containsKey(attributeType.getIdentifier()))) { - this.counter++; - } - this.items[attributeType.getIdentifier()] = attributeType; - } + this.put(list[i]); } }, @@ -1634,15 +1535,17 @@ define('attributeTypeList',[ 'easejs', 'abstractList', 'attributeType', 'paramet */ 'public contains' : function(_item) { if (Class.isA(AttributeType, _item)) { - var tmp = this.getItem(_item.getIdentifier()); - if (!(typeof tmp === 'undefined') - && tmp.equals(_item)) { - return true; + for (var index in this.items) { + var tmp = this.items[index]; + if (tmp.equals(_item)) { + return true; + } } } return false; }, + /** * Compare the specified AttributeTypeList with this instance. * @@ -1654,31 +1557,15 @@ define('attributeTypeList',[ 'easejs', 'abstractList', 'attributeType', 'paramet */ 'public equals' : function(_list) { if (Class.isA(AttributeTypeList, _list) && _list.size() == this.size()) { - var items = _list.getItems(); - for (var i in items) { - var item = items[i]; - if (!this.contains(item)) { - return false; - } + for (var index in _list.getItems()) { + var theAttributeType = _list.getItems()[index]; + if (!this.contains(theAttributeType)) return false; } return true; } return false; }, - /** - * Returns the attribute type that matches the provided identifier. - * @public - * @override - * @alias getItem - * @memberof AttributeTypeList# - * @param {string} _identifier The identifier that should be searched for. - * @returns {AttributeType} - */ - 'override public getItem' : function(_identifier) { - return this.items[_identifier]; - }, - /** * Creates a clone of the current list. * @@ -1698,7 +1585,35 @@ define('attributeTypeList',[ 'easejs', 'abstractList', 'attributeType', 'paramet newList.put(newAttributeType); } return newList; - } + }, + + 'public removeAttributeType': function(_attributeType, _allOccurrences) { + _allOccurrences = typeof _allOccurrences == "undefined" ? false : _allOccurrences; + for (var index in this.items) { + var theAttributeType = this.items[index]; + if (theAttributeType.equals(_attributeType)) { + this.items.splice(index, 1); + } + } + if (_allOccurrences && this.contains(_attributeType)) this.removeAttributeType(_attributeType, _allOccurrences); + }, + + 'public hasAttributesWithInputParameters': function() { + for (var index in this.items) { + var theAttributeType = this.items[index]; + if (theAttributeType.hasInputParameter()) return true; + } + return false; + }, + + 'public getAttributesWithInputParameters': function() { + var list = new AttributeTypeList(); + for (var index in this.items) { + var theAttributeType = this.items[index]; + if (theAttributeType.hasInputParameter()) list.put(theAttributeType); + } + return list; + } }); return AttributeTypeList; @@ -1725,14 +1640,7 @@ define('attributeValueList',['easejs', 'abstractList', 'attributeValue', 'attrib * @requires AttributeTypeList) */ var AttributeValueList = Class('AttributeValueList').extend(AbstractList,{ - /** - * @alias counter - * @protected - * @type {integer} - * @memberof AttributeValueList# - * @desc Number of items. - */ - 'protected counter' : 0, + /** * @alias items * @protected @@ -1740,7 +1648,7 @@ define('attributeValueList',['easejs', 'abstractList', 'attributeValue', 'attrib * @memberof AttributeValueList# * @desc ItemList. */ - 'protected items' : {}, + 'protected items' : [], /** * Builder for item list. @@ -1759,13 +1667,7 @@ define('attributeValueList',['easejs', 'abstractList', 'attributeValue', 'attrib _attributeValueList)) { list = _attributeValueList.getItems(); } - for (var i in list) { - var attributeValue = list[i]; - if (Class.isA(AttributeValue, attributeValue)) { - this.items[attributeValue.getIdentifier()] = attributeValue; - this.counter++; - } - } + this.items = list; return this; }, @@ -1777,12 +1679,13 @@ define('attributeValueList',['easejs', 'abstractList', 'attributeValue', 'attrib * @memberof AttributeValueList# * @param {AttributeValue} _attributeValue AttributeValue */ - 'public put' : function(_attributeValue) { + 'public put' : function(_attributeValue, _multipleInstances) { if (Class.isA(AttributeValue, _attributeValue)) { - if (!(this.containsKey(_attributeValue.getIdentifier()))) { - this.counter++; + if (_multipleInstances || !(this.containsAttributeType(_attributeValue.getAttributeType()))) { + this.items.push(_attributeValue); + } else { + this.updateValue(_attributeValue); } - this.items[_attributeValue.getIdentifier()] = _attributeValue; } }, @@ -1802,14 +1705,8 @@ define('attributeValueList',['easejs', 'abstractList', 'attributeValue', 'attrib } else if (Class.isA(AttributeValueList, _attributeValueList)) { list = _attributeValueList.getItems(); } - for ( var i in list) { - var attributeValue = list[i]; - if (Class.isA(AttributeValue, attributeValue)) { - if (!(this.containsKey(attributeValue.getIdentifier()))) { - this.counter++; - } - this.items[attributeValue.getIdentifier()] = attributeValue; - } + for (var i in list) { + this.put(list[i]); } }, @@ -1820,14 +1717,28 @@ define('attributeValueList',['easejs', 'abstractList', 'attributeValue', 'attrib * @public * @alias contains * @memberof AttributeValueList# - * @param {AttributeValue} _item AttributeValue that should be verified. + * @param {AttributeValue} _attributeValue AttributeValue that should be verified. * @returns {boolean} */ - 'public contains' : function(_item) { - if (Class.isA(AttributeValue, _item)) { - var tmp = this.getItem(_item.getIdentifier()); - if (!(typeof tmp === 'undefined') && tmp.equals(_item)) { - return true; + 'public contains' : function(_attributeValue) { + if (Class.isA(AttributeValue, _attributeValue)) { + for (var index in this.items) { + var tmp = this.items[index]; + if (tmp.equals(_attributeValue)) { + return true; + } + } + } + return false; + }, + + 'public containsAttributeType': function(_attributeType) { + if (Class.isA(AttributeType, _attributeType)) { + for (var index in this.items) { + var tmp = this.items[index].getAttributeType(); + if (tmp.equals(_attributeType)) { + return true; + } } } return false; @@ -1845,12 +1756,9 @@ define('attributeValueList',['easejs', 'abstractList', 'attributeValue', 'attrib */ 'public equals' : function(_list) { if (Class.isA(AttributeValueList, _list) && _list.size() == this.size()) { - var items = _list.getItems(); - for ( var i in items) { - var item = items[i]; - if (!this.contains(item)) { - return false; - } + for (var index in _list.getItems()) { + var theAttributeValue = _list.getItems()[index]; + if (!this.contains(theAttributeValue)) return false; } return true; } @@ -1878,7 +1786,7 @@ define('attributeValueList',['easejs', 'abstractList', 'attributeValue', 'attrib for ( var i in list) { var attributeType = list[i]; if (Class.isA(AttributeType, attributeType)) { - var attribute = this.items[attributeType.getIdentifier()]; + var attribute = this.getValueObjectForAttributeType(attributeType); if (typeof attribute != "undefined") { response.put(attribute); } @@ -1907,25 +1815,12 @@ define('attributeValueList',['easejs', 'abstractList', 'attributeValue', 'attrib for (var i in list) { var attributeType = list[i]; if (Class.isA(AttributeType, attributeType)) { - response.removeItem(attributeType.getIdentifier()); + response.removeAttributeValuesWithAttributeType(attributeType); } } return response; }, - /** - * Alias for {#getItem}. - * - * @public - * @alias getValue - * @memberof AttributeValueList# - * @param _key The value key. - * @returns {*} - */ - 'public getAttributeValue': function(_key) { - return this.getItem(_key); - }, - /** * Returns the attribute value that matches the provided attribute type. * @@ -1936,9 +1831,29 @@ define('attributeValueList',['easejs', 'abstractList', 'attributeValue', 'attrib * @returns {AttributeValue} */ 'public getValueForAttributeType': function(_attributeType) { - return this.getAttributeValue(_attributeType.getIdentifier()).getValue(); - } + return this.getValueObjectForAttributeType(_attributeType).getValue(); + }, + + 'public getValueObjectForAttributeType': function(_attributeType) { + for (var index in this.getItems()) { + var theAttributeValue = this.getItems()[index]; + if (theAttributeValue.getAttributeType().equals(_attributeType)) return theAttributeValue; + } + }, + + 'public removeAttributeValuesWithAttributeType': function(_attributeType) { + for (var index in this.items) { + var theAttributeValue = this.items[index]; + if (theAttributeValue.getAttributeType().equals(_attributeType)) this.items.splice(index, 1); + } + }, + 'public updateValue': function(_attributeValue) { + for (var index in this.items) { + var theAttributeValue = this.items[index]; + if (theAttributeValue.getAttributeType().equals(_attributeValue.getAttributeType())) this.items[index] = _attributeValue; + } + } }); return AttributeValueList; @@ -2107,16 +2022,16 @@ define('storage',['easejs', 'attributeValue', 'attributeValueList', 'attributeTy 'private createTable' : function(_attribute, _function){ if(this.db){ var tableName = this.tableName(_attribute); - var statement = 'CREATE TABLE IF NOT EXISTS ' + tableName + ' (value_, type_, created_)'; + var statement = 'CREATE TABLE IF NOT EXISTS "' + tableName + '" (value_, type_, created_)'; + console.log('CREATE TABLE IF NOT EXISTS "' + tableName + '"'); if(_function && typeof(_function) == 'function'){ this.db.transaction(function(tx){tx.executeSql(statement);}, this.errorCB, _function); } else { this.db.transaction(function(tx){tx.executeSql(statement);}, this.errorCB, this.successCB); } - if(!this.attributeNames.indexOf(name) > -1){ + if(!this.attributeNames.indexOf(_attribute.getName()) > -1){ this.attributeNames.push(tableName); } - console.log('CREATE TABLE IF NOT EXISTS ' + tableName); } }, @@ -2133,18 +2048,17 @@ define('storage',['easejs', 'attributeValue', 'attributeValueList', 'attributeTy 'private insertIntoTable' : function(_attributeValue, _function){ if(this.db && _attributeValue && Class.isA(AttributeValue, _attributeValue)){ var tableName = this.tableName(_attributeValue); - var statement = 'INSERT INTO ' + tableName - + ' (value_, type_, created_) VALUES ("' + var statement = 'INSERT INTO "' + tableName + + '" (value_, type_, created_) VALUES ("' + _attributeValue.getValue() + '", "' + _attributeValue.getType() + '", "' + _attributeValue.getTimestamp() + '")'; - + console.log('INSERT INTO "'+tableName+'" VALUES ('+_attributeValue.getValue()+", "+_attributeValue.getType()+", "+_attributeValue.getTimestamp()); if(_function && typeof(_function) == 'function'){ this.db.transaction(function(tx){tx.executeSql(statement);}, this.errorCB, _function); } else { this.db.transaction(function(tx){tx.executeSql(statement);}, this.errorCB, this.successCB); } - console.log('INSERT INTO '+tableName+' VALUES ('+_attributeValue.getValue()+", "+_attributeValue.getType()+", "+_attributeValue.getTimestamp()); } }, @@ -2410,10 +2324,8 @@ define('storage',['easejs', 'attributeValue', 'attributeValueList', 'attributeTy if(self.data.size() == 0){ return; } - var keys = self.data.getKeys(); - for(var i in keys){ - var key = keys[i]; - var item = self.data.getItem(key); + for(var i in self.data.getItems()){ + var item = self.data.getItems()[i]; if(!self.tableExists(item)){ self.createTable(item, function(){self.insertIntoTable(item);}); } else { @@ -2483,15 +2395,7 @@ define('storage',['easejs', 'attributeValue', 'attributeValueList', 'attributeTy * @returns{String} */ 'private tableName' : function(_attribute){ - var tableName = _attribute.getName(); - var parameterList = _attribute.getParameters(); - if(!parameterList.isEmpty()){ - var keys = parameterList.getKeys(); - for(var i in keys){ - tableName = tableName + '__' +keys[i] + '_'+parameterList.getItem(keys[i]); - } - } - return tableName; + return _attribute.getAttributeType().toString(); }, /** @@ -4286,7 +4190,7 @@ define('widget',[ 'easejs', 'MathUuid', 'callback', 'callbackList', 'attributeTy * @returns {*} */ 'public getAttributeValue': function(_attributeType) { - return this.getAttributeValues().getItem(_attributeType.getIdentifier()).getValue(); + return this.getAttributeValues().getItemForAttributeType(_attributeType).getValue(); }, /** @@ -4745,7 +4649,7 @@ define('widget',[ 'easejs', 'MathUuid', 'callback', 'callbackList', 'attributeTy * */ 'virtual public putData' : function(_data) { - var list = new Array(); + var list = []; if (_data instanceof Array) { list = _data; } else if (Class.isA(AttributeValueList, _data)) { @@ -5181,12 +5085,12 @@ define('interpreterResult',['easejs', 'attributeValueList'], * @public * @alias setTimestamp * @memberof InterpreterResult# - * @param {date} _timstamp interpretation time + * @param {date} _timestamp interpretation time */ - 'public setTimestamp' : function(_timesstamp){ - if(_timesstamp instanceof Date){ - this.type = _timesstamp; - }; + 'public setTimestamp' : function(_timestamp){ + if(_timestamp instanceof Date){ + this.type = _timestamp; + } }, /** @@ -5201,10 +5105,10 @@ define('interpreterResult',['easejs', 'attributeValueList'], if (_outAttributes instanceof Array) { for(var i in _outAttributes){ this.outAttributes.put(_outAttributes[i]); - }; + } } else if (Class.isA(AttributeValueList, _outAttributes)) { this.outAttributes = _outAttributes; - }; + } }, /** @@ -5219,10 +5123,10 @@ define('interpreterResult',['easejs', 'attributeValueList'], if (_inAttributes instanceof Array) { for(var i in _outAttributes){ this.inAttributes.put(_inAttributes[i]); - }; + } } else if (Class.isA(AttributeValueList, _inAttributes)) { this.inAttributes = _inAttributes; - }; + } } }); @@ -5481,7 +5385,7 @@ define('interpreter',[ 'easejs', 'MathUuid', 'attributeType', 'attributeTypeList * @param {string} _name name of the attribute * @param {string} _type type of the attribute * @param {string} _value value of the attribute - * @param {ParameterList|Array} _parameter Parameter of the attribute. + * @param {ParameterList|Array} _parameters Parameter of the attribute. */ 'protected setOutAttribute' : function(_name, _type, _value,_parameters) { var attributeValue = new AttributeValue().withName(_name) @@ -5510,17 +5414,13 @@ define('interpreter',[ 'easejs', 'MathUuid', 'attributeType', 'attributeTypeList * @public * @alias callInterpreter * @memberof Interpreter# - * @param {AttributeValueList} _dataToInterpret Data that should be interpreted. + * @param {AttributeValueList} _inAttributeValues Data that should be interpreted. * @param {?function} _function For additional actions, if an asynchronous function is used. */ - 'public callInterpreter' : function(_dataToInterpret, _function) { - if (_dataToInterpret && this.canHandle(_dataToInterpret)) { - if(_function && typeof(_function) == 'function'){ - this.interpretData(_dataToInterpret, _function); - } else { - this.interpretData(_dataToInterpret); - } - this.setInAttributeValues(_dataToInterpret); + 'public callInterpreter' : function(_inAttributeValues, _outAttributeValues, _function) { + if (_inAttributeValues && this.canHandle(_inAttributeValues)) { + this.interpretData(_inAttributeValues, _outAttributeValues, _function); + this.setInAttributeValues(_inAttributeValues); this.lastInterpretation = new Date(); } else { var list = this.outAttributeTypes.getItems(); @@ -5553,7 +5453,7 @@ define('interpreter',[ 'easejs', 'MathUuid', 'attributeType', 'attributeTypeList * @param {AttributeValueList} _inAtts Data that should be verified. */ 'protected canHandle' : function(_inAtts) { - var list = [] + var list = []; if (_inAtts instanceof Array) { list = _inAtts; } else if (Class.isA(AttributeValueList, _inAtts)) { @@ -5571,21 +5471,6 @@ define('interpreter',[ 'easejs', 'MathUuid', 'attributeType', 'attributeTypeList return true; }, - /** - * Returns the interpreted data. - * - * @protected - * @alias getInterpretedData - * @memberof Interpreter# - * @returns {AttributeValueList} - */ - 'public getInterpretedData' : function() { - var result = new InterpreterResult().withTimestamp(this.lastInterpretation). - withInAttributes(this.inAttributeValues). - withOutAttributes(this.outAttributeValues); - return result; - }, - /** * Returns the time of the last interpretation. * @@ -5640,6 +5525,18 @@ define('interpreter',[ 'easejs', 'MathUuid', 'attributeType', 'attributeTypeList this.discoverer.registerNewComponent(this); } + }, + + /** + * + * @returns {boolean} + */ + 'public hasOutAttributesWithInputParameters': function() { + return this.outAttributeTypes.hasAttributesWithInputParameters(); + }, + + 'public getOutAttributesWithInputParameters': function() { + return this.outAttributeTypes.getAttributesWithInputParameters(); } // /** @@ -5661,6 +5558,28 @@ define('interpreter',[ 'easejs', 'MathUuid', 'attributeType', 'attributeTypeList return Interpreter; }); +/** + * Created by tobias on 15.04.15. + */ +define('interpretation',['easejs', 'interpreter', 'attributeTypeList'], + function(easejs, Interpreter, AttributeTypeList) { + var Class = easejs.Class; + var Interpretation = Class('Interpretation', { + 'public interpreterId' : null, + 'public inAttributeTypes' : new AttributeTypeList(), + 'public outAttributeTypes' : new AttributeTypeList(), + + 'public __construct' : function(_interpreterId, _inAttributes, _outAttributes){ + this.interpreterId = _interpreterId; + this.inAttributeTypes = _inAttributes; + this.outAttributeTypes = _outAttributes; + } + + }); + + return Interpretation; + } +); /** * This module representing a Context Aggregator. * It aggregates data from multiple widgets. @@ -5670,10 +5589,10 @@ define('interpreter',[ 'easejs', 'MathUuid', 'attributeType', 'attributeTypeList */ define('aggregator',['easejs', 'MathUuid','widget', 'attributeType', 'attributeValue', 'attributeValueList', 'subscriber', - 'subscriberList', 'callbackList', 'storage', 'widgetDescription', 'interpreter', 'attributeTypeList'], + 'subscriberList', 'callbackList', 'storage', 'widgetDescription', 'interpreter', 'attributeTypeList', 'interpretation'], function(easejs, MathUuid, Widget, AttributeType, AttributeValue, AttributeValueList, Subscriber, SubscriberList, - CallbackList, Storage, WidgetDescription, Interpreter, AttributeTypeList){ + CallbackList, Storage, WidgetDescription, Interpreter, AttributeTypeList, Interpretation){ var Class = easejs.Class; var Aggregator = Class('Aggregator'). @@ -5707,14 +5626,10 @@ define('aggregator',['easejs', 'MathUuid','widget', */ 'protected widgets' : [], - /** - * @alias interpreters - * @protected - * @type {Array} - * @memberof Aggregator# - * @desc List of subscribed interpreters referenced by ID. - */ - 'protected interpreters' : [], + /** + * @type {Array.<Interpretation>} + */ + 'protected interpretations' : [], /** * @alias db @@ -5748,7 +5663,7 @@ define('aggregator',['easejs', 'MathUuid','widget', { this.id = Math.uuid(); this.widgets = []; - this.interpreters = []; + this.interpretations = []; this.__super(_discoverer, _attributeTypes); }, @@ -5774,11 +5689,11 @@ define('aggregator',['easejs', 'MathUuid','widget', * @memberof Aggregator# * @param {AttributeType} _attributeType attributeType */ - 'protected addAttributeType' : function(_attributeType){ + 'protected addAttributeType' : function(_attributeType, _multipleInstances){ if(Class.isA( AttributeType, _attributeType )){ - this.attributeTypes.put(_attributeType); + this.attributeTypes.put(_attributeType, _multipleInstances); var attVal = new AttributeValue().buildFromAttributeType(_attributeType); - this.attributes.put(attVal); + this.attributes.put(attVal, _multipleInstances); } }, @@ -5966,30 +5881,6 @@ define('aggregator',['easejs', 'MathUuid','widget', */ 'virtual protected setAggregatorCallbacks' : function() { - }, - - /** - * Adds an interpreter to the aggregator. - * - * @public - * @alias addInterpreter - * @memberof Aggregator# - * @param _theInterpreter - */ - 'public addInterpreter': function(_theInterpreter) { - this.interpreters.push(_theInterpreter.getId()); - }, - - /** - * Returns an array with the UUIDs of the interpreters that where added to the aggregator. - * - * @public - * @alias getInterpreters - * @memberof Aggregator# - * @returns {Array} The UUIDs of the connected interpreters. - */ - 'public getInterpreters': function() { - return this.interpreters; }, /** @@ -6138,44 +6029,15 @@ define('aggregator',['easejs', 'MathUuid','widget', * @alias interpretData * @memberof Aggregator# * @param {String} _interpreterId ID of the searched Interpreter - * @param {(AttributeValueList|Array)} _data data that should be interpreted * @param {?function} _function for additional actions, if an asynchronous function is used */ - 'public interpretData' : function(_interpreterId, _function){ + 'public interpretData' : function(_interpreterId, _inAttributeValues, _outAttributeValues, _function){ var interpreter = this.discoverer.getComponent(_interpreterId); if (Class.isA(Interpreter, interpreter)) { - interpreter.callInterpreter(this.getAttributeValues(interpreter.getInAttributeTypes()), _function); + interpreter.callInterpreter(_inAttributeValues, _outAttributeValues, _function); } }, - /** - * Calls the given Interpreter for getting the data. - * - * @public - * @alias getInterpretedData - * @memberof Aggregator# - * @param {String} _interpreterId ID of the searched Interpreter - * @returns {?AttributeValueList} - */ - 'public getInterpretedData' : function(_interpreterId){ - var response = 'undefined'; - var interpreter = this.discoverer.getComponent(_interpreterId); - if (interpreter) { - response = interpreter.getInterpretedData(); - var attributeList = response.getOutAttributes().getItems(); - for (var i in attributeList) { - var theAttribute = attributeList[i]; - if (Class.isA(AttributeValue, theAttribute) && this.isAttribute(theAttribute)) { - this.addAttribute(theAttribute); - if(this.db){ - this.store(theAttribute); - } - } - } - } - return response; - }, - /** * Initializes the database with the specified name. * @@ -6282,7 +6144,14 @@ define('aggregator',['easejs', 'MathUuid','widget', * @returns {Array.<T>} The UUIDs. */ 'private getComponentUUIDs': function() { - return this.widgets.concat(this.interpreters); + var uuids = []; + uuids = uuids.concat(this.widgets); + for (var index in this.interpretations) { + var theInterpretation = this.interpretations[index]; + uuids.push(theInterpretation.interpreterId); + + } + return uuids; }, /** @@ -6355,8 +6224,8 @@ define('aggregator',['easejs', 'MathUuid','widget', var widgetOutAttribute = outAttributes[widgetOutAttributeIndex]; // add the attribute type to the aggregators list of handled attribute types if (!this.getAttributeTypes().contains(widgetOutAttribute)) this.addAttributeType(widgetOutAttribute); - console.log("I can now satisfy attribute "+widgetOutAttribute.getIdentifier()+" with the help of "+theComponent.getName()+"! That was easy :)"); - _unsatisfiedAttributes.removeItem(widgetOutAttribute.getIdentifier()); + console.log("I can now satisfy attribute "+widgetOutAttribute+" with the help of "+theComponent.getName()+"! That was easy :)"); + _unsatisfiedAttributes.removeAttributeType(widgetOutAttribute); } } else if (Class.isA(Interpreter, theComponent)) { // if the component is an interpreter and all its in attributes can be satisfied, add the interpreter console.log("It's an interpreter."); @@ -6368,39 +6237,44 @@ define('aggregator',['easejs', 'MathUuid','widget', for (var inAttributeIdentifier in inAttributes) { // get the attribute var theInAttribute = inAttributes[inAttributeIdentifier]; - console.log("The interpreter needs the attribute "+theInAttribute.getIdentifier()+"."); + console.log("The interpreter needs the attribute "+theInAttribute+"."); // if required attribute is not already satisfied by the aggregator search for components that do if (!this.doesSatisfyAttributeType(theInAttribute)) { - console.log("It seems that I can't satisfy "+theInAttribute.getIdentifier()+", but I will search for components that can."); + console.log("It seems that I can't satisfy "+theInAttribute+", but I will search for components that can."); var newAttributeList = new AttributeTypeList(); newAttributeList.put(theInAttribute); this.getComponentsForUnsatisfiedAttributeTypes(newAttributeList, false, [Widget, Interpreter]); // if the attribute still can't be satisfied drop the interpreter if (!this.doesSatisfyAttributeType(theInAttribute)) { - console.log("I couldn't find a component to satisfy "+theInAttribute.getIdentifier()+". Dropping interpreter "+theComponent.getName()+". Bye bye."); + console.log("I couldn't find a component to satisfy "+theInAttribute+". Dropping interpreter "+theComponent.getName()+". Bye bye."); canSatisfyInAttributes = false; break; } } else { - console.log("It seems that I already satisfy the attribute "+theInAttribute.getIdentifier()+". Let's move on."); + console.log("It seems that I already satisfy the attribute "+theInAttribute+". Let's move on."); } } if (canSatisfyInAttributes) { - this.addInterpreter(theComponent); // remove satisfied attribute for (var interpreterOutAttributeIndex in outAttributes) { var interpreterOutAttribute = outAttributes[interpreterOutAttributeIndex]; // add the attribute type to the aggregators list of handled attribute types - if (!this.getAttributeTypes().contains(interpreterOutAttribute)) this.addAttributeType(interpreterOutAttribute); - console.log("I can now satisfy attribute "+interpreterOutAttribute.getIdentifier()+" with the help of "+theComponent.getName()+"! Great!"); - _unsatisfiedAttributes.removeItem(interpreterOutAttribute.getIdentifier()); + for (var unsatisfiedAttributeIndex in _unsatisfiedAttributes.getItems()) { + var theUnsatisfiedAttribute = _unsatisfiedAttributes.getItems()[unsatisfiedAttributeIndex]; + if (theUnsatisfiedAttribute.equals(interpreterOutAttribute)) { + this.addAttributeType(theUnsatisfiedAttribute); + console.log("I can now satisfy attribute "+theUnsatisfiedAttribute+" with the help of "+theComponent.getName()+"! Great!"); + this.interpretations.push(new Interpretation(theComponent.getId(), theComponent.getInAttributeTypes(), new AttributeTypeList().withItems([theUnsatisfiedAttribute]))); + } + } + _unsatisfiedAttributes.removeAttributeType(interpreterOutAttribute, true); } - } else { + } else { console.log("Found interpreter but can't satisfy required attributes."); for (var j in theComponent.getDescription().getInAttributeTypes().getItems()) { - console.log("Missing "+theComponent.getDescription().getInAttributeTypes().getItems()[j].getIdentifier()+"."); + console.log("Missing "+theComponent.getDescription().getInAttributeTypes().getItems()[j]+"."); } } } @@ -6426,8 +6300,9 @@ define('aggregator',['easejs', 'MathUuid','widget', // get all interpreters that satisfy attribute types this.getComponentsForUnsatisfiedAttributeTypes(unsatisfiedAttributes, false, [Interpreter]); - //console.log(unsatisfiedAttributes); - //console.log(this.attributeTypes); + console.log("Unsatisfied attributes: "+unsatisfiedAttributes.size()); + console.log("Satisfied attributes: "+this.getAttributeTypes().size()); + console.log("Interpretations "+this.interpretations.length); }, /** @@ -6474,26 +6349,36 @@ define('aggregator',['easejs', 'MathUuid','widget', var self = this; var completedQueriesCounter = 0; - if (this.interpreters.length > 0) { - for(var index in this.interpreters) { - var theInterpreterId = this.interpreters[index]; + if (this.interpretations.length > 0) { + for (var index in this.interpretations) { + var theInterpretation = this.interpretations[index]; + var theInterpreterId = theInterpretation.interpreterId; + var interpretationInAttributeValues = this.getAttributeValues(theInterpretation.inAttributeTypes); + var interpretationOutAttributeValues = this.getAttributeValues(theInterpretation.outAttributeTypes); - self.interpretData(theInterpreterId, function() { - self.getInterpretedData(theInterpreterId); + self.interpretData(theInterpreterId, interpretationInAttributeValues, interpretationOutAttributeValues, function(_interpretedData) { + for (var j in _interpretedData.getItems()) { + var theInterpretedData = _interpretedData.getItems()[j]; - completedQueriesCounter++; - if (completedQueriesCounter == self.interpreters.length) { - if (_callback && typeof(_callback) == 'function') { - _callback(self.getAttributeValues()); - } - } - }); - } - } else { - if (_callback && typeof(_callback) == 'function') { - _callback(self.getAttributeValues()); - } - } + self.addAttribute(theInterpretedData); + if(self.db){ + self.store(theInterpretedData); + } + } + + completedQueriesCounter++; + if (completedQueriesCounter == self.interpretations.length) { + if (_callback && typeof(_callback) == 'function') { + _callback(self.getAttributeValues()); + } + } + }); + } + } else { + if (_callback && typeof(_callback) == 'function') { + _callback(self.getAttributeValues()); + } + } }, /** diff --git a/js/config.js b/js/config.js index 55edd40..398dda8 100644 --- a/js/config.js +++ b/js/config.js @@ -11,6 +11,7 @@ requirejs.config({ retrievalResult: 'modules/aggregator/storage/retrievalResult', storage: 'modules/aggregator/storage/storage', aggregator: 'modules/aggregator/aggregator', + interpretation: 'modules/aggregator/interpretation', attributeType: 'modules/attribute/attributeType', attributeValue: 'modules/attribute/attributeValue', attributeTypeList: 'modules/attribute/attributeTypeList', diff --git a/js/modules/abstractList.js b/js/modules/abstractList.js index 87814cb..04a65f0 100644 --- a/js/modules/abstractList.js +++ b/js/modules/abstractList.js @@ -14,14 +14,7 @@ define([ 'easejs' ], function(easejs) { */ var AbstractList = AbstractClass('AbstractList', { - /** - * @alias counter - * @protected - * @type {int} - * @memberof AbstractList# - * @desc Number of Items. - */ - 'protected counter' : 0, + /** * @alias items * @protected @@ -137,7 +130,7 @@ define([ 'easejs' ], function(easejs) { * @returns {Array} */ 'public getKeys' : function() { - var listKeys = new Array(); + var listKeys = []; for ( var key in this.items) { listKeys.push(key); } @@ -153,11 +146,7 @@ define([ 'easejs' ], function(easejs) { * @returns {Array} */ 'virtual public getItems' : function() { - var listValues = new Array(); - for ( var key in this.items) { - listValues.push(this.items[key]); - } - return listValues; + return this.items; }, /** @@ -169,7 +158,7 @@ define([ 'easejs' ], function(easejs) { * @returns {int} */ 'public size' : function() { - return this.counter; + return this.items.length; }, /** @@ -180,7 +169,7 @@ define([ 'easejs' ], function(easejs) { * @returns {boolean} */ 'public isEmpty' : function() { - return this.counter == 0; + return this.size() == 0; }, /** @@ -191,7 +180,6 @@ define([ 'easejs' ], function(easejs) { */ 'public clear' : function() { this.items = []; - this.counter = 0; } }); diff --git a/js/modules/aggregator/aggregator.js b/js/modules/aggregator/aggregator.js index 4eef945..3c44715 100644 --- a/js/modules/aggregator/aggregator.js +++ b/js/modules/aggregator/aggregator.js @@ -7,10 +7,10 @@ */ define(['easejs', 'MathUuid','widget', 'attributeType', 'attributeValue', 'attributeValueList', 'subscriber', - 'subscriberList', 'callbackList', 'storage', 'widgetDescription', 'interpreter', 'attributeTypeList'], + 'subscriberList', 'callbackList', 'storage', 'widgetDescription', 'interpreter', 'attributeTypeList', 'interpretation'], function(easejs, MathUuid, Widget, AttributeType, AttributeValue, AttributeValueList, Subscriber, SubscriberList, - CallbackList, Storage, WidgetDescription, Interpreter, AttributeTypeList){ + CallbackList, Storage, WidgetDescription, Interpreter, AttributeTypeList, Interpretation){ var Class = easejs.Class; var Aggregator = Class('Aggregator'). @@ -44,14 +44,10 @@ define(['easejs', 'MathUuid','widget', */ 'protected widgets' : [], - /** - * @alias interpreters - * @protected - * @type {Array} - * @memberof Aggregator# - * @desc List of subscribed interpreters referenced by ID. - */ - 'protected interpreters' : [], + /** + * @type {Array.<Interpretation>} + */ + 'protected interpretations' : [], /** * @alias db @@ -85,7 +81,7 @@ define(['easejs', 'MathUuid','widget', { this.id = Math.uuid(); this.widgets = []; - this.interpreters = []; + this.interpretations = []; this.__super(_discoverer, _attributeTypes); }, @@ -111,11 +107,11 @@ define(['easejs', 'MathUuid','widget', * @memberof Aggregator# * @param {AttributeType} _attributeType attributeType */ - 'protected addAttributeType' : function(_attributeType){ + 'protected addAttributeType' : function(_attributeType, _multipleInstances){ if(Class.isA( AttributeType, _attributeType )){ - this.attributeTypes.put(_attributeType); + this.attributeTypes.put(_attributeType, _multipleInstances); var attVal = new AttributeValue().buildFromAttributeType(_attributeType); - this.attributes.put(attVal); + this.attributes.put(attVal, _multipleInstances); } }, @@ -303,30 +299,6 @@ define(['easejs', 'MathUuid','widget', */ 'virtual protected setAggregatorCallbacks' : function() { - }, - - /** - * Adds an interpreter to the aggregator. - * - * @public - * @alias addInterpreter - * @memberof Aggregator# - * @param _theInterpreter - */ - 'public addInterpreter': function(_theInterpreter) { - this.interpreters.push(_theInterpreter.getId()); - }, - - /** - * Returns an array with the UUIDs of the interpreters that where added to the aggregator. - * - * @public - * @alias getInterpreters - * @memberof Aggregator# - * @returns {Array} The UUIDs of the connected interpreters. - */ - 'public getInterpreters': function() { - return this.interpreters; }, /** @@ -475,44 +447,15 @@ define(['easejs', 'MathUuid','widget', * @alias interpretData * @memberof Aggregator# * @param {String} _interpreterId ID of the searched Interpreter - * @param {(AttributeValueList|Array)} _data data that should be interpreted * @param {?function} _function for additional actions, if an asynchronous function is used */ - 'public interpretData' : function(_interpreterId, _function){ + 'public interpretData' : function(_interpreterId, _inAttributeValues, _outAttributeValues, _function){ var interpreter = this.discoverer.getComponent(_interpreterId); if (Class.isA(Interpreter, interpreter)) { - interpreter.callInterpreter(this.getAttributeValues(interpreter.getInAttributeTypes()), _function); + interpreter.callInterpreter(_inAttributeValues, _outAttributeValues, _function); } }, - /** - * Calls the given Interpreter for getting the data. - * - * @public - * @alias getInterpretedData - * @memberof Aggregator# - * @param {String} _interpreterId ID of the searched Interpreter - * @returns {?AttributeValueList} - */ - 'public getInterpretedData' : function(_interpreterId){ - var response = 'undefined'; - var interpreter = this.discoverer.getComponent(_interpreterId); - if (interpreter) { - response = interpreter.getInterpretedData(); - var attributeList = response.getOutAttributes().getItems(); - for (var i in attributeList) { - var theAttribute = attributeList[i]; - if (Class.isA(AttributeValue, theAttribute) && this.isAttribute(theAttribute)) { - this.addAttribute(theAttribute); - if(this.db){ - this.store(theAttribute); - } - } - } - } - return response; - }, - /** * Initializes the database with the specified name. * @@ -619,7 +562,14 @@ define(['easejs', 'MathUuid','widget', * @returns {Array.<T>} The UUIDs. */ 'private getComponentUUIDs': function() { - return this.widgets.concat(this.interpreters); + var uuids = []; + uuids = uuids.concat(this.widgets); + for (var index in this.interpretations) { + var theInterpretation = this.interpretations[index]; + uuids.push(theInterpretation.interpreterId); + + } + return uuids; }, /** @@ -692,8 +642,8 @@ define(['easejs', 'MathUuid','widget', var widgetOutAttribute = outAttributes[widgetOutAttributeIndex]; // add the attribute type to the aggregators list of handled attribute types if (!this.getAttributeTypes().contains(widgetOutAttribute)) this.addAttributeType(widgetOutAttribute); - console.log("I can now satisfy attribute "+widgetOutAttribute.getIdentifier()+" with the help of "+theComponent.getName()+"! That was easy :)"); - _unsatisfiedAttributes.removeItem(widgetOutAttribute.getIdentifier()); + console.log("I can now satisfy attribute "+widgetOutAttribute+" with the help of "+theComponent.getName()+"! That was easy :)"); + _unsatisfiedAttributes.removeAttributeType(widgetOutAttribute); } } else if (Class.isA(Interpreter, theComponent)) { // if the component is an interpreter and all its in attributes can be satisfied, add the interpreter console.log("It's an interpreter."); @@ -705,39 +655,44 @@ define(['easejs', 'MathUuid','widget', for (var inAttributeIdentifier in inAttributes) { // get the attribute var theInAttribute = inAttributes[inAttributeIdentifier]; - console.log("The interpreter needs the attribute "+theInAttribute.getIdentifier()+"."); + console.log("The interpreter needs the attribute "+theInAttribute+"."); // if required attribute is not already satisfied by the aggregator search for components that do if (!this.doesSatisfyAttributeType(theInAttribute)) { - console.log("It seems that I can't satisfy "+theInAttribute.getIdentifier()+", but I will search for components that can."); + console.log("It seems that I can't satisfy "+theInAttribute+", but I will search for components that can."); var newAttributeList = new AttributeTypeList(); newAttributeList.put(theInAttribute); this.getComponentsForUnsatisfiedAttributeTypes(newAttributeList, false, [Widget, Interpreter]); // if the attribute still can't be satisfied drop the interpreter if (!this.doesSatisfyAttributeType(theInAttribute)) { - console.log("I couldn't find a component to satisfy "+theInAttribute.getIdentifier()+". Dropping interpreter "+theComponent.getName()+". Bye bye."); + console.log("I couldn't find a component to satisfy "+theInAttribute+". Dropping interpreter "+theComponent.getName()+". Bye bye."); canSatisfyInAttributes = false; break; } } else { - console.log("It seems that I already satisfy the attribute "+theInAttribute.getIdentifier()+". Let's move on."); + console.log("It seems that I already satisfy the attribute "+theInAttribute+". Let's move on."); } } if (canSatisfyInAttributes) { - this.addInterpreter(theComponent); // remove satisfied attribute for (var interpreterOutAttributeIndex in outAttributes) { var interpreterOutAttribute = outAttributes[interpreterOutAttributeIndex]; // add the attribute type to the aggregators list of handled attribute types - if (!this.getAttributeTypes().contains(interpreterOutAttribute)) this.addAttributeType(interpreterOutAttribute); - console.log("I can now satisfy attribute "+interpreterOutAttribute.getIdentifier()+" with the help of "+theComponent.getName()+"! Great!"); - _unsatisfiedAttributes.removeItem(interpreterOutAttribute.getIdentifier()); + for (var unsatisfiedAttributeIndex in _unsatisfiedAttributes.getItems()) { + var theUnsatisfiedAttribute = _unsatisfiedAttributes.getItems()[unsatisfiedAttributeIndex]; + if (theUnsatisfiedAttribute.equals(interpreterOutAttribute)) { + this.addAttributeType(theUnsatisfiedAttribute); + console.log("I can now satisfy attribute "+theUnsatisfiedAttribute+" with the help of "+theComponent.getName()+"! Great!"); + this.interpretations.push(new Interpretation(theComponent.getId(), theComponent.getInAttributeTypes(), new AttributeTypeList().withItems([theUnsatisfiedAttribute]))); + } + } + _unsatisfiedAttributes.removeAttributeType(interpreterOutAttribute, true); } - } else { + } else { console.log("Found interpreter but can't satisfy required attributes."); for (var j in theComponent.getDescription().getInAttributeTypes().getItems()) { - console.log("Missing "+theComponent.getDescription().getInAttributeTypes().getItems()[j].getIdentifier()+"."); + console.log("Missing "+theComponent.getDescription().getInAttributeTypes().getItems()[j]+"."); } } } @@ -763,8 +718,9 @@ define(['easejs', 'MathUuid','widget', // get all interpreters that satisfy attribute types this.getComponentsForUnsatisfiedAttributeTypes(unsatisfiedAttributes, false, [Interpreter]); - //console.log(unsatisfiedAttributes); - //console.log(this.attributeTypes); + console.log("Unsatisfied attributes: "+unsatisfiedAttributes.size()); + console.log("Satisfied attributes: "+this.getAttributeTypes().size()); + console.log("Interpretations "+this.interpretations.length); }, /** @@ -811,26 +767,36 @@ define(['easejs', 'MathUuid','widget', var self = this; var completedQueriesCounter = 0; - if (this.interpreters.length > 0) { - for(var index in this.interpreters) { - var theInterpreterId = this.interpreters[index]; - - self.interpretData(theInterpreterId, function() { - self.getInterpretedData(theInterpreterId); + if (this.interpretations.length > 0) { + for (var index in this.interpretations) { + var theInterpretation = this.interpretations[index]; + var theInterpreterId = theInterpretation.interpreterId; + var interpretationInAttributeValues = this.getAttributeValues(theInterpretation.inAttributeTypes); + var interpretationOutAttributeValues = this.getAttributeValues(theInterpretation.outAttributeTypes); + + self.interpretData(theInterpreterId, interpretationInAttributeValues, interpretationOutAttributeValues, function(_interpretedData) { + for (var j in _interpretedData.getItems()) { + var theInterpretedData = _interpretedData.getItems()[j]; + + self.addAttribute(theInterpretedData); + if(self.db){ + self.store(theInterpretedData); + } + } - completedQueriesCounter++; - if (completedQueriesCounter == self.interpreters.length) { - if (_callback && typeof(_callback) == 'function') { - _callback(self.getAttributeValues()); - } - } - }); - } - } else { - if (_callback && typeof(_callback) == 'function') { - _callback(self.getAttributeValues()); - } - } + completedQueriesCounter++; + if (completedQueriesCounter == self.interpretations.length) { + if (_callback && typeof(_callback) == 'function') { + _callback(self.getAttributeValues()); + } + } + }); + } + } else { + if (_callback && typeof(_callback) == 'function') { + _callback(self.getAttributeValues()); + } + } }, /** diff --git a/js/modules/aggregator/interpretation.js b/js/modules/aggregator/interpretation.js new file mode 100644 index 0000000..b18b1f0 --- /dev/null +++ b/js/modules/aggregator/interpretation.js @@ -0,0 +1,22 @@ +/** + * Created by tobias on 15.04.15. + */ +define(['easejs', 'interpreter', 'attributeTypeList'], + function(easejs, Interpreter, AttributeTypeList) { + var Class = easejs.Class; + var Interpretation = Class('Interpretation', { + 'public interpreterId' : null, + 'public inAttributeTypes' : new AttributeTypeList(), + 'public outAttributeTypes' : new AttributeTypeList(), + + 'public __construct' : function(_interpreterId, _inAttributes, _outAttributes){ + this.interpreterId = _interpreterId; + this.inAttributeTypes = _inAttributes; + this.outAttributeTypes = _outAttributes; + } + + }); + + return Interpretation; + } +); \ No newline at end of file diff --git a/js/modules/aggregator/storage/storage.js b/js/modules/aggregator/storage/storage.js index 569909e..31d84c3 100644 --- a/js/modules/aggregator/storage/storage.js +++ b/js/modules/aggregator/storage/storage.js @@ -162,16 +162,16 @@ define(['easejs', 'attributeValue', 'attributeValueList', 'attributeType', 'private createTable' : function(_attribute, _function){ if(this.db){ var tableName = this.tableName(_attribute); - var statement = 'CREATE TABLE IF NOT EXISTS ' + tableName + ' (value_, type_, created_)'; + var statement = 'CREATE TABLE IF NOT EXISTS "' + tableName + '" (value_, type_, created_)'; + console.log('CREATE TABLE IF NOT EXISTS "' + tableName + '"'); if(_function && typeof(_function) == 'function'){ this.db.transaction(function(tx){tx.executeSql(statement);}, this.errorCB, _function); } else { this.db.transaction(function(tx){tx.executeSql(statement);}, this.errorCB, this.successCB); } - if(!this.attributeNames.indexOf(name) > -1){ + if(!this.attributeNames.indexOf(_attribute.getName()) > -1){ this.attributeNames.push(tableName); } - console.log('CREATE TABLE IF NOT EXISTS ' + tableName); } }, @@ -188,18 +188,17 @@ define(['easejs', 'attributeValue', 'attributeValueList', 'attributeType', 'private insertIntoTable' : function(_attributeValue, _function){ if(this.db && _attributeValue && Class.isA(AttributeValue, _attributeValue)){ var tableName = this.tableName(_attributeValue); - var statement = 'INSERT INTO ' + tableName - + ' (value_, type_, created_) VALUES ("' + var statement = 'INSERT INTO "' + tableName + + '" (value_, type_, created_) VALUES ("' + _attributeValue.getValue() + '", "' + _attributeValue.getType() + '", "' + _attributeValue.getTimestamp() + '")'; - + console.log('INSERT INTO "'+tableName+'" VALUES ('+_attributeValue.getValue()+", "+_attributeValue.getType()+", "+_attributeValue.getTimestamp()); if(_function && typeof(_function) == 'function'){ this.db.transaction(function(tx){tx.executeSql(statement);}, this.errorCB, _function); } else { this.db.transaction(function(tx){tx.executeSql(statement);}, this.errorCB, this.successCB); } - console.log('INSERT INTO '+tableName+' VALUES ('+_attributeValue.getValue()+", "+_attributeValue.getType()+", "+_attributeValue.getTimestamp()); } }, @@ -465,10 +464,8 @@ define(['easejs', 'attributeValue', 'attributeValueList', 'attributeType', if(self.data.size() == 0){ return; } - var keys = self.data.getKeys(); - for(var i in keys){ - var key = keys[i]; - var item = self.data.getItem(key); + for(var i in self.data.getItems()){ + var item = self.data.getItems()[i]; if(!self.tableExists(item)){ self.createTable(item, function(){self.insertIntoTable(item);}); } else { @@ -538,15 +535,7 @@ define(['easejs', 'attributeValue', 'attributeValueList', 'attributeType', * @returns{String} */ 'private tableName' : function(_attribute){ - var tableName = _attribute.getName(); - var parameterList = _attribute.getParameters(); - if(!parameterList.isEmpty()){ - var keys = parameterList.getKeys(); - for(var i in keys){ - tableName = tableName + '__' +keys[i] + '_'+parameterList.getItem(keys[i]); - } - } - return tableName; + return _attribute.getAttributeType().toString(); }, /** diff --git a/js/modules/attribute/attributeType.js b/js/modules/attribute/attributeType.js index 7dbb4e1..18f78b3 100644 --- a/js/modules/attribute/attributeType.js +++ b/js/modules/attribute/attributeType.js @@ -207,26 +207,18 @@ define(['easejs', * @param {AttributeType} _attributeType AttributeType that should be compared * @returns {boolean} */ - 'virtual public equals' : function(_attributeType) { - if(Class.isA(AttributeType, _attributeType)){ - if (this.getIdentifier() == _attributeType.getIdentifier()) { - return true; - } + 'virtual public equals' : function(_attributeType) { + if (Class.isA(AttributeType, _attributeType)) { + if (this.getName() == _attributeType.getName() && + this.getType() == _attributeType.getType() && + this.getParameters().equals(_attributeType.getParameters())) + return true; } return false; }, - /** - * Returns a string that describes the attribute type. - * - * @virtual - * @public - * @alias toString - * @memberof AttributeType# - * @returns {String} - */ - 'virtual public toString': function() { - return this.getIdentifier(); + 'public hasInputParameter': function() { + return this.hasParameters() && this.parameterList.hasInputParameter(); }, /** @@ -235,16 +227,19 @@ define(['easejs', * Format: (AttributeName:AttributeType)#[FirstParameterName:FirstParameterValue]… * * @public - * @alias getIdentifier + * @alias toString * @memberof AttributeType# * @returns {String} * @example (CI_USER_LOCATION_DISTANCE:FLOAT)#[CP_TARGET_LATITUDE:52][CP_TARGET_LONGITUDE:13][CP_UNIT:KILOMETERS] */ - 'public getIdentifier': function() { + 'virtual public toString': function() { var identifier = "("+this.name+":"+this.type+")"; if (this.hasParameters()) { identifier += "#"; - identifier += this.parameterList.getIdentifier(); + for (var index in this.parameterList.getItems()) { + var theParameter = this.parameterList.getItems()[index]; + identifier += theParameter.toString(); + } } return identifier; } diff --git a/js/modules/attribute/attributeTypeList.js b/js/modules/attribute/attributeTypeList.js index 54e5c77..d720139 100644 --- a/js/modules/attribute/attributeTypeList.js +++ b/js/modules/attribute/attributeTypeList.js @@ -16,14 +16,7 @@ define([ 'easejs', 'abstractList', 'attributeType', 'parameterList' ], * @requires AttributeType */ var AttributeTypeList = Class('AttributeTypeList').extend(AbstractList, { - /** - * @alias counter - * @protected - * @type {integer} - * @memberof AttributeTypeList# - * @desc Number of items. - */ - 'protected counter' : 0, + /** * @alias items * @protected @@ -31,7 +24,7 @@ define([ 'easejs', 'abstractList', 'attributeType', 'parameterList' ], * @memberof AttributeTypeList# * @desc ItemList */ - 'protected items' : {}, + 'protected items' : [], /** * Builder for item list. @@ -43,19 +36,13 @@ define([ 'easejs', 'abstractList', 'attributeType', 'parameterList' ], * @returns {AttributeTypeList} */ 'public withItems' : function(_attributeTypeList) { - var list = {}; + var list = []; if (_attributeTypeList instanceof Array) { list = _attributeTypeList; } else if (Class.isA(AttributeTypeList, _attributeTypeList)) { list = _attributeTypeList.getItems(); } - for ( var i in list) { - var attributeType = list[i]; - if (Class.isA(AttributeType, attributeType)) { - this.items[attributeType.getIdentifier()] = attributeType; - this.counter++; - } - } + this.items = list; return this; }, @@ -67,12 +54,12 @@ define([ 'easejs', 'abstractList', 'attributeType', 'parameterList' ], * @memberof AttributeTypeList# * @param {AttributeType} _attributeType AttributeType */ - 'public put' : function(_attributeType) { + 'public put' : function(_attributeType, _multipleInstances) { + var _multipleInstances = typeof _multipleInstances == "undefined" ? false : _multipleInstances; if (Class.isA(AttributeType, _attributeType)) { - if (!(this.containsKey(_attributeType.getIdentifier()))) { - this.counter++; + if (_multipleInstances || !(this.contains(_attributeType))) { + this.items.push(_attributeType); } - this.items[_attributeType.getIdentifier()] = _attributeType; } }, @@ -93,13 +80,7 @@ define([ 'easejs', 'abstractList', 'attributeType', 'parameterList' ], list = _attributeTypeList.getItems(); } for ( var i in list) { - var attributeType = list[i]; - if (Class.isA(AttributeType, attributeType)) { - if (!(this.containsKey(attributeType.getIdentifier()))) { - this.counter++; - } - this.items[attributeType.getIdentifier()] = attributeType; - } + this.put(list[i]); } }, @@ -115,15 +96,17 @@ define([ 'easejs', 'abstractList', 'attributeType', 'parameterList' ], */ 'public contains' : function(_item) { if (Class.isA(AttributeType, _item)) { - var tmp = this.getItem(_item.getIdentifier()); - if (!(typeof tmp === 'undefined') - && tmp.equals(_item)) { - return true; + for (var index in this.items) { + var tmp = this.items[index]; + if (tmp.equals(_item)) { + return true; + } } } return false; }, + /** * Compare the specified AttributeTypeList with this instance. * @@ -135,31 +118,15 @@ define([ 'easejs', 'abstractList', 'attributeType', 'parameterList' ], */ 'public equals' : function(_list) { if (Class.isA(AttributeTypeList, _list) && _list.size() == this.size()) { - var items = _list.getItems(); - for (var i in items) { - var item = items[i]; - if (!this.contains(item)) { - return false; - } + for (var index in _list.getItems()) { + var theAttributeType = _list.getItems()[index]; + if (!this.contains(theAttributeType)) return false; } return true; } return false; }, - /** - * Returns the attribute type that matches the provided identifier. - * @public - * @override - * @alias getItem - * @memberof AttributeTypeList# - * @param {string} _identifier The identifier that should be searched for. - * @returns {AttributeType} - */ - 'override public getItem' : function(_identifier) { - return this.items[_identifier]; - }, - /** * Creates a clone of the current list. * @@ -179,7 +146,35 @@ define([ 'easejs', 'abstractList', 'attributeType', 'parameterList' ], newList.put(newAttributeType); } return newList; - } + }, + + 'public removeAttributeType': function(_attributeType, _allOccurrences) { + _allOccurrences = typeof _allOccurrences == "undefined" ? false : _allOccurrences; + for (var index in this.items) { + var theAttributeType = this.items[index]; + if (theAttributeType.equals(_attributeType)) { + this.items.splice(index, 1); + } + } + if (_allOccurrences && this.contains(_attributeType)) this.removeAttributeType(_attributeType, _allOccurrences); + }, + + 'public hasAttributesWithInputParameters': function() { + for (var index in this.items) { + var theAttributeType = this.items[index]; + if (theAttributeType.hasInputParameter()) return true; + } + return false; + }, + + 'public getAttributesWithInputParameters': function() { + var list = new AttributeTypeList(); + for (var index in this.items) { + var theAttributeType = this.items[index]; + if (theAttributeType.hasInputParameter()) list.put(theAttributeType); + } + return list; + } }); return AttributeTypeList; diff --git a/js/modules/attribute/attributeValue.js b/js/modules/attribute/attributeValue.js index 667be39..9fa6abb 100644 --- a/js/modules/attribute/attributeValue.js +++ b/js/modules/attribute/attributeValue.js @@ -122,9 +122,8 @@ define([ 'easejs', 'attributeType' ], function(easejs, AttributeType) { */ 'override public equals' : function(_attributeValue) { if (Class.isA(AttributeValue, _attributeValue)) { - if (this.__super(_attributeValue.getAttributeType()) - && _attributeValue.getValue() == this - .getValue()) { + if (_attributeValue.getAttributeType().equals(this.getAttributeType()) && + _attributeValue.getValue() == this.getValue()) { return true; } } @@ -157,7 +156,7 @@ define([ 'easejs', 'attributeType' ], function(easejs, AttributeType) { 'public buildFromAttributeType' : function(_attributeType) { if (Class.isA(AttributeType, _attributeType)) { return new AttributeValue().withName(_attributeType.getName()) - .withType(_attributeType.getType()).withParameters(_attributeType.getParameters()).withValue('undefined'); + .withType(_attributeType.getType()).withParameters(_attributeType.getParameters()).withValue('NO_VALUE'); } return null; }, @@ -172,7 +171,7 @@ define([ 'easejs', 'attributeType' ], function(easejs, AttributeType) { * @returns {string} */ 'override public toString': function() { - return this.getIdentifier()+":"+this.getValue(); + return this.__super().toString()+":"+this.getValue(); } }); diff --git a/js/modules/attribute/attributeValueList.js b/js/modules/attribute/attributeValueList.js index ad83a4c..5280c6c 100644 --- a/js/modules/attribute/attributeValueList.js +++ b/js/modules/attribute/attributeValueList.js @@ -20,14 +20,7 @@ define(['easejs', 'abstractList', 'attributeValue', 'attributeType', 'attributeT * @requires AttributeTypeList) */ var AttributeValueList = Class('AttributeValueList').extend(AbstractList,{ - /** - * @alias counter - * @protected - * @type {integer} - * @memberof AttributeValueList# - * @desc Number of items. - */ - 'protected counter' : 0, + /** * @alias items * @protected @@ -35,7 +28,7 @@ define(['easejs', 'abstractList', 'attributeValue', 'attributeType', 'attributeT * @memberof AttributeValueList# * @desc ItemList. */ - 'protected items' : {}, + 'protected items' : [], /** * Builder for item list. @@ -54,13 +47,7 @@ define(['easejs', 'abstractList', 'attributeValue', 'attributeType', 'attributeT _attributeValueList)) { list = _attributeValueList.getItems(); } - for (var i in list) { - var attributeValue = list[i]; - if (Class.isA(AttributeValue, attributeValue)) { - this.items[attributeValue.getIdentifier()] = attributeValue; - this.counter++; - } - } + this.items = list; return this; }, @@ -72,12 +59,13 @@ define(['easejs', 'abstractList', 'attributeValue', 'attributeType', 'attributeT * @memberof AttributeValueList# * @param {AttributeValue} _attributeValue AttributeValue */ - 'public put' : function(_attributeValue) { + 'public put' : function(_attributeValue, _multipleInstances) { if (Class.isA(AttributeValue, _attributeValue)) { - if (!(this.containsKey(_attributeValue.getIdentifier()))) { - this.counter++; + if (_multipleInstances || !(this.containsAttributeType(_attributeValue.getAttributeType()))) { + this.items.push(_attributeValue); + } else { + this.updateValue(_attributeValue); } - this.items[_attributeValue.getIdentifier()] = _attributeValue; } }, @@ -97,14 +85,8 @@ define(['easejs', 'abstractList', 'attributeValue', 'attributeType', 'attributeT } else if (Class.isA(AttributeValueList, _attributeValueList)) { list = _attributeValueList.getItems(); } - for ( var i in list) { - var attributeValue = list[i]; - if (Class.isA(AttributeValue, attributeValue)) { - if (!(this.containsKey(attributeValue.getIdentifier()))) { - this.counter++; - } - this.items[attributeValue.getIdentifier()] = attributeValue; - } + for (var i in list) { + this.put(list[i]); } }, @@ -115,14 +97,28 @@ define(['easejs', 'abstractList', 'attributeValue', 'attributeType', 'attributeT * @public * @alias contains * @memberof AttributeValueList# - * @param {AttributeValue} _item AttributeValue that should be verified. + * @param {AttributeValue} _attributeValue AttributeValue that should be verified. * @returns {boolean} */ - 'public contains' : function(_item) { - if (Class.isA(AttributeValue, _item)) { - var tmp = this.getItem(_item.getIdentifier()); - if (!(typeof tmp === 'undefined') && tmp.equals(_item)) { - return true; + 'public contains' : function(_attributeValue) { + if (Class.isA(AttributeValue, _attributeValue)) { + for (var index in this.items) { + var tmp = this.items[index]; + if (tmp.equals(_attributeValue)) { + return true; + } + } + } + return false; + }, + + 'public containsAttributeType': function(_attributeType) { + if (Class.isA(AttributeType, _attributeType)) { + for (var index in this.items) { + var tmp = this.items[index].getAttributeType(); + if (tmp.equals(_attributeType)) { + return true; + } } } return false; @@ -140,12 +136,9 @@ define(['easejs', 'abstractList', 'attributeValue', 'attributeType', 'attributeT */ 'public equals' : function(_list) { if (Class.isA(AttributeValueList, _list) && _list.size() == this.size()) { - var items = _list.getItems(); - for ( var i in items) { - var item = items[i]; - if (!this.contains(item)) { - return false; - } + for (var index in _list.getItems()) { + var theAttributeValue = _list.getItems()[index]; + if (!this.contains(theAttributeValue)) return false; } return true; } @@ -173,7 +166,7 @@ define(['easejs', 'abstractList', 'attributeValue', 'attributeType', 'attributeT for ( var i in list) { var attributeType = list[i]; if (Class.isA(AttributeType, attributeType)) { - var attribute = this.items[attributeType.getIdentifier()]; + var attribute = this.getValueObjectForAttributeType(attributeType); if (typeof attribute != "undefined") { response.put(attribute); } @@ -202,25 +195,12 @@ define(['easejs', 'abstractList', 'attributeValue', 'attributeType', 'attributeT for (var i in list) { var attributeType = list[i]; if (Class.isA(AttributeType, attributeType)) { - response.removeItem(attributeType.getIdentifier()); + response.removeAttributeValuesWithAttributeType(attributeType); } } return response; }, - /** - * Alias for {#getItem}. - * - * @public - * @alias getValue - * @memberof AttributeValueList# - * @param _key The value key. - * @returns {*} - */ - 'public getAttributeValue': function(_key) { - return this.getItem(_key); - }, - /** * Returns the attribute value that matches the provided attribute type. * @@ -231,9 +211,29 @@ define(['easejs', 'abstractList', 'attributeValue', 'attributeType', 'attributeT * @returns {AttributeValue} */ 'public getValueForAttributeType': function(_attributeType) { - return this.getAttributeValue(_attributeType.getIdentifier()).getValue(); - } + return this.getValueObjectForAttributeType(_attributeType).getValue(); + }, + + 'public getValueObjectForAttributeType': function(_attributeType) { + for (var index in this.getItems()) { + var theAttributeValue = this.getItems()[index]; + if (theAttributeValue.getAttributeType().equals(_attributeType)) return theAttributeValue; + } + }, + 'public removeAttributeValuesWithAttributeType': function(_attributeType) { + for (var index in this.items) { + var theAttributeValue = this.items[index]; + if (theAttributeValue.getAttributeType().equals(_attributeType)) this.items.splice(index, 1); + } + }, + + 'public updateValue': function(_attributeValue) { + for (var index in this.items) { + var theAttributeValue = this.items[index]; + if (theAttributeValue.getAttributeType().equals(_attributeValue.getAttributeType())) this.items[index] = _attributeValue; + } + } }); return AttributeValueList; diff --git a/js/modules/attribute/parameter.js b/js/modules/attribute/parameter.js index 0f18492..b780d1d 100644 --- a/js/modules/attribute/parameter.js +++ b/js/modules/attribute/parameter.js @@ -121,45 +121,31 @@ define(['easejs'], * @returns {boolean} */ 'public equals' : function(_parameter) { + var ignoreValue = false; if(Class.isA(Parameter, _parameter)){ - if(_parameter.getIdentifier() == this.getIdentifier()){ - return true; + if (_parameter.getValue() == "PV_INPUT" || this.getValue() == "PV_INPUT") { + return this.getKey() == _parameter.getKey(); + } else { + return this.getKey() == _parameter.getKey() && this.getValue() == _parameter.getValue(); } } return false; }, - /** - * Returns a string that describes the parameter. - * - * @public - * @override - * @alias toString - * @memberof Parameter# - * @returns {string} - */ - 'public toString': function() { - return this.getIdentifier(); - }, - /** * Returns an identifier that uniquely describes the parameter. * The identifier can be used to compare two parameters. * Format: [ParameterName:ParameterValue] * * @public - * @alias getIdentifier + * @alias toString * @memberof Parameter# * @returns {String} * @example [CP_UNIT:KILOMETERS] */ - 'public getIdentifier': function() { - if (this.value != "PV_INPUT") { - return "["+this.key+":"+this.value+"]"; - } else { - return "["+this.key+"]"; - } + 'public toString': function() { + return "["+this.key+":"+this.value+"]"; } }); diff --git a/js/modules/attribute/parameterList.js b/js/modules/attribute/parameterList.js index 8a1858e..a78daa1 100644 --- a/js/modules/attribute/parameterList.js +++ b/js/modules/attribute/parameterList.js @@ -44,18 +44,10 @@ define([ 'easejs', 'abstractList', 'parameter' ], * @returns {ParameterList} */ 'public withItems' : function(_parameterList) { - var list = []; if (_parameterList instanceof Array) { - list = _parameterList; + this.items = _parameterList; } else if (Class.isA(ParameterList, _parameterList)) { - list = _parameterList.getItems(); - } - for ( var i in list) { - var parameter = list[i]; - if (Class.isA(Parameter, parameter)) { - this.items[parameter.getKey()] = parameter.getValue(); - this.counter++; - } + this.items = _parameterList.getItems(); } return this; }, @@ -70,11 +62,9 @@ define([ 'easejs', 'abstractList', 'parameter' ], */ 'public put' : function(_parameter) { if (Class.isA(Parameter, _parameter)) { - - if (!(this.containsKey(_parameter.getKey()))) { - this.counter++; + if (!(this.contains(_parameter))) { + this.items.push(_parameter); } - this.items[_parameter.getKey()] = _parameter.getValue(); } }, @@ -93,14 +83,8 @@ define([ 'easejs', 'abstractList', 'parameter' ], } else if (Class.isA(ParameterList, _parameterList)) { list = _parameterList.getItems(); } - for ( var i in list) { - var parameter = list[i]; - if (Class.isA(Parameter, parameter)) { - if (!(this.containsKey(parameter.getKey()))) { - this.counter++; - } - this.items[parameter.getKey()] = parameter.getValue(); - } + for (var i in list) { + this.put(list[i]); } }, @@ -110,16 +94,16 @@ define([ 'easejs', 'abstractList', 'parameter' ], * @public * @alias contains * @memberof ParameterList# - * @param {Parameter} - * _item Parameter that should be - * verified + * @param {Parameter} _item Parameter that should be verified * @returns {boolean} */ 'public contains' : function(_item) { if (Class.isA(Parameter, _item)) { - var tmp = this.getItem(_item.getKey()); - if (tmp === _item.getValue()) { - return true; + for (var index in this.items) { + var tmp = this.items[index]; + if (tmp.equals(_item)) { + return true; + } } } return false; @@ -131,37 +115,20 @@ define([ 'easejs', 'abstractList', 'parameter' ], * @public * @alias equals * @memberof ParameterList# - * @param {ParameterList} _list ParameterList that should be compared + * @param {ParameterList} _parameterList ParameterList that should be compared * @returns {boolean} */ 'public equals' : function(_parameterList) { if (Class.isA(ParameterList, _parameterList) && _parameterList.size() == this.size()) { - for (var index in _parameterList.getItems()) { - var theParameter = _parameterList.getItems()[index]; - if (!this.contains(theParameter)) return false; - } + for (var index in _parameterList.getItems()) { + var theParameter = _parameterList.getItems()[index]; + if (!this.contains(theParameter)) return false; + } return true; } return false; }, - /** - * Returns all items as parameter objects. - * @public - * @alias getItems - * @memberof ParameterList# - * @returns {Array<Parameter>} - */ - 'override public getItems' : function() { - var parameters = []; - for (var key in this.items) { - var parameter = new Parameter().withKey(key) - .withValue(this.items[key]); - parameters.push(parameter); - } - return parameters; - }, - /** * Returns the objects of the list as JSON objects. * @@ -173,34 +140,19 @@ define([ 'easejs', 'abstractList', 'parameter' ], 'public getItemsAsJson': function() { var parameters = {}; for (var key in this.items) { - parameters[key] = this.items[key]; + var theParameter = this.items[key]; + parameters[theParameter.getKey()] = theParameter.getValue(); } return parameters; }, - /** - * Returns an identifier of all the parameters in the list. - * The identifier can be used to compare two parameter lists. <br/> - * Format: [FirstParameterName:FirstParameterValue][SecondParameterName:SecondParameterValue]… - * - * @public - * @alias getIdentifier - * @memberof ParameterList# - * @returns {String} - * @example [CP_TARGET_LATITUDE:52][CP_TARGET_LONGITUDE:13][CP_UNIT:KILOMETERS] - */ - 'public getIdentifier': function() { - var identifier = ""; - for (var key in this.items) { - var value = this.items[key]; - if (value != "PV_INPUT") { - identifier += "["+key+":"+value+"]"; - } else { - identifier += "["+key+"]"; - } - } - return identifier; - } + 'public hasInputParameter': function() { + for (var index in this.items) { + var theParameter = this.items[index]; + if (theParameter.getValue() == "PV_INPUT") return true; + } + return false; + } }); return ParameterList; diff --git a/js/modules/interpreter/interpreter.js b/js/modules/interpreter/interpreter.js index 9d036a4..d0aa7e0 100644 --- a/js/modules/interpreter/interpreter.js +++ b/js/modules/interpreter/interpreter.js @@ -249,7 +249,7 @@ define([ 'easejs', 'MathUuid', 'attributeType', 'attributeTypeList', * @param {string} _name name of the attribute * @param {string} _type type of the attribute * @param {string} _value value of the attribute - * @param {ParameterList|Array} _parameter Parameter of the attribute. + * @param {ParameterList|Array} _parameters Parameter of the attribute. */ 'protected setOutAttribute' : function(_name, _type, _value,_parameters) { var attributeValue = new AttributeValue().withName(_name) @@ -278,17 +278,13 @@ define([ 'easejs', 'MathUuid', 'attributeType', 'attributeTypeList', * @public * @alias callInterpreter * @memberof Interpreter# - * @param {AttributeValueList} _dataToInterpret Data that should be interpreted. + * @param {AttributeValueList} _inAttributeValues Data that should be interpreted. * @param {?function} _function For additional actions, if an asynchronous function is used. */ - 'public callInterpreter' : function(_dataToInterpret, _function) { - if (_dataToInterpret && this.canHandle(_dataToInterpret)) { - if(_function && typeof(_function) == 'function'){ - this.interpretData(_dataToInterpret, _function); - } else { - this.interpretData(_dataToInterpret); - } - this.setInAttributeValues(_dataToInterpret); + 'public callInterpreter' : function(_inAttributeValues, _outAttributeValues, _function) { + if (_inAttributeValues && this.canHandle(_inAttributeValues)) { + this.interpretData(_inAttributeValues, _outAttributeValues, _function); + this.setInAttributeValues(_inAttributeValues); this.lastInterpretation = new Date(); } else { var list = this.outAttributeTypes.getItems(); @@ -321,7 +317,7 @@ define([ 'easejs', 'MathUuid', 'attributeType', 'attributeTypeList', * @param {AttributeValueList} _inAtts Data that should be verified. */ 'protected canHandle' : function(_inAtts) { - var list = [] + var list = []; if (_inAtts instanceof Array) { list = _inAtts; } else if (Class.isA(AttributeValueList, _inAtts)) { @@ -339,21 +335,6 @@ define([ 'easejs', 'MathUuid', 'attributeType', 'attributeTypeList', return true; }, - /** - * Returns the interpreted data. - * - * @protected - * @alias getInterpretedData - * @memberof Interpreter# - * @returns {AttributeValueList} - */ - 'public getInterpretedData' : function() { - var result = new InterpreterResult().withTimestamp(this.lastInterpretation). - withInAttributes(this.inAttributeValues). - withOutAttributes(this.outAttributeValues); - return result; - }, - /** * Returns the time of the last interpretation. * @@ -408,6 +389,18 @@ define([ 'easejs', 'MathUuid', 'attributeType', 'attributeTypeList', this.discoverer.registerNewComponent(this); } + }, + + /** + * + * @returns {boolean} + */ + 'public hasOutAttributesWithInputParameters': function() { + return this.outAttributeTypes.hasAttributesWithInputParameters(); + }, + + 'public getOutAttributesWithInputParameters': function() { + return this.outAttributeTypes.getAttributesWithInputParameters(); } // /** diff --git a/js/modules/interpreter/interpreterResult.js b/js/modules/interpreter/interpreterResult.js index 3ec7fef..8c71a09 100644 --- a/js/modules/interpreter/interpreterResult.js +++ b/js/modules/interpreter/interpreterResult.js @@ -134,12 +134,12 @@ define(['easejs', 'attributeValueList'], * @public * @alias setTimestamp * @memberof InterpreterResult# - * @param {date} _timstamp interpretation time + * @param {date} _timestamp interpretation time */ - 'public setTimestamp' : function(_timesstamp){ - if(_timesstamp instanceof Date){ - this.type = _timesstamp; - }; + 'public setTimestamp' : function(_timestamp){ + if(_timestamp instanceof Date){ + this.type = _timestamp; + } }, /** @@ -154,10 +154,10 @@ define(['easejs', 'attributeValueList'], if (_outAttributes instanceof Array) { for(var i in _outAttributes){ this.outAttributes.put(_outAttributes[i]); - }; + } } else if (Class.isA(AttributeValueList, _outAttributes)) { this.outAttributes = _outAttributes; - }; + } }, /** @@ -172,10 +172,10 @@ define(['easejs', 'attributeValueList'], if (_inAttributes instanceof Array) { for(var i in _outAttributes){ this.inAttributes.put(_inAttributes[i]); - }; + } } else if (Class.isA(AttributeValueList, _inAttributes)) { this.inAttributes = _inAttributes; - }; + } } }); diff --git a/js/modules/widget/widget.js b/js/modules/widget/widget.js index e7a320a..b63b0a6 100644 --- a/js/modules/widget/widget.js +++ b/js/modules/widget/widget.js @@ -221,7 +221,7 @@ define([ 'easejs', 'MathUuid', 'callback', 'callbackList', 'attributeType', * @returns {*} */ 'public getAttributeValue': function(_attributeType) { - return this.getAttributeValues().getItem(_attributeType.getIdentifier()).getValue(); + return this.getAttributeValues().getItemForAttributeType(_attributeType).getValue(); }, /** @@ -680,7 +680,7 @@ define([ 'easejs', 'MathUuid', 'callback', 'callbackList', 'attributeType', * */ 'virtual public putData' : function(_data) { - var list = new Array(); + var list = []; if (_data instanceof Array) { list = _data; } else if (Class.isA(AttributeValueList, _data)) { -- GitLab