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 14:40]
Denis Pallez
tuto_ecj_android [2014/03/03 16:05]
Denis Pallez
Line 1: Line 1:
-<​html>​ +====== How to install ECJ library on Android environment ? ====== 
-  <​body>​ +===== 1. Installation of Eclispe and Android SDK ===== 
-    <h1 id="​begin">​Tutorial<​br> ​How to install ECJ library on Android environment ? </​h1>​ + 
- <​ol>​ +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]]\\  
- <​li><​a href="#​install">​Installation of Eclispe and Android SDK</​a></​li>​ + 
- <​li><​a href="#​createANewProject">​Create a new Android Project</​a></​li>​ +[[#begin|Back to begin]] 
- <​li><​a href="#​addEcj">​Add ECJ library in your project</​a></​li>​ + 
- <​li><​a href="#​collectInfo">​Collect configuration data</​a></​li>​ +===== 2. Create a new Android Project ​===== 
- <​li><​a href="#​printInfo">​Print configuration data</​a></​li>​ + 
- <​li><​a href="#​writeInfo">​Write in a File with Android</​a></​li>​ +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. 
- <​li><​a href="#​useNewConfigFile">​Use a file in an another one</​a></​li>​ +- In your workspace, your new project should be as shown below :</code>\\ \\ {{ECJ_Android_fichiers:2.png}}\\  
- </​ol>​ + 
- <​h2><​div id="​install">​1. Installation of Eclispe and Android SDK</​div></​h2>​ +[[#begin|Back to begin]] 
- <p> + 
- First of all, to make the development of Android app easier, you have  +===== 3. Add ECJ library in your project ​===== 
-to install the Eclipse framework. In this environment,​ you will have to  +<​code>​ 
-install the Android SDK to get all the tools. +- Add in the libs folder the ecj.jar file. 
- This two first steps are explained here : <​br>​ +</code
- <a href="http://​www.tutomobile.fr/​installer-le-sdk-android-sur-eclipse-tutoriel-android-n%C2%B01/​09/​06/​2010/​">- In French</​a><​br>​ +Even if ecj.jar is in the libs folder, it shouldn'​t appear in your workspace. You have to add it in your build path. 
- <a href="http://​developer.android.com/​sdk/​installing/​installing-adt.html">- In English</​a><​br>​ +<​code>​- Right click on the project name -build path -configure build path... 
- Then, you have to download the ECJ library ​ +- Libraries tab -add external JARs... and select "​ecj.jar"​ in the "​libs"​ folder 
- <a href="http://​cs.gmu.edu/​%7Eeclab/​projects/​ecj/​">here</​a><​br>​ +- Go on the Order and Export tab -select "​ecj.jar"​ </code>\\ \\ {{ECJ_Android_fichiers:3.png}}\\ {{ECJ_Android_fichiers:4.png}}\\ 
- You can find the official ECJ manual ​ +Now, the ECJ library can be used in your Android app, but this library uses some external files including a configuration file. From your Android app point of view, this file is an external file. It's not obvious to include in your app. We will see how to collect the information of this configuration file. First of all you have to create it :  
- <a href="http://​cs.gmu.edu/​%7Eeclab/​projects/​ecj/​docs/​manual/​manual.pdf">here</​a><​br>​ +<​code>​ 
- </p> +- Right click on src -new -package 
- <div class="​return"><​a href="#begin">Back to begin</​a></​div>​ +- Name it "​assets"​ </code>\\ \\ {{ECJ_Android_fichiers:5.png}} 
- <​h2><​div id="​createANewProject">​2. Create a new Android Project</​div></​h2>​ +<​code>​ 
- <p> +- Right click on this new package -new -File 
- When the two first steps are done, we will create a new Android project with eclipse. +- Name the new file "​config_ecj.params"​ for exemple</​code>\\ \\ You can now edit your configuration file as usual.\\ {{ECJ_Android_fichiers:6.png}}\\  
- <pre class="​code"> + 
-- File -&​gt; ​new -&​gt; ​Android application project +[[#begin|Back to begin]] 
- </pre><​br>​ + 
- <img src="​https://​denispallez.i3s.unice.fr/​lib/​exe/​fetch.php?​media=ecj_android_fichiers:1.png"><br> +===== 4. Collect configuration data ===== 
- <span class="​code"> + 
- - Give a name at your project and let the default configurations.<br> +Now, to get the all data in "​config_ecj.params",​ we have to create an InputStream.\\ Add these lines in your code :  
- - In your workspace, your new project should be as shown below : <​br>​ +<​code ​java
- </span><​br>​ +InputStream configStream = null; 
- <img src="​https://​denispallez.i3s.unice.fr/​lib/​exe/​fetch.php?​media=ecj_android_fichiers:2.png"><​br>​ +try { 
- </p> +  Context mContext = this; 
- <div class="​return"><​a href="#begin">Back to begin</​a></​div>​ +  configStream = mContext.getAssets().open("​config_ecj.params"​);​ 
- <​h2><​div id="​addEcj">​3. Add ECJ library in your project</​div></​h2>​ +} 
- <p> +catch (IOException e) { 
- <span class="​code"+  e.printStackTrace();​ 
- - Add in the libs folder the ecj.jar file. +
- </span><​br+</code
- Even if ecj.jar is in the libs folder, it shouldn'​t appear in your workspace. You have to add it in your build path.  + 
- <span class="​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. 
- - Right click on the project name -&​gt; ​build path -&​gt; ​configure build path...<br> +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 : 
- - Libraries tab -&​gt; ​add external JARs... and select "​ecj.jar"​ in the "​libs"​ folder<br> +<​code ​java
- - Go on the Order and Export tab -&​gt; ​select "​ecj.jar"​<​br>​ +ParameterDatabase paramDB = null; 
- </span><​br>​ +try { 
- <img src="​https://​denispallez.i3s.unice.fr/​lib/​exe/​fetch.php?​media=ecj_android_fichiers:3.png"><​br>​ +  paramDB = new ParameterDatabase(configStream);​ 
- <img src="​https://​denispallez.i3s.unice.fr/​lib/​exe/​fetch.php?​media=ecj_android_fichiers:4.png"><​br>​ +
- Now, the ECJ library can be used in your Android app, but this library +catch (IOException e) { 
- uses some external files including a configuration file. From your  +  e.printStackTrace();​ 
-Android app point of view, this file is an external file. It's not  +
-obvious to include in your app. We will see how to collect the  +</code
-information of this configuration file.  + 
- First of all you have to create it :  +Now, you could run your evolutionary algorithm with the code below (more details are given in the ECJ manual, page 44) 
- <span class="​code"+<​code ​java
- - Right click on src -&​gt; ​new -&​gt; ​package<br> +EvolutionState evaluatedState = Evolve.initialize(paramDB,​ 0); 
- - Name it "​assets"​<​br>​ +evaluatedState.startFresh();​ 
- </span><​br>​ +int result = EvolutionState.R_NOTDONE;​ 
- <img src="​https://​denispallez.i3s.unice.fr/​lib/​exe/​fetch.php?​media=ecj_android_fichiers:5.png"><​br>​ +while( result == EvolutionState.R_NOTDONE ) 
- <span class="​code"+  result = evaluatedState.evolve();​ 
- - Right click on this new package -&​gt; ​new -&​gt; ​File<br> + </code>  
- - Name the new file "​config_ecj.params"​ for exemple<​br>​ + 
- </span><​br>​ +[[#begin|Back to begin]] 
- You can now edit your configuration file as usual.<​br>​ + 
- <img src="​https://​denispallez.i3s.unice.fr/​lib/​exe/​fetch.php?​media=ecj_android_fichiers:6.png"><​br>​ +===== 5. Print configuration data ===== 
- </p> + 
- <div class="​return"><​a href="#begin">Back to begin</​a></​div>​ +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 :  
- <​h2><​div id="​collectInfo">​4. Collect configuration data</​div></​h2>​ +<​code ​java
- <p> +String out = "";​ 
- Now, to get the all data in "​config_ecj.params",​ we have to create an InputStream. ​<​br>​ +try{ 
- Add these lines in your code :  +  InputStreamReader configStreamReader = new InputStreamReader(configStream);​ 
- <span class="​code"+  BufferedReader br = new BufferedReader(configStreamReader);​ 
- InputStream configStream = null;<br> +  String ligne = "";​ 
- try {<br> +  while ( (ligne=br.readLine()) != null) { 
- <span class="​unAlin">​Context mContext = this;<​br></​span>​ +    out += ligne + "​\n";​ 
- <span class="​unAlin">​configStream = mContext.getAssets().open("​config_ecj.params"​);​<​br></​span>​ +  
- } catch (IOException e) {<br> +  br.close();​ 
- <span class="​unAlin">​e.printStackTrace();​<​br></​span>​ + 
- }<br> +catch (Exception e) { 
- </span+  e.printStackTrace();​ 
- </​p><​p>​ +
- If your configuration file is named "​config_ecj.params",​ located in  +System.out.println("​Out =" + out); 
-the assets package and "​this"​ correspond to the current context of your  +</code
-application,​ this code shouldn'​t cause any problem.<br> +\\ {{ECJ_Android_fichiers:7.png}}\\  
- 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 :  +[[#begin|Back to begin]] 
- <pre class="​code"+ 
- ParameterDatabase paramDB = null;<br> +===== 6. Write in a File with Android ​===== 
- try {<br> + 
- <span class="​unAlin">​paramDB = new ParameterDatabase(configStream);​<​br></​span>​ +However, ECJ may need a real File to run, and not an InputStream. For example, to initialize a population. 
- }<br> + 
- catch (IOException e) {<br> +In this part we'll see how to write a file. 
- <span class="​unAlin">​e.printStackTrace();​<​br></​span>​ +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). 
- }<br> +After, you'll need the context of you application : <​code ​java>Context mContext = this; </code
- </pre><​br+Then, you have to load your file on your smartphone : <​code ​java>File file = mContext.getFileStreamPath("​config_ecj_in.in"​);​ </code
- Now, you could run your evolutionary algorithm with the code below (more details are given in the ECJ manual, page 44) +Finaly, you can write into your file thanks to a FileWrite and a BufferedWriter : <​code ​java
- <span class="​code"+FileWriter fileWriter = null; 
- EvolutionState evaluatedState = Evolve.initialize(paramDB,​ 0);<br> +try { 
- evaluatedState.startFresh();​<br> +  fileWriter = new FileWriter(file);​ 
- int result = EvolutionState.R_NOTDONE;​<br> +} 
- while( result == EvolutionState.R_NOTDONE )<br> +catch (IOException e1) { 
- <span class="​unAlin">​result = evaluatedState.evolve();​<​br></​span>​ +  e1.printStackTrace();​ 
- </span><​br+
- </p> +BufferedWriter output = new BufferedWriter(fileWriter);​ 
- <div class="​return"><​a href="#begin">Back to begin</​a></​div>​ +try { 
- <​h2><​div id="​printInfo">​5. Print configuration data</​div></​h2>​ +  output.write("​Hello World ! "); 
- <p> +
- If you want to be convinced that your configurations are in the  +output.flush();​ 
-InputStream,​ you can add this code who's in charge of printing the data  +catch (Exception e) { 
-in the InputStream on logCat console :  +  Toast.makeText(mContext,​ "​Settings not saved",​ Toast.LENGTH_SHORT).show();​ 
- <span class="​code"+
- String out = "";​<br> +finally { 
- try{<br> +  try { 
- <span class="​unAlin">​InputStreamReader configStreamReader = new InputStreamReader(configStream);​<​br></​span>​ +    output.close();​ 
- <span class="​unAlin">​BufferedReader br = new BufferedReader(configStreamReader);​<​br></​span>​ +    fileWriter.close();​ 
- <span class="​unAlin">​String ligne = "";​<​br></​span>​ +  } 
- <span class="​unAlin">​while ( (ligne=br.readLine()) != null) {<​br></​span>​ +  ​catch (IOException e) { 
- <​span class="​deuxAlin">​out += ligne + "​\n";​<​br></​span>​ +    Toast.makeText(mContext,​ "​Settings not saved",​ Toast.LENGTH_SHORT).show();​ 
- <span class="​unAlin">​}<​br></​span>​ +  
- br.close(); ​<br> +
- } catch (Exception e) {<br> +</code
- <span class="​unAlin">​e.printStackTrace();​<​br></​span>​ + 
- }<br> +As you can understand, this code write the text "Hello World" into the file "​config_ecj_in.in"​.\\  
- System.out.println("​Out =&​gt; ​" + out);<br> + 
- </span><​br+[[#begin|Back to begin]] 
- <img src="​https://​denispallez.i3s.unice.fr/​lib/​exe/​fetch.php?​media=ecj_android_fichiers:7.png"><​br>​ + 
- </p> +===== 7. Use a file in an another one ===== 
- <div class="​return"><​a href="#begin">Back to begin</​a></​div>​ + 
- <​h2><​div id="​writeInfo">​6. Write in a File with Android</​div></​h2>​ +Now, you can use this file as in the previous ​[[#​collectInfo|example]].\\  
- <p> + 
- However, ECJ may need a real File to run, and not an InputStream. For example, to initialize a population. ​<​br><​br>​ +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":​ 
- In this part we'll see how to write a file.<br> +<​code>​ 
- As in the previous example you'll need a package named "​assets"​. ​<​br>​ +pop.file = /​data/​data/​name_of_your_current_project/​files/​config_ecj_in.in 
- Create a file in your workspace named "​config_ecj_in.in"​ (for an initialization file, by example).<br> +</code
- After, you'll need the context of you application : <br> +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 !\\  
- <span class="​code"> + 
- Context mContext = this; +[[#begin|Back to begin]] 
- </span+ 
- Then, you have to load your file on your smartphone :  +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 !]] 
- <span class="​code"> + 
- File file = mContext.getFileStreamPath("​config_ecj_in.in"​);​ +<note>written by Ancelin Arnaud, Aliénor Diaz, Olivier Fauvel-Jaeger,​ Frédéric Guégan in 2013/06</note>
- </span+
- Finaly, you can write into your file thanks to a FileWrite and a BufferedWriter :  +
- <span class="​code"+
- FileWriter fileWriter = null;<br> +
- try {<br> +
- <span class="​unAlin">​fileWriter = new FileWriter(file);​<​br></​span>​ +
- } catch (IOException e1) {<br> +
- <span class="​unAlin">​e1.printStackTrace();​<​br></​span>​ +
- }<br> +
- BufferedWriter output = new BufferedWriter(fileWriter);​<br> +
-         ​try { <br> +
- <span class="​unAlin">​output.write("​Hello World ! ");<​br></​span>​ +
- }<br> +
-         ​output.flush(); ​ <​br>​ +
-         ​catch (Exception e) {       <br> +
-         <span class="​unAlin">​Toast.makeText(mContext,​ "​Settings not saved",​ Toast.LENGTH_SHORT).show(); ​<​br></​span>​ +
- <br> +
-         ​finally { <br> +
-         <span class="​unAlin">​try { <​br></​span>​ +
-             <span class="​deuxAlin">​output.close(); ​<​br></​span>​ +
-                 <span class="​deuxAlin">​fileWriter.close(); ​<​br></​span>​ +
-            <​span class="​unAlin">​} catch (IOException e) { <​br></​span>​ +
-             <span class="​deuxAlin">​Toast.makeText(mContext,​ "​Settings not saved",​ Toast.LENGTH_SHORT).show();​<​br></​span> ​ +
-             <span class="​unAlin">​<​br></​span>​ +
-       }<br> +
- </span+
- As you can understand, this code write the text "Hello World" into the file "​config_ecj_in.in"​.<br> +
- </p> +
- <div class="​return"><​a href="#begin">Back to begin</​a></​div>​ +
- <​h2><​div id="​useNewConfigFile">​7. Use a file in an another one</​div></​h2>​ +
- Now, you can use this file as in the previous ​<a href="#​collectInfo">example</a>.<br> +
- <p> +
- But, as we said before, ECJ may need a file. Or more precisely, the path of this file.<​br>​ +
-For a population initialization,​ you give the path of the initialization file to the configuration file. <​br>​ +
-This path is the following one: <​br>​ +
-<span class="​deuxAlin">​/​data/​data/​name_of_your_current_project/​files/​config_ecj_in.in. ​ </​span><​br><​br>​ +
- If you want to initialize your population "​config_ecj_in.in"​ from the  +
-configuration file "​mainConfigFile.params",​ add this line in  +
-"​mainConfigFile.params":​<br> +
- <span class="​code"+
- pop.file = /​data/​data/​name_of_your_current_project/​files/​config_ecj_in.in<br> +
- </span+
- Now, if you load "​mainConfigFile.params"​ as show in this <a href="#​collectInfo">example</​a> ​you will also initialize you popullation with "​config_ecj_in.in"​.<​br>​ +
- Simple, isn't it ?<​br>​ +
- Here is the end of our tutorial. Good luck ! <br> +
- </p>  +
- <div class="​return"><​a href="#begin">Back to begin</​a></​div>​ +
- <div id="​footer">​ +
- <p> +
- Any questions or suggestions ?  +
- <a href="​mailto:​guegan@polytech.unice.fr;​fauvelja@polytech.unice.fr;​adiaz@polytech.unice.fr;​arnaud@polyech.unice.fr?​subject=Tutorial%20ECJ%20in%20Android">Contact us !</a> +
- </p> +
- </div> +
-  ​+
  
-</​body></​html>​ 
tuto_ecj_android.txt · Last modified: 2014/03/03 17:27 by Denis Pallez