diff --git a/AlgebraicDataflowArchitectureModel/src/algorithm/CodeGenerator.java b/AlgebraicDataflowArchitectureModel/src/algorithm/CodeGenerator.java index 925110c..a3077a6 100644 --- a/AlgebraicDataflowArchitectureModel/src/algorithm/CodeGenerator.java +++ b/AlgebraicDataflowArchitectureModel/src/algorithm/CodeGenerator.java @@ -3,12 +3,53 @@ import java.util.ArrayList; import models.*; +import models.algebra.Expression; +import models.algebra.Field; +import models.algebra.InvalidMessage; +import models.algebra.Parameter; +import models.algebra.ParameterizedIdentifierIsFutureWork; +import models.algebra.Symbol; +import models.algebra.Term; +import models.algebra.UnificationFailed; +import models.algebra.ValueUndefined; +import models.dataConstraintModel.ChannelMember; import models.dataConstraintModel.IdentifierTemplate; import models.dataFlowModel.*; +import models.dataFlowModel.DataflowChannelGenerator.IResourceStateAccessor; public class CodeGenerator { static public ArrayList doGenerate(ResourceDependencyGraph graph) { ArrayList codes = new ArrayList<>(); + IResourceStateAccessor pushAccessor = new IResourceStateAccessor() { + @Override + public Expression getCurrentStateAccessorFor(IdentifierTemplate target, IdentifierTemplate from) { + if (target.equals(from)) { + return new Field(target.getResourceName(), target.getResourceStateType()); + } + return null; + } + + @Override + public Expression getNextStateAccessorFor(IdentifierTemplate target, IdentifierTemplate from) { + return new Parameter(target.getResourceName(), target.getResourceStateType()); + } + }; + IResourceStateAccessor pullAccessor = new IResourceStateAccessor() { + @Override + public Expression getCurrentStateAccessorFor(IdentifierTemplate target, IdentifierTemplate from) { + if (target.equals(from)) { + return new Field(target.getResourceName(), target.getResourceStateType()); + } + return null; + } + + @Override + public Expression getNextStateAccessorFor(IdentifierTemplate target, IdentifierTemplate from) { + Term getter = new Term(new Symbol("get" + target.getResourceName(), 1, Symbol.Type.METHOD)); + getter.addChild(new Field(target.getResourceName(), target.getResourceStateType())); + return getter; + } + }; codes.add("public class Main {"); for (Node n : graph.getNodes()) { ResourceNode resource = (ResourceNode) n; @@ -18,24 +59,76 @@ + resource.getIdentifierTemplate().getResourceName() + " = new " + resource.getIdentifierTemplate().getResourceName().substring(0, 1).toUpperCase() + resource.getIdentifierTemplate().getResourceName().substring(1) + "("; - if (((StoreAttribute) resource.getAttribute()).isStored()) { - for (Node pn : resource.getSuccessors()) { - str += ((ResourceNode) pn).getIdentifierTemplate().getResourceName().substring(0, 1).toUpperCase() + ((ResourceNode) pn).getIdentifierTemplate().getResourceName().substring(1) + ","; - } - if(str.substring(str.length() - 1, str.length()) == ",") str = str.substring(0, str.length() - 1); - } + str += ");"; codes.add(str); } codes.add("}"); + codes.add(""); for (Node n : graph.getNodes()) { ResourceNode resource = (ResourceNode) n; - codes.add("public class " + resource.getIdentifierTemplate().getResourceName().substring(0, 1) - .toUpperCase() + resource.getIdentifierTemplate().getResourceName().substring(1) + " {"); - if(((StoreAttribute) resource.getAttribute()).isStored()) { - + String tmp; + codes.add("public class " + resource.getIdentifierTemplate().getResourceName().substring(0, 1).toUpperCase() + + resource.getIdentifierTemplate().getResourceName().substring(1) + " {"); + if (((StoreAttribute) resource.getAttribute()).isStored()) { + for (Edge e : resource.getOutEdges()) { + ResourceDependency re = (ResourceDependency) e; + for(ChannelMember cm:re.getChannelGenerator().getChannelMembers()) { + try { + Expression exp = re.getChannelGenerator().deriveUpdateExpressionOf(cm, pushAccessor); + if(exp != null) { + tmp = "\t" + exp.toImplementation(); + codes.add(tmp); + } + } catch (ParameterizedIdentifierIsFutureWork e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } catch (ResolvingMultipleDefinitionIsFutureWork e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } catch (InvalidMessage e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } catch (UnificationFailed e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } catch (ValueUndefined e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } + } + } else { + for (Edge e : resource.getInEdges()) { + ResourceDependency re = (ResourceDependency) e; + for(ChannelMember cm:re.getChannelGenerator().getChannelMembers()) { + try { + Expression exp = re.getChannelGenerator().deriveUpdateExpressionOf(cm, pullAccessor); + if(exp != null) { + tmp = "\t" + exp.toImplementation(); + codes.add(tmp); + } + } catch (ParameterizedIdentifierIsFutureWork e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } catch (ResolvingMultipleDefinitionIsFutureWork e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } catch (InvalidMessage e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } catch (UnificationFailed e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } catch (ValueUndefined e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } + } } codes.add("}"); + codes.add(""); } return codes; }