Documentation/Modules/IModule: Unterschied zwischen den Versionen
Dirk (Diskussion | Beiträge) (Created page with "=Interface <tt>IModule</tt>: Guidelines for Writing Modules= All <tt>Modules</tt> in OpenOpal must implement the Interface <tt> IModule</tt>. The interface contains a set of met...") |
Admin (Diskussion | Beiträge) |
||
(16 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt) | |||
Zeile 1: | Zeile 1: | ||
− | =Interface <tt>IModule</tt> | + | =Interface <tt>IModule</tt>= |
− | All <tt>Modules</tt> in | + | All <tt>Modules</tt> in OpenDino must implement the Interface <tt> IModule</tt>. |
The interface contains a set of methods. The purpose and the functionality of these methods is described in the following sections. | The interface contains a set of methods. The purpose and the functionality of these methods is described in the following sections. | ||
− | It is essential for a correct program execution to ensure that the guidelines are exactly followed by the < | + | Furthermore, the <tt>Modules</tt> must have a class <tt>Options</tt> that comprises all user settings as simple types (<tt>boolean</tt>, <tt>long</tt>, <tt>double</tt>, <tt>String</tt>) or vectors/arrays of those types. The options are automatically read using Java Reflection API. |
+ | |||
+ | It is essential for a correct program execution to ensure that the guidelines are exactly followed by the <tt>Modules</tt> programmers. | ||
== Method <tt>Options getOptions()</tt> == | == Method <tt>Options getOptions()</tt> == | ||
# Functionality | # Functionality | ||
− | #* returns the current <tt>Options</tt>. | + | #* returns the current <tt>Options</tt> class. |
#* ensure that cloned <tt>Options</tt> are returned! | #* ensure that cloned <tt>Options</tt> are returned! | ||
# Objectives | # Objectives | ||
− | #* returning a clone of the <tt>Options</tt> ensures that changing <tt>Options</tt> outside the < | + | #* returning a clone of the <tt>Options</tt> ensures that changing <tt>Options</tt> outside the <tt>Module</tt> has no impact. |
# Sample Code | # Sample Code | ||
<pre> | <pre> | ||
Zeile 22: | Zeile 24: | ||
== Method <tt>void setOptions(Options o)</tt> == | == Method <tt>void setOptions(Options o)</tt> == | ||
# Functionality | # Functionality | ||
− | #* sets the <tt>Options</tt> for a Module | + | #* sets the <tt>Options</tt> for a <tt>Module</tt> |
#* ensure that <tt>Options</tt> are cloned before they are set! | #* ensure that <tt>Options</tt> are cloned before they are set! | ||
# Objectives | # Objectives | ||
− | #* setting a clone of the <tt>Options</tt> ensures that changing <tt>Options</tt> outside the < | + | #* setting a clone of the <tt>Options</tt> ensures that changing <tt>Options</tt> outside the <tt>Modules</tt> has no impact. |
# Sample Code | # Sample Code | ||
<pre> | <pre> | ||
Zeile 35: | Zeile 37: | ||
== Method <tt>String check() </tt> == | == Method <tt>String check() </tt> == | ||
# Functionality | # Functionality | ||
− | #* check, if this < | + | #* check, if this <tt>Modules</tt> is set up correctly, i.e. checks if |
− | #** | + | #** the <tt>Options</tt> are correct? |
− | #** | + | #** all required <tt>Connections</tt> of the <tt>Modules</tt> are available (e.g. an optimization algorithm may require one connection to a problem) |
− | #** | + | #** the settings (e.g. Options) agree with the properties of connected <tt>Modules</tt>? |
#* checking requires several steps: | #* checking requires several steps: | ||
− | #*# if the < | + | #*# if the <tt>Modules</tt> is connected to other <tt>Modules</tt>, it should first |
− | #*#* call <tt>check()</tt> of the connected < | + | #*#* call <tt>check()</tt> of the connected <tt>Modules</tt> and |
− | #*#* return a message, if any connected < | + | #*#* return a message, if any connected <tt>Modules</tt>’s check is unsuccessful (i.e. a non-empty string is returned). |
− | #*# if checking the connected < | + | #*# if checking the connected <tt>Modules</tt> was successful, the <tt>Modules</tt> should check itself and return a non-empty string if checking fails. |
# Properties | # Properties | ||
#* checks are usually fast | #* checks are usually fast | ||
Zeile 74: | Zeile 76: | ||
</pre> | </pre> | ||
− | == Method <tt>String init(long | + | == Method <tt>String init(long index)</tt> == |
− | + | Currently the <tt>index</tt> is not used! | |
# Functionality | # Functionality | ||
− | #* initialize < | + | #* initialize <tt>Modules</tt> such that executing optimizations or learning processes is possible. |
#* Initialization requires several steps: | #* Initialization requires several steps: | ||
− | #*# check if the argument ( | + | #*# check if the argument (index) has changed compared to the time of the last check (see also example below). If the argument has not changed, the system has not changed and a new initialization is not necessary. Thus, return the message of the previous check and do no further checking. |
− | #*# If the < | + | #*# If the <tt>Modules</tt> is connected to other Modules, it should first |
− | #*#* call the <tt>init()</tt> of the connected < | + | #*#* call the <tt>init()</tt> of the connected <tt>Modules</tt> and |
− | #*#* return a message, if any connected < | + | #*#* return a message, if any connected <tt>Modules</tt> fails to initialize |
− | #*# If the initialization of the connected < | + | #*# If the initialization of the connected <tt>Modules</tt> is successful, it should then get the properties of the connected <tt>Modules</tt> |
− | #*#* return a message, if any connected < | + | #*#* return a message, if any connected <tt>Modules</tt> fails to return properties. |
− | #*# Initialize itself and return a string if initialization fails | + | #*# Initialize itself and return a string if initialization fails. |
# Objectives | # Objectives | ||
− | #* initialization should never lead to exceptions | + | #* initialization should never lead to exceptions. |
#* It must not call <tt>check()</tt>! | #* It must not call <tt>check()</tt>! | ||
# Properties | # Properties | ||
− | #* the <tt>init()</tt> method is the only method of <tt>IModule</tt> that may require CPU time (e.g. for loading a large data source). Thus | + | #* the <tt>init()</tt> method is the only method of <tt>IModule</tt> that may require CPU time (e.g. for loading a large data source). Thus this method should not be called too often. |
# Advanced Features | # Advanced Features | ||
− | #* the < | + | #* the<tt>Modules</tt> should check if initialization is necessary. Typically initialization is necessary when |
− | #** the <tt>Options</tt> of the < | + | #** the <tt>Options</tt> of the<tt>Modules</tt> are changed |
− | #** the connections of the < | + | #** the connections of the<tt>Modules</tt> changed |
#** the properties of the connected <i>Modules</i> changed | #** the properties of the connected <i>Modules</i> changed | ||
#* if nothing changed, it should not initialize in order to save computer resources | #* if nothing changed, it should not initialize in order to save computer resources | ||
Zeile 127: | Zeile 129: | ||
} | } | ||
</pre> | </pre> | ||
+ | |||
+ | '''Summary''' | ||
+ | |||
+ | Properties of a <tt>Module</tt> should only change when the Method <tt>init()</tt> is called. | ||
+ | This guarantees that <tt>Modules</tt> do not change while they are checked or other <tt>Modules</tt> are running! | ||
== Method <tt>int getID()</tt> == | == Method <tt>int getID()</tt> == | ||
# Functionality | # Functionality | ||
− | #* gets the ID for this | + | #* gets the ID for this <tt>Module</tt>. |
# Objectives | # Objectives | ||
− | #* the ID is important for debugging, e.g. < | + | #* the ID is important for debugging, e.g. <tt>Modules</tt> can identify to which other <tt>Modules</tt> they are connected. |
== Method <tt>void setID(int id)</tt> == | == Method <tt>void setID(int id)</tt> == | ||
# Functionality | # Functionality | ||
− | #* sets the ID for this < | + | #* sets the ID for this <tt>Module</tt>. |
# Objectives | # Objectives | ||
#* IDs should be set at the time of instantiation and should not be modified. | #* IDs should be set at the time of instantiation and should not be modified. | ||
− | #* the ID is important for debugging, e.g. < | + | #* the ID is important for debugging, e.g. <tt>Modules</tt> can identify to which other <tt>Modules</tt> they are connected. |
== Method <tt>String getInfo()</tt> == | == Method <tt>String getInfo()</tt> == | ||
# Functionality | # Functionality | ||
#* Returns a String in HTML or plain text, describing the module | #* Returns a String in HTML or plain text, describing the module | ||
− | #* A < | + | #* A<tt>Modules</tt> such as an optimization algorithm should return the problem properties when it this<tt>Modules</tt> is called. |
# Objectives | # Objectives | ||
− | #* get information about < | + | #* get information about<tt>Modules</tt> properties for checking or debugging user settings |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Aktuelle Version vom 19. März 2014, 23:06 Uhr
Inhaltsverzeichnis
Interface IModule
All Modules in OpenDino must implement the Interface IModule. The interface contains a set of methods. The purpose and the functionality of these methods is described in the following sections.
Furthermore, the Modules must have a class Options that comprises all user settings as simple types (boolean, long, double, String) or vectors/arrays of those types. The options are automatically read using Java Reflection API.
It is essential for a correct program execution to ensure that the guidelines are exactly followed by the Modules programmers.
Method Options getOptions()
- Functionality
- returns the current Options class.
- ensure that cloned Options are returned!
- Objectives
- returning a clone of the Options ensures that changing Options outside the Module has no impact.
- Sample Code
public Options getOptions() { return (Options) options.clone(); }
Method void setOptions(Options o)
- Functionality
- sets the Options for a Module
- ensure that Options are cloned before they are set!
- Objectives
- setting a clone of the Options ensures that changing Options outside the Modules has no impact.
- Sample Code
public void setOptions(Options o) { options = (Options) o.clone(); }
Method String check()
- Functionality
- check, if this Modules is set up correctly, i.e. checks if
- the Options are correct?
- all required Connections of the Modules are available (e.g. an optimization algorithm may require one connection to a problem)
- the settings (e.g. Options) agree with the properties of connected Modules?
- checking requires several steps:
- if the Modules is connected to other Modules, it should first
- call check() of the connected Modules and
- return a message, if any connected Modules’s check is unsuccessful (i.e. a non-empty string is returned).
- if checking the connected Modules was successful, the Modules should check itself and return a non-empty string if checking fails.
- if the Modules is connected to other Modules, it should first
- check, if this Modules is set up correctly, i.e. checks if
- Properties
- checks are usually fast
- Return Value
- empty string “” if successful, else a message
- Sample Code for Optimization:
public String check(){ s = ""; // 1. check connected modules for (Object o: evaluables) { String so=((IModule)o).check(); if (so.length > 0){ s += “checking connected module with ID “ + (IModule o).getID() + “ failed with message: \\n” + so; } } if (s.length > 0){ return “checking connected modules failed: \\n” + s; // 2. check this module, return String if not successful if (opt.verbose > 3) s+= "Option verbose must be between 0 and 3, but is set to " + opt.verbose + "\\n"; // … return s; }
Method String init(long index)
Currently the index is not used!
- Functionality
- initialize Modules such that executing optimizations or learning processes is possible.
- Initialization requires several steps:
- check if the argument (index) has changed compared to the time of the last check (see also example below). If the argument has not changed, the system has not changed and a new initialization is not necessary. Thus, return the message of the previous check and do no further checking.
- If the Modules is connected to other Modules, it should first
- call the init() of the connected Modules and
- return a message, if any connected Modules fails to initialize
- If the initialization of the connected Modules is successful, it should then get the properties of the connected Modules
- return a message, if any connected Modules fails to return properties.
- Initialize itself and return a string if initialization fails.
- Objectives
- initialization should never lead to exceptions.
- It must not call check()!
- Properties
- the init() method is the only method of IModule that may require CPU time (e.g. for loading a large data source). Thus this method should not be called too often.
- Advanced Features
- theModules should check if initialization is necessary. Typically initialization is necessary when
- the Options of theModules are changed
- the connections of theModules changed
- the properties of the connected Modules changed
- if nothing changed, it should not initialize in order to save computer resources
- theModules should check if initialization is necessary. Typically initialization is necessary when
- Return Value
- empty string “” if successful, else a message
- Sample Code for Optimization:
public string init(long init_id){ s = ""; if (this.init_id == init_id){ return [the message of the last initialization]; this.init_id = init_id; for (Object o: evaluables) { String so=((IModule)o).init(init_id); if (so.length > 0){ s += “initializing connected module with ID “ + (IModule o).getID() + “ failed with message: \\n” + so; } } if (s.length > 0){ return “initializing connected modules failed: \\n” + s; // initialize this module, return String if not successful // … return s; }
Summary
Properties of a Module should only change when the Method init() is called. This guarantees that Modules do not change while they are checked or other Modules are running!
Method int getID()
- Functionality
- gets the ID for this Module.
- Objectives
- the ID is important for debugging, e.g. Modules can identify to which other Modules they are connected.
Method void setID(int id)
- Functionality
- sets the ID for this Module.
- Objectives
- IDs should be set at the time of instantiation and should not be modified.
- the ID is important for debugging, e.g. Modules can identify to which other Modules they are connected.
Method String getInfo()
- Functionality
- Returns a String in HTML or plain text, describing the module
- AModules such as an optimization algorithm should return the problem properties when it thisModules is called.
- Objectives
- get information aboutModules properties for checking or debugging user settings