Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added server with JavaExpress and Gson #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ repositories {
}

dependencies {
api 'com.github.CST-Group:cst:0.6.1'
api 'com.github.CST-Group:cst:0.6.1'
implementation 'io.vacco.java-express:java-express:0.2.1'
implementation 'com.google.code.gson:gson:2.8.6'
}

task uberJar(type: Jar) {
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/MindViewerTest/CodeletJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package MindViewerTest;

import br.unicamp.cst.core.entities.Codelet;

import java.util.ArrayList;
import java.util.List;

public class CodeletJson {
private double activation;
private long timestamp;
private String name;
private List<MemoryJson> broadcast = new ArrayList<MemoryJson>();
private List<MemoryJson> inputs = new ArrayList<MemoryJson>();
private List<MemoryJson> outputs = new ArrayList<MemoryJson>();

public CodeletJson(Codelet cod) {
this.activation = cod.getActivation();
this.timestamp = System.currentTimeMillis();
this.name = cod.getName();
for (int i = 0; i < cod.getBroadcast().size(); i++) {
this.broadcast.add(new MemoryJson(cod.getBroadcast().get(i)));
}
for (int i = 0; i < cod.getInputs().size(); i++) {
this.inputs.add(new MemoryJson(cod.getInputs().get(i)));
}
for (int i = 0; i < cod.getOutputs().size(); i++) {
this.outputs.add(new MemoryJson(cod.getOutputs().get(i)));
}
}
}
19 changes: 18 additions & 1 deletion src/main/java/MindViewerTest/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
import br.unicamp.cst.core.entities.MemoryObject;
import br.unicamp.cst.core.entities.Mind;
import br.unicamp.cst.util.MindViewer;
import com.google.gson.Gson;
import express.Express;
import express.middleware.CorsOptions;
import express.middleware.Middleware;

import java.util.Timer;
import java.util.TimerTask;

Expand Down Expand Up @@ -158,14 +163,26 @@ private void createAndShowGUI(Mind m) {

public Main() {
Mind m = prepareMind();
createAndShowGUI(m);
//Caso queira ver a interface, descomentar a linha abaixo
//createAndShowGUI(m);
}

public static void main(String[] args) {
Main mainApp = new Main();
mainApp.StartTimer();
//MindViewer ov = new MindViewer(m, "Mind", new ArrayList<>());
//ov.setVisible(true);

Express app = new Express();
Gson gson = new Gson();
CorsOptions corsOptions = new CorsOptions();
corsOptions.setOrigin("*");
app.use(Middleware.cors(corsOptions));
app.get("/", (req, res) -> {
MyJson myJson = new MyJson(mainApp.m.getRawMemory().getAllMemoryObjects(),mainApp.m.getCodeRack().getAllCodelets());
res.send(gson.toJson(myJson));
});
app.listen(5000);
}

}
30 changes: 30 additions & 0 deletions src/main/java/MindViewerTest/MemoryJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package MindViewerTest;

import br.unicamp.cst.core.entities.Memory;
import br.unicamp.cst.core.entities.MemoryContainer;

import java.util.ArrayList;
import java.util.List;

public class MemoryJson {
private Long timestamp;
private volatile Double evaluation;
private volatile Object I;
private String name;
private ArrayList<MemoryJson> memories = new ArrayList<MemoryJson>();

public MemoryJson(Memory memo) {
timestamp = memo.getTimestamp();
evaluation = memo.getEvaluation();
I = memo.getI();
name = memo.getName();
if (memo instanceof MemoryContainer) {
MemoryContainer memoAux = (MemoryContainer) memo;
List<Memory> memoList = memoAux.getAllMemories();
for (int i = 0; i < memoList.size(); i++) {
this.memories.add(new MemoryJson(memoList.get(i)));
}
}

}
}
21 changes: 21 additions & 0 deletions src/main/java/MindViewerTest/MyJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package MindViewerTest;

import br.unicamp.cst.core.entities.Codelet;
import br.unicamp.cst.core.entities.Memory;

import java.util.ArrayList;
import java.util.List;

public class MyJson {
private List<MemoryJson> memories = new ArrayList<MemoryJson>();
private List<CodeletJson> codelets = new ArrayList<CodeletJson>();

public MyJson(List<Memory> memories, List<Codelet> cods) {
for (int i = 0; i < memories.size(); i++) {
this.memories.add(new MemoryJson(memories.get(i)));
}
for (int i = 0; i < cods.size(); i++) {
this.codelets.add(new CodeletJson(cods.get(i)));
}
}
}