Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 44dab13308 |
@@ -4,9 +4,11 @@
|
|||||||
<exclude-output />
|
<exclude-output />
|
||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
<orderEntry type="library" name="javafx-swt" level="application" />
|
<orderEntry type="library" name="javafx-swt" level="application" />
|
||||||
|
<orderEntry type="library" name="junit.jupiter" level="project" />
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
||||||
|
Can't render this file because it is too large.
|
|
Can't render this file because it contains an unexpected character in line 1 and column 1304.
|
105
src/ecoparasite/input/InputFactory.java
Normal file
105
src/ecoparasite/input/InputFactory.java
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
package ecoparasite.input;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
public class InputFactory {
|
||||||
|
|
||||||
|
final private static String DATA_FOLDER = "data/";
|
||||||
|
|
||||||
|
final public static String DATA_MACKEREL = "Campagne/mackerel.97442.csv";
|
||||||
|
final public static String DATA_MERLU = "Campagne/merlu2018_75164.csv";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construit le chemin vers le fichier de données voulu.
|
||||||
|
* @param data De préference via une constante DATA_... de notre classe actuelle.
|
||||||
|
* @return Le chemin complet vers le dossier
|
||||||
|
*/
|
||||||
|
public static String buildDataPath( String data ){
|
||||||
|
return InputFactory.DATA_FOLDER + data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Permet de lire les données brutes d'un fichier CSV.
|
||||||
|
* @param dataFileName Le chemin du fichier CSV, tiré des constantes DATA_... de notre classe actuelle.
|
||||||
|
* @return Une instance de RawData permettant de manipuler les données.
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public static RawData readData( String dataFileName ) throws InputFileException {
|
||||||
|
return readData( dataFileName, ";" );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Permet de lire les données brutes d'un fichier CSV.
|
||||||
|
* @param dataFileName Le chemin du fichier CSV, tiré des constantes DATA_... de notre classe actuelle.
|
||||||
|
* @param separator Le séparateur du fichier CSV, par défaut : ;
|
||||||
|
* @return Une instance de RawData permettant de manipuler les données.
|
||||||
|
*
|
||||||
|
* @throws InputFileException - Une erreur durant la lecture du fichier.
|
||||||
|
*/
|
||||||
|
public static RawData readData( String dataFileName, String separator ) throws InputFileException {
|
||||||
|
|
||||||
|
Path path = Paths.get(InputFactory.buildDataPath(dataFileName));
|
||||||
|
|
||||||
|
BufferedReader reader;
|
||||||
|
try {
|
||||||
|
reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new InputFileException(dataFileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
return createRawData(dataFileName, reader, separator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fonction appellé par readData, permet de créer l'instance de RawData stockant les données.
|
||||||
|
*
|
||||||
|
* @param dataFileName Le nom du fichier.
|
||||||
|
* @param reader Le BufferedReader des données.
|
||||||
|
* @param separator Le séparateur du fichier CSV, par défaut ";"
|
||||||
|
* @return L'instance RawData.
|
||||||
|
*
|
||||||
|
* @throws InputFileException Si une erreur durant la lecture d'une ligne s'est produite.
|
||||||
|
*/
|
||||||
|
private static RawData createRawData( String dataFileName, BufferedReader reader, String separator ) throws InputFileException {
|
||||||
|
|
||||||
|
ArrayList<ArrayList<String>> data = new ArrayList<>();
|
||||||
|
String line;
|
||||||
|
|
||||||
|
// Lecture de la première ligne, le nom des colonnes.
|
||||||
|
try {
|
||||||
|
line = reader.readLine();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new InputFileException( dataFileName );
|
||||||
|
}
|
||||||
|
String[] rowFields = line.split(separator);
|
||||||
|
|
||||||
|
for( int i = 0; i < rowFields.length; i++ ){
|
||||||
|
ArrayList<String> element = new ArrayList<>();
|
||||||
|
element.add(rowFields[i]);
|
||||||
|
data.add( element );
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lecture des autres lignes.
|
||||||
|
try {
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
rowFields = line.split(separator);
|
||||||
|
for (int i = 0; i < rowFields.length; i++) {
|
||||||
|
ArrayList<String> element = data.get(i);
|
||||||
|
element.add(rowFields[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new InputFileException( dataFileName );
|
||||||
|
}
|
||||||
|
|
||||||
|
return new RawData(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
19
src/ecoparasite/input/InputFileException.java
Normal file
19
src/ecoparasite/input/InputFileException.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package ecoparasite.input;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Est déclenché si une erreur est survenue lors de la lecture d'un fichier.
|
||||||
|
*/
|
||||||
|
public class InputFileException extends Exception {
|
||||||
|
|
||||||
|
final private String dataFilePath;
|
||||||
|
|
||||||
|
public InputFileException(String dataFilePath) {
|
||||||
|
this.dataFilePath = dataFilePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMessage(){
|
||||||
|
return "Une erreur a été déclenché durant la lecture du fichier d'entrée : " + dataFilePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
122
src/ecoparasite/input/RawData.java
Normal file
122
src/ecoparasite/input/RawData.java
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
package ecoparasite.input;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Permet de stocker les données brutes d'un fichier chargé CSV, avant d'être changé en Classe plus spécifique.
|
||||||
|
*/
|
||||||
|
public class RawData {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stocke les données brutes sous formes de deux ArrayList.
|
||||||
|
*/
|
||||||
|
final private ArrayList<ArrayList<String>> data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructeur.
|
||||||
|
* @param data ArrayList qui demande des ArrayList<String> pour faire des lignes/colonnes.
|
||||||
|
*/
|
||||||
|
public RawData(ArrayList<ArrayList<String>> data) {
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter des données brutes. À n'utiliser que pour des opérations compliquées non descriptibles par une fonction.
|
||||||
|
* @return ArrayList d'ArrayList de String qui contient les données brutes.
|
||||||
|
*/
|
||||||
|
public ArrayList<ArrayList<String>> getData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Permet d'avoir un affichage debug du contenu de notre jeu de données.
|
||||||
|
* @return La string symbolisant nos données.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String toString()
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for( int i = 0; i < data.size(); i++ )
|
||||||
|
{
|
||||||
|
ArrayList<String> row = data.get(i);
|
||||||
|
for( int j = 0; j < row.size(); j++ ){
|
||||||
|
sb.append(row.get(j)).append(" | ");
|
||||||
|
}
|
||||||
|
sb.append(System.lineSeparator());
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Permet d'obtenir le nom des colonnes de notre jeu de données.
|
||||||
|
*
|
||||||
|
* @return HashSet du nom des colonnes
|
||||||
|
*/
|
||||||
|
public HashSet<String> getColumnsNames(){
|
||||||
|
|
||||||
|
HashSet<String> columns = new HashSet<>();
|
||||||
|
for (ArrayList<String> row : data) {
|
||||||
|
for (String colName : row) {
|
||||||
|
columns.add(colName);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return columns;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Permet d'avoir toutes les valeurs associées à la colonne.
|
||||||
|
* @param colName Le nom de la colonne dans le fichier CSV
|
||||||
|
* @return HashSet des données brutes.
|
||||||
|
*/
|
||||||
|
public HashSet<String> getDataFromColumn( String colName ){
|
||||||
|
|
||||||
|
boolean goodColumn = false;
|
||||||
|
HashSet<String> rows = new HashSet<>();
|
||||||
|
|
||||||
|
for( ArrayList<String> row : data ){
|
||||||
|
for( String value : row ) {
|
||||||
|
if( !goodColumn ) {
|
||||||
|
if (colName.equals(value))
|
||||||
|
goodColumn = true;
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
} else { // Si on a trouvé la bonne colonne, on ajoute les éléments.
|
||||||
|
rows.add(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if( goodColumn )
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Permet d'obtenir tous les champs d'une entrée.
|
||||||
|
* @param index Entrée que vous voulez obtenir, le minimum est l'entrée 1. Le maximum dépend du Dataframe.
|
||||||
|
* @return Une HashMap <colonne, valeur>.
|
||||||
|
* @throws RawDataOverflow Si vous avez dépasses le maximum d'entrées du DataFrame.
|
||||||
|
*/
|
||||||
|
public HashMap<String,String> getEntry(int index) throws RawDataOverflow {
|
||||||
|
|
||||||
|
if( index <= 0 ){
|
||||||
|
index = 1; // 0 = nombre de colonnes, fallback.
|
||||||
|
}
|
||||||
|
|
||||||
|
HashMap<String,String> entry = new HashMap<>();
|
||||||
|
|
||||||
|
for( ArrayList<String> row : data ){
|
||||||
|
if( index > row.size()){
|
||||||
|
throw new RawDataOverflow( index, row.size() );
|
||||||
|
}
|
||||||
|
entry.put( row.getFirst(), row.get( index ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
22
src/ecoparasite/input/RawDataOverflow.java
Normal file
22
src/ecoparasite/input/RawDataOverflow.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package ecoparasite.input;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Est déclenché lors du dépassement d'index lors de la récupération d'une entrée d'un RawData.
|
||||||
|
*/
|
||||||
|
public class RawDataOverflow extends Exception {
|
||||||
|
|
||||||
|
final private int excepted;
|
||||||
|
final private int limit;
|
||||||
|
|
||||||
|
public RawDataOverflow( int excepted, int limit ) {
|
||||||
|
this.excepted = excepted;
|
||||||
|
this.limit = limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMessage()
|
||||||
|
{
|
||||||
|
return "Dépassement d'index : " + this.excepted + " voulu, " + this.limit + " maximum autorisé par le RawData.";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
21
tests/ecoparasite/input/InputFactoryTest.java
Normal file
21
tests/ecoparasite/input/InputFactoryTest.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package ecoparasite.input;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class InputFactoryTest {
|
||||||
|
|
||||||
|
// TODO : Test a être effectué par Sébastien.
|
||||||
|
@Test
|
||||||
|
void readFile() throws InputFileException, RawDataOverflow {
|
||||||
|
RawData r = InputFactory.readData( InputFactory.DATA_MACKEREL );
|
||||||
|
System.out.println( r );
|
||||||
|
System.out.println( r.getColumnsNames() );
|
||||||
|
System.out.println( r.getDataFromColumn( "Sample_codE" ) );
|
||||||
|
System.out.println( r.getEntry( 2 ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user