From 6420a4c6061e96dde62f5133bf46072f4bbcbb24 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Wed, 1 Apr 2026 14:34:07 +0200 Subject: [PATCH 1/2] =?UTF-8?q?A=20lot=20of=20things=C2=B2.=20-=20Change?= =?UTF-8?q?=20DataParsing=20to=20a=20generic=20interface.=20-=20Add=20Part?= =?UTF-8?q?iePoisson=20-=20Add=20comments=20to=20Population=20class.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ecoparasite/input/DataParsing.java | 16 +- .../input/InvalidParsingException.java | 37 +++++ src/ecoparasite/input/RawData.java | 2 +- src/ecoparasite/poisson/Mackerel.java | 36 ++++- src/ecoparasite/poisson/Merlu.java | 17 +- src/ecoparasite/poisson/PartiePoisson.java | 59 ++++++- src/ecoparasite/poisson/Poisson.java | 16 ++ src/ecoparasite/population/Population.java | 152 +++++++++++++----- .../population/PopulationArgInterval.java | 41 +++++ .../population/PopulationArgs.java | 50 ++++++ .../population/PopulationParsing.java | 24 ++- .../population/PopulationParsingTest.java | 5 +- 12 files changed, 401 insertions(+), 54 deletions(-) create mode 100644 src/ecoparasite/input/InvalidParsingException.java diff --git a/src/ecoparasite/input/DataParsing.java b/src/ecoparasite/input/DataParsing.java index c7e5352..dc6a0de 100644 --- a/src/ecoparasite/input/DataParsing.java +++ b/src/ecoparasite/input/DataParsing.java @@ -9,7 +9,21 @@ import java.util.HashSet; */ public interface DataParsing { - public static HashSet parse(RawData data) throws RawDataOverflow { + /** + * Permet de parser un élément à partir d'un objet RawData. + * @param data l'objet RawData + * @param parseTypeId Permet d'indiquer le type de Parsing que l'on veut. + * @return Un HashSet de données T retourné. + * @param Notre paramètre générique adaptable à plusieurs classes. + * @throws RawDataOverflow Si on dépasse la taille de l'objet RawData. + * @throws InvalidParsingException Si le type de parsing n'existe pas. + */ + public static HashSet parse(RawData data, int parseTypeId ) throws RawDataOverflow, InvalidParsingException { return null; } + + public static HashSet parse(RawData data) throws RawDataOverflow, InvalidParsingException { + return null; + } + } diff --git a/src/ecoparasite/input/InvalidParsingException.java b/src/ecoparasite/input/InvalidParsingException.java new file mode 100644 index 0000000..ea024bb --- /dev/null +++ b/src/ecoparasite/input/InvalidParsingException.java @@ -0,0 +1,37 @@ +package ecoparasite.input; + +/** + * Gère une exception lorsque qu'un type de parsing n'existe pas ou bien + * qu'une erreur est survenue durant le parsing d'une donnée. + */ +public class InvalidParsingException extends Exception { + + /** + * L'ID du parsing voulant être réalisé. + */ + private int parsingId; + + /** + * De quel classe provient ce parsing. + */ + private String parsingSource; + + /** + * Constructeur. + * @param parsingId ID du parsing + * @param parsingSource La classe d'où provient ce parsing. + */ + public InvalidParsingException(int parsingId, String parsingSource) { + this.parsingId = parsingId; + this.parsingSource = parsingSource; + super(); + } + + public int getParsingId() { + return parsingId; + } + public String getParsingSource() { + return parsingSource; + } + +} diff --git a/src/ecoparasite/input/RawData.java b/src/ecoparasite/input/RawData.java index 7e5c470..c8f244c 100644 --- a/src/ecoparasite/input/RawData.java +++ b/src/ecoparasite/input/RawData.java @@ -99,7 +99,7 @@ public class RawData { * 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 . - * @throws RawDataOverflow Si vous avez dépasses le maximum d'entrées du DataFrame. + * @throws RawDataOverflow Si vous avez dépassé le maximum d'entrées du DataFrame. */ public HashMap getEntry(int index) throws RawDataOverflow { diff --git a/src/ecoparasite/poisson/Mackerel.java b/src/ecoparasite/poisson/Mackerel.java index c5bd16c..b5acdab 100644 --- a/src/ecoparasite/poisson/Mackerel.java +++ b/src/ecoparasite/poisson/Mackerel.java @@ -1,6 +1,7 @@ package ecoparasite.poisson; import ecoparasite.input.DataParsing; +import ecoparasite.input.InvalidParsingException; import ecoparasite.input.RawData; import ecoparasite.input.RawDataOverflow; @@ -24,10 +25,25 @@ public class Mackerel extends Poisson implements DataParsing { super(id, length, null, infestation); } + /** + * Implémentation de la fonction parse de DataParsing. + * Renvoie un tableau de poissons à partir d'un RawData. + * + * @param data Notre RawData + * @param parseTypeId L'ID du type de parsing, ignoré ici. + * @return Le tableau de poissons. + * @throws RawDataOverflow Si on a un dépassement de données dans notre RawData. + * @throws InvalidParsingException + */ + public static HashSet parse(RawData data, int parseTypeId) throws RawDataOverflow, InvalidParsingException { + return Mackerel.parse(data); + } + /** * Implémentation de la fonction parse de Dataparsing - * @param data + * @param data Notre RawData. * @return tableau des poissons + * @throws RawDataOverflow Si on a un dépassement de données dans notre RawDataOverflow. */ public static HashSet parse(RawData data) throws RawDataOverflow { @@ -39,11 +55,27 @@ public class Mackerel extends Poisson implements DataParsing { for (int i = 0; i < data.getData().getFirst().size(); i++) { temp = data.getEntry(i); Poisson newP = new Mackerel(temp.get("Sample_code"),valueOf(temp.get("StandardLength")),valueOf(temp.get("NParasitesViscera"))); - + newP.setFishParts( Mackerel.parsePartiePoisson(temp) ); fishSet.add(newP); } return fishSet; } + + private static HashSet parsePartiePoisson(HashMap entry){ + + HashSet response = new HashSet<>(); + + for( String k: entry.keySet() ){ + if( k.contains( "Anisakis_" ) ){ + String bodyPart = k.split( "Anisakis_" )[1]; + Double value = valueOf(entry.get(k)); + PartiePoisson p = new PartiePoisson(bodyPart, value); + response.add(p); + } + } + + return response; + } } diff --git a/src/ecoparasite/poisson/Merlu.java b/src/ecoparasite/poisson/Merlu.java index b81f23f..810f5b3 100644 --- a/src/ecoparasite/poisson/Merlu.java +++ b/src/ecoparasite/poisson/Merlu.java @@ -1,6 +1,7 @@ package ecoparasite.poisson; import ecoparasite.input.DataParsing; +import ecoparasite.input.InvalidParsingException; import ecoparasite.input.RawData; import ecoparasite.input.RawDataOverflow; @@ -24,10 +25,24 @@ public class Merlu extends Poisson implements DataParsing { super(id, length, null, infestation); } + /** + * Implémentation de la fonction parse de DataParsing. + * Renvoie un tableau de poissons à partir d'un RawData. + * + * @param data Notre RawData + * @param parseTypeId L'ID du type de parsing, ignoré ici. + * @return Le tableau de poissons. + * @throws RawDataOverflow Si on a un dépassement de données dans notre RawData. + */ + public static HashSet parse(RawData data, int parseTypeId) throws RawDataOverflow, InvalidParsingException { + return Mackerel.parse(data); + } + /** * Implémentation de la fonction parse de Dataparsing - * @param data + * @param data Notre RawData * @return tableau des poissons + * @throws RawDataOverflow Si on a un dépassement des données de notre objet RawData. */ public static HashSet parse(RawData data) throws RawDataOverflow { diff --git a/src/ecoparasite/poisson/PartiePoisson.java b/src/ecoparasite/poisson/PartiePoisson.java index 63d1a1c..0a76080 100644 --- a/src/ecoparasite/poisson/PartiePoisson.java +++ b/src/ecoparasite/poisson/PartiePoisson.java @@ -1,4 +1,61 @@ package ecoparasite.poisson; -public abstract class PartiePoisson { +/** + * Permet d'illustrer une partie d'un poisson. + */ +public class PartiePoisson { + + /** + * Nom de la partie du poisson. + */ + private String name; + + /** + * Le taux d'infestation de cette partie du poisson. + */ + private Double infestation; + + /** + * Constructeur. + * Génère une partie complète. + * + * @param name Le nom de la partie. + * @param infestation Le taux d'infestation de la partie du poisson. + */ + public PartiePoisson(String name, Double infestation) { + this.name = name; + this.infestation = infestation; + } + + /** + * Constructeur. + * Génère une infestation de null comme non communiqué. + * + * @param name Le nom de la partie. + */ + public PartiePoisson(String name){ + this.name = name; + this.infestation = null; + } + + /* + GETTERS / SETTERS + */ + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Double getInfestation() { + return infestation; + } + + public void setInfestation(Double infestation) { + this.infestation = infestation; + } + } diff --git a/src/ecoparasite/poisson/Poisson.java b/src/ecoparasite/poisson/Poisson.java index ae70357..58055e1 100644 --- a/src/ecoparasite/poisson/Poisson.java +++ b/src/ecoparasite/poisson/Poisson.java @@ -61,6 +61,22 @@ public class Poisson{ return infestation; } + /** + * Getter de l'attribut des parties de poisson. + * @return Le HashSet des parties de poisson. + */ + public HashSet getFishParts() { + return fishParts; + } + + /** + * Setter de l'attribut des parties de poisson. + * @param fishParts Un hashset de parties de poisson. + */ + public void setFishParts(HashSet fishParts) { + this.fishParts = fishParts; + } + /** * Permet d'afficher les informations de notre poisson * @return La string contenant les informations diff --git a/src/ecoparasite/population/Population.java b/src/ecoparasite/population/Population.java index 51404be..fcb09cf 100644 --- a/src/ecoparasite/population/Population.java +++ b/src/ecoparasite/population/Population.java @@ -1,50 +1,118 @@ - package ecoparasite.population; +package ecoparasite.population; - import java.util.HashMap; - import java.util.HashSet; +import ecoparasite.input.DataParsing; +import ecoparasite.input.InvalidParsingException; +import ecoparasite.input.RawData; +import ecoparasite.input.RawDataOverflow; - public class Population { +import java.util.HashMap; +import java.util.HashSet; - private String id; - private PopulationArgs total; - private HashMap perYear; +/** + * Permet de gérer une population selon les années. + */ +public class Population implements DataParsing { - public Population(String id, PopulationArgs total, HashMap perYear) { - this.id = id; - this.total = total; - this.perYear = perYear; - } + /** + * Espèce de la population. + */ + private String id; - public Population(String id, PopulationArgs total) { - this.id = id; - this.total = total; - this.perYear = new HashMap<>(); - } + /** + * Les informations de la population sur le total. + */ + private PopulationArgs total; - public Population(String id) { - this.id = id; - this.total = null; - this.perYear = new HashMap<>(); - } - - public String getId() { - return id; - } - - public PopulationArgs getTotal() { - return total; - } - - public HashMap getPerYear() { - return perYear; - } - - public void setTotal(PopulationArgs total) { - this.total = total; - } - - public void setPerYear(HashMap perYear) { - this.perYear = perYear; - } + /** + * Les informations de la population par année. + * Clé : Année, Valeur : Informations de la population. + */ + private HashMap perYear; + /** + * Constructeur. + * @param id Espèce + * @param total Les informations totales. + * @param perYear Les informations par année. + */ + public Population(String id, PopulationArgs total, HashMap perYear) { + this.id = id; + this.total = total; + this.perYear = perYear; } + + /** + * Constructeur. + * Pour les informations par année, crée une HashMap vide. Devra être rempli après coup. + * @param id Espèce de la population. + * @param total Les informations totales. + */ + public Population(String id, PopulationArgs total) { + this.id = id; + this.total = total; + this.perYear = new HashMap<>(); + } + + /** + * Constructeur. + * Pour les informations du total, sont remplacés par null. + * Pour les informations par année, crée une HashMap vide. Devra être rempli après coup. + * @param id Espèce de la population. + */ + public Population(String id) { + this.id = id; + this.total = null; + this.perYear = new HashMap<>(); + } + + /* + GETTERS / SETTERS + */ + + public String getId() { + return id; + } + + public PopulationArgs getTotal() { + return total; + } + + public HashMap getPerYear() { + return perYear; + } + + public void setTotal(PopulationArgs total) { + this.total = total; + } + + public void setPerYear(HashMap perYear) { + this.perYear = perYear; + } + + /** + * Raccourci vers les méthodes de DataParsing. + * @param data Notre objet RawData. + * @param parseTypeId L'ID de parsing. + * @return + */ + public static HashSet parse(RawData data, int parseTypeId ) throws RawDataOverflow, InvalidParsingException { + switch (parseTypeId) { + case 1: + return PopulationParsing.parseParasitesPeru(data); + default: + throw new InvalidParsingException(parseTypeId, Population.class.getSimpleName() ); + } + } + + /** + * Raccourci vers les méthodes de DataParsing. + * Va par défaut faire ceux des parasites du Perou. + * + * @param data Notre objet RawData. + * @return Notre tableau de populations. + */ + public static HashSet parse(RawData data){ + return PopulationParsing.parseParasitesPeru(data); + } + +} diff --git a/src/ecoparasite/population/PopulationArgInterval.java b/src/ecoparasite/population/PopulationArgInterval.java index f85e9dc..caff7c0 100644 --- a/src/ecoparasite/population/PopulationArgInterval.java +++ b/src/ecoparasite/population/PopulationArgInterval.java @@ -1,17 +1,44 @@ package ecoparasite.population; +/** + * Classe qui permet de gérer un intervalle de données issues d'un CSV. + * + * Trois données : minimum, maximum et moyenne entre minimum et maximum. + */ public class PopulationArgInterval { + /** + * Valeur minimale. + */ private Double min; + + /** + * Valeur maximale. + */ private Double max; + /** + * Moyenne entre la valeur minimale et maximale. + */ private Double mean; + /** + * Constructeur. La moyenne doit être précisée. + * @param min Valeur minimale + * @param max Valeur maximale + * @param mean Moyenne + */ public PopulationArgInterval(Double min, Double max, Double mean) { this.min = min; this.max = max; this.mean = mean; } + + /** + * Constructeur. La moyenne est calculé automatiquement. + * @param min Valeur minimale + * @param max Valeur maximale. + */ public PopulationArgInterval(Double min, Double max) { this.min = min; this.max = max; @@ -28,12 +55,26 @@ public class PopulationArgInterval { return this.mean; } + /** + * Permet de transformer un intervalle en double. + * Si le minimum est égal au maximum, cette valeur est retourné. + * Sinon la moyenne est retourné. + * + * @return La valeur transformée en Double. + */ public Double transformToDouble(){ if( this.min == this.max ) return this.min; return this.mean; } + /** + * Permet de transformer une String (Extraite d'un fichier CSV) et de la transformer en Intervalle. + * Gère les séparateurs +/- ainsi que l'entre deux. Sinon, elle sera juste convertie avec minimum = maximum. + * + * @param rawValue La valeur brute. + * @return La valeur en tant qu'intervalle. + */ public static PopulationArgInterval fromString( String rawValue ){ if( rawValue.contains( "±" ) ){ // Plus ou moins. diff --git a/src/ecoparasite/population/PopulationArgs.java b/src/ecoparasite/population/PopulationArgs.java index 5c788ce..8ae149e 100644 --- a/src/ecoparasite/population/PopulationArgs.java +++ b/src/ecoparasite/population/PopulationArgs.java @@ -1,9 +1,19 @@ package ecoparasite.population; +/** + * Permet de stocker les paramètres d'une population pour une année spécifique. + * Par convention, si on est sur le total, on mettra comme année 0. + */ public class PopulationArgs { + /** + * Année de référence pour ces paramètres. + */ private int year; + /** + * Le nombre d'individus analysés. + */ private int number; private PopulationArgInterval length; private PopulationArgInterval width; @@ -12,6 +22,19 @@ public class PopulationArgs { private Double intensity; private Double abondance; + /** + * Constructeur. + * Toutes les données peuvent être préremplis dans ce constructeur sauf l'année. + * L'année sera par défaut à zéro. + * + * @param N + * @param length + * @param width + * @param prevalence + * @param ic + * @param intensity + * @param abondance + */ public PopulationArgs( int N, PopulationArgInterval length, @@ -31,6 +54,19 @@ public class PopulationArgs { this.abondance = abondance; } + /** + * Constructeur. + * Toutes les données ainsi que l'année peuvent être préremplis. + * + * @param year + * @param N + * @param length + * @param width + * @param prevalence + * @param ic + * @param intensity + * @param abondance + */ public PopulationArgs( int year, int N, @@ -51,16 +87,30 @@ public class PopulationArgs { this.abondance = abondance; } + /** + * Constructeur. + * Permet de remplir tout d'abord l'année, les autres paramètres seront remplis par les setters. + * + * @param year + */ public PopulationArgs( int year ){ this.year = year; } + /** + * Constructeur. + * Initialise l'objet avec une année par défaut (0). + */ public PopulationArgs(){ this.year = 0; } + /* + GETTERS / SETTERS + */ + public int getYear() { return year; } diff --git a/src/ecoparasite/population/PopulationParsing.java b/src/ecoparasite/population/PopulationParsing.java index 7813d62..e82f4f2 100644 --- a/src/ecoparasite/population/PopulationParsing.java +++ b/src/ecoparasite/population/PopulationParsing.java @@ -6,9 +6,18 @@ import ecoparasite.input.RawDataOverflow; import java.util.HashMap; import java.util.HashSet; +/** + * Permet de parser une population spécifique via les schémas fournis. + */ public class PopulationParsing { - public static HashMap parseParasitesPeru(RawData peruRawData ){ + /** + * Permet, à partir d'un objet RawData, de parse les données tel que le fichier parasitesPeru est structuré. + * + * @param peruRawData + * @return Une liste des populations incluses. + */ + public static HashSet parseParasitesPeru(RawData peruRawData ){ HashMap response = new HashMap<>(); @@ -19,9 +28,9 @@ public class PopulationParsing { HashMap fields = peruRawData.getEntry(index); String espece = fields.get("Espèce"); - System.out.println(espece); String parametre = fields.get("Paramètre"); + // Récupère la population si elle existe déjà. Population population = null; if( response.containsKey( espece ) ){ population = response.get(espece); @@ -59,13 +68,20 @@ public class PopulationParsing { response.put( espece, population ); index++; } - } catch (RawDataOverflow e){ + } catch (RawDataOverflow e){ // Débordement, on a atteint la fin de l'objet RawData. // Stop. } - return response; + return new HashSet( response.values() ); } + /** + * Permet d'appliquer la valeur au paramètre respectif en se basant sur le nom de la colonne. + * + * @param populationArgs Les paramètres de la population actuelle. + * @param column Le nom de la colonne dans le fichier CSV + * @param value La valeur a affecter + */ private static void applyValueForParasitesPeru( PopulationArgs populationArgs, String column, String value ){ switch( column ){ case "N": diff --git a/tests/ecoparasite/population/PopulationParsingTest.java b/tests/ecoparasite/population/PopulationParsingTest.java index 4be0184..0b7a904 100644 --- a/tests/ecoparasite/population/PopulationParsingTest.java +++ b/tests/ecoparasite/population/PopulationParsingTest.java @@ -6,7 +6,7 @@ import ecoparasite.input.RawData; import ecoparasite.input.RawDataOverflow; import org.junit.jupiter.api.Test; -import java.util.HashMap; +import java.util.HashSet; import static org.junit.jupiter.api.Assertions.*; @@ -16,7 +16,8 @@ class PopulationParsingTest { void parseParasitesPeru() throws InputFileException, RawDataOverflow { RawData parasitesPeru = InputFactory.readData( InputFactory.DATA_PARASITES_PERU, "," ); - HashMap populations = PopulationParsing.parseParasitesPeru( parasitesPeru ); + HashSet populations = Population.parse( parasitesPeru ); System.out.println( populations); } + } \ No newline at end of file From 1a1e7a24d9fca7c08fb8fb68a6cd16d50e0dbcca Mon Sep 17 00:00:00 2001 From: Benjamin Date: Wed, 1 Apr 2026 15:05:27 +0200 Subject: [PATCH 2/2] Update Puml --- UML/classes.puml | 51 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/UML/classes.puml b/UML/classes.puml index e48eb5d..59124fb 100644 --- a/UML/classes.puml +++ b/UML/classes.puml @@ -27,7 +27,7 @@ namespace ecoparasite { InputFactory o--> RawData interface DataParsing { - + HashSet parse() + + HashSet parse() } exception InputFileException { @@ -41,6 +41,11 @@ namespace ecoparasite { + String getMessage() } + exception InvalidParsingException { + - int parsingId + - String parsingClass + } + } namespace ecoparasite.poissons { @@ -64,12 +69,54 @@ namespace ecoparasite { + Mackerel() } - abstract class PartiePoisson { + class PartiePoisson { + - String name + - Double infestation + + PartiePoisson(String name, Double infestation) } Poisson o--> PartiePoisson : # fishParts } + namespace ecoparasite.population { + + class Population implements ecoparasite.input.DataParsing { + - String id + - HashMap perYear + + Population() + } + + Population o--> PopulationArgs : - total + + class PopulationArgs { + - int year + - int number + - Double intensity + - Double abondance + + PopulationArgs() + } + + PopulationArgs o--> PopulationArgInterval : - length + PopulationArgs o--> PopulationArgInterval : - width + PopulationArgs o--> PopulationArgInterval : - prevalence + PopulationArgs o--> PopulationArgInterval : - ic + + class PopulationArgInterval { + - Double min + - Double max + - Double mean + + PopulationArgInterval() + + PopulationArgInterval() + + Double transformToDouble() + + {static} PopulationArgInterval fromString() + } + + class PopulationParsing { + + {static} HashSet parseParasitesPeru() + - {static} void applyValueForParasitesPeru() + } + } + namespace ecoparasite.unknown { note top of ecoparasite.unknown : Ce paquet est temporaire pour des classes / interfaces qui devront avoir plus de déclinaisons.