« GReadWrite interface » : différence entre les versions

De GENIUS
Aller à la navigation Aller à la recherche
Ligne 168 : Ligne 168 :
== Multiple search ==
== Multiple search ==


First, let us consider such a data structure as shown in this XML file :
First, let us consider such a data structure as shown in this <font color=#FF8C00 title="Extensible Markup Language">XML</font> file :


<syntaxhighlight lang="XML">
<syntaxhighlight lang="XML">
Ligne 194 : Ligne 194 :
</syntaxhighlight>
</syntaxhighlight>


We see that the "Vehicle" structure, including three rela data with '''drymass''', '''ergmass''' and '''sref''') is existing in both places in the file.


[[WELCOME_TO_THE_GENIUS_WIKI|Return to the introduction ↑]]  
[[WELCOME_TO_THE_GENIUS_WIKI|Return to the introduction ↑]]  
[[Modified_data|Go to the next page →]]
[[Modified_data|Go to the next page →]]

Version du 20 août 2019 à 14:52

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>

Multiple search

First, let us consider such a data structure as shown in this XML file :

<?xml version="1.0" encoding="UTF-8"?>
<data>
  <Vehicle name="vehicle">
    <!--Dry mass:-->
    <Real name="dryMass" unit="kg">1.0E0</Real>
    <!--Ergol mass:-->
    <Real name="ergMass" unit="kg">2.0E0</Real>
    <!--Reference surface:-->
    <Real name="sref" unit="m^2">3.0E0</Real>
  </Vehicle>
  <SubData name="subdata">
	  <Vehicle name="vehicle">
	    <!--Dry mass:-->
	    <Real name="dryMass" unit="kg">11.0E0</Real>
	    <!--Ergol mass:-->
	    <Real name="ergMass" unit="kg">22.0E0</Real>
	    <!--Reference surface:-->
	    <Real name="sref" unit="m^2">33.0E0</Real>
	  </Vehicle>
  </SubData>
</data>

We see that the "Vehicle" structure, including three rela data with drymass, ergmass and sref) is existing in both places in the file.

Return to the introduction ↑ Go to the next page →