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
tuto_ecj_android [2014/03/03 15:10]
Denis Pallez [3. Add ECJ library in your project]
tuto_ecj_android [2014/03/03 17:27] (current)
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 =====
  
 First of all, to make the development of Android app easier, you have to install the Eclipse framework. In this environment,​ you will have to install the Android SDK to get all the tools. This two first steps are explained here :\\ [[http://​www.tutomobile.fr/​installer-le-sdk-android-sur-eclipse-tutoriel-android-n%C2%B01/​09/​06/​2010/​|- In French]]\\ [[http://​developer.android.com/​sdk/​installing/​installing-adt.html|- In English]]\\ Then, you have to download the ECJ library [[http://​cs.gmu.edu/​%7Eeclab/​projects/​ecj/​|here]]\\ You can find the official ECJ manual [[http://​cs.gmu.edu/​%7Eeclab/​projects/​ecj/​docs/​manual/​manual.pdf|here]]\\ ​ First of all, to make the development of Android app easier, you have to install the Eclipse framework. In this environment,​ you will have to install the Android SDK to get all the tools. This two first steps are explained here :\\ [[http://​www.tutomobile.fr/​installer-le-sdk-android-sur-eclipse-tutoriel-android-n%C2%B01/​09/​06/​2010/​|- In French]]\\ [[http://​developer.android.com/​sdk/​installing/​installing-adt.html|- In English]]\\ Then, you have to download the ECJ library [[http://​cs.gmu.edu/​%7Eeclab/​projects/​ecj/​|here]]\\ You can find the official ECJ manual [[http://​cs.gmu.edu/​%7Eeclab/​projects/​ecj/​docs/​manual/​manual.pdf|here]]\\ ​
- 
-[[#​begin|Back to begin]] 
  
 ===== 2. Create a new Android Project ===== ===== 2. Create a new Android Project =====
Line 19: Line 8:
 When the two first steps are done, we will create a new Android project with eclipse. <​code>​ - File -> new -> Android application project</​code>​\\ {{ECJ_Android_fichiers:​1.png}}\\ <​code>​- Give a name at your project and let the default configurations. When the two first steps are done, we will create a new Android project with eclipse. <​code>​ - File -> new -> Android application project</​code>​\\ {{ECJ_Android_fichiers:​1.png}}\\ <​code>​- Give a name at your project and let the default configurations.
 - In your workspace, your new project should be as shown below :</​code>​\\ \\ {{ECJ_Android_fichiers:​2.png}}\\ ​ - In your workspace, your new project should be as shown below :</​code>​\\ \\ {{ECJ_Android_fichiers:​2.png}}\\ ​
- 
-[[#​begin|Back to begin]] 
  
 ===== 3. Add ECJ library in your project ===== ===== 3. Add ECJ library in your project =====
Line 37: Line 24:
 - Right click on this new package -> new -> File - Right click on this new package -> new -> File
 - Name the new file "​config_ecj.params"​ for exemple</​code>​\\ \\ You can now edit your configuration file as usual.\\ {{ECJ_Android_fichiers:​6.png}}\\ ​ - Name the new file "​config_ecj.params"​ for exemple</​code>​\\ \\ You can now edit your configuration file as usual.\\ {{ECJ_Android_fichiers:​6.png}}\\ ​
- 
-[[#​begin|Back to begin]] 
  
 ===== 4. Collect configuration data ===== ===== 4. Collect configuration data =====
  
-Now, to get the all data in "​config_ecj.params",​ we have to create an InputStream.\\ Add these lines in your code : InputStream configStream = null;\\ try {\\ Context mContext = this;\\  ​configStream = mContext.getAssets().open("​config_ecj.params"​);​\\  ​} catch (IOException e) {\\ e.printStackTrace();​\\  ​}\\ +Now, to get the all data in "​config_ecj.params",​ we have to create an InputStream.\\ Add these lines in your code :  
 +<code java> 
 +InputStream configStream = null; 
 +try { 
 +  ​Context mContext = this; 
 +  ​configStream = mContext.getAssets().open("​config_ecj.params"​);​ 
 +} 
 +catch (IOException e) { 
 +  ​e.printStackTrace();​ 
 +} 
 +</​code>​
  
-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 : ParameterDatabase paramDB = null;\\ try {\\ paramDB = new ParameterDatabase(configStream);​\\  ​}\\ catch (IOException e) {\\ e.printStackTrace();​\\  ​}\\ \\ Now, you could run your evolutionary algorithm with the code below (more details are given in the ECJ manual, page 44) EvolutionState evaluatedState = Evolve.initialize(paramDB,​ 0);\\ evaluatedState.startFresh();​\\ int result = EvolutionState.R_NOTDONE;​\\ while( result == EvolutionState.R_NOTDONE )\\ result = evaluatedState.evolve();​\\ \\ +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 : 
 +<code java> 
 +ParameterDatabase paramDB = null; 
 +try { 
 +  ​paramDB = new ParameterDatabase(configStream);​ 
 +} 
 +catch (IOException e) { 
 +  ​e.printStackTrace();​ 
 +} 
 +</code>
  
-[[#​begin|Back to begin]]+Now, you could run your evolutionary algorithm with the code below (more details are given in the ECJ manual, page 44) 
 +<code java> 
 +EvolutionState evaluatedState = Evolve.initialize(paramDB,​ 0); 
 +evaluatedState.startFresh();​ 
 +int result = EvolutionState.R_NOTDONE;​ 
 +while( result == EvolutionState.R_NOTDONE ) 
 +  result = evaluatedState.evolve();​ 
 + </​code> ​
  
 ===== 5. Print configuration data ===== ===== 5. Print configuration data =====
  
-If you want to be convinced that your configurations are in the InputStream,​ you can add this code who's in charge of printing the data in the InputStream on logCat console : String out = "";​\\ try{\\ InputStreamReader configStreamReader = new InputStreamReader(configStream);​\\  ​BufferedReader br = new BufferedReader(configStreamReader);​\\  ​String ligne = "";​\\  while ( (ligne=br.readLine()) != null) {\\  ​out += ligne + "​\n";​\\  }\\  ​br.close();​\\ } catch (Exception e) {\\ e.printStackTrace();​\\  ​}\\ System.out.println("​Out => " + out);\\ \\ {{ECJ_Android_fichiers:​7.png}}\\ ​ +If you want to be convinced that your configurations are in the InputStream,​ you can add this code who's in charge of printing the data in the InputStream on logCat console :  
- +<code java> 
-[[#​begin|Back to begin]]+String out = "";​ 
 +try{ 
 +  ​InputStreamReader configStreamReader = new InputStreamReader(configStream);​ 
 +  ​BufferedReader br = new BufferedReader(configStreamReader);​ 
 +  ​String ligne = "";​ 
 +  while ( (ligne=br.readLine()) != null) { 
 +    ​out += ligne + "​\n";​ 
 +  } 
 +  ​br.close();​ 
 + 
 +catch (Exception e) { 
 +  ​e.printStackTrace();​ 
 +} 
 +System.out.println("​Out => " + out); 
 +</​code>​ 
 +\\ {{ECJ_Android_fichiers:​7.png}}\\ ​
  
 ===== 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>
  
-[[#​begin|Back to begin]]+As you can understand, this code write the text "Hello World" into the file "​config_ecj_in.in"​.\\ ​
  
 ===== 7. Use a file in an another one ===== ===== 7. Use a file in an another one =====
Line 64: Line 122:
 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>​ 
-[[#​begin|Back to begin]]+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 !\\ 
  
 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.1393855807.txt.gz · Last modified: 2014/03/03 15:10 by Denis Pallez