Commit d1ea8129 authored by Florian Grabowski's avatar Florian Grabowski
Browse files

Finished JsonParser and prepared for Treemap

parent d94398f9
package de._82grfl1bif.KPI_Visualizer.commands;
import de._82grfl1bif.KPI_Visualizer.data.DataHolder;
import de._82grfl1bif.KPI_Visualizer.data.JsonParser;
import de._82grfl1bif.KPI_Visualizer.structures.*;
import org.bukkit.Bukkit;
import org.bukkit.Location;
......@@ -19,16 +20,25 @@ public class generateLayout implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (args.length < 2){
sender.sendMessage("please type or select the needed Arguments with tab.");
return false;
}
sender.sendMessage("generating Layout");
Server server = sender.getServer();
Player player = server.getPlayer(sender.getName());
JsonParser jsonParser = new JsonParser();
if (!jsonParser.evalJson(args[0])){
sender.sendMessage("failed to parse Json");
return false;
}
if(player != null) {
Location location = player.getLocation();
BukkitRunnable t = new BukkitRunnable() {
@Override
public void run() {
DataHolder.ComplexData();
//DataHolder.ComplexData();
DataHolder.foundation.setLocation(location);
DataHolder.foundation.organizeFoundation();
DataHolder.foundation.correctAllLocations(location);
......
......@@ -12,18 +12,20 @@ public class DataHolder {
public static Foundation foundation = new Foundation(0, Material.COBBLESTONE);
public static void generateSimpleData(int buildingCount){
@Deprecated
public static void generateSimpleData(int buildingCount) {
foundation.removeChildren();
ArrayList<Structure> buildings = new ArrayList<>();
for(int c = 0; c < buildingCount; c++){
for (int c = 0; c < buildingCount; c++) {
buildings.add(generateBuilding());
}
foundation.addChildren(buildings);
foundation.optimizeLayout();
}
public static void ComplexData(){
Foundation foundation3 = new Foundation(3,Material.BLACK_CONCRETE);
@Deprecated
public static void ComplexData() {
Foundation foundation3 = new Foundation(3, Material.BLACK_CONCRETE);
ArrayList<Structure> buildings3 = new ArrayList<>();
for (int i = 0; i < 5; i++) {
buildings3.add(generateBuilding());
......@@ -31,9 +33,8 @@ public class DataHolder {
foundation3.addChildren(buildings3);
ArrayList<Structure> buildings2_1 = new ArrayList<>();
Foundation foundation2_1 = new Foundation(2,Material.BROWN_CONCRETE);
Foundation foundation2_1 = new Foundation(2, Material.BROWN_CONCRETE);
for (int i = 0; i < 5; i++) {
buildings2_1.add(generateBuilding());
}
......@@ -41,7 +42,7 @@ public class DataHolder {
foundation2_1.addChildren(buildings2_1);
ArrayList<Structure> buildings2_2 = new ArrayList<>();
Foundation foundation2_2 = new Foundation(2,Material.LIGHT_GRAY_CONCRETE);
Foundation foundation2_2 = new Foundation(2, Material.LIGHT_GRAY_CONCRETE);
for (int i = 0; i < 5; i++) {
buildings2_2.add(generateBuilding());
}
......@@ -49,7 +50,7 @@ public class DataHolder {
ArrayList<Structure> buildings1 = new ArrayList<>();
Foundation foundation1 = new Foundation(1,Material.GRAY_CONCRETE);
Foundation foundation1 = new Foundation(1, Material.GRAY_CONCRETE);
for (int i = 0; i < 5; i++) {
buildings1.add(generateBuilding());
}
......@@ -62,13 +63,43 @@ public class DataHolder {
buildings1.add(generateBuilding());
}
buildings0.add(foundation1);
foundation = new Foundation(0,Material.COBBLESTONE);
foundation = new Foundation(0, Material.COBBLESTONE);
foundation.addChildren(buildings0);
foundation.setDepth(0);
}
public static void generateTreeFromKlassenList(ArrayList<Klasse> klassen) {
foundation = new Foundation(0, Material.YELLOW_CONCRETE);
ArrayList<String> Pakete = new ArrayList<>();
for (Klasse currentKlasse : klassen) {
if (currentKlasse.depth >= 0) {
String[] strings = currentKlasse.path.split("/");
Foundation f = foundation;
StringBuilder sb = new StringBuilder();
for (int i = 2; i < currentKlasse.depth + 2; i++) {
sb.append(strings[i]);
if (!(strings[i].contains(".java"))){//is foundation
sb.append("/");
}
if (foundation.getFromTree(sb.toString()) == null) {// checks Tree for Element doesn't find it
if (strings[i].contains(".java")) {//is not foundation
f.generateChild(false, currentKlasse, sb.toString());
f = foundation;
} else {
f = (Foundation) f.generateChild(true,currentKlasse,sb.toString());
}
}else{//finds Element in tree
if ((foundation.getFromTree(sb.toString()).getClass() == Foundation.class)){
f = (Foundation) foundation.getFromTree(sb.toString());
}
}
}
}
}
}
private static Building generateBuilding() {
Building result = new Building(ThreadLocalRandom.current().nextInt(4, 60 + 1),ThreadLocalRandom.current().nextInt(4, 200 + 1),Material.LIME_CONCRETE);
Building result = new Building(ThreadLocalRandom.current().nextInt(4, 60 + 1), ThreadLocalRandom.current().nextInt(4, 200 + 1), Material.LIME_CONCRETE);
result.setLocation(foundation.getLocation());
return result;
}
......
package de._82grfl1bif.KPI_Visualizer.data;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
public class FileInputTabCompleter implements TabCompleter {
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
if (args.length == 1) {
return new JsonParser().getFileHistory();
}
if (args.length == 2) {
List<String> result = new ArrayList<>();
result.add("SquarifiedTreemap");
result.add("SimpleCornerOptimizer");
return result;
}
return null;
}
}
package de._82grfl1bif.KPI_Visualizer.data;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import org.bukkit.Bukkit;
import java.io.FileReader;
import org.jetbrains.annotations.NotNull;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Level;
public class JsonParser {
String standardPath = "";
String standardPath = System.getProperty("user.dir");
private boolean evalJson(String fileName) {
public boolean evalJson(@NotNull String fileName) {
if (!fileName.contains(".json")) {
fileName = fileName + ".json";
} //Add .json in case user forgets
if (!(fileName.contains("/"))){
fileName = standardPath + fileName;
if (!(fileName.contains("/"))) {
fileName = standardPath + "/" + fileName;
}
try (JsonReader reader = new JsonReader(new FileReader("/Users/flgr/Desktop/PaperServer/LayoutJson" + fileName))) {
Gson gson = new Gson();
try (FileReader jsonFileReader = new FileReader(fileName)) {
JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(jsonFileReader);
if (!(getFileHistory().contains(fileName))) { //if file isn't in history add it.
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(standardPath + "/plugins/JsonFileHistory",true))){
bufferedWriter.newLine();
bufferedWriter.write(fileName);
} catch (Exception e) {
Bukkit.getLogger().log(Level.SEVERE, String.valueOf(e));
}
}
JSONArray entities = (JSONArray) jsonObject.get("entities");
ArrayList<Klasse> entitiesList = new ArrayList<>();
for (int i=0; i < entities.size(); i++) {
JSONObject object = (JSONObject) entities.get(i);
Klasse currentKlasse = new Klasse(
Integer.parseInt(object.get("entity_id").toString()),
object.get("name").toString(),
object.get("path").toString(),
Integer.parseInt(object.get("qloc").toString()),
Integer.parseInt(object.get("loc").toString()),
((JSONArray) object.get("commits")).size(),
((JSONArray) object.get("functions")).size(),
((JSONArray) object.get("variables")).size());
entitiesList.add(currentKlasse);
}
entitiesList.sort(new DepthComparator());
DataHolder.generateTreeFromKlassenList(entitiesList);
} catch (Exception ex) {
Bukkit.getLogger().log(Level.SEVERE, String.valueOf(ex));
return false;
......@@ -26,4 +57,32 @@ public class JsonParser {
return true;
}
private static class DepthComparator implements Comparator<Klasse> {
@Override
public int compare(Klasse o1, Klasse o2) {
return Integer.compare(o1.depth,o2.depth);
}
}
public List<String> getFileHistory() {
String path = System.getProperty("user.dir");
String fileName = "/plugins/JsonFileHistory";
List<String> filesInHistory = new ArrayList<>();
try {
File file = new File(path + fileName);
if (file.createNewFile()) {//Create file
return null;
} //File exists
BufferedReader br = new BufferedReader(new FileReader(path + fileName));
String line;
while ((line = br.readLine()) != null) {
filesInHistory.add(line);
}
br.close();
} catch (Exception e) {
Bukkit.getLogger().log(Level.SEVERE, String.valueOf(e));
return null;
}
return filesInHistory;
}
}
package de._82grfl1bif.KPI_Visualizer.data;
public class Klasse {
public String name;
public String path;
public int qloc;
public int loc;
public int commits;
public int depth;
public final int id;
public int functions;
public int variables;
public Klasse(int id, String name, String path, int qloc, int loc, int commits, int functions, int variables){
this.id = id;
this.commits = commits;
this.depth = getDepthOfPath(path);
this.loc = loc;
this.qloc = qloc;
this.functions = functions;
this.variables = variables;
this.name = name;
this.path = path;
}
private int getDepthOfPath(String path){
int result = -1;
if(path.contains("tmp")){
result = path.split("/").length-2;
}
return result;
}
}
......@@ -7,8 +7,6 @@ import java.util.ArrayList;
public class Quadrat extends Shape{
private Boolean belegt;
public Structure getStructure() {
......
......@@ -2,6 +2,7 @@ package de._82grfl1bif.KPI_Visualizer;
import de._82grfl1bif.KPI_Visualizer.commands.SetPreset;
import de._82grfl1bif.KPI_Visualizer.commands.generateLayout;
import de._82grfl1bif.KPI_Visualizer.data.FileInputTabCompleter;
import org.bukkit.plugin.java.JavaPlugin;
public final class main extends JavaPlugin {
......@@ -11,6 +12,7 @@ public final class main extends JavaPlugin {
// Plugin startup logic
getCommand("setPreset").setExecutor(new SetPreset());
getCommand("generateLayout").setExecutor(new generateLayout());
getCommand("generateLayout").setTabCompleter(new FileInputTabCompleter());
}
@Override
......
package de._82grfl1bif.KPI_Visualizer.structures;
import de._82grfl1bif.KPI_Visualizer.data.Klasse;
import org.bukkit.Material;
import java.awt.Point;
......@@ -26,7 +27,23 @@ public class Building extends Structure{
}
}
public Building(Material material, Klasse klasse, String name){ //Buildings are Square -> no depth
this.material = material;
this.width = new Point(5,5); //TODO: make this work with treemap and square
this.name = name;
this.height = klasse.commits; // con be changed to likings TODO: find out which fits best.
if(this.width.x < 3 || this.width.y < 3){
throw new IllegalArgumentException("keine Gebäude kleiner 3 wegen 2 Gehweg");
}
}
public int getHeight() {
return this.height;
}
@Override
public Structure getFromTree(String checkName) {
if(this.name.equals(checkName))return this;
return null;
}
}
package de._82grfl1bif.KPI_Visualizer.structures;
import de._82grfl1bif.KPI_Visualizer.data.Klasse;
import de._82grfl1bif.KPI_Visualizer.helpers.Layout;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.util.Vector;
import javax.management.AttributeNotFoundException;
import java.awt.Point;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Random;
public class Foundation extends Structure{
public class Foundation extends Structure {
private ArrayList<Structure> children = new ArrayList<>();
private Layout layout;
public Foundation(int depth, Material material){ //Foundations are uniform Height
public Foundation(int depth, Material material) { //Foundations are uniform Height
this.width.x = 0;
this.depth = depth;
this.material = material;
}
public Foundation(int depth, Material material, String name) { //Foundations are uniform Height
this.width.x = 0;
this.name = name;
this.depth = depth;
this.material = material;
}
public ArrayList<Structure> getChildren() {
return children;
}
public void addChildren(ArrayList<Structure> children) {
for (Structure s:children) {
for (Structure s : children) {
s.setDepth(this.depth);
}
this.children.addAll(children);
}
public void removeChildren(){
public Structure generateChild(boolean isFoundation, Klasse klasse, String name) {
Structure result;
if (isFoundation) {
result = new Foundation(this.depth + 1, nextMaterial(), name);
} else {
result = new Building(Material.LIME_CONCRETE, klasse, name);
}
this.children.add(result);
return result;
}
public Material nextMaterial() {
Material result = null;
Random rand = new Random();
while ((result == this.material) || (result == null)) {
int r = rand.nextInt(15); //there are 16 kins of concrete
switch (r) {
case 0:
result = Material.BLACK_CONCRETE;
break;
case 1:
result = Material.BROWN_CONCRETE;
break;
case 2:
result = Material.GRAY_CONCRETE;
break;
case 3:
result = Material.LIME_CONCRETE;
break;
case 4:
result = Material.YELLOW_CONCRETE;
break;
case 5:
result = Material.LIGHT_GRAY_CONCRETE;
break;
case 6:
result = Material.RED_CONCRETE;
break;
case 7:
result = Material.BLUE_CONCRETE;
break;
case 8:
result = Material.CYAN_CONCRETE;
break;
case 9:
result = Material.GREEN_CONCRETE;
break;
case 10:
result = Material.LIGHT_BLUE_CONCRETE;
break;
case 11:
result = Material.MAGENTA_CONCRETE;
break;
case 12:
result = Material.ORANGE_CONCRETE;
break;
case 13:
result = Material.PINK_CONCRETE;
break;
case 14:
result = Material.PURPLE_CONCRETE;
break;
case 15:
result = Material.WHITE_CONCRETE;
break;
}
}
return result;
}
public void removeChildren() {
this.children = new ArrayList<>();
}
public void optimizeLayout(){
public void optimizeLayout() {
int cWith = 0;
for (Structure s : children) {
s.setLocation(location.clone().add(new Vector(1,depth,((cWith)+1))));
s.setLocation(location.clone().add(new Vector(1, depth, ((cWith) + 1))));
cWith += (s.getWidth().x + 2);
}
this.width.x = cWith;
}
private static class DepthComparator implements Comparator<Foundation>{
@Override
public Structure getFromTree(String checkName) {
if (this.name != null) {
if (this.name.equals(checkName)) {
return this;
} else {
for (Structure s : this.children) {
if (s.getFromTree(checkName) != null) return s.getFromTree(checkName);
}
}
} else {
for (Structure s : this.children) {
if (s.getFromTree(checkName) != null) return s.getFromTree(checkName);
}
}
return null;
}
public Structure findByName(String findName) {
if (this.name.equals(findName)) {
return this;
} else {
for (Structure s : this.children) {
if (s.getClass() == Foundation.class) {
if ((((Foundation) s).findByName(findName)) != null) {
return s;
}
}
}
}
return null;
}
private static class DepthComparator implements Comparator<Foundation> {
@Override
public int compare(Foundation o1, Foundation o2) {
return Integer.compare(o1.getDepth(), o2.getDepth());
}
}
private static class WidthComparator implements Comparator<Structure>{
private static class WidthComparator implements Comparator<Structure> {
@Override
public int compare(Structure o1, Structure o2) {
......@@ -63,17 +175,17 @@ public class Foundation extends Structure{
private boolean baseFoundation = true;
public void organizeFoundation(){
if(this.depth == 0 && baseFoundation){ //rootElement prepwork and recursion call
public void organizeFoundation() {
if (this.depth == 0 && baseFoundation) { //rootElement prepwork and recursion call
ArrayList<Foundation> allFoundations = collectAllFoundations();
baseFoundation = false;
allFoundations.sort(new DepthComparator());
Collections.reverse(allFoundations); //higher Depths first
for (Foundation f:allFoundations) {
for (Foundation f : allFoundations) {
//if(!this.equals(f))
f.organizeFoundation();
}
}else{ //normal Call and recursion
} else { //normal Call and recursion
this.children.sort(new WidthComparator());
Collections.reverse(this.children); //Widest Building or Foundation first
this.layout = new Layout(this.children);
......@@ -82,14 +194,14 @@ public class Foundation extends Structure{
}
}
private ArrayList<Foundation> collectAllFoundations(){ //called on the base Foundation
private ArrayList<Foundation> collectAllFoundations() { //called on the base Foundation
ArrayList<Foundation> fCollection = new ArrayList<>();
fCollection.add(this);
int currentDepth = this.depth;
int lastDepthAdded = -1;
while (lastDepthAdded != 0){ //while there are new nextDepths in this Depth.
for (int i = 0; i < fCollection.size();i++) {
if (!(fCollection.get(i).getDepth() < currentDepth)){ //if f is not part of a historical depth
while (lastDepthAdded != 0) { //while there are new nextDepths in this Depth.
for (int i = 0; i < fCollection.size(); i++) {
if (!(fCollection.get(i).getDepth() < currentDepth)) { //if f is not part of a historical depth
fCollection.addAll(fCollection.get(i).getNextDepth());
lastDepthAdded = fCollection.get(i).getNextDepth().size();
}
......@@ -99,28 +211,28 @@ public class Foundation extends Structure{
return fCollection;
}
private ArrayList<Foundation> getNextDepth(){
private ArrayList<Foundation> getNextDepth() {
ArrayList<Foundation> result = new ArrayList<>();
for (Structure s:this.children) {
if (s.getClass() == Foundation.class){
for (Structure s : this.children) {
if (s.getClass() == Foundation.class) {
result.add((Foundation) s);
}else {
} else {
s.setDepth(this.depth);
}
}
for (Foundation f:result) {
f.setDepth((this.depth)+1);
for (Foundation f : result) {
f.setDepth((this.depth) + 1);
}
return result;
}
public void correctAllLocations(Location location){
for (Structure s:this.children) {
public void correctAllLocations(Location location) {
for (Structure s : this.children) {
try {
Point temp = this.layout.getCoordinateOf(s);
s.setLocation(location.clone().add(temp.x,0,temp.y));
if (s.getClass() == Foundation.class){
Foundation f = (Foundation)s;
s.setLocation(location.clone().add(temp.x, 0, temp.y));
if (s.getClass() == Foundation.class) {
Foundation f = (Foundation) s;
f.correctAllLocations(f.getLocation());
}
} catch (AttributeNotFoundException e) {
......
......@@ -8,12 +8,13 @@ import javax.persistence.Column;
import javax.persistence.Entity;
import java.awt.*;
public class Structure{
public abstract class Structure{
protected Point width = new Point(0,0);
protected Material material;
protected Location location;
protected int depth;
protected String name;
public Material getMaterial() {
return material;
......@@ -42,4 +43,6 @@ public class Structure{
public int getDepth() {
return depth;
}
public abstract Structure getFromTree(String checkName);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment