Skip to content
Snippets Groups Projects
Commit 549f8caa authored by Matias Mennecart's avatar Matias Mennecart
Browse files

Ajout de la possibilité de selectionner l'attribut sur lequel classifié

parent 08888531
Branches
Tags
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.Scene?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<?import javafx.stage.Stage?>
<Stage fx:id="stage" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fr.univlille.sae.classification.controller.ChooseAttributesController">
<scene>
<Scene>
<AnchorPane prefHeight="175.0" prefWidth="317.0">
<children>
<VBox alignment="CENTER" prefHeight="200.0" prefWidth="315.0" spacing="30.0" stylesheets="@../css/style.css">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Veuillez selectionner l'attribut sur lequel vous souhaitez classifier les données" textAlignment="CENTER" wrappingWidth="254.06924438476562" />
<ChoiceBox fx:id="choice" prefWidth="150.0" stylesheets="@../css/style.css" />
<Button mnemonicParsing="false" onAction="#validate" stylesheets="@../css/style.css" text="Valider" textFill="WHITE" />
</children>
</VBox>
</children></AnchorPane>
</Scene>
</scene>
</Stage>
......@@ -68,14 +68,14 @@
</HBox>
<VBox prefHeight="65.0" prefWidth="801.0">
<children>
<HBox fx:id="legend" alignment="CENTER" prefHeight="58.0" prefWidth="762.0" spacing="10.0">
<VBox fx:id="legend" alignment="CENTER" prefHeight="58.0" prefWidth="762.0" spacing="10.0">
<opaqueInsets>
<Insets />
</opaqueInsets>
<padding>
<Insets left="2.0" right="2.0" />
</padding>
</HBox>
</VBox>
</children>
</VBox>
</children>
......
package fr.univlille.sae.classification.controller;
import fr.univlille.sae.classification.model.ClassificationModel;
import fr.univlille.sae.classification.model.LoadableData;
import javafx.fxml.FXML;
import javafx.scene.control.ChoiceBox;
import javafx.stage.Stage;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ChooseAttributesController {
@FXML
Stage stage;
@FXML
ChoiceBox<String> choice;
@FXML
public void initialize() {
LoadableData rdmData = ClassificationModel.getClassificationModel().getDatas().get(0);
choice.getItems().addAll(rdmData.getClassifiedAttributes().keySet());
String value = rdmData.getAttributesNames().keySet().toArray(new String[0])[rdmData.getClassificationType()];
if(!rdmData.getClassifiedAttributes().containsKey(value)) {
value = (String) rdmData.getClassifiedAttributes().keySet().toArray()[0];
}
choice.setValue(value);
}
public void validate() {
String selected = choice.getValue();
LoadableData rdmData = ClassificationModel.getClassificationModel().getDatas().get(0);
List<String> temp = new ArrayList<>(rdmData.getAttributesNames().keySet());
System.out.println(selected);
System.out.println("Index: " + temp.indexOf(selected));
try {
rdmData.setClassificationType(temp.indexOf(selected));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
stage.close();
}
}
......@@ -4,6 +4,8 @@ import com.opencsv.exceptions.CsvException;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
import fr.univlille.sae.classification.model.ClassificationModel;
import fr.univlille.sae.classification.model.DataType;
import fr.univlille.sae.classification.view.ChooseAttributesView;
import fr.univlille.sae.classification.view.MainStageView;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
......@@ -77,6 +79,8 @@ public class LoadDataController {
ClassificationModel.getClassificationModel().setType(typeChoisi);
try {
ClassificationModel.getClassificationModel().loadData(file);
ChooseAttributesView chooseAttributesView = new ChooseAttributesView(ClassificationModel.getClassificationModel(), (Stage) stage.getOwner());
chooseAttributesView.show();
}catch (RuntimeException | CsvRequiredFieldEmptyException e) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.initOwner(stage);
......
......@@ -90,14 +90,11 @@ public class ClassificationModel extends Observable {
.withType(type.getClazz())
.build().parse();
Set<String> types = new HashSet<>();
for (LoadableData d : datas) {
types.add(d.getClassification());
}
Collections.shuffle(datas);
LoadableData.setClassificationTypes(types);
LoadableData.setClassificationTypes(getDatas());
notifyObservers();
} catch (IOException e) {
System.err.println("Erreur lors du chargement des données : " + e.getMessage());
......
......@@ -34,6 +34,7 @@ public class Iris extends LoadableData {
*/
public Iris(double sepalLength, double sepalWidth, double petalLength, double petalWidth, String variety) {
super();
classificationType = 4;
this.sepalWidth = sepalWidth;
this.sepalLength = sepalLength;
this.petalWidth = petalWidth;
......@@ -60,31 +61,55 @@ public class Iris extends LoadableData {
}
/**
* Renvoie la classification (variété) de l'Iris.
* @return variété de l'Iris.
* Renvoie la classification de l'objet.
*
* @return classification sous forme de chaîne.
*/
@Override
public String getClassification() {
return variety;
public String getClassification() throws IllegalAccessException {
return (String) this.getClass().getDeclaredFields()[classificationType].get(this).toString();
}
/**
* Permet de modifier l'attribut sur lequelle ont souhaite classifier
* Ne sont valable que les attributs present dans getClassificationAttributes()
*
* Le numéro de l'attribut correspond a sa place dans la liste de tous le attributs.
* @param classificationType
*/
@Override
public void setClassificationType(int classificationType) throws IllegalArgumentException, IllegalAccessException {
if(classificationType < 0 || classificationType > getAttributesNames().size()) throw new IllegalArgumentException("Cette attribut n'existe pas");
String keyToVerify = getAttributesNames().keySet().toArray(new String[0])[classificationType];
if(!getClassifiedAttributes().containsKey(keyToVerify)) throw new IllegalArgumentException("Cette attribut ne peut pas être utiliser pour la classification");
LoadableData.classificationType = classificationType;
System.out.println("Set type to : " + classificationType);
LoadableData.setClassificationTypes(ClassificationModel.getClassificationModel().getDatas());
}
@Override
public Map<String, Object> getClassifiedAttributes() {
return Map.of();
Map<String, Object> attributes = new LinkedHashMap<>();
attributes.put("Variété", variety);
return attributes;
}
@Override
public int getClassificationType() {
return 0;
return classificationType;
}
/**
* Définit la classification (variété) de l'Iris.
* @param classification variété à définir.
* Définit la classification de l'objet.
*
* @param classification classification à définir.
*/
@Override
public void setClassification(String classification) {
this.variety = classification;
public void setClassification(String classification) throws IllegalAccessException {
this.getClass().getDeclaredFields()[classificationType].set("", classification);
}
/**
......@@ -146,6 +171,7 @@ public class Iris extends LoadableData {
attrNames.put("Largeur des sépales", sepalWidth);
attrNames.put("Longueur des pétales", petalLength);
attrNames.put("Largeur des pétales", petalWidth);
attrNames.put("Variété", variety);
return attrNames;
}
......@@ -155,6 +181,7 @@ public class Iris extends LoadableData {
*/
@Override
public String toString() {
try {
return (
"Sepal length: " + sepalLength + "\n" +
"Sepal width: " + sepalWidth + "\n" +
......@@ -162,5 +189,8 @@ public class Iris extends LoadableData {
"Petal width: " + petalWidth + "\n" +
"Variety: " + getClassification()
);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
......@@ -2,12 +2,9 @@ package fr.univlille.sae.classification.model;
import javafx.scene.paint.Color;
import java.util.Map;
import java.util.HashMap;
import java.util.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* Classe abstraite représentant des données pouvant être chargées.
......@@ -18,6 +15,8 @@ public abstract class LoadableData {
private static Map<String, Color> classification = new HashMap<>() ;
protected static int classificationType = 1;
/**
* Constructeur par défaut.
*/
......@@ -43,12 +42,25 @@ public abstract class LoadableData {
return classification;
}
public static void setClassificationTypeGlobal(int classificationType) throws IllegalArgumentException, IllegalAccessException {
ClassificationModel.getClassificationModel().getDatas().get(0).setClassificationType(classificationType);
}
public abstract void setClassificationType(int classificationType) throws IllegalArgumentException, IllegalAccessException;
/**
*
* Définit les types de classification disponibles.
* @param classificationTypes ensemble de types de classification à définir.
*/
public static void setClassificationTypes(Set<String> classificationTypes) {
LoadableData.classificationTypes = classificationTypes;
public static void setClassificationTypes(List<LoadableData> datas) throws IllegalAccessException {
Set<String> types = new HashSet<>();
for (LoadableData d : datas) {
types.add(d.getClassification());
}
classificationTypes = types;
LoadableData.classification.clear();
int nbOfColors = classificationTypes.size() + 1;
int nb = 0;
......
......@@ -36,10 +36,11 @@ public class Pokemon extends LoadableData{
@CsvBindByName(column = "is_legendary")
private boolean isLegendary;
private int classificationType = 5;
public Pokemon(String name, int attack, int baseEggSteps, double captureRate, int defense, int experienceGrowth, int hp, int spAttack, int spDefense, String type1, String type2, double speed, boolean isLegendary) {
super();
classificationType = 9;
this.name = name;
this.attack = attack;
this.baseEggSteps = baseEggSteps;
......@@ -98,17 +99,21 @@ public class Pokemon extends LoadableData{
* Le numéro de l'attribut correspond a sa place dans la liste de tous le attributs.
* @param classificationType
*/
public void setClassificationType(int classificationType) throws IllegalArgumentException{
public void setClassificationType(int classificationType) throws IllegalArgumentException, IllegalAccessException {
if(classificationType < 0 || classificationType > getAttributesNames().size()) throw new IllegalArgumentException("Cette attribut n'existe pas");
if(getClassifiedAttributes().containsKey(getAttributesNames().keySet().toArray()[classificationType])) throw new IllegalArgumentException("Cette attribut ne peut pas être utiliser pour la classification");
this.classificationType = classificationType;
String keyToVerify = getAttributesNames().keySet().toArray(new String[0])[classificationType];
if(!getClassifiedAttributes().containsKey(keyToVerify)) throw new IllegalArgumentException("Cette attribut ne peut pas être utiliser pour la classification");
LoadableData.classificationType = classificationType;
System.out.println("Set type to : " + classificationType);
LoadableData.setClassificationTypes(ClassificationModel.getClassificationModel().getDatas());
}
public Map<String, Object> getClassifiedAttributes() {
Map<String, Object> attributes = new HashMap<>();
Map<String, Object> attributes = new LinkedHashMap<>();
attributes.put("Experience growth", experienceGrowth);
attributes.put("Type 1", type1);
......
package fr.univlille.sae.classification.view;
import fr.univlille.sae.classification.controller.AddDataController;
import fr.univlille.sae.classification.controller.ChooseAttributesController;
import fr.univlille.sae.classification.model.ClassificationModel;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Alert;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class ChooseAttributesView {
private ClassificationModel model;
private Stage owner;
/**
* Constructeur pour initialiser la vue de choix des attributs
* @param model le modèle de classification
* @param owner la fenêtre parente de cette vue.
*/
public ChooseAttributesView(ClassificationModel model, Stage owner) {
this.model = model;
this.owner = owner;
}
public void show() {
FXMLLoader loader = new FXMLLoader();
URL fxmlFileUrl = getClass().getClassLoader().getResource("stages"+ File.separator+"choose-attributes.fxml");
if (fxmlFileUrl == null) {
System.out.println("Impossible de charger le fichier fxml");
System.exit(-1);
}
loader.setLocation(fxmlFileUrl);
try {
Stage root = loader.load();
// ChooseAttributesController controller = loader.getController();
if (model.getDatas().isEmpty()) {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Erreur");
alert.setHeaderText(null);
alert.setContentText("Veuillez d'abord charger les données avant de pouvoir choisir l'attribut a classifier");
alert.showAndWait();
return;
}
root.setResizable(false);
root.initOwner(owner);
root.initModality(Modality.APPLICATION_MODAL);
root.setTitle("Choix d'attribut");
root.showAndWait();
} catch (IOException e) {
System.out.println("Erreur lors du chargement de la scène : " + e.getMessage());
e.printStackTrace();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment