diff --git a/AlgebraicDataflowArchitectureModel/src/generators/JavaImplementationVisitor.java b/AlgebraicDataflowArchitectureModel/src/generators/JavaImplementationVisitor.java new file mode 100644 index 0000000..3b1d8ce --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/generators/JavaImplementationVisitor.java @@ -0,0 +1,193 @@ +package generators; + +import models.algebra.*; +import models.dataConstraintModel.DataConstraintModel; +import models.dataConstraintModel.JsonAccessor; +import models.dataConstraintModel.JsonTerm; + +/** + * This class is a Java implementation of {@link IExpressionVisitor} + * which holds and manages the actual method of implementation code generation + * + * @author s-yamagiwa + */ +public class JavaImplementationVisitor implements IExpressionVisitor { + private static int jsonCount = 0; + + @Override + public String visit(Term term, String[] sideEffects) { + int[] implParamOrder = term.getSymbol().getImplParamOrder(); + if (term.getSymbol().isImplLambda()) { + String[] components = term.getSymbol().getImplName().split("->"); + String component0 = components[0].replace("(", "").replace(")", ""); + String[] params = component0.split(","); + String exp = components[1]; + String receiver = ""; + if (implParamOrder == null) { + receiver = term.getChildren().get(0).accept(this, sideEffects); + exp = exp.replace(params[0], receiver); + for (int i = 1; i < params.length; i++) { + exp = exp.replace(params[i], term.getChildren().get(i).accept(this, sideEffects)); + } + } else { + receiver = term.getChildren().get(implParamOrder[0]).accept(this, sideEffects); + exp = exp.replace(params[0], receiver); + for (int i = 1; i < params.length; i++) { + exp = exp.replace(params[i], term.getChildren().get(implParamOrder[i]).accept(this, sideEffects)); + } + } + if (term.getSymbol().isImplWithSideEffect()) { + sideEffects[0] = sideEffects[0] + exp + ";\n"; + exp = receiver; + } + return exp; + } + if (term.getSymbol().isImplGenerative()) { + Type[] childrenTypes = new Type[term.getChildren().size()]; + String[] childrenImpl = new String[term.getChildren().size()]; + String[] childrenSideEffects = new String[term.getChildren().size()]; + if (implParamOrder == null) { + for (int i = 0; i < term.getChildren().size(); i++) { + Expression child = term.getChildren().get(i); + if (child instanceof Variable) { + childrenTypes[i] = ((Variable) child).getType(); + } else if (child instanceof Term) { + childrenTypes[i] = ((Term) child).getType(); + } + String[] childSideEffect = new String[]{""}; + childrenImpl[i] = child.accept(this, childSideEffect); + childrenSideEffects[i] = childSideEffect[0]; + } + String exp = term.getSymbol().generate(term.getType(), childrenTypes, childrenImpl, childrenSideEffects, sideEffects); + if (term.getSymbol().isImplWithSideEffect()) { + sideEffects[0] = sideEffects[0] + exp; + exp = childrenImpl[0]; // the value of this term + } + return exp; + } else { + for (int i = 0; i < term.getChildren().size(); i++) { + Expression child = term.getChildren().get(implParamOrder[i]); + if (child instanceof Variable) { + childrenTypes[i] = ((Variable) child).getType(); + } else if (child instanceof Term) { + childrenTypes[i] = ((Term) child).getType(); + } + String[] childSideEffect = new String[]{""}; + childrenImpl[i] = child.accept(this, childSideEffect); + childrenSideEffects[i] = childSideEffect[0]; + } + String exp = term.getSymbol().generate(term.getType(), childrenTypes, childrenImpl, childrenSideEffects, sideEffects); + if (term.getSymbol().isImplWithSideEffect()) { + sideEffects[0] = sideEffects[0] + exp; + exp = childrenImpl[0]; // the value of this term + } + return exp; + } + } + if (term.getArity() == 2 && term.getSymbol().isImplInfix()) { + if (implParamOrder == null) { + return "(" + term.getChildren().get(0).accept(this, sideEffects) + term.getSymbol().toImplementation() + term.getChildren().get(1).accept(this, sideEffects) + ")"; + } else { + return "(" + term.getChildren().get(implParamOrder[0]).accept(this, sideEffects) + term.getSymbol().toImplementation() + term.getChildren().get(implParamOrder[1]).accept(this, sideEffects) + ")"; + } + } + if ((term.getArity() >= 1 || term.getArity() == -1) && term.getSymbol().isImplMethod()) { + if (implParamOrder == null) { + String exp = null; + String receiver = ""; + if (term.getChildren().size() > 0 && term.getChildren().get(0) != null) { + receiver = term.getChildren().get(0).accept(this, sideEffects); + exp = receiver + "." + term.getSymbol().toImplementation() + "("; + } else { + exp = term.getSymbol().toImplementation() + "("; + } + String delimiter = ""; + for (int i = 1; i < term.getChildren().size(); i++) { + Expression e = term.getChildren().get(i); + exp += (delimiter + e.accept(this, sideEffects)); + delimiter = ","; + } + exp += ")"; + if (term.getSymbol().isImplWithSideEffect()) { + sideEffects[0] = sideEffects[0] + exp + ";\n"; + exp = receiver; + } + return exp; + } else { + String receiver = term.getChildren().get(implParamOrder[0]).accept(this, sideEffects); + String exp = receiver + "." + term.getSymbol().toImplementation() + "("; + String delimiter = ""; + for (int i = 1; i < term.getChildren().size(); i++) { + Expression e = term.getChildren().get(implParamOrder[i]); + exp += (delimiter + e.accept(this, sideEffects)); + delimiter = ","; + } + exp += ")"; + if (term.getSymbol().isImplWithSideEffect()) { + sideEffects[0] = sideEffects[0] + exp + ";\n"; + exp = receiver; + } + return exp; + } + } else { + if (implParamOrder == null) { + String exp = term.getSymbol().toImplementation() + "("; + String delimiter = ""; + for (Expression e : term.getChildren()) { + exp += (delimiter + e.accept(this, sideEffects)); + delimiter = ","; + } + return exp + ")"; + } else { + String exp = term.getSymbol().toImplementation() + "("; + String delimiter = ""; + for (int i = 0; i < term.getChildren().size(); i++) { + Expression e = term.getChildren().get(implParamOrder[i]); + exp += (delimiter + e.accept(this, sideEffects)); + delimiter = ","; + } + return exp + ")"; + } + } + } + + @Override + public String visit(Field field, String[] sideEffects) { + return "this." + visit((Constant) field, sideEffects); + } + + @Override + public String visit(Constant constant, String[] sideEffects) { + if (constant.getSymbol().isImplGenerative()) { + String exp = constant.getSymbol().generate(constant.getType(), new Type[]{}, new String[]{}, new String[]{}, sideEffects); + return exp; + } + return constant.getSymbol().getImplName(); + } + + @Override + public String visit(Variable variable, String[] sideEffects) { + return variable.getName(); + } + + @Override + public String visit(JsonTerm jsonTerm, String[] sideEffects) { + String temp = "temp_json" + jsonCount; + jsonCount++; + String impl = ""; + impl += "Map " + temp + " = new HashMap<>();\n"; + for (String key : jsonTerm.keySet()) { + impl += temp + ".put(\"" + key + "\", " + jsonTerm.get(key).accept(this, sideEffects) + ");\n"; + } + sideEffects[0] += impl; + return temp; + } + + @Override + public String visit(JsonAccessor jsonAccessor, String[] sideEffects) { + if (jsonAccessor.getSymbol().equals(DataConstraintModel.dotParam)) { + return jsonAccessor.getChildren().get(0).accept(this, sideEffects) + "." + jsonAccessor.getSymbol().toImplementation() + "(" + jsonAccessor.getChildren().get(1).accept(this, sideEffects) + ")"; + } + return visit((Term) jsonAccessor, sideEffects); + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/models/algebra/Constant.java b/AlgebraicDataflowArchitectureModel/src/models/algebra/Constant.java index 50207b6..2b0e579 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/algebra/Constant.java +++ b/AlgebraicDataflowArchitectureModel/src/models/algebra/Constant.java @@ -1,16 +1,18 @@ package models.algebra; +import generators.JavaImplementationVisitor; + import java.util.ArrayList; public class Constant extends Term { - + public Constant(String value) { super(new Symbol(value, 0), new ArrayList()); } public Constant(String value, Type type) { - super(new Symbol((type == null ? value: type.valueToRepresentation(value)), 0), new ArrayList()); - symbol.setSignature(new Type[] {type}); + super(new Symbol((type == null ? value : type.valueToRepresentation(value)), 0), new ArrayList()); + symbol.setSignature(new Type[]{type}); } public Constant(Symbol symbol) { @@ -25,7 +27,7 @@ @Override public Object clone() { - Constant c = new Constant(symbol); + Constant c = new Constant(symbol); c.setType(type); return c; } @@ -42,10 +44,11 @@ } public String toImplementation(String[] sideEffects) { - if (symbol.isImplGenerative()) { - String exp = symbol.generate(getType(), new Type[] {}, new String[] {}, new String[] {}, sideEffects); - return exp; - } - return symbol.getImplName(); + return accept(new JavaImplementationVisitor(), sideEffects); + } + + @Override + public String accept(IExpressionVisitor visitor, String[] sideEffects) { + return visitor.visit(this, sideEffects); } } diff --git a/AlgebraicDataflowArchitectureModel/src/models/algebra/Expression.java b/AlgebraicDataflowArchitectureModel/src/models/algebra/Expression.java index 7695979..65345a1 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/algebra/Expression.java +++ b/AlgebraicDataflowArchitectureModel/src/models/algebra/Expression.java @@ -4,29 +4,39 @@ public abstract class Expression implements Cloneable { public abstract Expression getSubTerm(Position pos); + /** * Get the unification between this expression and another expression. + * * @param another another expression * @return unified expression */ public abstract Expression unify(Expression another); + /** * Get the inverse map to obtain a sub-term of a given output value back from the output value itself. + * * @param outputValue an output value (usually a term) - * @param targetPos a position in outputValue + * @param targetPos a position in outputValue * @return inverse map */ public abstract Expression getInverseMap(Expression outputValue, Position targetPos); + public abstract boolean contains(Expression exp); + public abstract Object clone(); + public abstract HashMap getSubTerms(Class clazz); public HashMap getVariables() { return getSubTerms(Variable.class); } + public abstract String accept(IExpressionVisitor visitor, String[] sideEffects); + /** * Get the implementation of this expression. + * * @param sideEffects an array with an optional implementation that should be written before the evaluation of this expression * @return the implementation to represent the value of this expression */ diff --git a/AlgebraicDataflowArchitectureModel/src/models/algebra/Field.java b/AlgebraicDataflowArchitectureModel/src/models/algebra/Field.java index 6702d3f..93076df 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/algebra/Field.java +++ b/AlgebraicDataflowArchitectureModel/src/models/algebra/Field.java @@ -1,21 +1,22 @@ package models.algebra; -import java.util.ArrayList; +import generators.JavaImplementationVisitor; /** * A field in the implementation (regarded as a constant in the algebraic system) + * * @author Nitta * */ public class Field extends Constant { - + public Field(String name) { super(name); } public Field(String name, Type type) { super(name); - symbol.setSignature(new Type[] {type}); + symbol.setSignature(new Type[]{type}); } public Field(Symbol symbol) { @@ -28,7 +29,7 @@ } return null; } - + @Override public boolean equals(Object another) { if (!(another instanceof Field)) return false; @@ -41,6 +42,11 @@ } public String toImplementation(String[] sideEffects) { - return "this." + super.toImplementation(sideEffects); + return accept(new JavaImplementationVisitor(), sideEffects); + } + + @Override + public String accept(IExpressionVisitor visitor, String[] sideEffects) { + return visitor.visit(this, sideEffects); } } diff --git a/AlgebraicDataflowArchitectureModel/src/models/algebra/IExpressionVisitor.java b/AlgebraicDataflowArchitectureModel/src/models/algebra/IExpressionVisitor.java new file mode 100644 index 0000000..a8a88f5 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/models/algebra/IExpressionVisitor.java @@ -0,0 +1,18 @@ +package models.algebra; + +import models.dataConstraintModel.JsonAccessor; +import models.dataConstraintModel.JsonTerm; + +public interface IExpressionVisitor { + String visit(Term term, String[] sideEffects); + + String visit(Field field, String[] sideEffects); + + String visit(Constant constant, String[] sideEffects); + + String visit(Variable variable, String[] sideEffects); + + String visit(JsonTerm jsonTerm, String[] sideEffects); + + String visit(JsonAccessor jsonAccessor, String[] sideEffects); +} diff --git a/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java b/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java index d259620..72f71a8 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java +++ b/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java @@ -1,5 +1,7 @@ package models.algebra; +import generators.JavaImplementationVisitor; + import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -15,19 +17,19 @@ super(); this.symbol = symbol; } - + public Term(Symbol symbol, List children) { super(); this.symbol = symbol; this.children = children; } - + public Term(Symbol symbol, Expression[] children) { super(); this.symbol = symbol; this.children = new ArrayList<>(Arrays.asList(children)); } - + public Symbol getSymbol() { return symbol; } @@ -40,14 +42,14 @@ this.type = type; } - public Type getType() { + public Type getType() { if (type == null) { if (symbol.getSignature() == null) return null; return symbol.getSignature()[0]; } return type; } - + public boolean addChild(Expression child) { if (getArity() != -1 && children.size() >= getArity()) return false; children.add(child); @@ -59,7 +61,7 @@ children.set(n, child); return true; } - + public void addChild(Expression child, boolean bForced) { if (!bForced && getArity() != -1 && children.size() >= getArity()) return; children.add(child); @@ -91,7 +93,7 @@ for (int i = 0; i < children.size(); i++) { if (children.get(i) != null) { HashMap terms = children.get(i).getSubTerms(clazz); - for (Entry term: terms.entrySet()) { + for (Entry term : terms.entrySet()) { Position pos = term.getKey(); pos.addHeadOrder(i); subTerms.put(pos, term.getValue()); @@ -113,7 +115,7 @@ public Term substitute(Variable variable, Expression value) { Term newTerm = (Term) this.clone(); HashMap variables = getVariables(); - for (Entry varEnt: variables.entrySet()) { + for (Entry varEnt : variables.entrySet()) { if (varEnt.getValue().equals(variable)) { newTerm.replaceSubTerm(varEnt.getKey(), value); } @@ -153,7 +155,7 @@ return unifiedTerm; } else { return null; - } + } } public Expression reduce() { @@ -176,7 +178,7 @@ } } else if (symbol.isCalculatable()) { List newChildren = new ArrayList<>(); - for (Expression child: children) { + for (Expression child : children) { if (child instanceof Term) { child = ((Term) child).reduce(); } @@ -189,7 +191,7 @@ // Calculate inverse map List newChildren = new ArrayList<>(); boolean bReduced = false; - for (Expression child: children) { + for (Expression child : children) { if (child instanceof Term && !(child instanceof Constant)) { Expression newChild = ((Term) (child)).reduce(); if (newChild != child) { @@ -239,7 +241,7 @@ @Override public boolean contains(Expression exp) { if (equals(exp)) return true; - for (Expression e: children) { + for (Expression e : children) { if (e.contains(exp)) return true; } return false; @@ -270,7 +272,7 @@ @Override public Object clone() { Term newTerm = new Term(symbol); - for (Expression e: children) { + for (Expression e : children) { if (e != null) { newTerm.addChild((Expression) e.clone()); } else { @@ -297,7 +299,7 @@ } else { String exp = symbol.toString() + "("; String delimiter = ""; - for (Expression e: children) { + for (Expression e : children) { if (e != null) { exp += (delimiter + e.toString()); } else { @@ -308,141 +310,13 @@ return exp + ")"; } } - public String toImplementation(String[] sideEffects) { - int[] implParamOrder = symbol.getImplParamOrder(); - if (symbol.isImplLambda()) { - String[] components = symbol.getImplName().split("->"); - String component0 = components[0].replace("(", "").replace(")", ""); - String[] params = component0.split(","); - String exp = components[1]; - String receiver = ""; - if (implParamOrder == null) { - receiver = children.get(0).toImplementation(sideEffects); - exp = exp.replace(params[0], receiver); - for (int i = 1; i < params.length; i++) { - exp = exp.replace(params[i], children.get(i).toImplementation(sideEffects)); - } - } else { - receiver = children.get(implParamOrder[0]).toImplementation(sideEffects); - exp = exp.replace(params[0], receiver); - for (int i = 1; i < params.length; i++) { - exp = exp.replace(params[i], children.get(implParamOrder[i]).toImplementation(sideEffects)); - } - } - if (symbol.isImplWithSideEffect()) { - sideEffects[0] = sideEffects[0] + exp + ";\n"; - exp = receiver; - } - return exp; - } - if (symbol.isImplGenerative()) { - Type childrenTypes[] = new Type[children.size()]; - String childrenImpl[] = new String[children.size()]; - String childrenSideEffects[] = new String[children.size()]; - if (implParamOrder == null) { - for (int i = 0; i < children.size(); i++) { - Expression child = children.get(i); - if (child instanceof Variable) { - childrenTypes[i] = ((Variable) child).getType(); - } else if (child instanceof Term) { - childrenTypes[i] = ((Term) child).getType(); - } - String childSideEffect[] = new String[] {""}; - childrenImpl[i] = child.toImplementation(childSideEffect); - childrenSideEffects[i] = childSideEffect[0]; - } - String exp = symbol.generate(getType(), childrenTypes, childrenImpl, childrenSideEffects, sideEffects); - if (symbol.isImplWithSideEffect()) { - sideEffects[0] = sideEffects[0] + exp; - exp = childrenImpl[0]; // the value of this term - } - return exp; - } else { - for (int i = 0; i < children.size(); i++) { - Expression child = children.get(implParamOrder[i]); - if (child instanceof Variable) { - childrenTypes[i] = ((Variable) child).getType(); - } else if (child instanceof Term) { - childrenTypes[i] = ((Term) child).getType(); - } - String childSideEffect[] = new String[] {""}; - childrenImpl[i] = child.toImplementation(childSideEffect); - childrenSideEffects[i] = childSideEffect[0]; - } - String exp = symbol.generate(getType(), childrenTypes, childrenImpl, childrenSideEffects, sideEffects); - if (symbol.isImplWithSideEffect()) { - sideEffects[0] = sideEffects[0] + exp; - exp = childrenImpl[0]; // the value of this term - } - return exp; - } - } - if (getArity() == 2 && symbol.isImplInfix()) { - if (implParamOrder == null) { - return "(" + children.get(0).toImplementation(sideEffects) + symbol.toImplementation() + children.get(1).toImplementation(sideEffects) + ")"; - } else { - return "(" + children.get(implParamOrder[0]).toImplementation(sideEffects) + symbol.toImplementation() + children.get(implParamOrder[1]).toImplementation(sideEffects) + ")"; - } - } - if ((getArity() >= 1 || getArity() == -1) && symbol.isImplMethod()) { - if (implParamOrder == null) { - String exp = null; - String receiver = ""; - if (children.size() > 0 && children.get(0) != null) { - receiver = children.get(0).toImplementation(sideEffects); - exp = receiver + "." + symbol.toImplementation() + "("; - } else { - exp = symbol.toImplementation() + "("; - } - String delimiter = ""; - for (int i = 1; i < children.size(); i++) { - Expression e = children.get(i); - exp += (delimiter + e.toImplementation(sideEffects)); - delimiter = ","; - } - exp += ")"; - if (symbol.isImplWithSideEffect()) { - sideEffects[0] = sideEffects[0] + exp + ";\n"; - exp = receiver; - } - return exp; - } else { - String receiver = children.get(implParamOrder[0]).toImplementation(sideEffects); - String exp = receiver + "." + symbol.toImplementation() + "("; - String delimiter = ""; - for (int i = 1; i < children.size(); i++) { - Expression e = children.get(implParamOrder[i]); - exp += (delimiter + e.toImplementation(sideEffects)); - delimiter = ","; - } - exp += ")"; - if (symbol.isImplWithSideEffect()) { - sideEffects[0] = sideEffects[0] + exp + ";\n"; - exp = receiver; - } - return exp; - } - } else { - if (implParamOrder == null) { - String exp = symbol.toImplementation() + "("; - String delimiter = ""; - for (Expression e: children) { - exp += (delimiter + e.toImplementation(sideEffects)); - delimiter = ","; - } - return exp + ")"; - } else { - String exp = symbol.toImplementation() + "("; - String delimiter = ""; - for (int i = 0; i < children.size(); i++) { - Expression e = children.get(implParamOrder[i]); - exp += (delimiter + e.toImplementation(sideEffects)); - delimiter = ","; - } - return exp + ")"; - } - } + return accept(new JavaImplementationVisitor(), sideEffects); + } + + @Override + public String accept(IExpressionVisitor visitor, String[] sideEffects) { + return visitor.visit(this, sideEffects); } } diff --git a/AlgebraicDataflowArchitectureModel/src/models/algebra/Variable.java b/AlgebraicDataflowArchitectureModel/src/models/algebra/Variable.java index fa0095b..336a87f 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/algebra/Variable.java +++ b/AlgebraicDataflowArchitectureModel/src/models/algebra/Variable.java @@ -1,5 +1,7 @@ package models.algebra; +import generators.JavaImplementationVisitor; + import java.util.HashMap; public class Variable extends Expression { @@ -10,13 +12,13 @@ super(); this.name = name; } - + public Variable(String name, Type type) { super(); this.name = name; this.type = type; } - + public String getName() { return name; } @@ -24,11 +26,11 @@ public Type getType() { return type; } - + public void setType(Type type) { this.type = type; } - + @Override public HashMap getSubTerms(Class clazz) { HashMap subTerms = new HashMap<>(); @@ -48,18 +50,18 @@ public Expression unify(Expression another) { return (Expression) another.clone(); } - + @Override public Expression getInverseMap(Expression outputValue, Position targetPos) { if (targetPos.isEmpty()) return outputValue; return null; } - + @Override public boolean contains(Expression exp) { return equals(exp); } - + @Override public boolean equals(Object another) { if (!(another instanceof Variable)) return false; @@ -82,6 +84,11 @@ } public String toImplementation(String[] sideEffects) { - return name; + return accept(new JavaImplementationVisitor(), sideEffects); + } + + @Override + public String accept(IExpressionVisitor visitor, String[] sideEffects) { + return visitor.visit(this, sideEffects); } } diff --git a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/JsonAccessor.java b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/JsonAccessor.java index 43e428c..1cf0c22 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/JsonAccessor.java +++ b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/JsonAccessor.java @@ -1,22 +1,15 @@ package models.dataConstraintModel; +import generators.JavaImplementationVisitor; +import models.algebra.*; + import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; -import models.algebra.Constant; -import models.algebra.Expression; -import models.algebra.LambdaAbstraction; -import models.algebra.Position; -import models.algebra.Symbol; -import models.algebra.Term; -import models.algebra.Type; -import models.algebra.Variable; - public class JsonAccessor extends Term { - + public JsonAccessor(Symbol symbol) { super(symbol); } @@ -61,10 +54,10 @@ } return value; } - if (json.getChild(0) == null - || (json.getChild(0) instanceof Term && - (((Term) json.getChild(0)).getSymbol().equals(DataConstraintModel.null_)) - || ((Term) json.getChild(0)).getSymbol().equals(DataConstraintModel.nil))) return null; + if (json.getChild(0) == null + || (json.getChild(0) instanceof Term && + (((Term) json.getChild(0)).getSymbol().equals(DataConstraintModel.null_)) + || ((Term) json.getChild(0)).getSymbol().equals(DataConstraintModel.nil))) return null; return getValue((Term) json.getChild(0), key); } } @@ -83,10 +76,10 @@ } return value; } - if (json.getChild(0) == null - || (json.getChild(0) instanceof Term && - (((Term) json.getChild(0)).getSymbol().equals(DataConstraintModel.null_)) - || ((Term) json.getChild(0)).getSymbol().equals(DataConstraintModel.nil))) return null; + if (json.getChild(0) == null + || (json.getChild(0) instanceof Term && + (((Term) json.getChild(0)).getSymbol().equals(DataConstraintModel.null_)) + || ((Term) json.getChild(0)).getSymbol().equals(DataConstraintModel.nil))) return null; return getValue((Term) json.getChild(0), key); } if (json.getSymbol().equals(DataConstraintModel.insert)) { @@ -97,15 +90,15 @@ } return value; } - if (json.getChild(0) == null - || (json.getChild(0) instanceof Term && - (((Term) json.getChild(0)).getSymbol().equals(DataConstraintModel.null_)) - || ((Term) json.getChild(0)).getSymbol().equals(DataConstraintModel.nil))) return null; + if (json.getChild(0) == null + || (json.getChild(0) instanceof Term && + (((Term) json.getChild(0)).getSymbol().equals(DataConstraintModel.null_)) + || ((Term) json.getChild(0)).getSymbol().equals(DataConstraintModel.nil))) return null; return getValue((Term) json.getChild(0), key); } return new Constant(DataConstraintModel.null_); } - + @Override public Expression getInverseMap(Expression outputValue, Position targetPos) { if (targetPos.isEmpty()) return outputValue; @@ -139,8 +132,8 @@ keySet.add(keyName); } } - for (String key: keySet) { - Term addMemberTerm = new Term(DataConstraintModel.addMember); // addMember(jsonTerm, key, v) + for (String key : keySet) { + Term addMemberTerm = new Term(DataConstraintModel.addMember); // addMember(jsonTerm, key, v) addMemberTerm.addChild(jsonTerm); addMemberTerm.addChild(new Constant(key, DataConstraintModel.typeString)); Variable var = new Variable("v" + v); @@ -150,8 +143,8 @@ v++; } Variable var = vars.get(keyName); - LambdaAbstraction lambdaAbstraction = new LambdaAbstraction(var, jsonTerm); // v -> addMember(jsonTerm, key, v) - inverseSymbols = new Symbol[] { lambdaAbstraction }; + LambdaAbstraction lambdaAbstraction = new LambdaAbstraction(var, jsonTerm); // v -> addMember(jsonTerm, key, v) + inverseSymbols = new Symbol[]{lambdaAbstraction}; } } else if (symbol == DataConstraintModel.dotParam && getChildren().size() >= 2) { // this term is `json.{param}`. @@ -169,23 +162,23 @@ } else if (expKey instanceof Term) { keyType = ((Term) expKey).getType(); } - if (jsonType != null && keyType != null) { + if (jsonType != null && keyType != null) { if (DataConstraintModel.typeList.isAncestorOf(jsonType) || keyType.equals(DataConstraintModel.typeInt)) { - Term setElementTerm = new Term(DataConstraintModel.set); // set(list, idx, v) + Term setElementTerm = new Term(DataConstraintModel.set); // set(list, idx, v) setElementTerm.addChild(new Constant(DataConstraintModel.nil)); setElementTerm.addChild(expKey); Variable var = new Variable("v"); setElementTerm.addChild(var); - LambdaAbstraction lambdaAbstraction = new LambdaAbstraction(var, setElementTerm); // v -> set(list, idx, v) - inverseSymbols = new Symbol[] { lambdaAbstraction }; + LambdaAbstraction lambdaAbstraction = new LambdaAbstraction(var, setElementTerm); // v -> set(list, idx, v) + inverseSymbols = new Symbol[]{lambdaAbstraction}; } else if (DataConstraintModel.typeMap.isAncestorOf(jsonType) || keyType.equals(DataConstraintModel.typeString)) { - Term insertEntryTerm = new Term(DataConstraintModel.insert); // insert(map, key, v) + Term insertEntryTerm = new Term(DataConstraintModel.insert); // insert(map, key, v) insertEntryTerm.addChild(new Constant(DataConstraintModel.nil)); insertEntryTerm.addChild(expKey); Variable var = new Variable("v"); insertEntryTerm.addChild(var); - LambdaAbstraction lambdaAbstraction = new LambdaAbstraction(var, insertEntryTerm); // v -> insert(map, key, v) - inverseSymbols = new Symbol[] { lambdaAbstraction }; + LambdaAbstraction lambdaAbstraction = new LambdaAbstraction(var, insertEntryTerm); // v -> insert(map, key, v) + inverseSymbols = new Symbol[]{lambdaAbstraction}; } } } @@ -212,16 +205,18 @@ } public String toImplementation(String[] sideEffects) { - if (symbol.equals(DataConstraintModel.dotParam)) { - return children.get(0).toImplementation(sideEffects) + "." + symbol.toImplementation() + "(" + children.get(1).toImplementation(sideEffects) + ")"; - } - return super.toImplementation(sideEffects); + return accept(new JavaImplementationVisitor(), sideEffects); + } + + @Override + public String accept(IExpressionVisitor visitor, String[] sideEffects) { + return visitor.visit(this, sideEffects); } @Override public Object clone() { JsonAccessor newTerm = new JsonAccessor(symbol); - for (Expression e: children) { + for (Expression e : children) { newTerm.addChild((Expression) e.clone()); } newTerm.type = type; diff --git a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/JsonTerm.java b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/JsonTerm.java index 6f32281..608a27e 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/JsonTerm.java +++ b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/JsonTerm.java @@ -1,21 +1,18 @@ package models.dataConstraintModel; +import generators.JavaImplementationVisitor; +import models.algebra.*; +import parser.Parser; + import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; -import models.algebra.Constant; -import models.algebra.Expression; -import models.algebra.Symbol; -import models.algebra.Term; -import models.algebra.Variable; -import parser.Parser; - public class JsonTerm extends Term { private Map keyToIndex = new HashMap<>(); private static int count = 0; - + public JsonTerm() { super(new Symbol("json", -1)); setType(DataConstraintModel.typeJson); @@ -33,17 +30,17 @@ public Set keySet() { return keyToIndex.keySet(); } - + public Expression get(String key) { if (keyToIndex.get(key) == null) return null; return getChild(keyToIndex.get(key)); } - + public Expression get(Constant key) { if (keyToIndex.get(key.getValue()) == null) return null; return getChild(keyToIndex.get(key.getValue())); } - + public Expression get(Variable key) { if (keyToIndex.get(key.getName()) == null) return null; return getChild(keyToIndex.get(key.getName())); @@ -56,8 +53,8 @@ JsonTerm unifiedTerm = new JsonTerm(); Set keySet = new HashSet<>(); keySet.addAll(this.keySet()); - keySet.addAll(anotherTerm.keySet()); - for (String key: keySet) { + keySet.addAll(anotherTerm.keySet()); + for (String key : keySet) { if (this.keySet().contains(key)) { if (anotherTerm.keySet().contains(key)) { unifiedTerm.addMember(key, this.get(key).unify(anotherTerm.get(key))); @@ -76,7 +73,7 @@ @Override public Object clone() { JsonTerm newTerm = new JsonTerm(); - for (Expression e: children) { + for (Expression e : children) { newTerm.addChild((Expression) e.clone()); } newTerm.keyToIndex = new HashMap(keyToIndex); @@ -86,7 +83,7 @@ public String toString() { String jsonStr = "{"; String delim = ""; - for (String key: keyToIndex.keySet()) { + for (String key : keyToIndex.keySet()) { jsonStr += delim + Parser.DOUBLE_QUOT + key + Parser.DOUBLE_QUOT + ": " + getChild(keyToIndex.get(key)).toString(); delim = ", "; } @@ -94,14 +91,11 @@ } public String toImplementation(String[] sideEffects) { - String temp = "temp_json" + count; - count++; - String impl = ""; - impl += "Map " + temp + " = new HashMap<>();\n"; - for (String key: keySet()) { - impl += temp + ".put(\"" + key + "\", " + get(key).toImplementation(sideEffects) + ");\n"; - } - sideEffects[0] += impl; - return temp; + return accept(new JavaImplementationVisitor(), sideEffects); + } + + @Override + public String accept(IExpressionVisitor visitor, String[] sideEffects) { + return visitor.visit(this, sideEffects); } }