« GReadWrite interface » : différence entre les versions
(Page créée avec « TBW ... Return to the introduction ↑ Go to the next page → ») |
Aucun résumé des modifications |
||
Ligne 1 : | Ligne 1 : | ||
=== The GreadWrite Interface === | |||
<font color=#556B2F>**GENIUS**</font> proposes a simply way to __read__ and __write__ into files, consistent with the display: | |||
* By calling the [{{PathCurrentJavaDoc}}/fr/cnes/genius/main/GReadWrite.html GReadWrite] interface | |||
* By definition of the <font color=#4169E1>read()</font> and <font color=#4169E1>write()</font> methods calling the <font color=#4169E1>put()</font> 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 <font color=#4169E1>generic()</font> method and the <font color=#4169E1>display()</font>, <font color=#4169E1>read()</font> and <font color=#4169E1>write()</font> methods will only have to call the <font color=#4169E1>generic()</font> one ! | |||
<syntaxhighlight lang="java"> | |||
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(); } | |||
} | |||
</syntaxhighlight> | |||
So, in this example above: | |||
# the widget valS will be displayed only if the value of valI is strictly positive. | |||
# Moreover, the value of valS will be read only if the value of valI is strictly positive. | |||
# 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 <font color=#4169E1>read()</font>, <font color=#4169E1>write()</font> and <font color=#4169E1>display()</font> 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. | |||
<syntaxhighlight lang="java"> | |||
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); | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
=== 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: | |||
* We use static methods from class [{{PathCurrentJavaDoc}}/fr/cnes/genius/main/GFileManipulation.html|GFileManipulation]] | |||
* The file will be in XML MADONA format | |||
Remark #1 : the <font color=#556B2F>**GENIUS**</font> object may contain itself other <font color=#556B2F>'''GENIUS'''</font> objects and so on … | |||
Remark #2 : in that example, the flag set for the last argument of these methods is to tell to <font color=#556B2F>'''GENIUS'''</font> 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|Modified data]] topic. | |||
<syntaxhighlight lang="java"> | |||
MyGeniusObject obj = new MyGeniusObject (…); // Must implement the GReadWrite interface | |||
GFileManipulation.readConfig (fileName, XMLRootName, obj, false ); | |||
GFileManipulation.writeConfig (fileName, XMLRootName, obj, true ); | |||
</syntaxhighlight> | |||
It is possible to differentiate the label displayed on the screen and the XML variable name using method <font color=#4169E1>setNameInConfigFile()</font>. | |||
<syntaxhighlight lang="java"> | |||
valR.setNameInConfigFile("nomXML"); | |||
</syntaxhighlight> | |||
It is also possible to have data structures, using <font color=#4169E1>beginOfElement()</font> and <font color=#4169E1>endOfElement()</font> methods: | |||
<syntaxhighlight lang="java"> | |||
public void generic() { | |||
beginOfElement("typeOfStruct", "nameOfStruct"); | |||
put(valR); | |||
put(valI); | |||
put(valS); | |||
endOfElement(); } | |||
public void read() { generic(); } | |||
public void write() { generic(); } | |||
</syntaxhighlight> | |||
Note that the first argument of <font color=#4169E1>beginOfElement()</font> may be an enum (implementing [{{PathCurrentJavaDoc}}/fr/cnes/genius/main/GItemTypeInterface.html|GItemTypeInterface]]) rather than a string which could be more robust: | |||
<syntaxhighlight lang="java"> | |||
public enum StructType implements GItemTypeInterface { | |||
Orbit, | |||
Potential, | |||
Atmosphere, | |||
Maneuver, | |||
Attitude; | |||
} | |||
</syntaxhighlight> | |||
At the end, the format of the file will be something like that: | |||
<syntaxhighlight lang="XML"> | |||
<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> | |||
</syntaxhighlight> | |||
=== 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 <font color=#4169E1>setDefaultValue</font> method. For example: | |||
<syntaxhighlight lang="java"> | |||
valG0 = new GEntryReal("G0" , 9.80665); | |||
valG0.setDefaultValue(9.80665); | |||
} | |||
</syntaxhighlight> | |||
So, in that case, we will be able to read this file even if g0 does not appear ! | |||
<syntaxhighlight lang="XML"> | |||
<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> | |||
</syntaxhighlight> | |||
//[[user_manual:introduction|Return to the introduction ↑]]//\\ | |||
//[[user_manual:modified_data|Go to the next page →]]// | |||
[[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 5 mai 2017 à 09:21
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:
- the widget valS will be displayed only if the value of valI is strictly positive.
- Moreover, the value of valS will be read only if the value of valI is strictly positive.
- 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 [1]]
- 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 [2]]) 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 →//