diff --git a/AlgebraicDataflowArchitectureModel/models/JumpGame.model b/AlgebraicDataflowArchitectureModel/models/JumpGame.model index d8b002b..36ce9b7 100644 --- a/AlgebraicDataflowArchitectureModel/models/JumpGame.model +++ b/AlgebraicDataflowArchitectureModel/models/JumpGame.model @@ -3,8 +3,8 @@ out time(t:Double, gravity(y)) == t + 0.01 } channel CIO2 { - out move(v:Pair, moveX(x:Double)) == pair(x, snd(v)) - out move(v, moveY(y:Double)) == pair(fst(v), y) + out move(v:Pair, moveX(x:Double)) == pair(x, right(v)) + out move(v, moveY(y:Double)) == pair(left(v), y) } channel CIO3 { out mass(m:Double, setMass(x:Double)) == x @@ -17,34 +17,34 @@ ref onground(o, update1(f2, m2, o)) in force(f, update1(f2, m2, o)) == f2 in mass(m, update1(f2, m2, o)) == m2 - out acceleration(a:Pair, update1(f2, m2, o)) == if(o, pair(fst(f2) / m2, 0.0), pair(fst(f2) / m2, snd(f2) / m2)) + out acceleration(a:Pair, update1(f2, m2, o)) == if(o, pair(left(f2) / m2, 0.0), pair(left(f2) / m2, right(f2) / m2)) } channel C2 { ref onground(o, update3(a2, o)) in acceleration(a, update3(a2, o)) == a2 - out velocity(v:Pair, update3(a2, o)) == if(and(o, lt(snd(v), 0.0)), - pair(fst(v) + 0.01 * fst(a2), 0.0), - pair(fst(v) + 0.01 * fst(a2), snd(v) + 0.01 * snd(a2))) + out velocity(v:Pair, update3(a2, o)) == if(and(o, lt(right(v), 0.0)), + pair(left(v) + 0.01 * left(a2), 0.0), + pair(left(v) + 0.01 * left(a2), right(v) + 0.01 * right(a2))) } channel C3 { ref onground(o, update4(m2, o)) in move(m, update4(m2, o)) == m2 - out velocity(v:Pair, update4(m2, o)) == if(and(o, ge(snd(m2), 0.0)), m2, v) + out velocity(v:Pair, update4(m2, o)) == if(and(o, ge(right(m2), 0.0)), m2, v) } channel C4 { in velocity(v, update5(v2, g)) == v2 ref ground(g, update5(v2, g)) - out position(p:Pair, update5(v2, g)) == if(and(eq(g, true), lt(snd(p) + 0.01 * snd(v2), 0.0)), - pair(fst(p) + 0.01 * fst(v2), 0.0), - pair(fst(p) + 0.01 * fst(v2), snd(p) + 0.01 * snd(v2))) + out position(p:Pair, update5(v2, g)) == if(and(eq(g, true), lt(right(p) + 0.01 * right(v2), 0.0)), + pair(left(p) + 0.01 * left(v2), 0.0), + pair(left(p) + 0.01 * left(v2), right(p) + 0.01 * right(v2))) } channel C5 { in position(p, update2(p2, g2)) == p2 in ground(g, update2(p2, g2)) == g2 - out onground(o:Bool, update2(p2, g2)) == and(eq(g2, true), le(snd(p2), 0.0)) + out onground(o:Bool, update2(p2, g2)) == and(eq(g2, true), le(right(p2), 0.0)) } channel C6 { in position(p, update6(p2)) == p2 - out clear(c:Bool, update6(p2)) == if(gt(fst(p2), 100.0), true, false) - out gameover(go:Bool, update6(p2)) == if(lt(snd(p2), -1.0), true, false) + out clear(c:Bool, update6(p2)) == if(gt(left(p2), 100.0), true, false) + out gameover(go:Bool, update6(p2)) == if(lt(right(p2), -1.0), true, false) } \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/models/Twitter.model b/AlgebraicDataflowArchitectureModel/models/Twitter.model index f02f134..57de3b3 100644 --- a/AlgebraicDataflowArchitectureModel/models/Twitter.model +++ b/AlgebraicDataflowArchitectureModel/models/Twitter.model @@ -1,11 +1,9 @@ channel a_Tweet { out a_tweets(l:List, a_tweet(t:Str, time:Long)) == cons(tuple(time, t), l) - out a_tweets(l, e) == l } channel a_Follow { out a_following(f:List, a_follow(u:Int)) == cons(u, f) - out a_following(f, e) == f } channel a_Home { @@ -18,12 +16,10 @@ channel b_Tweet { out b_tweets(l:List, b_tweet(t:Str, time:Long)) == cons(tuple(time, t), l) - out b_tweets(l, e) == l } channel b_Follow { out b_following(f:List, b_follow(u:Int)) == cons(u, f) - out b_following(f, e) == f } channel b_Home { @@ -36,12 +32,10 @@ channel c_Tweet { out c_tweets(l:List, c_tweet(t:Str, time:Long)) == cons(tuple(time, t), l) - out c_tweets(l, e) == l } channel c_Follow { out c_following(f:List, c_follow(u:Int)) == cons(u, f) - out c_following(f, e) == f } channel c_Home { diff --git a/AlgebraicDataflowArchitectureModel/models/WeatherObservationSystem.model b/AlgebraicDataflowArchitectureModel/models/WeatherObservationSystem.model index 02ea307..0a14140 100644 --- a/AlgebraicDataflowArchitectureModel/models/WeatherObservationSystem.model +++ b/AlgebraicDataflowArchitectureModel/models/WeatherObservationSystem.model @@ -1,7 +1,11 @@ -channel CIO { +channel CIO1 { out temp_f(p:Double, observe(x)) == x } +channel CIO2 { + out highest(h:Double, reset(v)) == v +} + channel C1{ in temp_f(q:Double, conversion(y)) == y out temp_c(r:Double, conversion(z)) == (z-32) / 1.8 @@ -9,5 +13,5 @@ channel C2{ in temp_f(q:Double, update(y)) == y - out highest(r:Double, update(z)) == if(gt(z, r), z, r) + out highest(h:Double, update(z)) == if(gt(z, h), z, h) } \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/src/algorithms/JavaCodeGenerator.java b/AlgebraicDataflowArchitectureModel/src/algorithms/JavaCodeGenerator.java index 92b0f8c..821a517 100644 --- a/AlgebraicDataflowArchitectureModel/src/algorithms/JavaCodeGenerator.java +++ b/AlgebraicDataflowArchitectureModel/src/algorithms/JavaCodeGenerator.java @@ -143,10 +143,8 @@ constructor.addParameter(new VariableDeclaration(new Type(dstResName, dstResName), ((ResourceNode) re.getDestination()).getIdentifierTemplate().getResourceName())); block.addStatement("this." + dstResName.toLowerCase() + " = " + dstResName.toLowerCase() + ";"); - constructor.setBody(block); } } - block = new Block(); for (Edge e : rn.getInEdges()) { ResourceDependency re = (ResourceDependency) e; String srcResName = ((ResourceNode) re.getSource()).getIdentifierTemplate().getResourceName() @@ -159,7 +157,6 @@ constructor.addParameter(new VariableDeclaration(new Type(srcResName, srcResName), ((ResourceNode) re.getSource()).getIdentifierTemplate().getResourceName())); block.addStatement("this." + srcResName.toLowerCase() + " = " + srcResName.toLowerCase() + ";"); - constructor.setBody(block); } else { // Declare an update method in the type of the destination resource. ArrayList vars = new ArrayList<>(); @@ -182,11 +179,11 @@ type.addField(new FieldDeclaration(new Type(refResName, refResName), id.getResourceName())); constructor.addParameter(new VariableDeclaration(new Type(refResName, refResName), id.getResourceName())); block.addStatement("this." + refResName.toLowerCase() + " = " + refResName.toLowerCase() + ";"); - constructor.setBody(block); } } } } + constructor.setBody(block); if (constructor.getParameters() != null) type.addMethod(constructor); @@ -221,6 +218,17 @@ } } } + } else if (message.getClass() == Variable.class) { + MethodDeclaration input = new MethodDeclaration( + ((Variable) cm.getStateTransition().getMessageExpression()).getName(), + false, typeVoid, null); + type.addMethod(input); + String str = ((Variable) cm.getStateTransition().getMessageExpression()).getName(); + input = getMethod(mainType, str); + if (input == null) { + input = new MethodDeclaration(str, false, typeVoid, null); + mainType.addMethod(input); + } } } } @@ -244,16 +252,23 @@ codes.add(cu); } - // Declare the Pair class. boolean isCreatedPair = false; - for(ResourceNode rn : resources) { if(isCreatedPair) continue; if(model.getType("Pair").isAncestorOf(rn.getIdentifierTemplate().getResourceStateType())) { TypeDeclaration type = new TypeDeclaration("Pair"); - type.addField(new FieldDeclaration(new Type("Double","T"), "first")); - type.addField(new FieldDeclaration(new Type("Double","T"), "second")); + type.addField(new FieldDeclaration(new Type("Double", "T"), "left")); + type.addField(new FieldDeclaration(new Type("Double", "T"), "right")); + + MethodDeclaration constructor = new MethodDeclaration("Pair", true); + constructor.addParameter(new VariableDeclaration(new Type("Double", "T"), "left")); + constructor.addParameter(new VariableDeclaration(new Type("Double", "T"), "right")); + Block block = new Block(); + block.addStatement("this.left = left;"); + block.addStatement("this.right = right;"); + constructor.setBody(block); + type.addMethod(constructor); for(FieldDeclaration field : type.getFields()) { MethodDeclaration getter = new MethodDeclaration( @@ -272,10 +287,6 @@ } } - - - - // Declare getter methods in the main type. for (Node n : graph.getNodes()) { ResourceNode rn = (ResourceNode) n; @@ -285,7 +296,7 @@ rn.getIdentifierTemplate().getResourceStateType()); getter.setBody(new Block()); getter.getBody().addStatement( - "return " + rn.getIdentifierTemplate().getResourceName() + "." + getter.getName() + "();"); + "return " + rn.getIdentifierTemplate().getResourceName() + ".getValue();"); mainType.addMethod(getter); } @@ -322,42 +333,11 @@ } else { String cons = "\t" + "private " + field.getType().getInterfaceTypeName() + " " + field.getName() + " = new " + field.getType().getTypeName() + "("; - for (TypeDeclaration tree : codeTree) { - if (field.getType().getTypeName() == tree.getTypeName()) { - for (VariableDeclaration var : tree.getConstructors()) { - cons += var.getName() + ","; - } - if (!tree.getConstructors().isEmpty()) - cons = cons.substring(0, cons.length() - 1); - break; - } - } cons += ");"; codes.add(cons); } } codes.add(""); - if (type.getTypeName() != mainTypeName) { - if (!type.getConstructors().isEmpty()) { - String cons = "\t" + "public " + type.getTypeName() + "("; - for (VariableDeclaration constructor : type.getConstructors()) { - cons += constructor.getType().getTypeName() + " " + constructor.getName() + ","; - } - if (!type.getConstructors().isEmpty()) - cons = cons.substring(0, cons.length() - 1); - cons += "){"; - codes.add(cons); - for (FieldDeclaration field : type.getFields()) { - for (VariableDeclaration vari : type.getConstructors()) { - if (field.getType().getTypeName().equals(vari.getType().getTypeName())) { - codes.add("\t\t" + "this." + field.getName() + " = " + field.getName() + ";"); - } - } - } - codes.add("\t" + "}"); - codes.add(""); - } - } for (MethodDeclaration method : type.getMethods()) { String varstr = "\t" + "public " + method.getReturnType().getInterfaceTypeName() + " " + method.getName() + "("; @@ -451,7 +431,7 @@ @Override public Expression getCurrentStateAccessorFor(IdentifierTemplate target, IdentifierTemplate from) { if (target.equals(from)) { - return new Field(target.getResourceName(), + return new Field("value", target.getResourceStateType() != null ? target.getResourceStateType() : DataConstraintModel.typeInt); } diff --git a/AlgebraicDataflowArchitectureModel/src/algorithms/JavaMethodBodyGenerator.java b/AlgebraicDataflowArchitectureModel/src/algorithms/JavaMethodBodyGenerator.java index 6691254..b2f0f0a 100644 --- a/AlgebraicDataflowArchitectureModel/src/algorithms/JavaMethodBodyGenerator.java +++ b/AlgebraicDataflowArchitectureModel/src/algorithms/JavaMethodBodyGenerator.java @@ -162,8 +162,7 @@ if (ioChannelAndMembers != null) { Set outs = ioChannelAndMembers.getValue(); for (ChannelMember out: outs) { - Term message = (Term) out.getStateTransition().getMessageExpression(); - MethodDeclaration input = getMethod(type, message.getSymbol().getName()); + MethodDeclaration input = getInputMethod(type, out); if (input != null) { String[] sideEffects = new String[] {""}; Expression updateExp = ioChannelAndMembers.getKey().deriveUpdateExpressionOf(out, JavaCodeGenerator.pushAccessor); @@ -172,7 +171,7 @@ if (updateExp instanceof Term && ((Term) updateExp).getSymbol().isImplWithSideEffect()) { updateStatement = sideEffects[0]; } else { - updateStatement = sideEffects[0] + "this." + resourceName + " = " + newState + ";"; + updateStatement = sideEffects[0] + "this.value = " + newState + ";"; } if (input.getBody() == null || !input.getBody().getStatements().contains(updateStatement)) { input.addFirstStatement(updateStatement); @@ -182,9 +181,12 @@ if (mainInput != null) { String args = ""; String delimitar = ""; - for (Variable var: message.getVariables().values()) { - args += delimitar + var.getName(); - delimitar = ", "; + if (out.getStateTransition().getMessageExpression() instanceof Term) { + Term message = (Term) out.getStateTransition().getMessageExpression(); + for (Variable var: message.getVariables().values()) { + args += delimitar + var.getName(); + delimitar = ", "; + } } mainInput.addStatement("this." + resourceName + "." + input.getName() + "(" + args + ");"); } @@ -233,8 +235,7 @@ // I/O channel for (ChannelMember out: ch.getOutputChannelMembers()) { if (out.getIdentifierTemplate().equals(resource.getIdentifierTemplate())) { - if (out.getStateTransition().getMessageExpression() instanceof Term) { - // not an identity element + if (out.getStateTransition().getMessageExpression() instanceof Term || out.getStateTransition().getMessageExpression() instanceof Variable) { channel = ch; channelMembers.add(out); } @@ -251,18 +252,26 @@ // I/O channel for (ChannelMember out: channel.getOutputChannelMembers()) { if (out.getIdentifierTemplate().equals(resource.getIdentifierTemplate())) { - if (out.getStateTransition().getMessageExpression() instanceof Term) { - // not an identity element - Term message = (Term) out.getStateTransition().getMessageExpression(); - MethodDeclaration input = getMethod(type, message.getSymbol().getName()); - return input; - } + MethodDeclaration input = getInputMethod(type, out); + if (input != null) return input; } } } return null; } + private static MethodDeclaration getInputMethod(TypeDeclaration type, ChannelMember out) { + MethodDeclaration input = null; + if (out.getStateTransition().getMessageExpression() instanceof Term) { + Term message = (Term) out.getStateTransition().getMessageExpression(); + input = getMethod(type, message.getSymbol().getImplName()); + } else if (out.getStateTransition().getMessageExpression() instanceof Variable) { + Variable message = (Variable) out.getStateTransition().getMessageExpression(); + input = getMethod(type, message.getName()); + } + return input; + } + private static MethodDeclaration getMethod(TypeDeclaration type, String methodName) { for (MethodDeclaration m: type.getMethods()) { if (m.getName().equals(methodName)) return m; diff --git a/AlgebraicDataflowArchitectureModel/src/algorithms/JerseyCodeGenerator.java b/AlgebraicDataflowArchitectureModel/src/algorithms/JerseyCodeGenerator.java index 18556d6..034b89f 100644 --- a/AlgebraicDataflowArchitectureModel/src/algorithms/JerseyCodeGenerator.java +++ b/AlgebraicDataflowArchitectureModel/src/algorithms/JerseyCodeGenerator.java @@ -26,6 +26,7 @@ import models.dataConstraintModel.DataConstraintModel; import models.dataConstraintModel.IdentifierTemplate; import models.dataFlowModel.DataFlowModel; +import models.dataFlowModel.DataflowChannelGenerator; import models.dataFlowModel.DataflowChannelGenerator.IResourceStateAccessor; import models.dataFlowModel.PushPullAttribute; import models.dataFlowModel.PushPullValue; @@ -127,6 +128,24 @@ } } + // Declare a client field to connect to the source resource of reference transfer. + if (!bDeclareClientField) { + for (ChannelGenerator cg : model.getChannelGenerators()) { + DataflowChannelGenerator dcg = ((DataflowChannelGenerator) cg); + for (ChannelMember cm : dcg.getOutputChannelMembers()) { + if (cm.getIdentifierTemplate().getResourceName().equals(type.getTypeName().toLowerCase())) { + if (dcg.getReferenceChannelMembers().size() > 0) { + // If there exists one or more reference channel member. + type.addField(new FieldDeclaration(typeClient, "client", "ClientBuilder.newClient()")); + bDeclareClientField = true; + break; + } + } + } + if (bDeclareClientField) break; + } + } + // Declare input methods in resources. for (ChannelGenerator cg : model.getIOChannelGenerators()) { for (ChannelMember cm : cg.getChannelMembers()) { @@ -149,6 +168,16 @@ input.addAnnotation(new Annotation("POST")); } type.addMethod(input); + } else if (message.getClass() == Variable.class) { + MethodDeclaration input = new MethodDeclaration( + ((Variable) cm.getStateTransition().getMessageExpression()).getName(), + false, typeVoid, null); + if (cm.getStateTransition().isRightUnary()) { + input.addAnnotation(new Annotation("PUT")); + } else { + input.addAnnotation(new Annotation("POST")); + } + type.addMethod(input); } } } @@ -159,11 +188,11 @@ String initializer = "new " + rn.getIdentifierTemplate().getResourceStateType().getImplementationTypeName() + "()"; Type stateType = rn.getIdentifierTemplate().getResourceStateType(); if (!DataConstraintModel.typeList.isAncestorOf(stateType)) initializer = null; - type.addField(new FieldDeclaration(stateType, rn.getIdentifierTemplate().getResourceName(), initializer)); + type.addField(new FieldDeclaration(stateType, "value", initializer)); } // Declare the getter method to obtain the state in the type of each resource. - MethodDeclaration getter = new MethodDeclaration("get" + type.getTypeName(), rn.getIdentifierTemplate().getResourceStateType()); + MethodDeclaration getter = new MethodDeclaration("getValue", rn.getIdentifierTemplate().getResourceStateType()); getter.addAnnotation(new Annotation("Produces", "MediaType.APPLICATION_JSON")); getter.addAnnotation(new Annotation("GET")); type.addMethod(getter); @@ -180,6 +209,48 @@ codes.add(cu); } + // Declare the Pair class. + boolean isCreatedPair = false; + for(Node n : resources) { + ResourceNode rn = (ResourceNode) n; + if(isCreatedPair) continue; + if(model.getType("Pair").isAncestorOf(rn.getIdentifierTemplate().getResourceStateType())) { + TypeDeclaration type = new TypeDeclaration("Pair"); + type.addField(new FieldDeclaration(new Type("Double", "T"), "left")); + type.addField(new FieldDeclaration(new Type("Double", "T"), "right")); + + MethodDeclaration constructor = new MethodDeclaration("Pair", true); + constructor.addParameter(new VariableDeclaration(new Type("Double", "T"), "left")); + constructor.addParameter(new VariableDeclaration(new Type("Double", "T"), "right")); + Block block = new Block(); + block.addStatement("this.left = left;"); + block.addStatement("this.right = right;"); + constructor.setBody(block); + type.addMethod(constructor); + + for(FieldDeclaration field : type.getFields()) { + MethodDeclaration getter = new MethodDeclaration( + "get" + field.getName().substring(0,1).toUpperCase() + field.getName().substring(1), + new Type("Double","T")); + getter.setBody(new Block()); + getter.getBody().addStatement("return " + field.getName() + ";"); + type.addMethod(getter); + } + +// MethodDeclaration toStr = new MethodDeclaration("toString", false, DataConstraintModel.typeString, null); +// block = new Block(); +// block.addStatement("return \"{\\\"\" + left + \"\\\":\\\"\" + right + \"\\\"}\";"); +// toStr.setBody(block); +// type.addMethod(toStr); + + CompilationUnit cu = new CompilationUnit(type); + cu.addImport(new ImportDeclaration("java.util.*")); + codes.add(cu); + + isCreatedPair = true; + } + } + return codes; } @@ -198,42 +269,11 @@ } else { String cons = "\t" + "private " + field.getType().getInterfaceTypeName() + " " + field.getName() + " = new " + field.getType().getTypeName() + "("; - for (TypeDeclaration tree : codeTree) { - if (field.getType().getTypeName() == tree.getTypeName()) { - for (VariableDeclaration var : tree.getConstructors()) { - cons += var.getName() + ","; - } - if (!tree.getConstructors().isEmpty()) - cons = cons.substring(0, cons.length() - 1); - break; - } - } cons += ");"; codes.add(cons); } } codes.add(""); - if (type.getTypeName() != mainTypeName) { - if (!type.getConstructors().isEmpty()) { - String cons = "\t" + "public " + type.getTypeName() + "("; - for (VariableDeclaration constructor : type.getConstructors()) { - cons += constructor.getType().getTypeName() + " " + constructor.getName() + ","; - } - if (!type.getConstructors().isEmpty()) - cons = cons.substring(0, cons.length() - 1); - cons += "){"; - codes.add(cons); - for (FieldDeclaration field : type.getFields()) { - for (VariableDeclaration vari : type.getConstructors()) { - if (field.getType().getTypeName().equals(vari.getType().getTypeName())) { - codes.add("\t\t" + "this." + field.getName() + " = " + field.getName() + ";"); - } - } - } - codes.add("\t" + "}"); - codes.add(""); - } - } for (MethodDeclaration method : type.getMethods()) { String varstr = "\t" + "public " + method.getReturnType().getInterfaceTypeName() + " " + method.getName() + "("; @@ -263,7 +303,7 @@ @Override public Expression getCurrentStateAccessorFor(IdentifierTemplate target, IdentifierTemplate from) { if (target.equals(from)) { - return new Field(target.getResourceName(), + return new Field("value", target.getResourceStateType() != null ? target.getResourceStateType() : DataConstraintModel.typeInt); } @@ -281,7 +321,7 @@ @Override public Expression getCurrentStateAccessorFor(IdentifierTemplate target, IdentifierTemplate from) { if (target.equals(from)) { - return new Field(target.getResourceName(), + return new Field("value", target.getResourceStateType() != null ? target.getResourceStateType() : DataConstraintModel.typeInt); } diff --git a/AlgebraicDataflowArchitectureModel/src/algorithms/JerseyMethodBodyGenerator.java b/AlgebraicDataflowArchitectureModel/src/algorithms/JerseyMethodBodyGenerator.java index 89446db..26e29fc 100644 --- a/AlgebraicDataflowArchitectureModel/src/algorithms/JerseyMethodBodyGenerator.java +++ b/AlgebraicDataflowArchitectureModel/src/algorithms/JerseyMethodBodyGenerator.java @@ -22,6 +22,7 @@ import models.algebra.Type; import models.algebra.UnificationFailed; import models.algebra.ValueUndefined; +import models.algebra.Variable; import models.dataConstraintModel.ChannelGenerator; import models.dataConstraintModel.ChannelMember; import models.dataConstraintModel.DataConstraintModel; @@ -89,10 +90,10 @@ if (updateExp instanceof Term && ((Term) updateExp).getSymbol().isImplWithSideEffect()) { updateStatement = sideEffects[0]; } else { - updateStatement = sideEffects[0] + dstResourceName + " = " + curState + ";"; + updateStatement = sideEffects[0] + "this.value = " + curState + ";"; } if (update.getBody() == null || !update.getBody().getStatements().contains(updateStatement)) { - // add an update statement of the statement of dst side resource. + // add an update statement of the state of dst side resource. update.addFirstStatement(updateStatement); if (d.getChannelGenerator().getReferenceChannelMembers().size() > 0) { // For each reference channel member, get the current state of the reference side resource by pull data transfer. @@ -111,7 +112,7 @@ update.addFirstStatement(cashStatement); } } - // to convert a json param to a tuple object. + // to convert a json param to a tuple or pair object. for (VariableDeclaration param: update.getParameters()) { Type paramType = param.getType(); String paramName = param.getName(); @@ -128,16 +129,37 @@ paramConverter += "\t" + paramName + ".add(" + getCodeForConversionFromMapToTuple(compType, "i") + ");\n"; paramConverter += "}"; update.addThrow("JsonProcessingException"); + } else if (DataConstraintModel.typePair.isAncestorOf(compType)) { + param.setType(DataConstraintModel.typeListStr); + param.setName(paramName + "_json"); + paramConverter += paramType.getInterfaceTypeName() + " " + paramName + " = new " + paramType.getImplementationTypeName() + "();\n"; + paramConverter += "for (String str: " + param.getName() + ") {\n"; + String mapTypeName = convertFromEntryToMapType(compType); + paramConverter += "\t" + mapTypeName + " i = new ObjectMapper().readValue(str, HashMap.class);\n"; + paramConverter += "\t" + paramName + ".add(" + getCodeForConversionFromMapToPair(compType, "i") + ");\n"; + paramConverter += "}"; + update.addThrow("JsonProcessingException"); } } else if (DataConstraintModel.typeTuple.isAncestorOf(paramType)) { param.setType(DataConstraintModel.typeString); param.setName(paramName + "_json"); + paramConverter += paramType.getInterfaceTypeName() + " " + paramName + ";\n"; paramConverter += "{\n"; String mapTypeName = convertFromEntryToMapType(paramType); paramConverter += "\t" + mapTypeName + " i = new ObjectMapper().readValue(" + paramName + "_json" + ", HashMap.class);\n"; paramConverter += "\t" + paramName + " = " + getCodeForConversionFromMapToTuple(paramType, "i") + ";\n"; paramConverter += "}"; update.addThrow("JsonProcessingException"); + } else if (DataConstraintModel.typePair.isAncestorOf(paramType)) { + param.setType(DataConstraintModel.typeString); + param.setName(paramName + "_json"); + paramConverter += paramType.getInterfaceTypeName() + " " + paramName + ";\n"; + paramConverter += "{\n"; + String mapTypeName = convertFromEntryToMapType(paramType); + paramConverter += "\t" + mapTypeName + " i = new ObjectMapper().readValue(" + paramName + "_json" + ", HashMap.class);\n"; + paramConverter += "\t" + paramName + " = " + getCodeForConversionFromMapToPair(paramType, "i") + ";\n"; + paramConverter += "}"; + update.addThrow("JsonProcessingException"); } if (paramConverter.length() > 0) update.addFirstStatement(paramConverter); } @@ -145,7 +167,7 @@ if (((StoreAttribute) dst.getAttribute()).isStored()) { // returns the state stored in a field. if (getter.getBody() == null || getter.getBody().getStatements().size() == 0) { - getter.addStatement("return " + dstResourceName + ";"); + getter.addStatement("return value;"); } } // src side (for a chain of update method invocations) @@ -162,7 +184,7 @@ srcResName = srcResourceName; } if (!chainedCalls.contains(srcUpdate)) { - srcUpdate.addStatement(getHttpMethodParamsStatement(srcType.getTypeName(), src.getIdentifierTemplate().getResourceStateType(), srcResourceName)); + srcUpdate.addStatement(getHttpMethodParamsStatement(srcType.getTypeName(), src.getIdentifierTemplate().getResourceStateType(), srcResourceName, "this.value")); srcUpdate.addStatement("String result = " + getHttpMethodCallStatement(baseURL, dstResourceName, srcResName, httpMethod)); chainedCalls.add(srcUpdate); } else { @@ -178,7 +200,7 @@ srcResName = srcResourceName; } if (!chainedCalls.contains(srcInput)) { - srcInput.addStatement(getHttpMethodParamsStatement(srcType.getTypeName(), src.getIdentifierTemplate().getResourceStateType(), srcResourceName)); + srcInput.addStatement(getHttpMethodParamsStatement(srcType.getTypeName(), src.getIdentifierTemplate().getResourceStateType(), srcResourceName, "this.value")); srcInput.addStatement("String result = " + getHttpMethodCallStatement(baseURL, dstResourceName, srcResName, httpMethod)); chainedCalls.add(srcInput); } else { @@ -217,15 +239,14 @@ // getter method MethodDeclaration getter = getGetterMethod(type); if (getter.getBody() == null || getter.getBody().getStatements().size() == 0) { - getter.addStatement("return " + resource.getIdentifierTemplate().getResourceName() + ";"); + getter.addStatement("return value;"); } // methods for input events Map.Entry> ioChannelAndMembers = getIOChannelAndMembers(resource, model); if (ioChannelAndMembers != null) { Set outs = ioChannelAndMembers.getValue(); for (ChannelMember out: outs) { - Term message = (Term) out.getStateTransition().getMessageExpression(); - MethodDeclaration input = getMethod(type, message.getSymbol().getName()); + MethodDeclaration input = getInputMethod(type, out); if (input != null) { Expression updateExp = ioChannelAndMembers.getKey().deriveUpdateExpressionOf(out, JerseyCodeGenerator.pushAccessor); String[] sideEffects = new String[] {""}; @@ -234,7 +255,7 @@ if (updateExp instanceof Term && ((Term) updateExp).getSymbol().isImplWithSideEffect()) { updateStatement = sideEffects[0]; } else { - updateStatement = sideEffects[0] + "this." + resourceName + " = " + newState + ";"; + updateStatement = sideEffects[0] + "this.value = " + newState + ";"; } if (input.getBody() == null || !input.getBody().getStatements().contains(updateStatement)) { input.addFirstStatement(updateStatement); @@ -254,6 +275,7 @@ private static void generatePullDataTransfer(MethodDeclaration methodBody, String fromResourceName, Type fromResourceType) { String varName = new String(fromResourceName); String respTypeName = fromResourceType.getInterfaceTypeName(); + String respImplTypeName = fromResourceType.getImplementationTypeName(); String respConverter = ""; if (DataConstraintModel.typeList.isAncestorOf(fromResourceType) && fromResourceType != DataConstraintModel.typeList) { Type compType = TypeInference.getListComponentType(fromResourceType); @@ -268,24 +290,40 @@ methodBody.addThrow("JsonProcessingException"); } } else if (DataConstraintModel.typeTuple.isAncestorOf(fromResourceType)) { -// varName += "_json"; - respTypeName = fromResourceType.getInterfaceTypeName(); -// respConverter += fromResourceName + " = " + getCodeForConversionFromMapToTuple(fromResourceType, varName) + ";"; -// methodBody.addThrow("JsonProcessingException"); + varName += "_json"; + respTypeName = convertFromEntryToMapType(fromResourceType); + respConverter += fromResourceType.getInterfaceTypeName() + " " + fromResourceName + " = " + getCodeForConversionFromMapToTuple(fromResourceType, varName) + ";"; + respImplTypeName = "HashMap"; + } else if (DataConstraintModel.typePair.isAncestorOf(fromResourceType)) { + varName += "_json"; + respTypeName = convertFromEntryToMapType(fromResourceType); + respConverter += fromResourceType.getInterfaceTypeName() + " " + fromResourceName + " = " + getCodeForConversionFromMapToPair(fromResourceType, varName) + ";"; + respImplTypeName = "HashMap"; } if (respConverter.length() > 0) { methodBody.addFirstStatement(respConverter); } - methodBody.addFirstStatement(respTypeName + " " + varName + " = " + getHttpMethodCallStatementWithResponse(baseURL, fromResourceName, "get", fromResourceType.getImplementationTypeName())); + methodBody.addFirstStatement(respTypeName + " " + varName + " = " + getHttpMethodCallStatementWithResponse(baseURL, fromResourceName, "get", respImplTypeName)); } private static String convertFromEntryToMapType(Type type) { - String mapTypeName = type.getInterfaceTypeName(); - mapTypeName = mapTypeName.replace("Map.Entry", "Map"); - for (int idx = mapTypeName.indexOf("<", 0); idx >= 0; idx = mapTypeName.indexOf("<", idx + 1)) { - int to = mapTypeName.indexOf(",", idx); - if (to > idx) { - mapTypeName = mapTypeName.substring(0, idx + 1) + "String" + mapTypeName.substring(to); // All elements except for the last one have the string type. + String mapTypeName = null; + if (DataConstraintModel.typePair.isAncestorOf(type)) { + Type compType = TypeInference.getPairComponentType(type); + String wrapperType = DataConstraintModel.getWrapperType(compType); + if (wrapperType != null) { + mapTypeName = "Map"; + } else { + mapTypeName = "Map"; + } + } else { + mapTypeName = type.getInterfaceTypeName(); + mapTypeName = mapTypeName.replace("Map.Entry", "Map"); + for (int idx = mapTypeName.indexOf("<", 0); idx >= 0; idx = mapTypeName.indexOf("<", idx + 1)) { + int to = mapTypeName.indexOf(",", idx); + if (to > idx) { + mapTypeName = mapTypeName.substring(0, idx + 1) + "String" + mapTypeName.substring(to); // All elements except for the last one have the string type. + } } } return mapTypeName; @@ -297,16 +335,13 @@ String elementBase = mapVar; for (Type elmType: elementsTypes.subList(0, elementsTypes.size() - 1)) { elementBase += ".entrySet().iterator().next()"; - if (elmType == DataConstraintModel.typeBoolean) { - decoded = decoded.replace("$x", "new AbstractMap.SimpleEntry<>(Boolean.parseBoolean(" + elementBase + ".getKey()), $x)"); - } else if (elmType == DataConstraintModel.typeInt) { - decoded = decoded.replace("$x", "new AbstractMap.SimpleEntry<>(Integer.parseInt(" + elementBase + ".getKey()), $x)"); - } else if (elmType == DataConstraintModel.typeLong) { - decoded = decoded.replace("$x", "new AbstractMap.SimpleEntry<>(Long.parseLong(" + elementBase + ".getKey()), $x)"); - } else if (elmType == DataConstraintModel.typeFloat) { - decoded = decoded.replace("$x", "new AbstractMap.SimpleEntry<>(Float.parseFloat(" + elementBase + ".getKey()), $x)"); - } else if (elmType == DataConstraintModel.typeDouble) { - decoded = decoded.replace("$x", "new AbstractMap.SimpleEntry<>(Double.parseDouble(" + elementBase + ".getKey()), $x)"); + if (elmType == DataConstraintModel.typeBoolean + || elmType == DataConstraintModel.typeInt + || elmType == DataConstraintModel.typeLong + || elmType == DataConstraintModel.typeFloat + || elmType == DataConstraintModel.typeDouble) { + String elmVal = CodeUtil.getToValueExp(elmType.getImplementationTypeName(), elementBase + ".getKey()"); + decoded = decoded.replace("$x", "new AbstractMap.SimpleEntry<>(" + elmVal + ", $x)"); } else if (elmType == DataConstraintModel.typeString) { decoded = decoded.replace("$x", "new AbstractMap.SimpleEntry<>(" + elementBase + ".getKey(), $x)"); } else { @@ -318,18 +353,25 @@ return decoded; } - private static String getHttpMethodParamsStatement(String callerResourceName, Type paramType, String paramName) { + private static String getCodeForConversionFromMapToPair(Type pairType, String mapVar) { + String decoded = "$x"; + decoded = decoded.replace("$x", "new Pair<>(" + mapVar + ".get(\"left\"), $x)"); + decoded = decoded.replace("$x", mapVar + ".get(\"right\")"); + return decoded; + } + + private static String getHttpMethodParamsStatement(String callerResourceName, Type paramType, String paramName, String value) { if (DataConstraintModel.typeList.isAncestorOf(paramType)) { Type compType = TypeInference.getListComponentType(paramType); String statements = "Form form = new Form();\n"; String wrapperType = DataConstraintModel.getWrapperType(compType); if (wrapperType == null) { - statements += "for (" + compType.getInterfaceTypeName() + " i: " + paramName + ") {\n"; + statements += "for (" + compType.getInterfaceTypeName() + " i: " + value + ") {\n"; } else { - statements += "for (" + wrapperType + " i: " + paramName + ") {\n"; + statements += "for (" + wrapperType + " i: " + value + ") {\n"; } - if (DataConstraintModel.typeTuple.isAncestorOf(compType) || DataConstraintModel.typeList.isAncestorOf(compType)) { - statements += "\tform.param(\"" + paramName + "\", new ObjectMapper().writeValueAsString(i));\n"; + if (DataConstraintModel.typeTuple.isAncestorOf(compType) || DataConstraintModel.typePair.isAncestorOf(paramType) || DataConstraintModel.typeList.isAncestorOf(compType)) { + statements += "\tform.param(\"" + paramName + "\", new ObjectMapper().writeValueAsString(i));\n"; // typeTuple: {"1.0":2.0}, typePair: {"left": 1.0, "right":2.0} } else { statements += "\tform.param(\"" + paramName + "\", i.toString());\n"; } @@ -337,8 +379,12 @@ statements += "Entity
entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);"; return statements; // return "Entity entity = Entity.entity(" + paramName + ".toString(), MediaType.APPLICATION_JSON);"; + } else if (DataConstraintModel.typeTuple.isAncestorOf(paramType) || DataConstraintModel.typePair.isAncestorOf(paramType)) { + // typeTuple: {"1.0":2.0}, typePair: {"left": 1.0, "right":2.0} + return "Entity entity = Entity.entity(new Form().param(\"" + paramName + "\", new ObjectMapper().writeValueAsString(" + value + ")), MediaType.APPLICATION_FORM_URLENCODED_TYPE);"; + } else { + return "Entity entity = Entity.entity(new Form().param(\"" + paramName + "\", " + CodeUtil.getToStringExp(paramType.getImplementationTypeName(), value) + "), MediaType.APPLICATION_FORM_URLENCODED_TYPE);"; } - return "Entity entity = Entity.entity(new Form().param(\"" + paramName + "\", " + CodeUtil.getToStringExp(paramType.getImplementationTypeName(), paramName) + "), MediaType.APPLICATION_FORM_URLENCODED_TYPE);"; } private static String getHttpMethodCallStatement(String baseURL, String resourceName, String srcResName, String httpMethod) { @@ -390,8 +436,7 @@ // I/O channel for (ChannelMember out: ch.getOutputChannelMembers()) { if (out.getIdentifierTemplate().equals(resource.getIdentifierTemplate())) { - if (out.getStateTransition().getMessageExpression() instanceof Term) { - // not an identity element + if (out.getStateTransition().getMessageExpression() instanceof Term || out.getStateTransition().getMessageExpression() instanceof Variable) { channel = ch; channelMembers.add(out); } @@ -408,18 +453,26 @@ // I/O channel for (ChannelMember out: channel.getOutputChannelMembers()) { if (out.getIdentifierTemplate().equals(resource.getIdentifierTemplate())) { - if (out.getStateTransition().getMessageExpression() instanceof Term) { - // not an identity element - Term message = (Term) out.getStateTransition().getMessageExpression(); - MethodDeclaration input = getMethod(type, message.getSymbol().getName()); - return input; - } + MethodDeclaration input = getInputMethod(type, out); + if (input != null) return input; } } } return null; } + private static MethodDeclaration getInputMethod(TypeDeclaration type, ChannelMember out) { + MethodDeclaration input = null; + if (out.getStateTransition().getMessageExpression() instanceof Term) { + Term message = (Term) out.getStateTransition().getMessageExpression(); + input = getMethod(type, message.getSymbol().getImplName()); + } else if (out.getStateTransition().getMessageExpression() instanceof Variable) { + Variable message = (Variable) out.getStateTransition().getMessageExpression(); + input = getMethod(type, message.getName()); + } + return input; + } + private static MethodDeclaration getMethod(TypeDeclaration type, String methodName) { for (MethodDeclaration m: type.getMethods()) { if (m.getName().equals(methodName)) return m; diff --git a/AlgebraicDataflowArchitectureModel/src/algorithms/TypeInference.java b/AlgebraicDataflowArchitectureModel/src/algorithms/TypeInference.java index 73f9ef3..5c1ebbf 100644 --- a/AlgebraicDataflowArchitectureModel/src/algorithms/TypeInference.java +++ b/AlgebraicDataflowArchitectureModel/src/algorithms/TypeInference.java @@ -517,6 +517,82 @@ updateExps.put(System.identityHashCode(arg), arg); } tuple.put(System.identityHashCode(tupleExps), argType); + } else if (symbol.equals(DataConstraintModel.left)) { + // If the root symbol of the term is left. + List pairExps = new ArrayList<>(); + Expression arg = t.getChildren().get(0); + pairExps.add(arg); + expToPair.put(System.identityHashCode(arg), pairExps); + pairExps.add(t); + expToPair.put(System.identityHashCode(t), pairExps); + pairExps.add(null); + Type argType = null; + if (arg instanceof Variable) { + argType = ((Variable) arg).getType(); + } else if (arg instanceof Term) { + argType = ((Term) arg).getType(); + } + Type newPairType = DataConstraintModel.typePair; + if (argType == DataConstraintModel.typePair && t.getType() != null) { + List compTypeList = new ArrayList<>(); + compTypeList.add(t.getType()); + compTypeList.add(null); + newPairType = pairTypes.get(compTypeList); + if (newPairType == null) { + // Create new tuple type; + newPairType = createNewTupleType(compTypeList, DataConstraintModel.typePair); + } + } + if (argType != newPairType && newPairType != null) { + if (arg instanceof Variable) { + ((Variable) arg).setType(newPairType); + argType = newPairType; + } else if (arg instanceof Term) { + ((Term) arg).setType(newPairType); + argType = newPairType; + } + Map updateExps = getUpdateSet(updateFromPair, pairExps); + updateExps.put(System.identityHashCode(arg), arg); + } + pair.put(System.identityHashCode(pairExps), argType); + } else if (symbol.equals(DataConstraintModel.right)) { + // If the root symbol of the term is right. + List pairExps = new ArrayList<>(); + Expression arg = t.getChildren().get(0); + pairExps.add(arg); + expToPair.put(System.identityHashCode(arg), pairExps); + pairExps.add(null); + pairExps.add(t); + expToPair.put(System.identityHashCode(t), pairExps); + Type argType = null; + if (arg instanceof Variable) { + argType = ((Variable) arg).getType(); + } else if (arg instanceof Term) { + argType = ((Term) arg).getType(); + } + Type newPairType = DataConstraintModel.typePair; + if (argType == DataConstraintModel.typePair && t.getType() != null) { + List compTypeList = new ArrayList<>(); + compTypeList.add(null); + compTypeList.add(t.getType()); + newPairType = pairTypes.get(compTypeList); + if (newPairType == null) { + // Create new tuple type; + newPairType = createNewTupleType(compTypeList, DataConstraintModel.typePair); + } + } + if (argType != newPairType && newPairType != null) { + if (arg instanceof Variable) { + ((Variable) arg).setType(newPairType); + argType = newPairType; + } else if (arg instanceof Term) { + ((Term) arg).setType(newPairType); + argType = newPairType; + } + Map updateExps = getUpdateSet(updateFromPair, pairExps); + updateExps.put(System.identityHashCode(arg), arg); + } + pair.put(System.identityHashCode(pairExps), argType); } else if (symbol.equals(DataConstraintModel.cond)) { // If the root symbol of the term is if function. Expression c1 = t.getChild(1); diff --git a/AlgebraicDataflowArchitectureModel/src/base/Base.java b/AlgebraicDataflowArchitectureModel/src/base/Base.java deleted file mode 100644 index 4669340..0000000 --- a/AlgebraicDataflowArchitectureModel/src/base/Base.java +++ /dev/null @@ -1,25 +0,0 @@ -package base; - -import java.util.*; - -public class Base { - private R3 r3 = new R3(); - private R1 r1 = new R1(r3); - private R2 r2 = new R2(r3); - public void set1(int r1) { - this.r1.set1(r1); - } - public void set2(int r2) { - this.r2.set2(r2); - } - public int getR2() { - return r2.getR2(); - } - public int getR3() { - return r3.getR3(); - } - public int getR1() { - return r1.getR1(); - } -} - diff --git a/AlgebraicDataflowArchitectureModel/src/base/R1.java b/AlgebraicDataflowArchitectureModel/src/base/R1.java deleted file mode 100644 index e453b81..0000000 --- a/AlgebraicDataflowArchitectureModel/src/base/R1.java +++ /dev/null @@ -1,16 +0,0 @@ -package base; - -public class R1 { - private R3 r3; - private int r1; - public R1(R3 r3) { - this.r3 = r3; - } - public void set1(int r1) { - this.r1 = r1; - r3.updateR1(r1); - } - public int getR1() { - return r1; - } -} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/src/base/R2.java b/AlgebraicDataflowArchitectureModel/src/base/R2.java deleted file mode 100644 index abb7dd2..0000000 --- a/AlgebraicDataflowArchitectureModel/src/base/R2.java +++ /dev/null @@ -1,16 +0,0 @@ -package base; - -public class R2 { - private R3 r3; - private int r2; - public R2(R3 r3) { - this.r3 = r3; - } - public void set2(int r2) { - this.r2 = r2; - r3.updateR2(r2); - } - public int getR2() { - return r2; - } -} diff --git a/AlgebraicDataflowArchitectureModel/src/base/R3.java b/AlgebraicDataflowArchitectureModel/src/base/R3.java deleted file mode 100644 index 20923bc..0000000 --- a/AlgebraicDataflowArchitectureModel/src/base/R3.java +++ /dev/null @@ -1,20 +0,0 @@ -package base; - -import java.util.*; - -public class R3 { - private int r1; - private int r2; - private int r3; - public void updateR1(int r1) { - this.r1 = r1; - //r3 = ((((x1+r1)+x2)+r2)+this.r3); - } - public void updateR2(int r2) { - this.r2 = r2; - //r3 = ((((x1+r1)+x2)+r2)+this.r3); - } - public int getR3() { - return r3; - } -} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/ConstructorDeclaration.java b/AlgebraicDataflowArchitectureModel/src/code/ast/ConstructorDeclaration.java deleted file mode 100644 index 70bdc90..0000000 --- a/AlgebraicDataflowArchitectureModel/src/code/ast/ConstructorDeclaration.java +++ /dev/null @@ -1,21 +0,0 @@ -package code.ast; - -import models.algebra.Type; - -public class ConstructorDeclaration extends FieldDeclaration { - private VariableDeclaration constructor; - - public ConstructorDeclaration(Type type, String fieldName,VariableDeclaration constructor) { - super(type, fieldName); - this.setConstructor(constructor); - } - - public VariableDeclaration getConstructor() { - return constructor; - } - - public void setConstructor(VariableDeclaration constructor) { - this.constructor = constructor; - } - -} diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/TypeDeclaration.java b/AlgebraicDataflowArchitectureModel/src/code/ast/TypeDeclaration.java index 0d97782..1dc5cf3 100644 --- a/AlgebraicDataflowArchitectureModel/src/code/ast/TypeDeclaration.java +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/TypeDeclaration.java @@ -9,7 +9,6 @@ public class TypeDeclaration extends AbstractTypeDeclaration implements IAnnotatable { private List fields = new ArrayList<>(); private List methods = new ArrayList<>(); - private List constructors = new ArrayList<>(); private Map annotations = new HashMap<>(); public TypeDeclaration(String typeName) { @@ -27,13 +26,6 @@ this.methods = methods; } - public TypeDeclaration(String typeName, List fields, List methods,List constructors) { - this.typeName = typeName; - this.fields = fields; - this.methods = methods; - this.constructors = constructors; - } - public void addField(FieldDeclaration field) { fields.add(field); } @@ -42,10 +34,6 @@ methods.add(method); } - public void addConstructors(VariableDeclaration constructor) { - this.constructors.add(constructor); - } - public List getFields() { return fields; } @@ -54,10 +42,6 @@ return methods; } - public List getConstructors() { - return constructors; - } - @Override public Annotation getAnnotation(String name) { return annotations.get(name); diff --git a/AlgebraicDataflowArchitectureModel/src/experiment/PullExpriment.java b/AlgebraicDataflowArchitectureModel/src/experiment/PullExpriment.java deleted file mode 100644 index 85edb93..0000000 --- a/AlgebraicDataflowArchitectureModel/src/experiment/PullExpriment.java +++ /dev/null @@ -1,53 +0,0 @@ -package experiment; - -import experiment.pull.Main; - -public class PullExpriment { - public static void main(String[] args) { - Main main = new Main(); - Long times = (long) 0; - int num = 10000; - int kirisute = 100; - - for (int i = 0; i < num; i++) { - long startTime = System.nanoTime(); - main.purchase(100); - long endTime = System.nanoTime(); - main.getHistory().clear(); - if(i > kirisute) times += endTime - startTime; - } - System.out.println("purchase:" + (double) times / (num - kirisute)); - times = (long) 0; - for (int i = 0; i < num; i++) { - long startTime = System.nanoTime(); - main.getHistory(); - long endTime = System.nanoTime(); - if(i > kirisute) times += endTime - startTime; - } - System.out.println("getHistory:" + (double) times / (num - kirisute)); - times = (long) 0; - for (int i = 0; i < num; i++) { - long startTime = System.nanoTime(); - main.getPayment(); - long endTime = System.nanoTime(); - if(i > kirisute) times += endTime - startTime; - } - System.out.println("getPayment:" + (double) times / (num - kirisute)); - times = (long) 0; - for (int i = 0; i < num; i++) { - long startTime = System.nanoTime(); - main.getPoints(); - long endTime = System.nanoTime(); - if(i > kirisute) times += endTime - startTime; - } - System.out.println("getPoints:" + (double) times / (num - kirisute)); - times = (long) 0; - for (int i = 0; i < num; i++) { - long startTime = System.nanoTime(); - main.getTotal(); - long endTime = System.nanoTime(); - if(i > kirisute) times += endTime - startTime; - } - System.out.println("getTotal:" + (double) times / (num - kirisute)); - } -} diff --git a/AlgebraicDataflowArchitectureModel/src/experiment/PushExperiment.java b/AlgebraicDataflowArchitectureModel/src/experiment/PushExperiment.java deleted file mode 100644 index 5eee28f..0000000 --- a/AlgebraicDataflowArchitectureModel/src/experiment/PushExperiment.java +++ /dev/null @@ -1,53 +0,0 @@ -package experiment; - -import experiment.push.Main; - -public class PushExperiment { - public static void main(String[] args) { - Main main = new Main(); - Long times = (long) 0; - int num = 10000; - int kirisute = 100; - - for (int i = 0; i < num; i++) { - long startTime = System.nanoTime(); - main.purchase(100); - long endTime = System.nanoTime(); - main.getHistory().clear(); - if(i > kirisute) times += endTime - startTime; - } - System.out.println("purchase:" + (double) times / (num - kirisute)); - times = (long) 0; - for (int i = 0; i < num; i++) { - long startTime = System.nanoTime(); - main.getHistory(); - long endTime = System.nanoTime(); - if(i > kirisute) times += endTime - startTime; - } - System.out.println("getHistory:" + (double) times / (num - kirisute)); - times = (long) 0; - for (int i = 0; i < num; i++) { - long startTime = System.nanoTime(); - main.getPayment(); - long endTime = System.nanoTime(); - if(i > kirisute) times += endTime - startTime; - } - System.out.println("getPayment:" + (double) times / (num - kirisute)); - times = (long) 0; - for (int i = 0; i < num; i++) { - long startTime = System.nanoTime(); - main.getPoints(); - long endTime = System.nanoTime(); - if(i > kirisute) times += endTime - startTime; - } - System.out.println("getPoints:" + (double) times / (num - kirisute)); - times = (long) 0; - for (int i = 0; i < num; i++) { - long startTime = System.nanoTime(); - main.getTotal(); - long endTime = System.nanoTime(); - if(i > kirisute) times += endTime - startTime; - } - System.out.println("getTotal:" + (double) times / (num - kirisute)); - } -} diff --git a/AlgebraicDataflowArchitectureModel/src/experiment/pull/History.java b/AlgebraicDataflowArchitectureModel/src/experiment/pull/History.java deleted file mode 100644 index 81dce56..0000000 --- a/AlgebraicDataflowArchitectureModel/src/experiment/pull/History.java +++ /dev/null @@ -1,13 +0,0 @@ -package experiment.pull; - -import java.util.ArrayList; - -public class History { - private ArrayList history = new ArrayList(); - public void updatePayment(int payment) { - history.add(payment); - } - public ArrayList getHistory() { - return history; - } -} diff --git a/AlgebraicDataflowArchitectureModel/src/experiment/pull/Main.java b/AlgebraicDataflowArchitectureModel/src/experiment/pull/Main.java deleted file mode 100644 index 5aa2c3f..0000000 --- a/AlgebraicDataflowArchitectureModel/src/experiment/pull/Main.java +++ /dev/null @@ -1,25 +0,0 @@ -package experiment.pull; - -import java.util.ArrayList; - -public class Main { - private History history = new History(); - private Total total = new Total(history); - private Payment payment = new Payment(history); - private Points points = new Points(payment); - public void purchase(int payment) { - this.payment.purchase(payment); - } - public int getTotal() { - return total.getTotal(); - } - public int getPayment() { - return payment.getPayment(); - } - public ArrayList getHistory() { - return history.getHistory(); - } - public int getPoints() { - return points.getPoints(); - } -} diff --git a/AlgebraicDataflowArchitectureModel/src/experiment/pull/Payment.java b/AlgebraicDataflowArchitectureModel/src/experiment/pull/Payment.java deleted file mode 100644 index 0307cef..0000000 --- a/AlgebraicDataflowArchitectureModel/src/experiment/pull/Payment.java +++ /dev/null @@ -1,16 +0,0 @@ -package experiment.pull; - -public class Payment { - private History history; - private int payment; - public Payment(History history) { - this.history = history; - } - public void purchase(int payment) { - this.payment = payment; - history.updatePayment(payment); - } - public int getPayment() { - return payment; - } -} diff --git a/AlgebraicDataflowArchitectureModel/src/experiment/pull/Points.java b/AlgebraicDataflowArchitectureModel/src/experiment/pull/Points.java deleted file mode 100644 index 2eaaf56..0000000 --- a/AlgebraicDataflowArchitectureModel/src/experiment/pull/Points.java +++ /dev/null @@ -1,11 +0,0 @@ -package experiment.pull; - -public class Points { - private Payment payment; - public Points(Payment payment) { - this.payment = payment; - } - public int getPoints() { - return (int)Math.floor((payment.getPayment()*0.05)); - } -} diff --git a/AlgebraicDataflowArchitectureModel/src/experiment/pull/Total.java b/AlgebraicDataflowArchitectureModel/src/experiment/pull/Total.java deleted file mode 100644 index 384af94..0000000 --- a/AlgebraicDataflowArchitectureModel/src/experiment/pull/Total.java +++ /dev/null @@ -1,11 +0,0 @@ -package experiment.pull; - -public class Total { - private History history; - public Total(History history) { - this.history = history; - } - public int getTotal() { - return history.getHistory().stream().mapToInt(x->x).sum(); - } -} diff --git a/AlgebraicDataflowArchitectureModel/src/experiment/push/History.java b/AlgebraicDataflowArchitectureModel/src/experiment/push/History.java deleted file mode 100644 index 58d4fa3..0000000 --- a/AlgebraicDataflowArchitectureModel/src/experiment/push/History.java +++ /dev/null @@ -1,18 +0,0 @@ -package experiment.push; - -import java.util.ArrayList; - -public class History { - private Total total; - private ArrayList history = new ArrayList(); - public void updatePayment(int payment) { - history.add(payment); - total.updateHistory(history); - } - public History(Total total) { - this.total = total; - } - public ArrayList getHistory() { - return history; - } -} diff --git a/AlgebraicDataflowArchitectureModel/src/experiment/push/Main.java b/AlgebraicDataflowArchitectureModel/src/experiment/push/Main.java deleted file mode 100644 index 9edf4f4..0000000 --- a/AlgebraicDataflowArchitectureModel/src/experiment/push/Main.java +++ /dev/null @@ -1,25 +0,0 @@ -package experiment.push; - -import java.util.ArrayList; - -public class Main { - private Total total = new Total(); - private Points points = new Points(); - private History history = new History(total); - private Payment payment = new Payment(points,history); - public void purchase(int payment) { - this.payment.purchase(payment); - } - public int getTotal() { - return total.getTotal(); - } - public int getPayment() { - return payment.getPayment(); - } - public ArrayList getHistory() { - return history.getHistory(); - } - public int getPoints() { - return points.getPoints(); - } -} diff --git a/AlgebraicDataflowArchitectureModel/src/experiment/push/Payment.java b/AlgebraicDataflowArchitectureModel/src/experiment/push/Payment.java deleted file mode 100644 index 47e6ca3..0000000 --- a/AlgebraicDataflowArchitectureModel/src/experiment/push/Payment.java +++ /dev/null @@ -1,19 +0,0 @@ -package experiment.push; - -public class Payment { - private Points points; - private History history; - private int payment; - public Payment(Points points, History history) { - this.points = points; - this.history = history; - } - public void purchase(int payment) { - this.payment = payment; - points.updatePayment(payment); - history.updatePayment(payment); - } - public int getPayment() { - return payment; - } -} diff --git a/AlgebraicDataflowArchitectureModel/src/experiment/push/Points.java b/AlgebraicDataflowArchitectureModel/src/experiment/push/Points.java deleted file mode 100644 index 41a59c8..0000000 --- a/AlgebraicDataflowArchitectureModel/src/experiment/push/Points.java +++ /dev/null @@ -1,11 +0,0 @@ -package experiment.push; - -public class Points { - private int points; - public void updatePayment(int payment) { - points = (int)Math.floor((payment*0.05)); - } - public int getPoints() { - return points; - } -} diff --git a/AlgebraicDataflowArchitectureModel/src/experiment/push/Total.java b/AlgebraicDataflowArchitectureModel/src/experiment/push/Total.java deleted file mode 100644 index 25b5e24..0000000 --- a/AlgebraicDataflowArchitectureModel/src/experiment/push/Total.java +++ /dev/null @@ -1,13 +0,0 @@ -package experiment.push; - -import java.util.ArrayList; - -public class Total { - private int total; - public void updateHistory(ArrayList history) { - total = history.stream().mapToInt(x->x).sum(); - } - public int getTotal() { - return total; - } -} diff --git a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java index 9939c59..9d68816 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java +++ b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java @@ -58,11 +58,11 @@ @Override public String generate(Type type, String[] children, String[] sideEffects) { // TODO Auto-generated method stub - String temp = "temp_l" + count[0]; + String temp = "temp_if" + count[0]; String impl = ""; impl += type.getInterfaceTypeName() + " " + temp + ";\n"; - impl += "if(" + children[0] + ") {\n"; + impl += "if (" + children[0] + ") {\n"; impl +="\t" + temp + " = " + children[1] + ";\n"; impl += "} else {\n"; impl += "\t" + temp + " = " + children[2] + ";\n"; @@ -105,6 +105,8 @@ }); public static final Symbol fst = new Symbol("fst", 1, Symbol.Type.PREFIX, "getKey", Symbol.Type.METHOD); 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); static { add.setInverses(new Symbol[] {sub, sub}); @@ -129,7 +131,9 @@ true_.setSignature(new Type[] {typeBoolean}); false_.setSignature(new Type[] {typeBoolean}); pair.setSignature(new Type[] {typePair,null,null}); - pair.setInverses(new Symbol[] {fst, snd}); + pair.setInverses(new Symbol[] {left, right}); + left.setSignature(new Type[] {null, typePair}); + right.setSignature(new Type[] {null, typePair}); tuple.setSignature(new Type[] {typeTuple, null, null}); tuple.setInverses(new Symbol[] {fst, snd}); fst.setSignature(new Type[] {null, typeTuple}); @@ -176,6 +180,8 @@ addSymbol(true_); addSymbol(false_); addSymbol(pair); + addSymbol(left); + addSymbol(right); addSymbol(tuple); addSymbol(fst); addSymbol(snd); diff --git a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/StateTransition.java b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/StateTransition.java index e3493b5..011045a 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/StateTransition.java +++ b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/StateTransition.java @@ -157,13 +157,15 @@ Expression messageTerm = getMessageExpression(); HashMap messageVars = messageTerm.getVariables(); - for (Entry messageVarEnt: messageVars.entrySet()) { - Variable var = messageVarEnt.getValue(); - Position varPos = messageVarEnt.getKey(); - Expression valueCalc = concreteMessage.getSubTerm(varPos); - if (valueCalc != null) { - if (bindings.get(var) != null) throw new ResolvingMultipleDefinitionIsFutureWork(); - bindings.put(var, valueCalc); + if (concreteMessage != null) { + for (Entry messageVarEnt: messageVars.entrySet()) { + Variable var = messageVarEnt.getValue(); + Position varPos = messageVarEnt.getKey(); + Expression valueCalc = concreteMessage.getSubTerm(varPos); + if (valueCalc != null) { + if (bindings.get(var) != null) throw new ResolvingMultipleDefinitionIsFutureWork(); + bindings.put(var, valueCalc); + } } } diff --git a/AlgebraicDataflowArchitectureModel/src/models/dataFlowModel/DataflowChannelGenerator.java b/AlgebraicDataflowArchitectureModel/src/models/dataFlowModel/DataflowChannelGenerator.java index 2839622..40dd36d 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/dataFlowModel/DataflowChannelGenerator.java +++ b/AlgebraicDataflowArchitectureModel/src/models/dataFlowModel/DataflowChannelGenerator.java @@ -219,7 +219,9 @@ Expression curOutputStateAccessor = stateAccessor.getCurrentStateAccessorFor(targetIdentifier, targetIdentifier); if (unifiedMessage == null) { // for IOChannel - unifiedMessage = (Term) targetMember.getStateTransition().getMessageExpression(); + if (targetMember.getStateTransition().getMessageExpression() instanceof Term) { + unifiedMessage = (Term) targetMember.getStateTransition().getMessageExpression(); + } } return targetMember.getStateTransition().deriveNextStateExpressionFor(curOutputStateAccessor, unifiedMessage); }