GReadWrite interface : Différence entre versions

De Wiki
Aller à : navigation, rechercher
Ligne 1 : Ligne 1 :
  
=== The GreadWrite Interface ===
+
== The GreadWrite Interface ==
  
 
<font color=#556B2F>'''GENIUS'''</font> proposes a simply way to <u>read</u> and <u>write</u> into files, consistent with the display:
 
<font color=#556B2F>'''GENIUS'''</font> proposes a simply way to <u>read</u> and <u>write</u> into files, consistent with the display:
Ligne 80 : Ligne 80 :
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== Data file management ===
+
== Data file management ==
  
 
To read (or write) using <font color=#556B2F>'''GENIUS'''</font> tools, we only need to open a file and store inside the <font color=#556B2F>'''GENIUS'''</font> corresponding object the data contained in this file. To do it:
 
To read (or write) using <font color=#556B2F>'''GENIUS'''</font> tools, we only need to open a file and store inside the <font color=#556B2F>'''GENIUS'''</font> corresponding object the data contained in this file. To do it:
Ligne 142 : Ligne 142 :
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== Missing data ===
+
== Missing data ==
  
 
From 1.4 version there is the possibility to read such files even if some data are missing. Indeed, this may occur when you want to read files from a previous version of your tool when these data where not initially foreseen.
 
From 1.4 version there is the possibility to read such files even if some data are missing. Indeed, this may occur when you want to read files from a previous version of your tool when these data where not initially foreseen.

Version du 5 mai 2017 à 10:31

The GreadWrite Interface

GENIUS proposes a simply way to read and write into files, consistent with the display:

  • By calling the GReadWrite interface
  • By definition of the read() and write() methods calling the put() method, the mechanism being the same as for display (for example, using if/switch for conditionality).

Thus, if we consider that the data must be read or written with the same logic/order as for the display, we will put all inside the generic() method and the display(), read() and write() methods will only have to call the generic() one !

public class MyContainer extends GContainer implements GDisplay, GReadWrite {
 
  GEntryReal   valR;
  GEntryInt    valI;
  GEntryString valS;
 
  public MyContainer () {
    valR = new GEntryReal("Real value" , 0.);
    valI = new GEntryInt("Integer value", 0);
    valS = new GEntryString("Chain", "");
  }
 
  public void generic() throws GException {
    put(valR);
    put(valI);
    if ( valI.getValue() > 0 ) {
      put(valS);
    }
  }
 
  public void display() throws GException { generic(); }
  public void read()    throws GException { generic(); }
  public void write()   throws Gexception { generic(); }
 
}

So, in this example above:

  1. the widget valS will be displayed only if the value of valI is strictly positive.
  2. Moreover, the value of valS will be read only if the value of valI is strictly positive.
  3. Again, the value of valS will be written into a file only if the value of valI is strictly positive.

We see that all have been centralized in the same code bloc.

On the contrary, if the logic is different for reading, writing and displaying data, we allways have the possibility to dissociate the treatments, coding them respectively inside the read(), write() and display() methods.

In the example below, for reading and writing, all data will be respectively read and written as, for display, valS will only be shown if the value of valI is strictly positive.

public class MyContainer extends GContainer implements GDisplay, GReadWrite {
 
  GEntryReal   valR;
  GEntryInt    valI;
  GEntryString valS;
 
  public MyContainer () {
    valR = new GEntryReal("Real value" , 0.);
    valI = new GEntryInt("Integer value", 0);
    valS = new GEntryString("Chain", "");
  }
 
  public void generic() throws GException {
    put(valR);
    put(valI);
    put(valS);
  }
 
  public void read()    throws GException { generic(); }
  public void write()   throws Gexception { generic(); }
 
  public void display() throws GException { 
    put(valR);
    put(valI);
    if ( valI.getValue() > 0 ) {
      put(valS);
    }
  }
 
}

Data file management

To read (or write) using GENIUS tools, we only need to open a file and store inside the GENIUS corresponding object the data contained in this file. To do it:

  • We use static methods from class GFileManipulation
  • The file will be in XML MADONA format

Remark #1 : the GENIUS object may contain itself other GENIUS objects and so on …

Remark #2 : in that example, the flag set for the last argument of these methods is to tell to GENIUS to add a "*" character after reading (if values changed) and to remove it after writing the configuration. For more details about modified data, see specific Modified data topic.

  MyGeniusObject obj = new MyGeniusObject (); // Must implement the GReadWrite interface 
 
  GFileManipulation.readConfig  (fileName, XMLRootName, obj, false );
  GFileManipulation.writeConfig (fileName, XMLRootName, obj, true );

It is possible to differentiate the label displayed on the screen and the XML variable name using method setNameInConfigFile().

valR.setNameInConfigFile("nomXML");

It is also possible to have data structures, using beginOfElement() and endOfElement() methods:

  public void generic() {
    beginOfElement("typeOfStruct", "nameOfStruct");
      put(valR);
      put(valI);
      put(valS);
    endOfElement(); }
 
  public void read() { generic(); }
  public void write() { generic(); }

Note that the first argument of beginOfElement() may be an enum (implementing GItemTypeInterface) rather than a string which could be more robust:

public enum StructType implements GItemTypeInterface {
 
   Orbit,
   Potential,
   Atmosphere,
   Maneuver,
   Attitude;
 
}

At the end, the format of the file will be something like that:

 <Potential name=Earth">
      <Real name="mu" unit="km^3/s^2">398600.64</Real>
      <Real name="g0" unit="m/s^2">9.80665</Real>
      <Real name="rt" unit="km">6378.139</Real>
      <Real name="ze" unit="km">120.0</Real>
      <Real name="wt" unit="deg/s">0.004178071267451</Real>
 </Potential>

Missing data

From 1.4 version there is the possibility to read such files even if some data are missing. Indeed, this may occur when you want to read files from a previous version of your tool when these data where not initially foreseen.

The way to do it is very simple. In fact, you have just to call for the setDefaultValue method. For example:

    valG0 = new GEntryReal("G0" , 9.80665);
    valG0.setDefaultValue(9.80665);
  }

So, in that case, we will be able to read this file even if g0 does not appear !

 <Potential name=Earth">
      <Real name="mu" unit="km^3/s^2">398600.64</Real>
      <Real name="rt" unit="km">6378.139</Real>
      <Real name="ze" unit="km">120.0</Real>
      <Real name="wt" unit="deg/s">0.004178071267451</Real>
 </Potential>


Return to the introduction ↑ Go to the next page →