diff --git a/AlgebraicDataflowArchitectureModel/models/CustomerOffice.model b/AlgebraicDataflowArchitectureModel/models/CustomerOffice.model new file mode 100644 index 0000000..83112ba --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/models/CustomerOffice.model @@ -0,0 +1,29 @@ +channel C_CustomerAOff_In { + out customerA.off(c, setOff(x)) == x +} + +channel C_CustomerBOff_In { + out customerB.off(c, setOff(x)) == x +} + +channel C_CompanyC1Add_In { + out companyC1.add(a, setAdd(y)) == y +} + +channel C_CompanyC2Add_In { + out companyC2.add(a, setAdd(y)) == y +} + +channel CA { + in customerA.off(c, sync(z, u, v)) == z + in companyC1.add(a1, sync(z, u, v)) == u + in companyC2.add(a2, sync(z, u, v)) == v + out customerA.add(a3, sync(z, u, v)) == if(eq(z, C1), u, if(eq(z, C2), v, nil)) +} + +channel CB { + in customerB.off(c, sync(z, u, v)) == z + in companyC1.add(a1, sync(z, u, v)) == u + in companyC2.add(a2, sync(z, u, v)) == v + out customerB.add(a3, sync(z, u, v)) == if(eq(z, C1), u, if(eq(z, C2), v, nil)) +} diff --git a/AlgebraicDataflowArchitectureModel/models/Kinetics.model b/AlgebraicDataflowArchitectureModel/models/Kinetics.model index 4b446cb..0076b7d 100644 --- a/AlgebraicDataflowArchitectureModel/models/Kinetics.model +++ b/AlgebraicDataflowArchitectureModel/models/Kinetics.model @@ -15,6 +15,6 @@ } channel C3 { - in velocity(v, update3(v)) == u - out position(p, update3(v)) == p + 0.01 * v + in velocity(v, update3(u)) == u + out position(p, update3(u)) == p + 0.01 * u } \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/src/models/algebra/Constant.java b/AlgebraicDataflowArchitectureModel/src/models/algebra/Constant.java index 690b9ec..1e698ed 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/algebra/Constant.java +++ b/AlgebraicDataflowArchitectureModel/src/models/algebra/Constant.java @@ -31,4 +31,8 @@ public String toString() { return symbol.getName(); } + + public String toImplementation() { + return symbol.getImplName(); + } } diff --git a/AlgebraicDataflowArchitectureModel/src/models/algebra/Expression.java b/AlgebraicDataflowArchitectureModel/src/models/algebra/Expression.java index 9ba946c..a99f62b 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/algebra/Expression.java +++ b/AlgebraicDataflowArchitectureModel/src/models/algebra/Expression.java @@ -13,4 +13,8 @@ public HashMap getVariables() { return getSubTerms(Variable.class); } + + public String toImplementation() { + return toString(); + } } diff --git a/AlgebraicDataflowArchitectureModel/src/models/algebra/Symbol.java b/AlgebraicDataflowArchitectureModel/src/models/algebra/Symbol.java index 0c9c9b8..1192085 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/algebra/Symbol.java +++ b/AlgebraicDataflowArchitectureModel/src/models/algebra/Symbol.java @@ -2,25 +2,47 @@ public class Symbol { private String name; + private String implName; private int arity = 0; private Type operatorType = Type.PREFIX; + private Type implOperatorType = Type.PREFIX; private Symbol[] inverses = null; private models.algebra.Type[] signature = null; + private int[] implParamOrder = null; public Symbol(String name) { this.name = name; + this.implName = name; this.arity = 0; } public Symbol(String name, int arity) { this.name = name; + this.implName = name; this.arity = arity; } public Symbol(String name, int arity, Type operatorType) { + this(name, arity); + this.operatorType = operatorType; + this.implOperatorType = operatorType; + } + + public Symbol(String name, int arity, Type operatorType, String implName, Type implOperatorType) { this.name = name; + this.implName = implName; this.arity = arity; this.operatorType = operatorType; + this.implOperatorType = implOperatorType; + } + + public Symbol(String name, int arity, Type operatorType, String implName, Type implOperatorType, int[] implParamOrder) { + this.name = name; + this.implName = implName; + this.arity = arity; + this.operatorType = operatorType; + this.implOperatorType = implOperatorType; + this.implParamOrder = implParamOrder; } public void setArity(int arity) { @@ -59,6 +81,26 @@ this.signature = signature; } + public String getImplName() { + return implName; + } + + public Type getImplOperatorType() { + return implOperatorType; + } + + public boolean isImplInfix() { + return (implOperatorType == Type.INFIX); + } + + public boolean isImplMethod() { + return (implOperatorType == Type.METHOD); + } + + public int[] getImplParamOrder() { + return implParamOrder; + } + public boolean equals(Object another) { if (!(another instanceof Symbol)) return false; return name.equals(((Symbol) another).name) && arity == ((Symbol) another).arity; @@ -73,6 +115,10 @@ return name; } + public String toImplementation() { + return implName; + } + public enum Type { PREFIX, INFIX, diff --git a/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java b/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java index 6a02785..b6e3ebb 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java +++ b/AlgebraicDataflowArchitectureModel/src/models/algebra/Term.java @@ -186,4 +186,55 @@ } } + + public String toImplementation() { + int[] implParamOrder = symbol.getImplParamOrder(); + if (getArity() == 2 && symbol.isImplInfix()) { + if (implParamOrder == null) { + return "(" + children.get(0).toImplementation() + symbol.toImplementation() + children.get(1).toImplementation() + ")"; + } else { + return "(" + children.get(implParamOrder[0]).toImplementation() + symbol.toImplementation() + children.get(implParamOrder[1]).toImplementation() + ")"; + } + } + if (getArity() >= 1 && symbol.isImplMethod()) { + if (implParamOrder == null) { + String exp = children.get(0).toImplementation() + "." + symbol.toImplementation() + "("; + String delimiter = ""; + for (int i = 1; i < children.size(); i++) { + Expression e = children.get(i); + exp += (delimiter + e.toImplementation()); + delimiter = ","; + } + return exp + ")"; + } else { + String exp = children.get(implParamOrder[0]).toImplementation() + "." + symbol.toImplementation() + "("; + String delimiter = ""; + for (int i = 1; i < children.size(); i++) { + Expression e = children.get(implParamOrder[i]); + exp += (delimiter + e.toImplementation()); + delimiter = ","; + } + return exp + ")"; + } + } else { + if (implParamOrder == null) { + String exp = symbol.toImplementation() + "("; + String delimiter = ""; + for (Expression e: children) { + exp += (delimiter + e.toImplementation()); + 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()); + delimiter = ","; + } + return exp + ")"; + } + } + } } diff --git a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java index 411eb5e..0ec0afb 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java +++ b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java @@ -19,13 +19,16 @@ public static final Symbol sub = new Symbol(Parser.SUB, 2, Symbol.Type.INFIX); public static final Symbol div = new Symbol(Parser.DIV, 2, Symbol.Type.INFIX); public static final Symbol minus = new Symbol(Parser.MINUS, 1); - public static final Symbol cons = new Symbol("cons", 2); - public static final Symbol head = new Symbol("head", 1); - public static final Symbol body = new Symbol("body", 1); + public static final Symbol cons = new Symbol("cons", 2, Symbol.Type.PREFIX, "add", Symbol.Type.METHOD, new int[] {1, 0}); + public static final Symbol head = new Symbol("head", 1, Symbol.Type.PREFIX, "head", Symbol.Type.METHOD); + public static final Symbol body = new Symbol("tail", 1, Symbol.Type.PREFIX, "tail", Symbol.Type.METHOD); + public static final Symbol cond = new Symbol("if", 3); + public static final Symbol eq = new Symbol("eq", 2, Symbol.Type.PREFIX, "==", Symbol.Type.INFIX); public static final Type typeInt = new Type("Int", "int"); public static final Type typeFloat = new Type("Float", "float"); public static final Type typeDouble = new Type("Double", "double"); public static final Type typeList = new Type("List", "List"); + public static final Type typeBoolean = new Type("Bool", "boolean"); static { add.setInverses(new Symbol[] {sub, sub}); @@ -49,6 +52,8 @@ addSymbol(cons); addSymbol(head); addSymbol(body); + addSymbol(cond); + addSymbol(eq); } public Collection getIdentifierTemplates() { diff --git a/AlgebraicDataflowArchitectureModel/src/tests/ParseTest.java b/AlgebraicDataflowArchitectureModel/src/tests/ParseTest.java index 598c922..864e816 100644 --- a/AlgebraicDataflowArchitectureModel/src/tests/ParseTest.java +++ b/AlgebraicDataflowArchitectureModel/src/tests/ParseTest.java @@ -34,7 +34,7 @@ public class ParseTest { public static void main(String[] args) { - File file = new File("models/POS2.model"); + File file = new File("models/POS.model"); try { Parser parser = new Parser(new BufferedReader(new FileReader(file))); DataFlowModel model; @@ -44,7 +44,7 @@ for (ChannelGenerator c: model.getChannelGenerators()) { for (ChannelMember out: ((DataflowChannelGenerator) c).getOutputChannelMembers()) { - System.out.println("next" + out.getIdentifierTemplate().getResourceName() + " = " + ((DataflowChannelGenerator) c).deriveUpdateExpressionOf(out)); + System.out.println("next" + out.getIdentifierTemplate().getResourceName() + " = " + ((DataflowChannelGenerator) c).deriveUpdateExpressionOf(out).toImplementation()); } } diff --git a/AlgebraicDataflowArchitectureModel/src/tests/UpdateCodeGenerationTest.java b/AlgebraicDataflowArchitectureModel/src/tests/UpdateCodeGenerationTest.java index 0955539..fcbfba3 100644 --- a/AlgebraicDataflowArchitectureModel/src/tests/UpdateCodeGenerationTest.java +++ b/AlgebraicDataflowArchitectureModel/src/tests/UpdateCodeGenerationTest.java @@ -30,19 +30,21 @@ public static void main(String[] args) { // Pre-defined symbols - Symbol floor = new Symbol("floor", 1); + Symbol floor = new Symbol("floor", 1, Symbol.Type.PREFIX, "(int)Math.floor", Symbol.Type.PREFIX); + Symbol sum = new Symbol("sum", 1, Symbol.Type.PREFIX, "stream().mapToInt(x->x).sum", Symbol.Type.METHOD); + // resources IdentifierTemplate payment = new IdentifierTemplate("payment", DataConstraintModel.typeInt, 0); // an identifier template to specify the payment resource IdentifierTemplate loyalty = new IdentifierTemplate("loyalty", DataConstraintModel.typeInt, 0); // an identifier template to specify the loyalty resource IdentifierTemplate history = new IdentifierTemplate("history", DataConstraintModel.typeList, 0);// an identifier template to specify the payment history resource IdentifierTemplate total = new IdentifierTemplate("total", DataConstraintModel.typeInt, 0); // an identifier template to specify the total payment resource - // fields + // fields in the Java program final Field fPayment = new Field("payment", DataConstraintModel.typeInt); final Field fLoyalty = new Field("loyalty", DataConstraintModel.typeInt); final Field fHistory = new Field("history", DataConstraintModel.typeList); final Field fTotal = new Field("total", DataConstraintModel.typeInt); - // parameters + // parameters in the Java program final Parameter pPayment = new Parameter("payment", DataConstraintModel.typeInt); final Parameter pLoyalty = new Parameter("loyalty", DataConstraintModel.typeInt); final Parameter pHistory = new Parameter("history", DataConstraintModel.typeList); @@ -68,6 +70,7 @@ } }; + // methods in the Java program final Symbol paymentGetter = new Symbol("getPayment", 1, Symbol.Type.METHOD); final Symbol loyltyGetter = new Symbol("getLoyalty", 1, Symbol.Type.METHOD); final Symbol historyGetter = new Symbol("getHistory", 1, Symbol.Type.METHOD); @@ -149,7 +152,7 @@ try { System.out.println("-----"); - System.out.println(c1.deriveUpdateExpressionOf(c1_loyalty)); + System.out.println(c1.deriveUpdateExpressionOf(c1_loyalty).toImplementation()); System.out.println("-- PUSH --"); Expression loyaltyPushUpdate = c1.deriveUpdateExpressionOf(c1_loyalty, pushAccessor); @@ -160,13 +163,13 @@ break; } } - System.out.println("void update(" + param.getType().getImplementastionTypeName() + " " + param + ") {"); - System.out.println("\t" + fLoyalty + " = " + loyaltyPushUpdate + ";"); + System.out.println("void update(" + param.getType().getImplementastionTypeName() + " " + param.toImplementation() + ") {"); + System.out.println("\t" + fLoyalty + " = " + loyaltyPushUpdate.toImplementation() + ";"); System.out.println("}"); System.out.println("-- PULL --"); - System.out.println(loyalty.getResourceStateType().getImplementastionTypeName() + " " + loyltyGetter + "() {"); - System.out.println("\t return " + c1.deriveUpdateExpressionOf(c1_loyalty, pullAccessor) + ";"); + System.out.println(loyalty.getResourceStateType().getImplementastionTypeName() + " " + loyltyGetter.toImplementation() + "() {"); + System.out.println("\t return " + c1.deriveUpdateExpressionOf(c1_loyalty, pullAccessor).toImplementation() + ";"); System.out.println("}"); } catch (ParameterizedIdentifierIsFutureWork | ResolvingMultipleDefinitionIsFutureWork | InvalidMessage | UnificationFailed | ValueUndefined e) { @@ -211,7 +214,7 @@ try { System.out.println("-----"); - System.out.println(c2.deriveUpdateExpressionOf(c2_history)); + System.out.println(c2.deriveUpdateExpressionOf(c2_history).toImplementation()); System.out.println("-- PUSH --"); Expression historyPushUpdate = c2.deriveUpdateExpressionOf(c2_history, pushAccessor); @@ -222,13 +225,13 @@ break; } } - System.out.println("void update(" + param.getType().getImplementastionTypeName() + " " + param + ") {"); - System.out.println("\t" + fHistory + " = " + historyPushUpdate + ";"); + System.out.println("void update(" + param.getType().getImplementastionTypeName() + " " + param.toImplementation() + ") {"); + System.out.println("\t" + fHistory + " = " + historyPushUpdate.toImplementation() + ";"); System.out.println("}"); System.out.println("-- PULL --"); - System.out.println(history.getResourceStateType().getImplementastionTypeName() + " " + historyGetter + "() {"); - System.out.println("\t return " + c2.deriveUpdateExpressionOf(c2_history, pullAccessor) + ";"); + System.out.println(history.getResourceStateType().getImplementastionTypeName() + " " + historyGetter.toImplementation() + "() {"); + System.out.println("\t return " + c2.deriveUpdateExpressionOf(c2_history, pullAccessor).toImplementation() + ";"); System.out.println("}"); } catch (ParameterizedIdentifierIsFutureWork | ResolvingMultipleDefinitionIsFutureWork | InvalidMessage | UnificationFailed | ValueUndefined e) { @@ -254,7 +257,7 @@ Term c3_message = new Term(update3); // update3(u) c3_message.addChild(u); Expression nextHistory2 = u; - Term nextTotal = new Term(new Symbol("sum", 1)); + Term nextTotal = new Term(sum); nextTotal.addChild(u); StateTransition c3_history_transition = new StateTransition(); @@ -273,7 +276,7 @@ try { System.out.println("-----"); - System.out.println(c3.deriveUpdateExpressionOf(c3_total)); + System.out.println(c3.deriveUpdateExpressionOf(c3_total).toImplementation()); System.out.println("-- PUSH --"); Expression totalPushUpdate = c3.deriveUpdateExpressionOf(c3_total, pushAccessor); @@ -284,13 +287,13 @@ break; } } - System.out.println("void update(" + param.getType().getImplementastionTypeName() + " " + param + ") {"); - System.out.println("\t" + fTotal + " = " + totalPushUpdate + ";"); + System.out.println("void update(" + param.getType().getImplementastionTypeName() + " " + param.toImplementation() + ") {"); + System.out.println("\t" + fTotal + " = " + totalPushUpdate.toImplementation() + ";"); System.out.println("}"); System.out.println("-- PULL --"); - System.out.println(total.getResourceStateType().getImplementastionTypeName() + " " + totalGetter + "() {"); - System.out.println("\t return " + c3.deriveUpdateExpressionOf(c3_total, pullAccessor) + ";"); + System.out.println(total.getResourceStateType().getImplementastionTypeName() + " " + totalGetter.toImplementation() + "() {"); + System.out.println("\t return " + c3.deriveUpdateExpressionOf(c3_total, pullAccessor).toImplementation() + ";"); System.out.println("}"); } catch (ParameterizedIdentifierIsFutureWork | ResolvingMultipleDefinitionIsFutureWork | InvalidMessage | UnificationFailed | ValueUndefined e) {