json import

This commit is contained in:
Guillaume BONABAU 2024-05-16 10:35:28 +02:00 committed by g le-chartier
parent 5e462cfd39
commit 204fd32ba7
6 changed files with 127 additions and 64 deletions

View File

@ -17,5 +17,7 @@ With the implied additional rules:
5. Any dead cell who doesnt have exactly 3 living neighbors stays dead,
unchanged.
TEST
To download the json library:
https://code.google.com/archive/p/json-simple/downloads
tutorial to install it on vscode:
https://www.youtube.com/watch?v=g6vvEEm2hhs

View File

@ -1,16 +1,17 @@
[{
[{"cell": {
"value" : 1,
"color" : [255,255,255],
"conditionCountNear" : [2,3],
"conditionHighestNear" : null,
"ifValue" : 1,
"elseValue" : 0
},
{
}},
{"cell": {
"value" : 0,
"color" : [0,0,0],
"conditionCountNear" : [3],
"conditionHighestNear" : null,
"ifValue" : 1,
"elseValue" : 0
}]
}}
]

View File

@ -1,48 +1,48 @@
[{
[{"cell": {
"value" : 5,
"color" : [255,255,255],
"conditionCountNear" : null,
"conditionHighestNear" : null,
"ifValue" : 4,
"elseValue" : null
},
{
}},
{"cell": {
"value" : 4,
"color" : [204,204,204],
"conditionCountNear" : null,
"conditionHighestNear" : null,
"ifValue" : 3,
"elseValue" : null
},
{
}},
{"cell": {
"value" : 3,
"color" : [153,153,153],
"conditionCountNear" : null,
"conditionHighestNear" : null,
"ifValue" : 2,
"elseValue" : null
},
{
}},
{"cell": {
"value" : 2,
"color" : [102,102,102],
"conditionCountNear" : null,
"conditionHighestNear" : null,
"ifValue" : 1,
"elseValue" : null
},
{
}},
{"cell": {
"value" : 1,
"color" : [51,51,51],
"conditionCountNear" : null,
"conditionHighestNear" : null,
"ifValue" : 0,
"elseValue" : null
},
{
}},
{"cell": {
"value" : 0,
"color" : [0,0,0],
"conditionCountNear" : null,
"conditionHighestNear" : [5],
"ifValue" : 5,
"elseValue" : 0
}]
}}]

View File

