«IMPORT messageDsl» «EXTENSION ch::ethz::origo::aranea::validation::MessageDslExtensions» «REM» Template of the message factory. Note that we treat projects called "Aranea" special, as this is the root-project that all client-applications inherit from. To single out a project like this is unusual in generated code, but it allows us in Aranea to directly use the generated A_MESSAGE_FACTORY class without any manual merging. These special blocks are wrapped inside "«IF this.name == "Aranea"»" tags. «ENDREM» «DEFINE factoryTemplate FOR Project» «FILE prefix.toLowerCase() + "message_factory.e"-» note description: "[ Factory to create message objects. GENERATED DO NOT MODIFY! ]" author: "Origo Team " date: "$Date: $" revision: "$Revision: $" class «prefix.toUpperCase()»MESSAGE_FACTORY inherit «IF this.name != "Aranea"-» A_MESSAGE_FACTORY rename «REM» This is a workaround, as currently Aranea's system-namespace clashes with Origo's.«ELSE» TODO: we need proper namespaces! «ENDREM -» system_namespace as aranea_system_namespace redefine register_builtin_handlers end «ENDIF-» «FOREACH namespaces AS namespace-» «IF !namespace.ignore -» «(prefix + namespace.name.camelCaseToUnderscore() + "_constants").toUpperCase()» «ENDIF-» «ENDFOREACH-» «IF this.name == "Aranea"-» A_CONSTANTS A_GENERAL_CONSTANTS A_SYSTEM_CONSTANTS «ENDIF-» create make «IF this.name == "Aranea" -» «REM»This is the root message-factory: add some special routines«ENDREM» feature {NONE} -- Initialization make is -- Initialize. do create handler_registry.make (5) register_builtin_handlers end feature -- Status is_error: BOOLEAN -- Was there an error? feature -- Update register_handler (a_namespace, a_type: STRING; a_handler: like handler_type) is -- Register a handler. require a_namespace_ok: a_namespace /= Void and then not a_namespace.is_empty a_type_ok: a_type /= Void and then not a_type.is_empty a_handler_ok: a_handler /= Void local l_hnd: HASH_TABLE [like handler_type, STRING] do l_hnd := handler_registry.item (a_namespace) if l_hnd = Void then create l_hnd.make (5) handler_registry.force (l_hnd, a_namespace) end l_hnd.force (a_handler, a_type) end feature -- Basic operation new_message_from_ems_message (a_msg: MESSAGE): A_MESSAGE is -- Create a new aranea message from an ems MAP_MESSAGE. require valid_ems_message: is_valid_ems_message (a_msg) local l_hnd: HASH_TABLE [like handler_type, STRING] do if not is_error then -- check if we have a handler to get a specific message l_hnd := handler_registry.item (a_msg.get_string_property (namespace_element)) if l_hnd /= Void then if l_hnd.has_key (a_msg.get_string_property (type_element)) then Result := l_hnd.found_item.item ([a_msg]) end end if Result = Void then -- just generate a generic message create {A_GENERIC_MESSAGE} Result.make_from_ems_message (a_msg) end end ensure Error_or_not_void: not is_error implies Result /= Void end feature -- Validity is_valid_ems_message (a_msg: MESSAGE): BOOLEAN is -- Is a_msg a valid ems message? do Result := a_msg /= Void and then a_msg.get_string_property (namespace_element) /= Void and a_msg.get_string_property (type_element) /= Void end «ENDIF -» feature -- Creator handlers «IF this.name == "Aranea" -» new_general_status (a_msg: MESSAGE): A_GENERAL_STATUS_MESSAGE is -- Create new general status message out of a_msg. require a_msg_not_void: a_msg /= Void do create Result.make_from_ems_message (a_msg) ensure Error_or_not_void: not is_error implies Result /= Void end new_general_string (a_msg: MESSAGE): A_GENERAL_STRING_MESSAGE is -- Create new general string message out of a_msg. require a_msg_not_void: a_msg /= Void do create Result.make_from_ems_message (a_msg) ensure Error_or_not_void: not is_error implies Result /= Void end «ENDIF -» «FOREACH namespaces AS namespace-» «IF !namespace.ignore -» «FOREACH namespace.messages AS message-» new_«(namespace.name.camelCaseToUnderscore() + "_" + message.name.camelCaseToUnderscore()).toLowerCase()» (a_msg: MESSAGE): «message.fullName().toUpperCase()»_MESSAGE -- Create new typed message out of a_msg. require a_msg_not_void: a_msg /= Void do create Result.make_from_ems_message (a_msg) ensure Error_or_not_void: not is_error implies Result /= Void end «ENDFOREACH-» «ENDIF-» «ENDFOREACH-» feature {NONE} -- Implementation «IF this.name == "Aranea" -» handler_registry: HASH_TABLE [HASH_TABLE [like handler_type, STRING], STRING] -- Handlers to create specific messages. «ENDIF» register_builtin_handlers -- Register the builtin handlers. do «IF this.name == "Aranea" -» register_handler (general_namespace, general_status_type, agent new_general_status) register_handler (general_namespace, general_string_type, agent new_general_string) «ELSE» Precursor «ENDIF-» «FOREACH namespaces AS namespace-» «IF !namespace.ignore -» «FOREACH namespace.messages AS message-» register_handler («namespace.name.camelCaseToUnderscore().toLowerCase()»_namespace, «(namespace.name.camelCaseToUnderscore() + "_" + message.name.camelCaseToUnderscore()).toLowerCase()»_type, agent new_«(namespace.name.camelCaseToUnderscore() + "_" + message.name.camelCaseToUnderscore()).toLowerCase()») «ENDFOREACH-» «ENDIF» «ENDFOREACH-» end «IF this.name == "Aranea" -» feature {NONE} -- Type anchors handler_type: FUNCTION [ANY, TUPLE [MESSAGE], A_MESSAGE] -- Type of the message creators, indexed by namespace and type. invariant handler_registry_not_void: handler_registry /= Void «ENDIF-» end «ENDFILE» «ENDDEFINE»