diff --git a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java index 6b1083d..a23b6ef 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java +++ b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java @@ -38,7 +38,6 @@ } return value.toString(); } - public Object representationToValue(String representation) { if (representation.startsWith(Parser.DOUBLE_QUOT) && representation.endsWith(Parser.DOUBLE_QUOT)) { return representation.substring(1, representation.length() - 1); @@ -182,31 +181,30 @@ } } }); - public static final Symbol mod = new Symbol(Parser.MOD, 2, Symbol.Type.INFIX, "%", Symbol.Type.INFIX, - new Symbol.ICalculator() { - @Override - public Expression calculate(List args) { - if (!(args.get(0).getClass() == Constant.class)) { - return null; - } - if (!(args.get(1).getClass() == Constant.class)) { - return null; - } - Constant arg0 = (Constant) args.get(0); - Constant arg1 = (Constant) args.get(1); - String sArg0 = arg0.getSymbol().toString(); - String sArg1 = arg1.getSymbol().toString(); - if (arg0.getType() != null && arg1.getType() != null) { - if (arg0.getType().equals(typeLong) || arg1.getType().equals(typeLong)) { - return new Constant(Long.toString(Long.parseLong(sArg0) % Long.parseLong(sArg1)), typeLong); - } else if (arg0.getType().equals(typeInt) || arg1.getType().equals(typeInt)) { - return new Constant(Integer.toString(Integer.parseInt(sArg0) % Integer.parseInt(sArg1)), - typeInt); - } - } - return new Constant(Integer.toString(Integer.parseInt(sArg0) % Integer.parseInt(sArg1))); + public static final Symbol mod = new Symbol(Parser.MOD, 2, Symbol.Type.INFIX, "%", Symbol.Type.INFIX, new Symbol.ICalculator() { + @Override + public Expression calculate(List args) { + if (!(args.get(0).getClass() == Constant.class)) { + return null; + } + if (!(args.get(1).getClass() == Constant.class)) { + return null; + } + Constant arg0 = (Constant) args.get(0); + Constant arg1 = (Constant) args.get(1); + String sArg0 = arg0.getSymbol().toString(); + String sArg1 = arg1.getSymbol().toString(); + if (arg0.getType() != null && arg1.getType() != null) { + if (arg0.getType().equals(typeLong) || arg1.getType().equals(typeLong)) { + return new Constant(Long.toString(Long.parseLong(sArg0) % Long.parseLong(sArg1)), typeLong); + } else if (arg0.getType().equals(typeInt) || arg1.getType().equals(typeInt)) { + return new Constant(Integer.toString(Integer.parseInt(sArg0) % Integer.parseInt(sArg1)), + typeInt); } - }); + } + return new Constant(Integer.toString(Integer.parseInt(sArg0) % Integer.parseInt(sArg1))); + } + }); public static final Symbol minus = new Symbol(Parser.MINUS, 1, new Symbol.ICalculator() { @Override public Expression calculate(List args) { @@ -236,7 +234,7 @@ public static final Symbol eq = new Symbol(Parser.EQ, 2, Symbol.Type.INFIX, new Symbol.IImplGenerator() { @Override public String generate(Type type, Type[] childrenTypes, String[] children, String[] childrenSideEffects, String[] sideEffect) { - for (String s : childrenSideEffects) { + for (String s: childrenSideEffects) { sideEffect[0] += s; } if (childrenTypes[0] != null && childrenTypes[0].equals(typeString) @@ -287,7 +285,7 @@ public static final Symbol neq = new Symbol(Parser.NEQ, 2, Symbol.Type.INFIX, new Symbol.IImplGenerator() { @Override public String generate(Type type, Type[] childrenTypes, String[] children, String[] childrenSideEffects, String[] sideEffect) { - for (String s : childrenSideEffects) { + for (String s: childrenSideEffects) { sideEffect[0] += s; } if (childrenTypes[0] != null && childrenTypes[0].equals(typeString) @@ -497,8 +495,7 @@ String sArg0 = arg0.getSymbol().toString(); String sArg1 = arg1.getSymbol().toString(); boolean result = false; - if (arg0.getType() == null || arg1.getType() == null) - return null; + if (arg0.getType() == null || arg1.getType() == null) return null; if (arg0.getType().equals(typeBoolean) || arg1.getType().equals(typeBoolean)) { result = (Boolean.parseBoolean(sArg0) && Boolean.parseBoolean(sArg1)); } @@ -523,8 +520,7 @@ String sArg0 = arg0.getSymbol().toString(); String sArg1 = arg1.getSymbol().toString(); boolean result = false; - if (arg0.getType() == null || arg1.getType() == null) - return null; + if (arg0.getType() == null || arg1.getType() == null) return null; if (arg0.getType().equals(typeBoolean) || arg1.getType().equals(typeBoolean)) { result = (Boolean.parseBoolean(sArg0) || Boolean.parseBoolean(sArg1)); } @@ -544,8 +540,7 @@ Constant arg0 = (Constant) args.get(0); String sArg0 = arg0.getSymbol().toString(); boolean result = false; - if (arg0.getType() == null) - return null; + if (arg0.getType() == null) return null; if (arg0.getType().equals(typeBoolean)) { result = !Boolean.parseBoolean(sArg0); } @@ -557,8 +552,8 @@ } }); public static final Symbol cons = new Symbol("cons", 2, Symbol.Type.PREFIX, "($x,$y)->$x.add(0, $y)", Symbol.Type.LAMBDA_WITH_SIDE_EFFECT, new int[] { 1, 0 }); - public static final Symbol append = new Symbol("append", 2, Symbol.Type.PREFIX, "add", Symbol.Type.METHOD_WITH_SIDE_EFFECT); // Don't calculate here (Calculated in simulator). - public static final Symbol remove = new Symbol("remove", 2, Symbol.Type.PREFIX, "remove", Symbol.Type.METHOD_WITH_SIDE_EFFECT); // Don't calculate here (Calculated in simulator). + public static final Symbol append = new Symbol("append", 2, Symbol.Type.PREFIX, "add", Symbol.Type.METHOD_WITH_SIDE_EFFECT); // Don't calculate here (Calculated in simulator). + public static final Symbol remove = new Symbol("remove", 2, Symbol.Type.PREFIX, "remove", Symbol.Type.METHOD_WITH_SIDE_EFFECT); // Don't calculate here (Calculated in simulator). public static final Symbol head = new Symbol("head", 1, Symbol.Type.PREFIX, "($x)->$x.get(0)", Symbol.Type.LAMBDA); public static final Symbol tail = new Symbol("tail", 1, Symbol.Type.PREFIX, "($x)->$x.subList(1, $x.size())", Symbol.Type.LAMBDA); public static final Symbol length = new Symbol("length", 1, Symbol.Type.PREFIX, "($x)->$x.size()", Symbol.Type.LAMBDA, new Symbol.ICalculator() { @@ -627,8 +622,7 @@ public static final Symbol get = new Symbol("get", 2, Symbol.Type.PREFIX, "get", Symbol.Type.METHOD, new Symbol.ICalculator() { @Override public Expression calculate(List args) { - if (args.get(1).getClass() == Constant.class - && ((Constant) args.get(1)).getType().equals(typeInt)) { + if (args.get(1).getClass() == Constant.class && ((Constant) args.get(1)).getType().equals(typeInt)) { int idx = Integer.parseInt(((Constant) args.get(1)).toString()); if (args.get(0) instanceof Term) { Term term = (Term) args.get(0); @@ -644,7 +638,7 @@ } if (term instanceof ListTerm) { ListTerm listTerm = (ListTerm) term.clone(); - for (Expression child : appendedChldren) { + for (Expression child: appendedChldren) { listTerm.append(child); } return listTerm.get(idx); @@ -655,7 +649,7 @@ return null; } }); - public static final Symbol set = new Symbol("set", 3, Symbol.Type.PREFIX, "set", Symbol.Type.METHOD_WITH_SIDE_EFFECT); // Don't calculate here (Calculated in simulator). + public static final Symbol set = new Symbol("set", 3, Symbol.Type.PREFIX, "set", Symbol.Type.METHOD_WITH_SIDE_EFFECT); // Don't calculate here (Calculated in simulator). public static final Symbol contains = new Symbol("contains", 2, Symbol.Type.PREFIX, new Symbol.IImplGenerator() { @Override public String generate(Type type, Type[] childrenTypes, String[] childrenImpl, String[] childrenSideEffects, @@ -663,8 +657,7 @@ for (String s : childrenSideEffects) { sideEffect[0] += s; } - if (childrenTypes[0] != null - && (typeMap.isAncestorOf(childrenTypes[0]) || typeJson.isAncestorOf(childrenTypes[0]))) { + if (childrenTypes[0] != null && (typeMap.isAncestorOf(childrenTypes[0]) || typeJson.isAncestorOf(childrenTypes[0]))) { return childrenImpl[0] + "." + "containsKey(" + childrenImpl[1] + ")"; } return childrenImpl[0] + "." + "contains(" + childrenImpl[1] + ")"; @@ -689,7 +682,7 @@ term = (Term) term.getChild(0); } if (term instanceof ListTerm) { - for (Expression element : term.getChildren()) { + for (Expression element: term.getChildren()) { if (element.equals(args.get(1))) { return new Constant(true_); } @@ -763,13 +756,13 @@ if (term instanceof ListTerm) { int idx = 0; ListTerm listTerm = (ListTerm) term; - for (Expression child : listTerm.getChildren()) { + for (Expression child: listTerm.getChildren()) { if (child.equals(args.get(1))) { return new Constant(Integer.toString(idx), typeInt); } idx++; } - for (Expression child : appendedChldren) { + for (Expression child: appendedChldren) { if (child.equals(args.get(1))) { return new Constant(Integer.toString(idx), typeInt); } @@ -782,12 +775,10 @@ } }); public static final Symbol search = new Symbol("search", 2, Symbol.Type.PREFIX, new Symbol.IImplGenerator() { - final int count[] = { 0 }; - + final int count[] = {0}; @Override - public String generate(Type type, Type[] childrenTypes, String[] childrenImpl, String[] childrenSideEffects, - String[] sideEffect) { - for (String s : childrenSideEffects) { + public String generate(Type type, Type[] childrenTypes, String[] childrenImpl, String[] childrenSideEffects, String[] sideEffect) { + for (String s: childrenSideEffects) { sideEffect[0] += s; } String temp = "temp_search" + count[0]; @@ -868,11 +859,9 @@ } }); public static final Symbol nil = new Symbol("nil", 0, Symbol.Type.PREFIX, new Symbol.IImplGenerator() { - final int count[] = { 0 }; - + final int count[] = {0}; @Override - public String generate(Type type, Type[] childrenTypes, String[] children, String[] childrenSideEffects, - String[] sideEffect) { + public String generate(Type type, Type[] childrenTypes, String[] children, String[] childrenSideEffects, String[] sideEffect) { String compType = ""; if (type != null) { String interfaceType = type.getInterfaceTypeName(); @@ -902,37 +891,31 @@ public static final Symbol true_ = new Symbol("true", 0, Symbol.Type.PREFIX, "true", Symbol.Type.PREFIX); public static final Symbol false_ = new Symbol("false", 0, Symbol.Type.PREFIX, "false", Symbol.Type.PREFIX); public static final Symbol cond = new Symbol("if", 3, Symbol.Type.PREFIX, new Symbol.IImplGenerator() { - final int count[] = { 0 }; - + final int count[] = {0}; @Override - public String generate(Type type, Type[] childrenTypes, String[] childrenImpl, String[] childrenSideEffects, - String[] sideEffect) { + public String generate(Type type, Type[] childrenTypes, String[] childrenImpl, String[] childrenSideEffects, String[] sideEffect) { String temp = "temp_if" + count[0]; String impl = ""; - + impl += type.getInterfaceTypeName() + " " + temp + ";\n"; - if (childrenSideEffects[0] != null && childrenSideEffects[0].length() > 0) - impl += childrenSideEffects[0]; + if (childrenSideEffects[0] != null && childrenSideEffects[0].length() > 0) impl += childrenSideEffects[0]; impl += "if (" + childrenImpl[0] + ") {\n"; - if (childrenSideEffects[1] != null && childrenSideEffects[1].length() > 0) - impl += "\t" + childrenSideEffects[1]; + if (childrenSideEffects[1] != null && childrenSideEffects[1].length() > 0) impl += "\t" + childrenSideEffects[1]; impl += "\t" + temp + " = " + childrenImpl[1] + ";\n"; impl += "} else {\n"; - if (childrenSideEffects[2] != null && childrenSideEffects[2].length() > 0) - impl += "\t" + childrenSideEffects[2]; + if (childrenSideEffects[2] != null && childrenSideEffects[2].length() > 0) impl += "\t" + childrenSideEffects[2]; impl += "\t" + temp + " = " + childrenImpl[2] + ";\n"; impl += "}\n"; - + sideEffect[0] += impl; - + count[0]++; return temp; } }, new Symbol.ICalculator() { @Override public Expression calculate(List args) { - if (!(args.get(0).getClass() == Constant.class)) - return null; + if (!(args.get(0).getClass() == Constant.class)) return null; if (((Constant) args.get(0)).getSymbol().equals(true_)) { return args.get(1); } else if (((Constant) args.get(0)).getSymbol().equals(false_)) { @@ -943,9 +926,8 @@ }); public static final Symbol pair = new Symbol("pair", -1, Symbol.Type.PREFIX, new Symbol.IImplGenerator() { @Override - public String generate(Type type, Type[] childrenTypes, String[] childrenImpl, String[] childrenSideEffects, - String[] sideEffect) { - for (String s : childrenSideEffects) { + public String generate(Type type, Type[] childrenTypes, String[] childrenImpl, String[] childrenSideEffects, String[] sideEffect) { + for (String s: childrenSideEffects) { sideEffect[0] += s; } String impl = "new Pair<>(" + childrenImpl[0] + "," + childrenImpl[1] + ")"; @@ -954,9 +936,8 @@ }); public static final Symbol tuple = new Symbol("tuple", -1, Symbol.Type.PREFIX, new Symbol.IImplGenerator() { @Override - public String generate(Type type, Type[] childrenTypes, String[] childrenImpl, String[] childrenSideEffects, - String[] sideEffect) { - for (String s : childrenSideEffects) { + public String generate(Type type, Type[] childrenTypes, String[] childrenImpl, String[] childrenSideEffects, String[] sideEffect) { + for (String s: childrenSideEffects) { sideEffect[0] += s; } String impl = "new AbstractMap.SimpleEntry<>(" + childrenImpl[0] + "$x)"; @@ -971,13 +952,12 @@ public static final Symbol snd = new Symbol("snd", 1, Symbol.Type.PREFIX, "getValue", Symbol.Type.METHOD); public static final Symbol left = new Symbol("left", 1, Symbol.Type.PREFIX, "getLeft", Symbol.Type.METHOD); public static final Symbol right = new Symbol("right", 1, Symbol.Type.PREFIX, "getRight", Symbol.Type.METHOD); - public static final Symbol insert = new Symbol("insert", 3, Symbol.Type.PREFIX, "put", Symbol.Type.METHOD_WITH_SIDE_EFFECT); // Don't calculate here (Calculated in simulator). - public static final Symbol delete = new Symbol("delete", 2, Symbol.Type.PREFIX, "remove", Symbol.Type.METHOD_WITH_SIDE_EFFECT); // Don't calculate here (Calculated in simulator). + public static final Symbol insert = new Symbol("insert", 3, Symbol.Type.PREFIX, "put", Symbol.Type.METHOD_WITH_SIDE_EFFECT); // Don't calculate here (Calculated in simulator). + public static final Symbol delete = new Symbol("delete", 2, Symbol.Type.PREFIX, "remove", Symbol.Type.METHOD_WITH_SIDE_EFFECT); // Don't calculate here (Calculated in simulator). public static final Symbol lookup = new Symbol("lookup", 2, Symbol.Type.PREFIX, "get", Symbol.Type.METHOD, new Symbol.ICalculator() { @Override public Expression calculate(List args) { - if (args.get(1).getClass() == Constant.class - && ((Constant) args.get(1)).getType().equals(typeString)) { + if (args.get(1).getClass() == Constant.class && ((Constant) args.get(1)).getType().equals(typeString)) { String key = (String) ((Constant) args.get(1)).getValue(); if (args.get(0) instanceof Term) { Term term = (Term) args.get(0); @@ -988,16 +968,14 @@ if (!(term.getChild(0) instanceof Term)) { return null; } - appendedChldren - .push(new AbstractMap.SimpleEntry<>(term.getChild(0), term.getChild(1))); + appendedChldren.push(new AbstractMap.SimpleEntry<>(term.getChild(0), term.getChild(1))); term = (Term) term.getChild(0); } if (term instanceof MapTerm) { MapTerm mapTerm = (MapTerm) term.clone(); - for (Map.Entry childEnt : appendedChldren) { - if (childEnt.getKey().getClass() == Constant.class) - mapTerm.insert((String) ((Constant) childEnt.getKey()).getValue(), - childEnt.getValue()); + for (Map.Entry childEnt: appendedChldren) { + if (childEnt.getKey().getClass() == Constant.class) + mapTerm.insert((String) ((Constant) childEnt.getKey()).getValue(), childEnt.getValue()); } return mapTerm.get(key); } @@ -1014,32 +992,24 @@ @Override public Expression calculate(List args) { return new Constant(Double.toString(Math.PI), typeDouble); - } - }); + }}); public static final Symbol E = new Symbol("E", 0, Symbol.Type.PREFIX, "Math.E", Symbol.Type.PREFIX, new Symbol.ICalculator() { @Override public Expression calculate(List args) { return new Constant(Double.toString(Math.E), typeDouble); - } - }); + }}); public static final Symbol sqrt = new Symbol("sqrt", 1, Symbol.Type.PREFIX, "Math.sqrt", Symbol.Type.PREFIX, new Symbol.ICalculator() { @Override public Expression calculate(List args) { if (args.get(0).getClass() == Constant.class) { if (((Constant) args.get(0)).getType().equals(typeDouble)) { - return new Constant( - Double.toString(Math - .sqrt(Double.parseDouble((String) ((Constant) args.get(0)).getValue()))), - typeDouble); + return new Constant(Double.toString(Math.sqrt(Double.parseDouble((String) ((Constant) args.get(0)).getValue()))), typeDouble); } else if (((Constant) args.get(0)).getType().equals(typeFloat)) { - return new Constant(Float.toString( - (float) Math.sqrt(Float.parseFloat((String) ((Constant) args.get(0)).getValue()))), - typeFloat); + return new Constant(Float.toString((float) Math.sqrt(Float.parseFloat((String) ((Constant) args.get(0)).getValue()))), typeFloat); } } return null; - } - }); + }}); public static final Symbol sin = new Symbol("sin", 1, Symbol.Type.PREFIX, "Math.sin", Symbol.Type.PREFIX); public static final Symbol cos = new Symbol("cos", 1, Symbol.Type.PREFIX, "Math.cos", Symbol.Type.PREFIX); public static final Symbol tan = new Symbol("tan", 1, Symbol.Type.PREFIX, "Math.tan", Symbol.Type.PREFIX); @@ -1050,13 +1020,13 @@ public static final Symbol exp = new Symbol("exp", 1, Symbol.Type.PREFIX, "Math.exp", Symbol.Type.PREFIX); public static final Symbol log = new Symbol("log", 1, Symbol.Type.PREFIX, "Math.log", Symbol.Type.PREFIX); public static final Symbol abs = new Symbol("abs", 1, Symbol.Type.PREFIX, "Math.abs", Symbol.Type.PREFIX); - + static { add.setInverses(new Symbol[] {sub, sub}); mul.setInverses(new Symbol[] {div, div}); sub.setInverses(new Symbol[] {add}); div.setInverses(new Symbol[] {mul}); - minus.setInverses(new Symbol[] {minus }); + minus.setInverses(new Symbol[] {minus}); mod.setSignature(new Type[] {typeInt, null, null}); eq.setSignature(new Type[] {typeBoolean, null, null}); neq.setSignature(new Type[] {typeBoolean, null, null}); @@ -1089,9 +1059,9 @@ tuple.setSignature(new Type[] {typeTuple, null, null}); tuple.setInverses(new Symbol[] {fst, snd}); fst.setSignature(new Type[] {null, typeTuple}); - fst.setInverses(new Symbol[] {new LambdaAbstraction(new Variable("x"), new Term(tuple, new Expression[] {new Variable("x"), new Variable("y") }))}); - snd.setSignature(new Type[] {null, typeTuple }); - snd.setInverses(new Symbol[] {new LambdaAbstraction(new Variable("y"), new Term(tuple, new Expression[] {new Variable("x"), new Variable("y") }))}); + fst.setInverses(new Symbol[] {new LambdaAbstraction(new Variable("x"), new Term(tuple, new Expression[] {new Variable("x"), new Variable("y")}))}); + snd.setSignature(new Type[] {null, typeTuple}); + snd.setInverses(new Symbol[] {new LambdaAbstraction(new Variable("y"), new Term(tuple, new Expression[] {new Variable("x"), new Variable("y")}))}); insert.setSignature(new Type[] {typeMap, typeMap, null, null}); delete.setSignature(new Type[] {typeMap, typeMap, null}); lookup.setSignature(new Type[] {null, typeMap, null}); @@ -1109,7 +1079,7 @@ acos.setSignature(new Type[] {typeDouble, typeDouble}); acos.setInverses(new Symbol[] {cos}); atan.setSignature(new Type[] {typeDouble, typeDouble}); - atan.setInverses(new Symbol[] {tan }); + atan.setInverses(new Symbol[] {tan}); pow.setSignature(new Type[] {typeDouble, typeDouble, typeDouble}); exp.setSignature(new Type[] {typeDouble, typeDouble}); exp.setInverses(new Symbol[] {log}); @@ -1117,7 +1087,7 @@ log.setInverses(new Symbol[] {exp}); abs.setSignature(new Type[] {typeDouble, typeDouble}); } - + public DataConstraintModel() { resourcePaths = new ArrayList<>(); resourceHierarchies = new HashMap<>(); @@ -1240,7 +1210,7 @@ } public ResourcePath getResourcePath(String path) { - for (ResourcePath resourcePath : resourcePaths) { + for (ResourcePath resourcePath: resourcePaths) { if (resourcePath.toString().equals(path)) return resourcePath; } return null; @@ -1335,10 +1305,10 @@ public boolean isPrimitiveType(Type type) { if (type == typeInt - || type == typeLong - || type == typeFloat - || type == typeDouble - || type == typeBoolean) { + || type == typeLong + || type == typeFloat + || type == typeDouble + || type == typeBoolean) { return true; } return false; @@ -1412,10 +1382,10 @@ if (init.length() > 0) { out += "init {\n" + init + "}\n"; } - for (Channel channel : inputChannels.values()) { + for (Channel channel: inputChannels.values()) { out += channel.getSourceText(); } - for (Channel channel : channels.values()) { + for (Channel channel: channels.values()) { out += channel.getSourceText(); } return out;