Commit 337b6778 authored by Rem Collier's avatar Rem Collier

removed json type from codebase

parent ef6893b6
{
"java.configuration.updateBuildConfiguration": "automatic"
}
\ No newline at end of file
......@@ -4,12 +4,12 @@
<modelVersion>4.0.0</modelVersion>
<groupId>astra</groupId>
<artifactId>astra-apis</artifactId>
<version>0.2.0</version>
<version>0.1.0</version>
<parent>
<groupId>astra</groupId>
<artifactId>astra-project</artifactId>
<version>0.2.0</version>
<artifactId>astra-core</artifactId>
<version>0.1.0</version>
</parent>
<repositories>
......@@ -23,7 +23,7 @@
<dependency>
<groupId>astra</groupId>
<artifactId>astra-interpreter</artifactId>
<version>0.2.0</version>
<version>0.1.0</version>
</dependency>
</dependencies>
......
......@@ -31,18 +31,6 @@ public class Console extends Module {
return true;
}
/**
* Action to print a string followed by a new line.
*
* @param JsonNode the string to be output
* @return
*/
@ACTION
public boolean println(JsonTerm node) {
java.lang.System.out.println("[" + agent.name() +"]" + node.node().toString());
return true;
}
/**
* Action to print a string followed by a new line.
*
......
......@@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>astra</groupId>
<artifactId>astra-base</artifactId>
<version>0.2.0</version>
<version>0.1.0</version>
<packaging>pom</packaging>
<name>Basic ASTRA Project</name>
......@@ -12,8 +12,8 @@
<parent>
<groupId>astra</groupId>
<artifactId>astra-project</artifactId>
<version>0.2.0</version>
<artifactId>astra-core</artifactId>
<version>0.1.0</version>
</parent>
<properties>
......
......@@ -4,12 +4,12 @@
<modelVersion>4.0.0</modelVersion>
<groupId>astra</groupId>
<artifactId>astra-compiler</artifactId>
<version>0.2.0</version>
<version>0.1.0</version>
<parent>
<groupId>astra</groupId>
<artifactId>astra-project</artifactId>
<version>0.2.0</version>
<artifactId>astra-core</artifactId>
<version>0.1.0</version>
</parent>
<repositories>
......@@ -23,12 +23,12 @@
<dependency>
<groupId>astra</groupId>
<artifactId>astra-interpreter</artifactId>
<version>0.2.0</version>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>astra</groupId>
<artifactId>astra-apis</artifactId>
<version>0.2.0</version>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
......
......@@ -6,11 +6,6 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.io.IOException;
import astra.ast.statement.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import astra.ast.definition.FormulaDefinition;
import astra.ast.definition.TypeDefinition;
......@@ -37,7 +32,44 @@ import astra.ast.formula.NOTFormula;
import astra.ast.formula.OrFormula;
import astra.ast.formula.PredicateFormula;
import astra.ast.formula.ScopedGoalFormula;
import astra.ast.term.*;
import astra.ast.statement.AssignmentStatement;
import astra.ast.statement.BlockStatement;
import astra.ast.statement.DeclarationStatement;
import astra.ast.statement.DoneStatement;
import astra.ast.statement.ForAllStatement;
import astra.ast.statement.ForEachStatement;
import astra.ast.statement.IfStatement;
import astra.ast.statement.MaintainBlockStatement;
import astra.ast.statement.MinusMinusStatement;
import astra.ast.statement.ModuleCallStatement;
import astra.ast.statement.PlanCallStatement;
import astra.ast.statement.PlusPlusStatement;
import astra.ast.statement.QueryStatement;
import astra.ast.statement.ScopedStatement;
import astra.ast.statement.SendStatement;
import astra.ast.statement.SpawnGoalStatement;
import astra.ast.statement.SubGoalStatement;
import astra.ast.statement.SynchronizedBlockStatement;
import astra.ast.statement.TRStatement;
import astra.ast.statement.TryRecoverStatement;
import astra.ast.statement.UpdateStatement;
import astra.ast.statement.WaitStatement;
import astra.ast.statement.WhileStatement;
import astra.ast.term.AtIndexTerm;
import astra.ast.term.Brackets;
import astra.ast.term.CountFormulaeTerm;
import astra.ast.term.CountTerm;
import astra.ast.term.Function;
import astra.ast.term.HeadTerm;
import astra.ast.term.InlineVariableDeclaration;
import astra.ast.term.ListSplitterTerm;
import astra.ast.term.ListTerm;
import astra.ast.term.Literal;
import astra.ast.term.ModuleTerm;
import astra.ast.term.Operator;
import astra.ast.term.QueryTerm;
import astra.ast.term.TailTerm;
import astra.ast.term.Variable;
import astra.ast.tr.FunctionCallAction;
import astra.ast.tr.TRModuleCallAction;
import astra.ast.tr.TRRuleElement;
......@@ -1186,17 +1218,6 @@ public class ASTRAParser {
first, last, tokenizer.getSource(first, last));
}
private static ObjectMapper objectMapper = new ObjectMapper();
public static boolean isValidJSON(final String json) throws IOException {
boolean valid = true;
try{
objectMapper.readTree(json);
} catch(JsonProcessingException e){
valid = false;
}
return valid;
}
public ITerm createTerm(List<Token> tokens) throws ParseException {
// System.out.println("createTerm: " + tokens);
Token first = tokens.get(0);
......@@ -1245,26 +1266,6 @@ public class ASTRAParser {
}
if (!tokens.isEmpty()) throw new ParseException("Unexpected Tokens after inline variable declaration" , getLast(tokens));
return new InlineVariableDeclaration(type, tok2.token, tok, tok2, tokenizer.getSource(tok, tok2));
} else if (tok.type == Token.LEFT_BRACE) {
List<Token> list2 = new LinkedList<Token>();
Token tok2 = tokens.remove(0);
StringBuffer buf = new StringBuffer();
buf.append("{");
while (tok2.type != Token.RIGHT_BRACE ) {
list2.add(tok2);
buf.append(tok2.token);
if (tokens.isEmpty()) throw new ParseException("Missing right brace '}' for JSON literal: " + buf.toString(), tok);
tok2 = tokens.remove(0);
}
buf.append("}");
try {
if (!isValidJSON(buf.toString())) throw new ParseException("Invalid JSON literal: " + buf.toString(), tok);
}catch (IOException e) {
throw new ParseException("Serious Error Validating JSON literal: " + buf.toString(), tok);
}
return new Literal(buf.toString(), new BasicType(Token.JSON), tok, tok2, tokenizer.getSource(tok, tok2));
} else if (tok.type == Token.COUNT) {
Token tok2 = tokens.remove(0);
if (tok2.type == Token.LEFT_BRACKET) {
......@@ -1328,32 +1329,6 @@ public class ASTRAParser {
new BasicType(Token.resolveType(terms.get(2).get(0).type)),
first, last, tokenizer.getSource(first, last));
}
} else if (tok.type == Token.FROM_JSON) {
Token tok2 = tokens.remove(0);
if (tok2.type == Token.LEFT_BRACKET) {
if (getLast(tokens).type != Token.RIGHT_BRACKET)
throw new ParseException("Unexpected Tokens", tok2, getLast(tokens));
tokens.remove(tokens.size()-1);
List<List<Token>> terms = this.getTermParts(tokens, false);
if (terms.size() != 2) throw new ParseException("Invalid Number of Parameters: " + terms.size() + " \nExpected: from_json(<list>, <type>)", first, last);
if (Token.resolveType(terms.get(1).get(0).type) > -1) {
throw new ParseException("Type parameter for from_json term must be a Java class type", first, last);
}
return new FromJsonTerm(
createTerm(terms.get(0)),
getQualifiedName(terms.get(1)),
first, last, tokenizer.getSource(first, last));
}
} else if (tok.type == Token.TO_JSON) {
Token tok2 = tokens.remove(0);
if (tok2.type == Token.LEFT_BRACKET) {
if (getLast(tokens).type != Token.RIGHT_BRACKET)
throw new ParseException("Unexpected Tokens", tok2, getLast(tokens));
tokens.remove(tokens.size()-1);
return new ToJsonTerm(
createTerm(tokens),
first, last, tokenizer.getSource(first, last));
}
} else if (tok.type == Token.LEFT_BRACKET) {
if (tokens.get(tokens.size()-1).type != Token.RIGHT_BRACKET) {
throw new ParseException("Bracket mismatch for term", tok, tokens.get(tokens.size()-1));
......
......@@ -111,8 +111,6 @@ public interface IElementVisitor {
Object visit(QueryTerm queryTerm, Object data) throws ParseException;
Object visit(Brackets brackets, Object data) throws ParseException;
Object visit(Function function, Object data) throws ParseException;
Object visit(ToJsonTerm toJsonTerm, Object data) throws ParseException;
Object visit(FromJsonTerm fromJsonTerm, Object data) throws ParseException;
// List Functionality
Object visit(CountTerm countTerm, Object data) throws ParseException;
......
......@@ -40,7 +40,6 @@ public class Token {
public static final int SPEECHACT = 30;
public static final int FUNCT = 58;
public static final int GOAL = 59;
public static final int JSON = 80;
// PUNCTUATION
public static final int PERIOD = 31;
......@@ -84,7 +83,6 @@ public class Token {
public static final int FUNCT_TYPE = 86;
public static final int GOAL_TYPE = 87;
public static final int RETURNS = 0;
public static final int JSON_TYPE = 90;
// STATEMENTS
public static final int SEND = 70;
......@@ -110,8 +108,6 @@ public class Token {
public static final int HEAD = 101;
public static final int TAIL = 102;
public static final int AT_INDEX = 103;
public static final int TO_JSON = 104;
public static final int FROM_JSON = 105;
public static final int COUNT_FORMULAE = 106;
public String token;
......@@ -155,7 +151,6 @@ public class Token {
typeMap.put("string", STRING_TYPE);
typeMap.put("boolean", BOOLEAN_TYPE);
typeMap.put("speechact", SPEECHACT_TYPE);
typeMap.put("json", JSON_TYPE);
typeMap.put("list", LIST_TYPE);
typeMap.put("formula", FORMULA);
typeMap.put("funct", FUNCT_TYPE);
......@@ -215,9 +210,6 @@ public class Token {
typeMap.put("list_tail", TAIL);
typeMap.put("at_index", AT_INDEX);
typeMap.put("to_json", TO_JSON);
typeMap.put("from_json", FROM_JSON);
// Speech Acts
typeMap.put("accept-proposal", SPEECHACT);
typeMap.put("agree", SPEECHACT);
......@@ -248,7 +240,6 @@ public class Token {
typePrecedence.add(Token.LIST);
typePrecedence.add(Token.SPEECHACT);
typePrecedence.add(Token.OBJECT);
typePrecedence.add(Token.JSON);
typePrecedence.add(Token.STRING);
typePrecedence.add(Token.FORMULA);
typePrecedence.add(Token.GOAL);
......@@ -261,7 +252,6 @@ public class Token {
typeToString.put(Token.CHARACTER, "char");
typeToString.put(Token.FORMULA, "formula");
typeToString.put(Token.BOOLEAN, "boolean");
typeToString.put(Token.JSON, "json");
typeToString.put(Token.LIST, "list");
typeToString.put(Token.FUNCT, "funct");
typeToString.put(Token.SPEECHACT, "speechact");
......@@ -274,7 +264,6 @@ public class Token {
typeToString.put(Token.BOOLEAN_TYPE, "boolean");
typeToString.put(Token.LIST_TYPE, "list");
typeToString.put(Token.FUNCT_TYPE, "funct");
typeToString.put(Token.JSON_TYPE, "json");
typeToString.put(Token.SPEECHACT_TYPE, "speechact");
typeToString.put(Token.GOAL_TYPE, "goal");
}
......@@ -359,7 +348,7 @@ public class Token {
return (type == Token.INTEGER_TYPE) || (type == Token.LONG_TYPE) || (type == Token.FLOAT_TYPE) ||
(type == Token.DOUBLE_TYPE) || (type == Token.STRING_TYPE) || (type == Token.CHARACTER_TYPE) ||
(type == Token.BOOLEAN_TYPE) || (type == Token.SPEECHACT_TYPE) || (type == Token.OBJECT_TYPE) ||
(type == Token.LIST_TYPE) || (type == Token.FUNCT_TYPE) || (type == Token.FORMULA) || (type == Token.JSON_TYPE);
(type == Token.LIST_TYPE) || (type == Token.FUNCT_TYPE) || (type == Token.FORMULA);
}
public static boolean isLiteral(int type) {
......@@ -381,7 +370,6 @@ public class Token {
if (type == Token.OBJECT_TYPE) return Token.OBJECT;
if (type == Token.LIST_TYPE) return Token.LIST;
if (type == Token.FUNCT_TYPE) return Token.FUNCT;
if (type == Token.JSON_TYPE) return Token.JSON;
if (type == Token.FORMULA) return Token.FORMULA;
// System.out.println("not handled");
return -1;
......
......@@ -31,8 +31,6 @@ public class MethodType {
types.put("Funct", "funct");
types.put("astra.formula.Formula", "formula");
types.put("Formula", "formula");
types.put("com.fasterxml.jackson.databind.JsonNode", "json");
types.put("JsonNode", "json");
}
public static String resolveType(String type) {
......
package astra.ast.term;
import astra.ast.core.*;
import astra.ast.type.ObjectType;
public class FromJsonTerm extends AbstractElement implements ITerm {
ITerm term;
String type;
public FromJsonTerm(ITerm term, String type, Token start, Token end, String source) {
super(start, end, source);
this.term = term;
this.type = type;
}
public ITerm term() {
return term;
}
public Object accept(IElementVisitor visitor, Object data) throws ParseException {
return visitor.visit(this, data);
}
public String toString() {
return term.toString();
}
public IType type() {
return new ObjectType(Token.OBJECT, type);
}
public String specifiedType() {
return type;
}
}
package astra.ast.term;
import astra.ast.core.*;
import astra.ast.type.BasicType;
public class ToJsonTerm extends AbstractElement implements ITerm {
ITerm term;
public ToJsonTerm(ITerm term, Token start, Token end, String source) {
super(start, end, source);
this.term = term;
}
public ITerm term() {
return term;
}
public Object accept(IElementVisitor visitor, Object data) throws ParseException {
return visitor.visit(this, data);
}
public String toString() {
return "to_json("+term.toString()+")";
}
public IType type() {
return new BasicType(Token.STRING);
}
}
......@@ -335,14 +335,6 @@ public class AbstractVisitor implements IElementVisitor {
return null;
}
public Object visit(ToJsonTerm term, Object data) throws ParseException {
return null;
}
public Object visit(FromJsonTerm term, Object data) throws ParseException {
return null;
}
public Object visit(CountFormulaeTerm formula, Object data) throws ParseException {
return null;
}
......
......@@ -76,7 +76,6 @@ public class CodeGeneratorVisitor extends AbstractVisitor {
types.put(Token.FORMULA, "Type.FORMULA");
types.put(Token.SPEECHACT, "Type.PERFORMATIVE");
types.put(Token.FUNCT, "Type.FUNCTION");
types.put(Token.JSON, "Type.JSON");
}
private Map<String, String> modules = new HashMap<>();
......@@ -1192,8 +1191,6 @@ public class CodeGeneratorVisitor extends AbstractVisitor {
public Object visit(Literal term, Object data) throws ParseException {
if (term.type().type() == Token.SPEECHACT) {
code.append(data + "new Performative(\"" + term.value() + "\")");
} else if (term.type().type() == Token.JSON) {
code.append(data + "new JsonTerm(" + Utilities.escapeForJava(term.value(), true) + ")");
} else {
code.append(data + "Primitive.newPrimitive(" + term.value() + ")");
}
......@@ -1342,21 +1339,4 @@ public class CodeGeneratorVisitor extends AbstractVisitor {
code.append(data + "new IsDone()");
return null;
}
public Object visit(ToJsonTerm term, Object data) throws ParseException {
code.append(data + "new ToJson(\n");
term.term().accept(this, data+"\t");
code.append("\n" + data + ")");
return null;
}
public Object visit(FromJsonTerm term, Object data) throws ParseException {
code.append(data + "new FromJson(\n");
term.term().accept(this, data+"\t");
code.append(",\n" + data + "\t\"" + term.specifiedType() +"\"");
code.append("\n" + data + ")");
return null;
}
}
......@@ -324,13 +324,17 @@ public class ComponentVisitor extends AbstractVisitor {
statement.term().accept(this, data);
// if (!statement.type().equals(statement.term().type())) {
// System.out.println("statement.type(): "+ statement.type());
// System.out.println("statement.term().type(): "+ statement.term().type());
if (typesMatch(statement.type(), statement.term().type())) {
throw new ParseException(
"Type mismatch: expected an expression of type '" + Token.toTypeString(statement.type().type()) +
"' but got an expression of type '" + Token.toTypeString(statement.term().type().type()) + "'",
statement);
// System.out.println("statement.type(): "+ statement.type());
// System.out.println("statement.term().type(): "+ statement.term().type());
try {
if (typesMatch(statement.type(), statement.term().type())) {
throw new ParseException(
"Type mismatch: expected an expression of type '" + Token.toTypeString(statement.type().type()) +
"' but got an expression of type '" + Token.toTypeString(statement.term().type().type()) + "'",
statement);
}
} catch (RuntimeException rte) {
throw new ParseException(rte.getMessage(), rte, statement);
}
}
((VariableTypeStack) data).addVariable(statement.variable(), statement.type());
......@@ -744,14 +748,4 @@ public class ComponentVisitor extends AbstractVisitor {
term.index().accept(this, data);
return null;
}
public Object visit(ToJsonTerm term, Object data) throws ParseException {
term.term().accept(this, data);
return null;
}
public Object visit(FromJsonTerm term, Object data) throws ParseException {
term.term().accept(this, data);
return null;
}
}
......@@ -545,14 +545,6 @@ public class TypeCheckVisitor implements IElementVisitor {
return null;
}
public Object visit(ToJsonTerm term, Object data) throws ParseException {
return null;
}
public Object visit(FromJsonTerm term, Object data) throws ParseException {
return null;
}
public Object visit(CountFormulaeTerm formula, Object data) throws ParseException {
return null;
}
......
......@@ -102,26 +102,7 @@ public class TestBasicTerms {
ITerm term = parser.createTerm(tokens);
assertEquals(ModuleTerm.class, term.getClass());
}
@Test
public void jsonTest() throws ParseException {
List<Token> tokens = setup("{ \"name\":\"Rem\" }");
ITerm term = parser.createTerm(tokens);
assertEquals(Literal.class, term.getClass());
}
@Test(expected = ParseException.class)
public void missingBracketJsonTest() throws ParseException {
List<Token> tokens = setup("{ \"name\":\"Rem\"");
parser.createTerm(tokens);
}
@Test(expected = ParseException.class)
public void invalidJsonTest() throws ParseException {
List<Token> tokens = setup("{ \"name\" \"Rem\"} ");
parser.createTerm(tokens);
}
@Test
public void countFormulaeTest() throws ParseException {
List<Token> tokens = setup("count_formulae(init(happy))");
......
......@@ -110,11 +110,21 @@ public class TestStatements {
IStatement term = parser.createStatement(tokens);
assertEquals(BlockStatement.class, term.getClass());
}
@Test
@Test
public void moduleCallAndModuleTest() throws ParseException {
List<Token> tokens = setup("map.getTopVote(string winner);");
IStatement term = parser.createStatement(tokens);
assertEquals(ModuleCallStatement.class, term.getClass());
}
//
@Test
public void classNameTest() throws ParseException {
List<Token> tokens = setup("JsonNode node = converter.parse(content);");
IStatement term = parser.createStatement(tokens);
assertEquals(DeclarationStatement.class, term.getClass());
DeclarationStatement declaration = (DeclarationStatement) term;
System.out.println("Type: " + declaration.type());
}
}
......@@ -91,30 +91,6 @@ public class TestVarTerms {
varComparison((InlineVariableDeclaration) term, new BasicType(Token.FUNCT), "X");
}
@Test
public void jsonVarTest() throws ParseException {
List<Token> tokens = setup("json X");
ITerm term = parser.createTerm(tokens);
assertEquals(InlineVariableDeclaration.class, term.getClass());
varComparison((InlineVariableDeclaration) term, new BasicType(Token.JSON), "X");
}
@Test(expected = AssertionError.class)
public void incorrectTypeVarTest() throws ParseException {
List<Token> tokens = setup("json X");
ITerm term = parser.createTerm(tokens);
assertEquals(InlineVariableDeclaration.class, term.getClass());
varComparison((InlineVariableDeclaration) term, new BasicType(Token.INTEGER), "X");
}
@Test(expected = AssertionError.class)
public void incorrectIdentifierVarTest() throws ParseException {
List<Token> tokens = setup("json X");
ITerm term = parser.createTerm(tokens);
assertEquals(InlineVariableDeclaration.class, term.getClass());
varComparison((InlineVariableDeclaration) term, new BasicType(Token.JSON), "Y");
}
@Test
public void objectVarTest() throws ParseException {
List<Token> tokens = setup("java.lang.Object X");
......
......@@ -7,12 +7,12 @@
<name>${project.groupId}:${project.artifactId}</name>
<description>Core interpreter for the ASTRA Programming Language</description>
<url>https://gitlab.com/astra-language/astra-interpreter</url>
<version>0.2.0</version>
<version>0.1.0</version>
<parent>
<groupId>astra</groupId>
<artifactId>astra-project</artifactId>
<version>0.2.0</version>
<artifactId>astra-core</artifactId>
<version>0.1.0</version>
</parent>
<dependencies>
......
......@@ -77,11 +77,6 @@ public class Intention {
return val;
}
if (term instanceof astra.term.JsonTerm) {
// loopy = 0;
return (T) ((JsonTerm) term).node();
}
if (term instanceof Variable) {
Term val = getValue((Variable) term);
// System.out.println("Value for variable: " + term + " is: " + val);
......
......@@ -8,7 +8,6 @@ import java.util.concurrent.Executors;
import astra.core.Agent;
import astra.core.Scheduler;
import astra.core.Task;
import astra.execution.SchedulerStrategy;