def create(self, message=None): if message is not None: self.actor = message.getActor() self.verb = message.getVerb() self.object = message.getObject() self.result = serializeObject(message.getResult()) self.speechAct = message.getSpeechAct() self.context = serializeObject(message.getContext()) self.timestamp = message.getTimestamp()
def _changeName(self, newName, data): if newName is None: newName = "" if (data is not None) and (newName == "" or not self.hasName(newName)): key = data.key name = self.getName(key) # Update any internal naming data if data.data_type == SERIALIZABLE_DATA_TYPE: value = data.value value = nativizeObject(value, None, JSON_FORMAT) if isinstance(value, NamedSerializable): if newName == "": value.setName(None) else: value.setName(newName) value = serializeObject(value, JSON_FORMAT) data.value = value # Update the storage service data data.name = newName if name is not None: del self.name_map[name] if newName != "": self.name_map[newName] = key isChanged = True else: isChanged = False return isChanged, data
def processStorageInform(self, bucket, verb, key=None, value=None, tags=None, aType=None, allowCreate=None, name=None, description=None, dataType=None): if verb == self.VALUE_VERB: logWarning("IS SETTING", value) if isinstance(value, SuperGlu_Serializable): logWarning("IS SERIALIZABLE") dataType = SERIALIZABLE_DATA_TYPE if key is None: key = value.getId() elif key != value.getId(): logWarning('BAD KEY for Storage (%s != %s)' % (key, value.getId())) return False if isinstance(value, NamedSerializable): if name is None: name = value.getName() elif name != value.getName(): logWarning('BAD NAME for Storage(%s != %s)' % (name, value.getName())) return False value = serializeObject(value, JSON_FORMAT) return bucket.setValue(key, value, name, description, tags, aType, dataType, True, allowCreate) elif verb == self.VOID_VERB: return bucket.delValue(key, name)
def _changeName(self, newName, data): if newName is None: newName = '' if ((data is not None) and (newName == '' or not self.hasName(newName))): key = data.key name = self.getName(key) # Update any internal naming data if data.data_type == SERIALIZABLE_DATA_TYPE: value = data.value value = nativizeObject(value, None, JSON_FORMAT) if isinstance(value, NamedSerializable): if newName == '': value.setName(None) else: value.setName(newName) value = serializeObject(value, JSON_FORMAT) data.value = value # Update the storage service data data.name = newName if (name is not None): del self.name_map[name] if newName != '': self.name_map[newName] = key isChanged = True else: isChanged = False return isChanged, data
def processStorageInform( self, bucket, verb, key=None, value=None, tags=None, aType=None, allowCreate=None, name=None, description=None, dataType=None, ): if verb == self.VALUE_VERB: logWarning("IS SETTING", value) if isinstance(value, Serializable): logWarning("IS SERIALIZABLE") dataType = SERIALIZABLE_DATA_TYPE if key is None: key = value.getId() elif key != value.getId(): logWarning("BAD KEY for Storage (%s != %s)" % (key, value.getId())) return False if isinstance(value, NamedSerializable): if name is None: name = value.getName() elif name != value.getName(): logWarning("BAD NAME for Storage(%s != %s)" % (name, value.getName())) return False value = serializeObject(value, JSON_FORMAT) return bucket.setValue(key, value, name, description, tags, aType, dataType, True, allowCreate) elif verb == self.VOID_VERB: return bucket.delValue(key, name)
def testDataTransforms(self): st_1 = StorageToken(10, "TestProp", {"Prop11": 1, "Prop12": "Hello!"}) st_2 = StorageToken(11, "TestClass3", {"Prop21": 1, "Prop22": st_1}) obj1 = TestProp({"Prop11": 1, "Prop12": "Hello!"}) obj2 = TestClass3({"Prop21": 1, "Prop22": obj1}) dataStructures = [None, 1, 5.0, range(5), {"S":1,"R":[1,3,4], "5": "S"}, obj1, obj2] x = nativizeObject(serializeObject(obj1)) x = nativizeObject(serializeObject(obj2)) for aFormat in VALID_SERIAL_FORMATS: for x in dataStructures: self.assertEqual(x, x) s = serializeObject(x, aFormat) y = nativizeObject(s, None, aFormat) if isinstance(x, dict): self.assertItemsEqual(x, y) else: self.assertEqual(x, y)
def sendMessage(self, msg): if self.isMessageOnGatewayExternalBlackList(msg): return if self.isMessageOnGatewayExternalWhiteList(msg): MessagingGateway.sendMessage(self, msg) if self.m_connection is None: return False if isinstance(msg, Message): msgAsString = serializeObject(msg) headers = { self.MESSAGE_SYSTEM_NAME: self.SUPERGLU, self.MESSAGE_TYPE: "SUPERGLU_MSG" } self.m_connection.send(destination=self.TOPIC_LABEL + self.m_scope, body=msgAsString, headers=headers, content_type="text/plain") elif isinstance(msg, VHMessage): msgAsString = msg.getFirstWord() + " " + msg.getBody() headers = { "ELVISH_SCOPE": "DEFAULT_SCOPE", "MESSAGE_PREFIX": msg.getFirstWord(), "VHMSG_VERSION": "1.0.0.0", "VHMSG": "VHMSG" } self.m_connection.send(destination=self.TOPIC_LABEL + self.m_scope, body=msgAsString, headers=headers, content_type="text/plain") elif isinstance(msg, GIFTMessage): msgAsString = serializeObject(msg.getPayload()) headers = {self.MESSAGE_TYPE: "GIFT_MSG", "Encoding": 0} self.m_connection.send(destination=self.TOPIC_LABEL + self.m_scope, body=msgAsString, headers=headers, content_type="text/plain") return True
def _dumpLog(self, msg): incomingMsg = IncomingMessage(rawMessage=serializeObject(msg)) allMessages = incomingMsg.find_all() attrs = [log.rawMessage for log in allMessages] joinedMessage = "" #joinedMessage = joinedMessage.join(attrs) outMsg = Message("DBLoggingService", "Dump Logs", "To Client", joinedMessage) outMsg.setContextValue("sessionId", msg.getContextValue("sessionId", None)) outMsg.setContextValue("sid", msg.getContextValue("sid", None)) self.sendMessage(outMsg)
def queueAJAXMessage(self, msg): #logWarning("QUEUE MSG", msg.saveToSerialized()) try: sessionId = msg.getContextValue(SESSION_KEY, None) sid = msg.getContextValue("sid", None) msg = serializeObject(msg) self._messages.put((sid, sessionId, msg)) print("AJAX message queued") except Exception as err: print("AJAX message failed to queue") logError(err, stack=traceback.format_exc())
def sendMessage(self, msg): MessagingGateway.sendMessage(self, msg) if self.m_connection is None: return False msgAsString = serializeObject(msg) headers = {self.MESSAGE_SYSTEM_NAME: self.SUPERGLU} self.m_connection.send(destination=self.TOPIC_LABEL + self.m_scope, body=msgAsString, headers=headers, content_type="text/plain") return True
def _logMessage(self, msg): serializedMsg = serializeObject(msg) if len(serializedMsg) <= self._maxMsgSize and msg is not None: #incomingMsg = DBLoggedMessage(actor=msg.getActor(), verb=msg.getVerb(), object=msg.getObject(), result=msg.getResult(), speechAct=msg.getSpeechAct(), context=msg.getContext(), timestamp=msg.getTimestamp()) incomingMsg = DBLoggedMessage.convert(msg) incomingMsg.id = msg.getId() if msg.getVerb() != "Dump Logs" or msg.getVerb() != VALUE_VERB: incomingMsg.save() if msg.getVerb() == COMPLETED_VERB: #print(getKCsForUserAfterAGivenTime('p1', 'KC1',"2016-02-04T00:57:14.000Z")) #print(getAverageKCScoreAfterAGivenTime('p1', 'KC1', "2016-02-04T23:27:14.000Z")) #print(getTotalScoreForAGivenUserAndTask('p1', 'http://localhost:5533/QueryLogDebug.html?')) data = DBSession() data.task = 'http://localhost:5533/QueryLogDebug.html?' data.students = ['p1'] data.startTime = '2016-02-04T23:27:14.000Z' #performance = data.getFeedback(False) #print(performance) else: print("Message size too long for msg #: " + msg.getId())
def create(self, serializableObject=None): if serializableObject is not None: self.jsonString = serializeObject(serializableObject) self.classId = serializableObject.getClassId()
self._changeName(name, data) self.save() return StorageObject.objects, self.adaptor_map, self.name_map if __name__ == '__main__': # Backup to files if False: outDataFile = 'StorageServiceBackup.log' conn = connect("StorageService_DB") try: storageService = MongoStorageService(conn) outData = storageService.exportBucket('ONR') except Exception, exc: logWarning(exc) finally: conn.close() outStr = serializeObject(outData) with open(outDataFile, 'wb') as aFile: aFile.write(outStr) if False: conn = connect("StorageService_DB") try: storageService = MongoStorageService(conn) bucket = storageService.getBucket("ONR") bucket.resolveInconsistencies() #except Exception as exc: # logWarning("EXCEPT: ", exc) finally: conn.close()
def receiveMessage(self, msg): if ( (msg.getActor() == STORAGE_SERVICE_NAME) and (msg.getContextValue(self.BUCKET_KEY, None) is not None) and (self.hasBucket(msg.getContextValue(self.BUCKET_KEY))) ): bucket = self.getBucket(msg.getContextValue(self.BUCKET_KEY)) # Inform: Set some value(s) if msg.getSpeechAct() == INFORM_ACT: try: logWarning("INFORMING") value = self.processStorageInform( bucket, msg.getVerb(), msg.getObject(), msg.getResult(), msg.getContextValue(self.TAGS_KEY, None), msg.getContextValue(self.TYPE_KEY, None), msg.getContextValue(self.ALLOW_CREATE_KEY, True), msg.getContextValue(self.NAME_KEY, None), msg.getContextValue(self.DESCRIPTION_KEY, None), msg.getContextValue(self.DATA_TYPE_KEY, None), ) if value == True: response = self.makeConfirmMessage(msg) else: response = self.makeDisconfirmMessage(msg) except Exception: self.sendMessage(self.makeDisconfirmMessage(msg)) raise self.sendMessage(response) # Request: get some value(s) elif msg.getSpeechAct() == REQUEST_ACT: try: value = self.processStorageRequest( bucket, msg.getVerb(), msg.getObject(), msg.getContextValue(self.TAGS_KEY, None), msg.getContextValue(self.TYPE_KEY, None), msg.getContextValue(self.NAME_KEY, None), ) if msg.getVerb() == self.CONTAINS_VERB: response = self.makeReplyToContainsMessage(msg, value[0], value[1]) elif msg.getVerb() != INFORM_REF_ACT or value is None: response = self.makeRequestAnswerMessage(msg, value) else: response = self.makeReplyToInformRefMessage(msg, value[0], value[1]) except Exception: self.sendMessage(self.makeDisconfirmMessage(msg)) raise self.sendMessage(response) # Inform about the name of a value (rename) elif msg.getSpeechAct() == INFORM_REF_ACT: try: value = self.processStorageRename( bucket, msg.getVerb(), msg.getObject(), msg.getResult(), msg.getContextValue(self.NAME_KEY, None), ) if value == True: response = self.makeConfirmMessage(msg) else: response = self.makeDisconfirmMessage(msg) except Exception: self.sendMessage(self.makeDisconfirmMessage(msg)) raise self.sendMessage(response) else: logWarning("COULD NOT PROCESS (%s): %s" % (self.getId(), serializeObject(msg)))
def receiveMessage(self, msg): # TODO: Restore bucket management #if ((msg.getActor() == STORAGE_SERVICE_NAME) and # (msg.getContextValue(self.BUCKET_KEY, None) is not None) and # (self.hasBucket(msg.getContextValue(self.BUCKET_KEY)))): print("STORAGE RECEIVED MESSAGE: %s" % (msg, )) if (msg.getActor() == STORAGE_SERVICE_NAME): bucket = self.getBucket(msg.getContextValue(self.BUCKET_KEY)) # Inform: Set some value(s) if (msg.getSpeechAct() == INFORM_ACT): try: logWarning("INFORMING") value = self.processStorageInform( bucket, msg.getVerb(), msg.getObject(), msg.getResult(), msg.getContextValue(self.TAGS_KEY, None), msg.getContextValue(self.TYPE_KEY, None), msg.getContextValue(self.ALLOW_CREATE_KEY, True), msg.getContextValue(self.NAME_KEY, None), msg.getContextValue(self.DESCRIPTION_KEY, None), msg.getContextValue(self.DATA_TYPE_KEY, None)) if (value == True): response = self.makeConfirmMessage(msg) else: response = self.makeDisconfirmMessage(msg) except Exception: self.sendMessage(self.makeDisconfirmMessage(msg)) raise self.sendMessage(response) # Request: get some value(s) elif (msg.getSpeechAct() == REQUEST_ACT): try: value = self.processStorageRequest( bucket, msg.getVerb(), msg.getObject(), msg.getContextValue(self.TAGS_KEY, None), msg.getContextValue(self.TYPE_KEY, None), msg.getContextValue(self.NAME_KEY, None)) if msg.getVerb() == self.CONTAINS_VERB: response = self.makeReplyToContainsMessage( msg, value[0], value[1]) elif msg.getVerb() != INFORM_REF_ACT or value is None: response = self.makeRequestAnswerMessage(msg, value) else: response = self.makeReplyToInformRefMessage( msg, value[0], value[1]) except Exception: self.sendMessage(self.makeDisconfirmMessage(msg)) raise self.sendMessage(response) # Inform about the name of a value (rename) elif (msg.getSpeechAct() == INFORM_REF_ACT): try: value = self.processStorageRename( bucket, msg.getVerb(), msg.getObject(), msg.getResult(), msg.getContextValue(self.NAME_KEY, None)) if (value == True): response = self.makeConfirmMessage(msg) else: response = self.makeDisconfirmMessage(msg) except Exception: self.sendMessage(self.makeDisconfirmMessage(msg)) raise self.sendMessage(response) else: logWarning("COULD NOT PROCESS (%s): %s" % (self.getId(), serializeObject(msg)))
def messageToString(self, msg): return serializeObject(msg)
def testReadFile(self): launcher = ServiceLauncher() config = launcher.readConfigurationFromFile("tests/agentsConfig.json", None) print(serializeObject(config, JSON_STANDARD_FORMAT))
self._changeName(name, data) self.save() return StorageObject.objects, self.adaptor_map, self.name_map if __name__ == "__main__": # Backup to files if False: outDataFile = "StorageServiceBackup.log" conn = connect("StorageService_DB") try: storageService = MongoStorageService(conn) outData = storageService.exportBucket("ONR") except Exception, exc: logWarning(exc) finally: conn.close() outStr = serializeObject(outData) with open(outDataFile, "wb") as aFile: aFile.write(outStr) if False: conn = connect("StorageService_DB") try: storageService = MongoStorageService(conn) bucket = storageService.getBucket("ONR") bucket.resolveInconsistencies() # except Exception as exc: # logWarning("EXCEPT: ", exc) finally: conn.close()