User Tools

Site Tools


tuto_ecj_android

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
tuto_ecj_android [2014/03/03 15:16]
Denis Pallez [5. Print configuration data]
tuto_ecj_android [2014/03/03 16:05]
Denis Pallez
Line 1: Line 1:
-====== ​TutorialHow ​to install ECJ library on Android environment ? ====== +====== ​How to install ECJ library on Android environment ? ======
- +
-  - [[#​install|Installation of Eclispe and Android SDK]] +
-  - [[#​createANewProject|Create a new Android Project]] +
-  - [[#​addEcj|Add ECJ library in your project]] +
-  - [[#​collectInfo|Collect configuration data]] +
-  - [[#​printInfo|Print configuration data]] +
-  - [[#​writeInfo|Write in a File with Android]] +
-  - [[#​useNewConfigFile|Use a file in an another one]] +
 ===== 1. Installation of Eclispe and Android SDK ===== ===== 1. Installation of Eclispe and Android SDK =====
  
Line 43: Line 34:
  
 Now, to get the all data in "​config_ecj.params",​ we have to create an InputStream.\\ Add these lines in your code :  Now, to get the all data in "​config_ecj.params",​ we have to create an InputStream.\\ Add these lines in your code : 
-<​code>​+<​code ​java>
 InputStream configStream = null; InputStream configStream = null;
 try { try {
Line 56: Line 47:
 If your configuration file is named "​config_ecj.params",​ located in the assets package and "​this"​ correspond to the current context of your application,​ this code shouldn'​t cause any problem. If your configuration file is named "​config_ecj.params",​ located in the assets package and "​this"​ correspond to the current context of your application,​ this code shouldn'​t cause any problem.
 It only remains to transfer the InputStream "​configStream"​ at the ParameterDatabase who's in charge of initializing ECJ. Here is how to initialize the ParameterDatabase : It only remains to transfer the InputStream "​configStream"​ at the ParameterDatabase who's in charge of initializing ECJ. Here is how to initialize the ParameterDatabase :
-<​code>​+<​code ​java>
 ParameterDatabase paramDB = null; ParameterDatabase paramDB = null;
 try { try {
Line 67: Line 58:
  
 Now, you could run your evolutionary algorithm with the code below (more details are given in the ECJ manual, page 44) Now, you could run your evolutionary algorithm with the code below (more details are given in the ECJ manual, page 44)
-<​code>​+<​code ​java>
 EvolutionState evaluatedState = Evolve.initialize(paramDB,​ 0); EvolutionState evaluatedState = Evolve.initialize(paramDB,​ 0);
 evaluatedState.startFresh();​ evaluatedState.startFresh();​
Line 102: Line 93:
 ===== 6. Write in a File with Android ===== ===== 6. Write in a File with Android =====
  
-However, ECJ may need a real File to run, and not an InputStream. For example, to initialize a population.\\ \\ In this part we'll see how to write a file.\\ As in the previous example you'll need a package named "​assets"​.\\ Create a file in your workspace named "​config_ecj_in.in"​ (for an initialization file, by example).\\ After, you'll need the context of you application :\\ Context mContext = this; Then, you have to load your file on your smartphone : File file = mContext.getFileStreamPath("​config_ecj_in.in"​);​ Finaly, you can write into your file thanks to a FileWrite and a BufferedWriter : FileWriter fileWriter = null;\\ try {\\ fileWriter = new FileWriter(file);​\\  ​} catch (IOException e1) {\\ e1.printStackTrace();​\\  ​}\\ BufferedWriter output = new BufferedWriter(fileWriter);​\\ try {\\ output.write("​Hello World ! ");\\  ​}\\ output.flush();​\\ catch (Exception e) {\\ Toast.makeText(mContext,​ "​Settings not saved",​ Toast.LENGTH_SHORT).show();​\\  ​}\\ finally {\\ try {\\  ​output.close();​\\  ​fileWriter.close();​\\  } catch (IOException e) {\\  ​Toast.makeText(mContext,​ "​Settings not saved",​ Toast.LENGTH_SHORT).show();​\\  }\\  ​}\\  ​As you can understand, this code write the text "Hello World" into the file "​config_ecj_in.in"​.\\ ​+However, ECJ may need a real File to run, and not an InputStream. For example, to initialize a population. 
 + 
 +In this part we'll see how to write a file. 
 +As in the previous example you'll need a package named "​assets"​.\\ Create a file in your workspace named "​config_ecj_in.in"​ (for an initialization file, by example). 
 +After, you'll need the context of you application : <code java>Context mContext = this; </​code>​ 
 +Then, you have to load your file on your smartphone : <code java>File file = mContext.getFileStreamPath("​config_ecj_in.in"​); ​</​code>​ 
 +Finaly, you can write into your file thanks to a FileWrite and a BufferedWriter : <code java> 
 +FileWriter fileWriter = null; 
 +try { 
 +  ​fileWriter = new FileWriter(file);​ 
 +} 
 +catch (IOException e1) { 
 +  ​e1.printStackTrace();​ 
 +} 
 +BufferedWriter output = new BufferedWriter(fileWriter);​ 
 +try { 
 +  ​output.write("​Hello World ! "); 
 +} 
 +output.flush();​ 
 +catch (Exception e) { 
 +  ​Toast.makeText(mContext,​ "​Settings not saved",​ Toast.LENGTH_SHORT).show();​ 
 +} 
 +finally { 
 +  ​try { 
 +    ​output.close();​ 
 +    ​fileWriter.close();​ 
 +  } 
 +  ​catch (IOException e) { 
 +    ​Toast.makeText(mContext,​ "​Settings not saved",​ Toast.LENGTH_SHORT).show();​ 
 +  } 
 +} 
 +</​code>​ 
 + 
 +As you can understand, this code write the text "Hello World" into the file "​config_ecj_in.in"​.\\ ​
  
 [[#​begin|Back to begin]] [[#​begin|Back to begin]]
Line 110: Line 134:
 Now, you can use this file as in the previous [[#​collectInfo|example]].\\ ​ Now, you can use this file as in the previous [[#​collectInfo|example]].\\ ​
  
-But, as we said before, ECJ may need a file. Or more precisely, the path of this file.\\ For a population initialization,​ you give the path of the initialization file to the configuration file.\\ This path is the following one:\\ /​data/​data/​name_of_your_current_project/​files/​config_ecj_in.in.\\ \\ If you want to initialize your population "​config_ecj_in.in"​ from the configuration file "​mainConfigFile.params",​ add this line in "​mainConfigFile.params":​\\ pop.file = /​data/​data/​name_of_your_current_project/​files/​config_ecj_in.in\\  ​Now, if you load "​mainConfigFile.params"​ as show in this [[#​collectInfo|example]] you will also initialize you popullation with "​config_ecj_in.in"​.\\ Simple, isn't it ?\\ Here is the end of our tutorial. Good luck !\\ +But, as we said before, ECJ may need a file. Or more precisely, the path of this file.\\ For a population initialization,​ you give the path of the initialization file to the configuration file.\\ This path is the following one:\\ /​data/​data/​name_of_your_current_project/​files/​config_ecj_in.in.\\ \\ If you want to initialize your population "​config_ecj_in.in"​ from the configuration file "​mainConfigFile.params",​ add this line in "​mainConfigFile.params":​ 
 +<​code>​ 
 +pop.file = /​data/​data/​name_of_your_current_project/​files/​config_ecj_in.in 
 +</​code>​ 
 +Now, if you load "​mainConfigFile.params"​ as show in this [[#​collectInfo|example]] you will also initialize you popullation with "​config_ecj_in.in"​.\\ Simple, isn't it ?\\ Here is the end of our tutorial. Good luck !\\ 
  
 [[#​begin|Back to begin]] [[#​begin|Back to begin]]
  
 Any questions or suggestions ? [[guegan@polytech.unice.fr;​fauvelja@polytech.unice.fr;​adiaz@polytech.unice.fr;​arnaud@polyech.unice.fr?​subject=Tutorial%20ECJ%20in%20Android|Contact us !]] Any questions or suggestions ? [[guegan@polytech.unice.fr;​fauvelja@polytech.unice.fr;​adiaz@polytech.unice.fr;​arnaud@polyech.unice.fr?​subject=Tutorial%20ECJ%20in%20Android|Contact us !]]
 +
 +<​note>​written by Ancelin Arnaud, Aliénor Diaz, Olivier Fauvel-Jaeger,​ Frédéric Guégan in 2013/​06</​note>​
 +
tuto_ecj_android.txt · Last modified: 2014/03/03 17:27 by Denis Pallez