diff --git a/AlgebraicDataflowArchitectureModel/src/generators/JavaCodeGenerator.java b/AlgebraicDataflowArchitectureModel/src/generators/JavaCodeGenerator.java index 32b9289..a15057e 100644 --- a/AlgebraicDataflowArchitectureModel/src/generators/JavaCodeGenerator.java +++ b/AlgebraicDataflowArchitectureModel/src/generators/JavaCodeGenerator.java @@ -1088,11 +1088,7 @@ if (res.getInitialValue() != null) { initializer = res.getInitialValue().toImplementation(new String[] {""}); } else if (stateType != null) { - if (DataConstraintModel.typeList.isAncestorOf(stateType)) { - initializer = "new " + res.getResourceStateType().getImplementationTypeName() + "()"; - } else if (DataConstraintModel.typeMap.isAncestorOf(stateType)) { - initializer = "new " + res.getResourceStateType().getImplementationTypeName() + "()"; - } + initializer = DataConstraintModel.getDefaultValue(stateType); } return initializer; } diff --git a/AlgebraicDataflowArchitectureModel/src/generators/JavaSpecific.java b/AlgebraicDataflowArchitectureModel/src/generators/JavaSpecific.java index 72d79a9..6134a5f 100644 --- a/AlgebraicDataflowArchitectureModel/src/generators/JavaSpecific.java +++ b/AlgebraicDataflowArchitectureModel/src/generators/JavaSpecific.java @@ -106,11 +106,7 @@ if (initialValue != null) { initializer = initialValue.toImplementation(new String[] {""}); } else if (type != null) { - if (DataConstraintModel.typeList.isAncestorOf(type)) { - initializer = "new " + type.getImplementationTypeName() + "()"; - } else if (DataConstraintModel.typeMap.isAncestorOf(type)) { - initializer = "new " + type.getImplementationTypeName() + "()"; - } + initializer = DataConstraintModel.getDefaultValue(type); } return initializer; } diff --git a/AlgebraicDataflowArchitectureModel/src/generators/JerseyCodeGenerator.java b/AlgebraicDataflowArchitectureModel/src/generators/JerseyCodeGenerator.java index 151ab6e..2d1c1c2 100644 --- a/AlgebraicDataflowArchitectureModel/src/generators/JerseyCodeGenerator.java +++ b/AlgebraicDataflowArchitectureModel/src/generators/JerseyCodeGenerator.java @@ -1165,12 +1165,8 @@ String initializer = null; if (res.getInitialValue() != null) { initializer = res.getInitialValue().toImplementation(new String[] {""}); - } else { - if (DataConstraintModel.typeList.isAncestorOf(stateType)) { - initializer = "new " + res.getResourceStateType().getImplementationTypeName() + "()"; - } else if (DataConstraintModel.typeMap.isAncestorOf(stateType)) { - initializer = "new " + res.getResourceStateType().getImplementationTypeName() + "()"; - } + } else if (stateType != null) { + initializer = DataConstraintModel.getDefaultValue(stateType); } return initializer; } diff --git a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java index 3a3de81..e44c50c 100644 --- a/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java +++ b/AlgebraicDataflowArchitectureModel/src/models/dataConstraintModel/DataConstraintModel.java @@ -884,9 +884,28 @@ public static final Symbol addMember = new Symbol("addMember", 3, Symbol.Type.PREFIX, "put", Symbol.Type.METHOD_WITH_SIDE_EFFECT); public static final Symbol dot = new Symbol(Parser.DOT, 2, Symbol.Type.INFIX, "get", Symbol.Type.METHOD); public static final Symbol dotParam = new Symbol(Parser.DOT, 2, Symbol.Type.INFIX, "get", Symbol.Type.METHOD); - public static final Symbol pi = new Symbol("PI", 0, Symbol.Type.PREFIX, "Math.PI", Symbol.Type.PREFIX); - public static final Symbol E = new Symbol("E", 0, Symbol.Type.PREFIX, "Math.E", Symbol.Type.PREFIX); - public static final Symbol sqrt = new Symbol("sqrt", 1, Symbol.Type.PREFIX, "Math.sqrt", Symbol.Type.PREFIX); + public static final Symbol pi = new Symbol("PI", 0, Symbol.Type.PREFIX, "Math.PI", Symbol.Type.PREFIX, new Symbol.ICalculator() { + @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); + } 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 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); @@ -1206,6 +1225,29 @@ return "new " + type.getImplementationTypeName() + "()"; } + public static Expression getDefaultValueExpression(Type type) { + if (type == typeInt) { + return new Constant("0", typeInt); + } else if (type == typeLong) { + return new Constant("0L", typeLong); + } else if (type == typeFloat) { + return new Constant("0.0f", typeFloat); + } else if (type == typeDouble) { + return new Constant("0.0", typeDouble); + } else if (type == typeBoolean) { + return new Constant(false_); + } else if (type == typeString) { + return new Constant("", typeString); + } else if (type.isAncestorOf(typeList)) { + return new ListTerm(); + } else if (type.isAncestorOf(typeMap)) { + return new MapTerm(); + } else if (type.isAncestorOf(typeJson)) { + return new JsonTerm(); + } + return null; + } + @Override public String toString() { String out = ""; diff --git a/AlgebraicDataflowArchitectureModel/src/simulator/Simulator.java b/AlgebraicDataflowArchitectureModel/src/simulator/Simulator.java index d1a5359..a2df91d 100644 --- a/AlgebraicDataflowArchitectureModel/src/simulator/Simulator.java +++ b/AlgebraicDataflowArchitectureModel/src/simulator/Simulator.java @@ -18,6 +18,7 @@ import models.algebra.Variable; import models.dataConstraintModel.Channel; import models.dataConstraintModel.ChannelMember; +import models.dataConstraintModel.DataConstraintModel; import models.dataConstraintModel.ResourceHierarchy; import models.dataConstraintModel.ResourcePath; import models.dataConstraintModel.Selector; @@ -59,6 +60,11 @@ initialValue = ((Term) initialValue).reduce(); } curState.updateResourceState(resource.getResourceIdentifier(), null, null, initialValue); + } else if (res.getResourceStateType() != null) { + initialValue = DataConstraintModel.getDefaultValueExpression(res.getResourceStateType()); + if (initialValue != null) { + curState.updateResourceState(resource.getResourceIdentifier(), null, null, initialValue); + } } } } @@ -195,7 +201,7 @@ Expression nextResState = null; outTarget[0] = out; outResVar[0] = new Variable(channel.getChannelName() + "$" + out.getResource().toString() + "$this"); // A special variable to represent the current state of each output resource. - if (!event.isInput()) { + if (!event.isInput() || event.getMessage() instanceof Variable) { nextResState = channel.deriveUpdateExpressionOf(out, resouceStateAccessor).getKey(); } else { nextResState = channel.deriveUpdateExpressionOf(out, (Term) event.getMessage(), resouceStateAccessor);