diff --git a/AlgebraicDataflowArchitectureModel/src/models/algebra/Constant.java b/AlgebraicDataflowArchitectureModel/src/models/algebra/Constant.java index eb5e89b..f458356 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/algebra/Constant.java +++ b/AlgebraicDataflowArchitectureModel/src/models/algebra/Constant.java @@ -3,14 +3,14 @@ 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 +25,7 @@ @Override public Object clone() { - Constant c = new Constant(symbol); + Constant c = new Constant(symbol); c.setType(type); return c; } @@ -43,7 +43,7 @@ public String toImplementation(String[] sideEffects) { if (symbol.isImplGenerative()) { - String exp = symbol.generate(getType(), new Type[] {}, new String[] {}, new String[] {}, sideEffects); + String exp = symbol.generate(getType(), new Type[]{}, new String[]{}, new String[]{}, sideEffects); return exp; } return symbol.getImplName(); diff --git a/AlgebraicDataflowArchitectureModel/src/models/algebra/Expression.java b/AlgebraicDataflowArchitectureModel/src/models/algebra/Expression.java index 88a25ac..65345a1 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/algebra/Expression.java +++ b/AlgebraicDataflowArchitectureModel/src/models/algebra/Expression.java @@ -4,21 +4,28 @@ 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() { @@ -29,6 +36,7 @@ /** * 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 d5c5e55..75a0858 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/algebra/Field.java +++ b/AlgebraicDataflowArchitectureModel/src/models/algebra/Field.java @@ -1,21 +1,20 @@ package models.algebra; -import java.util.ArrayList; - /** * 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 +27,7 @@ } return null; } - + @Override public boolean equals(Object another) { if (!(another instanceof Field)) return false; diff --git a/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java b/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java index 1798c20..d732dd9 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java +++ b/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java @@ -15,19 +15,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,7 +40,7 @@ this.type = type; } - public Type getType() { + public Type getType() { if (type == null) { if (symbol.getSignature() == null) return null; return symbol.getSignature()[0]; @@ -91,7 +91,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 +113,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); } @@ -176,7 +176,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 +189,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 +239,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 +270,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 +297,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,7 +308,6 @@ return exp + ")"; } } - public String toImplementation(String[] sideEffects) { int[] implParamOrder = symbol.getImplParamOrder(); @@ -349,14 +348,14 @@ } else if (child instanceof Term) { childrenTypes[i] = ((Term) child).getType(); } - String childSideEffect[] = new String[] {""}; + 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 + exp = childrenImpl[0]; // the value of this term } return exp; } else { @@ -367,14 +366,14 @@ } else if (child instanceof Term) { childrenTypes[i] = ((Term) child).getType(); } - String childSideEffect[] = new String[] {""}; + 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 + exp = childrenImpl[0]; // the value of this term } return exp; } @@ -428,7 +427,7 @@ if (implParamOrder == null) { String exp = symbol.toImplementation() + "("; String delimiter = ""; - for (Expression e: children) { + for (Expression e : children) { exp += (delimiter + e.toImplementation(sideEffects)); delimiter = ","; } diff --git a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/JsonAccessor.java b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/JsonAccessor.java index f16a0ce..8a97c99 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/JsonAccessor.java +++ b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/JsonAccessor.java @@ -1,15 +1,14 @@ package models.dataConstraintModel; +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.*; - public class JsonAccessor extends Term { - + public JsonAccessor(Symbol symbol) { super(symbol); } @@ -54,10 +53,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); } } @@ -76,10 +75,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)) { @@ -90,15 +89,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; @@ -132,8 +131,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); @@ -143,8 +142,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}`. @@ -162,23 +161,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}; } } } @@ -219,7 +218,7 @@ @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 ed5e46f..fbbf468 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/JsonTerm.java +++ b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/JsonTerm.java @@ -1,17 +1,17 @@ package models.dataConstraintModel; +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.*; -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); @@ -29,17 +29,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())); @@ -53,7 +53,7 @@ Set keySet = new HashSet<>(); keySet.addAll(this.keySet()); keySet.addAll(anotherTerm.keySet()); - for (String key: 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))); @@ -72,7 +72,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); @@ -82,7 +82,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,7 +94,7 @@ count++; String impl = ""; impl += "Map " + temp + " = new HashMap<>();\n"; - for (String key: keySet()) { + for (String key : keySet()) { impl += temp + ".put(\"" + key + "\", " + get(key).toImplementation(sideEffects) + ");\n"; } sideEffects[0] += impl;