Skip to content

Commit

Permalink
Merge pull request #47 from CST-Group/rest-server
Browse files Browse the repository at this point in the history
Rest server
  • Loading branch information
rgudwin authored May 25, 2022
2 parents 5b4a660 + d543164 commit 302db6c
Show file tree
Hide file tree
Showing 10 changed files with 1,208 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Note: This library is still under development, and some concepts or features mig
```
dependencies {
...
implementation 'com.github.CST-Group:cst:1.1.0'
implementation 'com.github.CST-Group:cst:1.2.0'
}
```

Expand All @@ -53,7 +53,7 @@ Sometimes, the version number (tag) in this README gets out of date, as maintain
<dependency>
<groupId>com.github.CST-Group</groupId>
<artifactId>cst</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
</dependency>
```

Expand Down
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ plugins {

group = 'com.github.CST-Group'

description = "CST is the Cognitive Systems Toolkit, a toolkit for the construction of Cognitive Systems and Cognitive Architectures"
description = "CST"

sourceCompatibility = 1.8
targetCompatibility = 1.8
version = '1.1.0'
version = '1.2.0'

repositories {
mavenCentral()
Expand All @@ -35,6 +35,7 @@ dependencies {
api group: 'org.opt4j', name: 'opt4j-satdecoding', version: '3.1'
api group: 'org.opt4j', name: 'opt4j-tutorial', version: '3.1'
api 'org.antlr:antlr4-runtime:4.5.3' // Used in OwrlBaseListener
api 'com.github.masecla22:java-express:0.2.2'
testImplementation group: 'junit', name: 'junit', version: '4.13'
}

Expand Down
56 changes: 56 additions & 0 deletions src/main/java/br/unicamp/cst/io/rest/CodeletJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/***********************************************************************************************
* Copyright (c) 2012 DCA-FEEC-UNICAMP
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Contributors:
* K. Raizer, A. L. O. Paraense, E. M. Froes, R. R. Gudwin - initial API and implementation
* **********************************************************************************************/
package br.unicamp.cst.io.rest;

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 String group;
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)));
}
}

public CodeletJson(Codelet cod, String group) {
this.activation = cod.getActivation();
this.timestamp = System.currentTimeMillis();
this.name = cod.getName();
this.group = group;
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)));
}
}
}
58 changes: 58 additions & 0 deletions src/main/java/br/unicamp/cst/io/rest/MemoryJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/***********************************************************************************************
* Copyright (c) 2012 DCA-FEEC-UNICAMP
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Contributors:
* K. Raizer, A. L. O. Paraense, E. M. Froes, R. R. Gudwin - initial API and implementation
* **********************************************************************************************/
package br.unicamp.cst.io.rest;

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 {
public Long timestamp;
public volatile Double evaluation;
public volatile Object I;
public String name;
public String group;
public 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)));
}
}

}

public MemoryJson(Memory memo, String group) {
timestamp = memo.getTimestamp();
evaluation = memo.getEvaluation();
I = memo.getI();
name = memo.getName();
this.group = group;
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)));
}
}

}
}

59 changes: 59 additions & 0 deletions src/main/java/br/unicamp/cst/io/rest/MindJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/***********************************************************************************************
* Copyright (c) 2012 DCA-FEEC-UNICAMP
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Contributors:
* K. Raizer, A. L. O. Paraense, E. M. Froes, R. R. Gudwin - initial API and implementation
* **********************************************************************************************/
package br.unicamp.cst.io.rest;

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

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

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

public MindJson(Mind m) {
if (m.getMemoryGroupsNumber() == 0) {
List<Memory> mems = m.getRawMemory().getAllMemoryObjects();

for (int i = 0; i < mems.size(); i++) {
this.memories.add(new MemoryJson(mems.get(i)));
}
}
else {
ConcurrentHashMap<String, ArrayList> mems = m.getMemoryGroups();

for (String key : mems.keySet()) {
ArrayList<Memory> memoList = mems.get(key);
for (int i = 0; i < memoList.size(); i++) {
this.memories.add(new MemoryJson(memoList.get(i), key));
}
}
}
if (m.getCodeletGroupsNumber() == 0) {
List<Codelet> cods = m.getCodeRack().getAllCodelets();
for (int i = 0; i < cods.size(); i++) {
this.codelets.add(new CodeletJson(cods.get(i)));
}
}
else {
ConcurrentHashMap<String, java.util.ArrayList> cods = m.getCodeletGroups();
for (String key : cods.keySet()) {
ArrayList<Codelet> codList = cods.get(key);
for (int i = 0; i < codList.size(); i++) {
this.codelets.add(new CodeletJson(codList.get(i), key));
}
}
}
}
}
102 changes: 102 additions & 0 deletions src/main/java/br/unicamp/cst/io/rest/RESTServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/***********************************************************************************************
* Copyright (c) 2012 DCA-FEEC-UNICAMP
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Contributors:
* K. Raizer, A. L. O. Paraense, E. M. Froes, R. R. Gudwin - initial API and implementation
* **********************************************************************************************/
package br.unicamp.cst.io.rest;

import br.unicamp.cst.core.entities.Memory;
import br.unicamp.cst.core.entities.MemoryContainer;
import br.unicamp.cst.core.entities.MemoryObject;
import br.unicamp.cst.core.entities.Mind;
import br.unicamp.cst.support.InterfaceAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import express.Express;
import express.middleware.CorsOptions;
import express.middleware.Middleware;

/**
* This is the main class for using REST to monitor inner activities from CST
* It is used to provide a REST server to monitor whatever is happening within a CST mind
* Depending on the constructor, the user can set the mind, the port to be used, if
* the JSON describing the inner details of the mind should be rendered with pretty printing
* and a way to limit the access of external users to the server
* @author rgudwin
*/
public class RESTServer {

long refresh = 0; // A refresh of 0 means that every call will generate a new probe in mind
long lastaccess = 0;
String lastmessage = "";

/**
*
* @param m the mind to observe
* @param port the port to install the REST server
*/
public RESTServer(Mind m, int port) {
this(m,port,false);
}

/**
*
* @param m the mind to observe
* @param port the port to install the REST server
* @param pretty set this to true to generate pretty printing JSON in the REST server
*/
public RESTServer(Mind m, int port, boolean pretty) {
this(m,port,pretty,"*");
}

/**
*
* @param m the mind to observe
* @param port the port to install the REST server
* @param pretty set this to true to generate pretty printing JSON in the REST server
* @param origin a pattern for users allowed to access the server - use "*" to allow everyone
*/
public RESTServer(Mind m, int port, boolean pretty, String origin) {
this(m,port,pretty,origin,0L);
}

/**
*
* @param m the mind to observe
* @param port the port to install the REST server
* @param pretty set this to true to generate pretty printing JSON in the REST server
* @param origin a pattern for users allowed to access the server - use "*" to allow everyone
* @param nrefresh the refresh period in milliseconds
*/
public RESTServer(Mind m, int port, boolean pretty, String origin, long nrefresh) {
refresh = nrefresh;
Express app = new Express();
Gson gson;
if (pretty)
gson = new GsonBuilder().registerTypeAdapter(Memory.class, new InterfaceAdapter<MemoryObject>())
.registerTypeAdapter(Memory.class, new InterfaceAdapter<MemoryContainer>())
.setPrettyPrinting().create();
else
gson = new Gson();
CorsOptions corsOptions = new CorsOptions();
corsOptions.setOrigin(origin);
app.use(Middleware.cors(corsOptions));
app.get("/", (req, res) -> {
long currentaccess = System.currentTimeMillis();
long diff = currentaccess - lastaccess;
if (diff > refresh) {
MindJson myJson = new MindJson(m);
lastmessage = gson.toJson(myJson);
lastaccess = currentaccess;
}
res.send(lastmessage);
});
app.listen(port);
}

}
Loading

0 comments on commit 302db6c

Please sign in to comment.