@ -1,15 +1,17 @@
package backend;
public class rules {
import java.util.ArrayList;
public class Rule {
private int value;
private int[] color;
private int[] conditionCountNear;
private int[] conditionHighestNear;
private ArrayList<Integer> color;
private ArrayList<Integer> conditionCountNear;
private ArrayList<Integer> conditionHighestNear;
private int ifValue;
private int elseValue;
public rules(int value , int[] color, int[] conditionCountNear, int[] conditionHighestNear, int ifValue, int elseValue) {
public Rule(int value , ArrayList<Integer> color, ArrayList<Integer> conditionCountNear, ArrayList<Integer> conditionHighestNear, int ifValue, int elseValue) {
this.value = value;
this.color = color;
this.conditionCountNear = conditionCountNear;
@ -22,27 +24,27 @@ public class rules {
return value;
}
public int[] getColor() {
public ArrayList<Integer> getColor() {
return color;
}
public void setColor(int[] color) {
public void setColor(ArrayList<Integer> color) {
this.color = color;
}
public int[] getConditionCountNear() {
public ArrayList<Integer> getConditionCountNear() {
return conditionCountNear;
}
public void setConditionCountNear(int[] conditionCountNear) {
public void setConditionCountNear(ArrayList<Integer> conditionCountNear) {
this.conditionCountNear = conditionCountNear;
}
public int[] getConditionHighestNear() {
public ArrayList<Integer> getConditionHighestNear() {
return conditionHighestNear;
}
public void setConditionHighestNear(int[] conditionHighestNear) {
public void setConditionHighestNear(ArrayList<Integer> conditionHighestNear) {
this.conditionHighestNear = conditionHighestNear;
}

View File

@ -1,6 +1,11 @@
package backend;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import windowInterface.MyInterface;
@ -32,6 +37,11 @@ public class Simulator extends Thread {
private Table table;
private boolean cellDensityToggle;
//Rules Arraylists
private ArrayList<Rule> ruleArrayList = new ArrayList<Rule>();
private ArrayList<ArrayList<Integer>> colorArrayList = new ArrayList<ArrayList<Integer>>();
public Simulator(MyInterface mjfParam) {
mjf = mjfParam;
stopFlag=false;
@ -54,6 +64,7 @@ public class Simulator extends Thread {
//Default rule : Survive always, birth never
for(int i =0; i<9; i++) {
fieldSurviveValues.add(i);
}
@ -438,34 +449,57 @@ public class Simulator extends Thread {
return null;
}
public void loadRule(ArrayList<String> lines) {
if(lines.size()<=0) {
System.out.println("empty rule file");
return;
}
//TODO-INPROGRESS : remove previous rule (=emptying lists)
fieldSurviveValues = new ArrayList<Integer>();
fieldBirthValues = new ArrayList<Integer>();
String surviveLine = lines.get(0);
String birthLine = lines.get(1);
String[] surviveElements = surviveLine.split(";");
for(int x=0; x<surviveElements.length;x++) {
String elem = surviveElements[x];
int value = Integer.parseInt(elem);
//TODO-INPROGRESS : add value to possible survive values
fieldSurviveValues.add(value);
@SuppressWarnings("unchecked")
public void loadRule(JSONArray cellList) {
ruleArrayList.clear();
colorArrayList.clear();
cellList.forEach( cell -> parseCellObject( (JSONObject) cell ) );
}
String[] birthElements = birthLine.split(";");
for(int x=0; x<birthElements.length;x++) {
String elem = birthElements[x];
int value = Integer.parseInt(elem);
//TODO-INPROGRESS : add value to possible birth values
fieldBirthValues.add(value);
@SuppressWarnings("unchecked")
private void parseCellObject(JSONObject cell) {
//Get cell object within list
JSONObject cellObject = (JSONObject) cell.get("cell");
//Get value
String cellValueString = String.valueOf((Long)cellObject.get("value"));
int cellValue = Integer.valueOf(cellValueString);
System.out.println("cell value rule loaded: "+cellValue);
//Get color
JSONArray colorValueJsonArray = (JSONArray) cellObject.get("color");
ArrayList<Integer> rgbList = new ArrayList<Integer>();
colorValueJsonArray.forEach(value -> rgbList.add(Integer.valueOf(String.valueOf((Long)value))));
//Get Condition Count Near
JSONArray countNearJsonArray = (JSONArray) cellObject.get("color");
ArrayList<Integer> conditionCountNearList = new ArrayList<Integer>();
countNearJsonArray.forEach(value -> conditionCountNearList.add(Integer.valueOf(String.valueOf((Long)value))));
//Get Condition highest near
JSONArray conditionHighestNearJsonArray = (JSONArray) cellObject.get("color");
ArrayList<Integer> conditionHighestNearList = new ArrayList<Integer>();
conditionHighestNearJsonArray.forEach(value -> conditionHighestNearList.add(Integer.valueOf(String.valueOf((Long)value))));
//Get ifValue
String ifValueString = String.valueOf((Long)cellObject.get("value"));
int ifValue = Integer.valueOf(ifValueString);
//Get elseValue
String elseValueString = String.valueOf((Long)cellObject.get("value"));
int elseValue = Integer.valueOf(elseValueString);
while (cellValue > colorArrayList.size()) {
colorArrayList.add(new ArrayList<Integer>());
}
colorArrayList.add(cellValue,rgbList);
Rule newRule = new Rule(cellValue, rgbList, conditionCountNearList, conditionHighestNearList, ifValue, elseValue);
while (cellValue > ruleArrayList.size()) {
ruleArrayList.add(newRule);
}
ruleArrayList.add(cellValue,newRule);
}
public void applyRule(){

View File

@ -19,12 +19,22 @@ import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
//added imports for loading jsons
import java.io.FileReader;
import java.util.Iterator;
import java.util.Map;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.*;
public class MyInterface extends JFrame {
private static final long serialVersionUID = -6840815447618468846L;
@ -300,17 +310,31 @@ public class MyInterface extends JFrame {
ArrayList<String> stringArray = new ArrayList<String>();
if (fileName.length()>0) {
try {
BufferedReader fileContent = new BufferedReader(new FileReader(fileName));
String line = fileContent.readLine();
while (line != null) {
stringArray.add(line);
line = fileContent.readLine();
//TODO-INPROGRESS load json
JSONParser jsonParser = new JSONParser();
try (FileReader reader = new FileReader(fileName))
{
//Read JSON file
Object obj = jsonParser.parse(reader);
JSONArray cellList = (JSONArray) obj;
//System.out.println(cellList);
mySimu.loadRule(cellList);
//Iterate over employee array
//cellList.forEach( emp -> parseEmployeeObject( (JSONObject) emp ) );
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
fileContent.close();
} catch (Exception e) {
e.printStackTrace();
}
mySimu.loadRule(stringArray);
this.repaint();
}
}