diff --git a/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PULL-first/History.java b/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PULL-first/History.java new file mode 100644 index 0000000..2038cd4 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PULL-first/History.java @@ -0,0 +1,25 @@ +import java.util.*; +import javax.ws.rs.*; +import javax.ws.rs.client.*; +import javax.ws.rs.core.*; +import org.springframework.stereotype.Component; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.core.JsonProcessingException; + +@Path("/history") +@Component +public class History { + private List value = new ArrayList<>(); + @Produces(MediaType.APPLICATION_JSON) + @GET + public List getValue() { + return value; + } + @POST + public void updateFromPayment(@FormParam("payment") double payment) { + this.value.add(0, payment); + } + public History(List history) { + this.history = history; + } +} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PULL-first/Payment.java b/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PULL-first/Payment.java new file mode 100644 index 0000000..95332ef --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PULL-first/Payment.java @@ -0,0 +1,30 @@ +import java.util.*; +import javax.ws.rs.*; +import javax.ws.rs.client.*; +import javax.ws.rs.core.*; +import org.springframework.stereotype.Component; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.core.JsonProcessingException; + +@Path("/payment") +@Component +public class Payment { + private double value = 0.0; + private Client client = ClientBuilder.newClient(); + @Produces(MediaType.APPLICATION_JSON) + @GET + public double getValue() { + return value; + } + public Payment(double payment) { + this.payment = payment; + } + @PUT + public void purchase(@FormParam("x") double x) throws JsonProcessingException { + Form form = new Form(); + form.param("payment", Double.toString(this.value)); + Entity
entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED); + String result = client.target("http://localhost:8080").path("/history").request().post(entity, String.class); + this.value = x; + } +} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PULL-first/Points.java b/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PULL-first/Points.java new file mode 100644 index 0000000..c557cf6 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PULL-first/Points.java @@ -0,0 +1,19 @@ +import java.util.*; +import javax.ws.rs.*; +import javax.ws.rs.client.*; +import javax.ws.rs.core.*; +import org.springframework.stereotype.Component; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.core.JsonProcessingException; + +@Path("/points") +@Component +public class Points { + private Client client = ClientBuilder.newClient(); + @Produces(MediaType.APPLICATION_JSON) + @GET + public int getValue() { + double payment = client.target("http://localhost:8080").path("/payment").request().get(double.class); + return (int)Math.floor((payment*0.05)); + } +} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PULL-first/Total.java b/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PULL-first/Total.java new file mode 100644 index 0000000..69d4735 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PULL-first/Total.java @@ -0,0 +1,23 @@ +import java.util.*; +import javax.ws.rs.*; +import javax.ws.rs.client.*; +import javax.ws.rs.core.*; +import org.springframework.stereotype.Component; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.core.JsonProcessingException; + +@Path("/total") +@Component +public class Total { + private Client client = ClientBuilder.newClient(); + @Produces(MediaType.APPLICATION_JSON) + @GET + public int getValue() { + List history = client.target("http://localhost:8080").path("/history").request().get(ArrayList.class); + Integer temp_sum1 = 0; + for (Integer x: history) { + temp_sum1 += x; + } + return temp_sum1; + } +} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PUSH-first/History.java b/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PUSH-first/History.java new file mode 100644 index 0000000..2a571d3 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PUSH-first/History.java @@ -0,0 +1,33 @@ +import java.util.*; +import javax.ws.rs.*; +import javax.ws.rs.client.*; +import javax.ws.rs.core.*; +import org.springframework.stereotype.Component; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.core.JsonProcessingException; + +@Path("/history") +@Component +public class History { + private List value = new ArrayList<>(); + private Client client = ClientBuilder.newClient(); + @Produces(MediaType.APPLICATION_JSON) + @GET + public List getValue() { + return value; + } + @POST + public void updateFromPayment(@FormParam("payment") double payment) throws JsonProcessingException { + this.value.add(0, payment); + + Form form = new Form(); + for (Double i: this.value) { + form.param("history", i.toString()); + } + Entity entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED); + String result = client.target("http://localhost:8080").path("/total").request().put(entity, String.class); + } + public History(List history) { + this.history = history; + } +} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PUSH-first/Payment.java b/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PUSH-first/Payment.java new file mode 100644 index 0000000..18e64ef --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PUSH-first/Payment.java @@ -0,0 +1,34 @@ +import java.util.*; +import javax.ws.rs.*; +import javax.ws.rs.client.*; +import javax.ws.rs.core.*; +import org.springframework.stereotype.Component; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.core.JsonProcessingException; + +@Path("/payment") +@Component +public class Payment { + private double value = 0.0; + private Client client = ClientBuilder.newClient(); + @Produces(MediaType.APPLICATION_JSON) + @GET + public double getValue() { + return value; + } + public Payment(double payment) { + this.payment = payment; + } + @PUT + public void purchase(@FormParam("x") double x) throws JsonProcessingException { + Form form = new Form(); + form.param("payment", Double.toString(this.value)); + Entity entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED); + String result = client.target("http://localhost:8080").path("/points").request().put(entity, String.class); + form = new Form(); + form.param("payment", Double.toString(this.value)); + entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED); + result = client.target("http://localhost:8080").path("/history").request().post(entity, String.class); + this.value = x; + } +} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PUSH-first/Points.java b/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PUSH-first/Points.java new file mode 100644 index 0000000..7dc8d20 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PUSH-first/Points.java @@ -0,0 +1,25 @@ +import java.util.*; +import javax.ws.rs.*; +import javax.ws.rs.client.*; +import javax.ws.rs.core.*; +import org.springframework.stereotype.Component; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.core.JsonProcessingException; + +@Path("/points") +@Component +public class Points { + private int value = 0; + @Produces(MediaType.APPLICATION_JSON) + @GET + public int getValue() { + return value; + } + @PUT + public void updateFromPayment(@FormParam("payment") double payment) { + this.value = (int)Math.floor((payment*0.05)); + } + public Points(int points) { + this.points = points; + } +} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PUSH-first/Total.java b/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PUSH-first/Total.java new file mode 100644 index 0000000..e91f4e7 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/prototypes/JAX-RS/POS/PUSH-first/Total.java @@ -0,0 +1,29 @@ +import java.util.*; +import javax.ws.rs.*; +import javax.ws.rs.client.*; +import javax.ws.rs.core.*; +import org.springframework.stereotype.Component; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.core.JsonProcessingException; + +@Path("/total") +@Component +public class Total { + private int value = 0; + @Produces(MediaType.APPLICATION_JSON) + @GET + public int getValue() { + return value; + } + @PUT + public void updateFromHistory(@FormParam("history") List history) { + Integer temp_sum1 = 0; + for (Integer x: history) { + temp_sum1 += x; + } + this.value = temp_sum1; + } + public Total(int total) { + this.total = total; + } +} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PULL-first/History.java b/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PULL-first/History.java new file mode 100644 index 0000000..75373bd --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PULL-first/History.java @@ -0,0 +1,11 @@ +import java.util.*; + +public class History { + private List value = new ArrayList<>(); + public List getValue() { + return new ArrayList<>(value); + } + public void updateFromPayment(int payment) { + this.value.add(0, payment); + } +} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PULL-first/POS.java b/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PULL-first/POS.java new file mode 100644 index 0000000..55d7d08 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PULL-first/POS.java @@ -0,0 +1,29 @@ +import java.util.*; + +public class POS { + private History history; + private Total total; + private Payment payment; + private Points points; + public POS() { + history = new History(); + total = new Total(history); + payment = new Payment(history); + points = new Points(payment); + } + public List getHistory() { + return this.history.getValue(); + } + public int getTotal() { + return this.total.getValue(); + } + public int getPayment() { + return this.payment.getValue(); + } + public void purchase(int x) { + this.payment.purchase(x); + } + public int getPoints() { + return this.points.getValue(); + } +} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PULL-first/Payment.java b/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PULL-first/Payment.java new file mode 100644 index 0000000..23c1088 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PULL-first/Payment.java @@ -0,0 +1,16 @@ +import java.util.*; + +public class Payment { + private int value = 0; + private History history; + public int getValue() { + return value; + } + public void purchase(int x) { + this.value = x; + this.history.updateFromPayment(value); + } + public Payment(History history) { + this.history = history; + } +} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PULL-first/Points.java b/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PULL-first/Points.java new file mode 100644 index 0000000..2b11644 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PULL-first/Points.java @@ -0,0 +1,11 @@ +import java.util.*; + +public class Points { + private Payment payment; + public int getValue() { + return (int)Math.floor((this.payment.getValue()*0.05)); + } + public Points(Payment payment) { + this.payment = payment; + } +} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PULL-first/Total.java b/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PULL-first/Total.java new file mode 100644 index 0000000..62564ff --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PULL-first/Total.java @@ -0,0 +1,15 @@ +import java.util.*; + +public class Total { + private History history; + public int getValue() { + Integer temp_sum1 = 0; + for (Integer x: this.history.getValue()) { + temp_sum1 += x; + } + return temp_sum1; + } + public Total(History history) { + this.history = history; + } +} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PUSH-first/History.java b/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PUSH-first/History.java new file mode 100644 index 0000000..6aa27b8 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PUSH-first/History.java @@ -0,0 +1,17 @@ +import java.util.*; + +public class History { + private List value = new ArrayList<>(); + private Total total; + public List getValue() { + return new ArrayList<>(value); + } + public void updateFromPayment(double payment) { + this.value.add(0, payment); + + this.total.updateFromHistory(value); + } + public History(Total total) { + this.total = total; + } +} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PUSH-first/POS.java b/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PUSH-first/POS.java new file mode 100644 index 0000000..2d3e544 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PUSH-first/POS.java @@ -0,0 +1,29 @@ +import java.util.*; + +public class POS { + private Points points; + private Total total; + private History history; + private Payment payment; + public POS() { + points = new Points(); + total = new Total(); + history = new History(total); + payment = new Payment(points,history); + } + public int getPoints() { + return this.points.getValue(); + } + public int getTotal() { + return this.total.getValue(); + } + public List getHistory() { + return this.history.getValue(); + } + public double getPayment() { + return this.payment.getValue(); + } + public void purchase(double x) { + this.payment.purchase(x); + } +} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PUSH-first/Payment.java b/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PUSH-first/Payment.java new file mode 100644 index 0000000..ebf56ac --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PUSH-first/Payment.java @@ -0,0 +1,19 @@ +import java.util.*; + +public class Payment { + private double value = 0.0; + private Points points; + private History history; + public double getValue() { + return value; + } + public void purchase(double x) { + this.value = x; + this.points.updateFromPayment(value); + this.history.updateFromPayment(value); + } + public Payment(Points points, History history) { + this.points = points; + this.history = history; + } +} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PUSH-first/Points.java b/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PUSH-first/Points.java new file mode 100644 index 0000000..55cb552 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PUSH-first/Points.java @@ -0,0 +1,11 @@ +import java.util.*; + +public class Points { + private int value = 0; + public int getValue() { + return value; + } + public void updateFromPayment(double payment) { + this.value = (int)Math.floor((payment*0.05)); + } +} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PUSH-first/Total.java b/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PUSH-first/Total.java new file mode 100644 index 0000000..2a3c00e --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/prototypes/Java/POS/PUSH-first/Total.java @@ -0,0 +1,15 @@ +import java.util.*; + +public class Total { + private int value = 0; + public int getValue() { + return value; + } + public void updateFromHistory(List history) { + Integer temp_sum1 = 0; + for (Integer x: history) { + temp_sum1 += x; + } + this.value = temp_sum1; + } +} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/src/tests/CodeGeneratorTest.java b/AlgebraicDataflowArchitectureModel/src/tests/CodeGeneratorTest.java deleted file mode 100644 index 8ee6b46..0000000 --- a/AlgebraicDataflowArchitectureModel/src/tests/CodeGeneratorTest.java +++ /dev/null @@ -1,56 +0,0 @@ -package tests; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.util.ArrayList; - -import algorithms.*; -import code.ast.CompilationUnit; -import code.ast.TypeDeclaration; -import generators.DataTransferMethodAnalyzer; -import generators.JavaCodeGenerator; -import generators.JavaMethodBodyGenerator; -import models.dataFlowModel.*; -import parser.*; -import parser.exceptions.ExpectedAssignment; -import parser.exceptions.ExpectedChannel; -import parser.exceptions.ExpectedChannelName; -import parser.exceptions.ExpectedColon; -import parser.exceptions.ExpectedDoubleQuotation; -import parser.exceptions.ExpectedEquals; -import parser.exceptions.ExpectedInOrOutOrRefOrSubKeyword; -import parser.exceptions.ExpectedLeftCurlyBracket; -import parser.exceptions.ExpectedRHSExpression; -import parser.exceptions.ExpectedRightBracket; -import parser.exceptions.ExpectedRightCurlyBracket; -import parser.exceptions.ExpectedStateTransition; -import parser.exceptions.WrongJsonExpression; -import parser.exceptions.WrongLHSExpression; -import parser.exceptions.WrongPathExpression; -import parser.exceptions.WrongRHSExpression; - -public class CodeGeneratorTest { - public static void main(String[] args) { - File file = new File("models/POS.model"); - try { - Parser parser = new Parser(new BufferedReader(new FileReader(file))); - DataTransferModel model; - try { - model = parser.doParse(); - DataFlowGraph graph = DataTransferModelAnalyzer.createDataFlowGraphWithStateStoringAttribute(model); - DataTransferModelAnalyzer.annotateWithSelectableDataTransferAttiribute(graph); - DataTransferMethodAnalyzer.decideToStoreResourceStates(graph); - ArrayList codetree = JavaMethodBodyGenerator.doGenerate(graph, model, JavaCodeGenerator.doGenerate(graph, model)); - System.out.println(codetree); - } catch (ExpectedChannel | ExpectedChannelName | ExpectedLeftCurlyBracket | ExpectedInOrOutOrRefOrSubKeyword - | ExpectedStateTransition | ExpectedEquals | ExpectedRHSExpression | WrongLHSExpression - | WrongRHSExpression | ExpectedRightBracket | ExpectedAssignment | ExpectedRightCurlyBracket | WrongPathExpression | WrongJsonExpression | ExpectedColon | ExpectedDoubleQuotation e) { - e.printStackTrace(); - } - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - } -} diff --git a/AlgebraicDataflowArchitectureModel/src/tests/DataStorageDecisionTest.java b/AlgebraicDataflowArchitectureModel/src/tests/DataStorageDecisionTest.java index 0db9f4b..9ba67ab 100644 --- a/AlgebraicDataflowArchitectureModel/src/tests/DataStorageDecisionTest.java +++ b/AlgebraicDataflowArchitectureModel/src/tests/DataStorageDecisionTest.java @@ -1,9 +1,14 @@ package tests; +import static org.junit.Assert.*; + import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; +import java.util.HashMap; + +import org.junit.Test; import algorithms.*; import generators.DataTransferMethodAnalyzer; @@ -28,8 +33,15 @@ import parser.exceptions.WrongRHSExpression; public class DataStorageDecisionTest { - public static void main(String[] args) { + + @Test + public void test() { File file = new File("models/POS2.model"); + HashMap exprectedDecision = new HashMap<>(); + exprectedDecision.put("history", true); + exprectedDecision.put("total", true); + exprectedDecision.put("points", false); + exprectedDecision.put("payment", true); try { Parser parser = new Parser(new BufferedReader(new FileReader(file))); DataTransferModel model = null; @@ -40,7 +52,11 @@ DataTransferModelAnalyzer.annotateWithSelectableDataTransferAttiribute(graph); DataTransferMethodAnalyzer.decideToStoreResourceStates(graph); for(Node resNode: graph.getResourceNodes()) { - System.out.println(((ResourceNode) resNode).getResourceName() + ":" + ((StoreAttribute) ((ResourceNode) resNode).getAttribute()).isStored()); + String resName = ((ResourceNode) resNode).getResourceName(); + boolean decision = ((StoreAttribute) ((ResourceNode) resNode).getAttribute()).isStored(); + System.out.println(resName + ":" + decision); + assertNotNull(exprectedDecision.get(resName)); + assertEquals(decision, exprectedDecision.get(resName)); } } catch (ExpectedChannel | ExpectedChannelName | ExpectedLeftCurlyBracket | ExpectedInOrOutOrRefOrSubKeyword | ExpectedStateTransition | ExpectedEquals | ExpectedRHSExpression | WrongLHSExpression diff --git a/AlgebraicDataflowArchitectureModel/src/tests/DataStorageNecessityTest.java b/AlgebraicDataflowArchitectureModel/src/tests/DataStorageNecessityTest.java index d95fb08..5d2c4d8 100644 --- a/AlgebraicDataflowArchitectureModel/src/tests/DataStorageNecessityTest.java +++ b/AlgebraicDataflowArchitectureModel/src/tests/DataStorageNecessityTest.java @@ -1,9 +1,15 @@ package tests; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; +import java.util.HashMap; + +import org.junit.Test; import algorithms.DataTransferModelAnalyzer; import models.Node; @@ -27,8 +33,15 @@ import parser.exceptions.WrongRHSExpression; public class DataStorageNecessityTest { - public static void main(String[] args) { + + @Test + public void test() { File file = new File("models/POS2.model"); + HashMap exprectedNecessity = new HashMap<>(); + exprectedNecessity.put("history", true); + exprectedNecessity.put("total", true); + exprectedNecessity.put("points", false); + exprectedNecessity.put("payment", false); try { Parser parser = new Parser(new BufferedReader(new FileReader(file))); DataTransferModel model; @@ -36,10 +49,16 @@ model = parser.doParse(); System.out.println(model); DataFlowGraph graph = DataTransferModelAnalyzer.createDataFlowGraphWithStateStoringAttribute(model); - for (Node n:graph.getNodes()) { - ResourceNode resource = (ResourceNode)n; - if((StoreAttribute)resource.getAttribute() != null) { - System.out.println(resource.toString() + ":" + ((StoreAttribute)resource.getAttribute()).isNeeded()); + for (Node n: graph.getNodes()) { + if (n instanceof ResourceNode) { + ResourceNode resource = (ResourceNode) n; + if((StoreAttribute) resource.getAttribute() != null) { + String resName = resource.getResourceName(); + boolean necessity = ((StoreAttribute) resource.getAttribute()).isNeeded(); + System.out.println(resName + ":" + necessity); + assertNotNull(exprectedNecessity.get(resName)); + assertEquals(necessity, exprectedNecessity.get(resName)); + } } } } catch (ExpectedChannel | ExpectedChannelName | ExpectedLeftCurlyBracket | ExpectedInOrOutOrRefOrSubKeyword diff --git a/AlgebraicDataflowArchitectureModel/src/tests/JavaCodeGeneratorTest.java b/AlgebraicDataflowArchitectureModel/src/tests/JavaCodeGeneratorTest.java new file mode 100644 index 0000000..572e43b --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/tests/JavaCodeGeneratorTest.java @@ -0,0 +1,317 @@ +package tests; + +import static org.junit.Assert.*; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +import org.junit.Test; + +import algorithms.*; +import code.ast.CompilationUnit; +import code.ast.FieldDeclaration; +import code.ast.MethodDeclaration; +import code.ast.TypeDeclaration; +import code.ast.VariableDeclaration; +import generators.DataTransferMethodAnalyzer; +import generators.JavaCodeGenerator; +import generators.JavaMethodBodyGenerator; +import models.dataFlowModel.*; +import parser.*; +import parser.exceptions.ExpectedAssignment; +import parser.exceptions.ExpectedChannel; +import parser.exceptions.ExpectedChannelName; +import parser.exceptions.ExpectedColon; +import parser.exceptions.ExpectedDoubleQuotation; +import parser.exceptions.ExpectedEquals; +import parser.exceptions.ExpectedInOrOutOrRefOrSubKeyword; +import parser.exceptions.ExpectedLeftCurlyBracket; +import parser.exceptions.ExpectedRHSExpression; +import parser.exceptions.ExpectedRightBracket; +import parser.exceptions.ExpectedRightCurlyBracket; +import parser.exceptions.ExpectedStateTransition; +import parser.exceptions.WrongJsonExpression; +import parser.exceptions.WrongLHSExpression; +import parser.exceptions.WrongPathExpression; +import parser.exceptions.WrongRHSExpression; + +public class JavaCodeGeneratorTest { + + @Test + public void test() { + testPOS(); + } + + private void testPOS() { + try { + ArrayList generatedCode = generateCode("models/POS.model"); + Map, // field name to type + Map, // arg types + Integer>>>>> // lines of code + exprectedStructure = new HashMap<>(); + exprectedStructure.put("Main", Map.entry(Map.of("history", "History", + "total", "Total", + "payment", "Payment", + "points", "Points"), + Map.of("Main", Map.entry("void", + Map.entry(Set.of(), + 4)), + "getHistory", Map.entry("List", + Map.entry(Set.of(), + 1)), + "getTotal", Map.entry("int", + Map.entry(Set.of(), + 1)), + "getPayment", Map.entry("int", + Map.entry(Set.of(), + 1)), + "purchase", Map.entry("void", + Map.entry(Set.of("int"), + 1)), + "getPoints", Map.entry("int", + Map.entry(Set.of(), + 1))))); + exprectedStructure.put("History", Map.entry(Map.of(), + Map.of("getValue", Map.entry("List", + Map.entry(Set.of(), + 1)), + "updateFromPayment", Map.entry("void", + Map.entry(Set.of("int"), + 1))))); + exprectedStructure.put("Total", Map.entry(Map.of(), + Map.of("getValue", Map.entry("int", + Map.entry(Set.of(), + 1)), + "Total", Map.entry("void", + Map.entry(Set.of("History"), + 1))))); + exprectedStructure.put("Payment", Map.entry(Map.of("value", "int", + "history", "History"), + Map.of("getValue", Map.entry("int", + Map.entry(Set.of(), + 1)), + "purchase", Map.entry("void", + Map.entry(Set.of("int"), + 2)), + "Payment", Map.entry("void", + Map.entry(Set.of("History"), + 1))))); + exprectedStructure.put("Points", Map.entry(Map.of(), + Map.of("getValue", Map.entry("int", + Map.entry(Set.of(), + 1)), + "Points", Map.entry("void", + Map.entry(Set.of("Payment"), + 1))))); + + checkStructure(generatedCode, exprectedStructure); + // generateCheckCode(generatedCode); + } catch (FileNotFoundException + | ExpectedChannel | ExpectedChannelName | ExpectedLeftCurlyBracket | ExpectedInOrOutOrRefOrSubKeyword + | ExpectedStateTransition | ExpectedEquals | ExpectedRHSExpression | WrongLHSExpression + | WrongRHSExpression | ExpectedRightBracket | ExpectedAssignment | ExpectedRightCurlyBracket + | WrongPathExpression | WrongJsonExpression | ExpectedColon | ExpectedDoubleQuotation e) { + e.printStackTrace(); + } + } + + private ArrayList generateCode(String fileName) throws FileNotFoundException, + ExpectedRightBracket, ExpectedChannel, ExpectedChannelName, ExpectedLeftCurlyBracket, ExpectedRightCurlyBracket, ExpectedInOrOutOrRefOrSubKeyword, + ExpectedStateTransition, ExpectedEquals, ExpectedRHSExpression, WrongLHSExpression, WrongRHSExpression, ExpectedAssignment, + WrongPathExpression, WrongJsonExpression, ExpectedColon, ExpectedDoubleQuotation { + File file = new File(fileName); + Parser parser = new Parser(new BufferedReader(new FileReader(file))); + DataTransferModel model; + model = parser.doParse(); + DataFlowGraph graph = DataTransferModelAnalyzer.createDataFlowGraphWithStateStoringAttribute(model); + DataTransferModelAnalyzer.annotateWithSelectableDataTransferAttiribute(graph); + DataTransferMethodAnalyzer.decideToStoreResourceStates(graph); + ArrayList codetree = JavaMethodBodyGenerator.doGenerate(graph, model, JavaCodeGenerator.doGenerate(graph, model)); + return codetree; + } + + private void checkStructure(ArrayList generatedCode, + Map, Map, Integer>>>>> exprectedStructure) { + for (var classEnt: exprectedStructure.entrySet()) { + String expectedClassName = classEnt.getKey(); + Entry, Map, Integer>>>> expectedClassStructure = classEnt.getValue(); + TypeDeclaration generatedClass = null; + for (CompilationUnit cu: generatedCode) { + for (TypeDeclaration type: cu.types()) { + if (type.getTypeName().equals(expectedClassName)) { + generatedClass = type; + break; + } + } + } + assertNotNull(generatedClass); + + Map exprectedFields = expectedClassStructure.getKey(); + Map, Integer>>> exprectedMethods = expectedClassStructure.getValue(); + + for (String expectedFieldName: exprectedFields.keySet()) { + FieldDeclaration generatedField = null; + for (FieldDeclaration field: generatedClass.getFields()) { + if (field.getName().equals(expectedFieldName)) { + generatedField = field; + break; + } + } + assertNotNull(generatedField); + + String expectedFieldType = exprectedFields.get(expectedFieldName); + if (expectedFieldType.equals("void")) { + assertNull(generatedField.getType()); + } else { + assertEquals(generatedField.getType().getInterfaceTypeName(), expectedFieldType); + } + } + } + } + + private void generateCheckCode(ArrayList generatedCode) { +// exprectedStructure.put("Main", Map.entry(Map.of("history", "History", +// "total", "Total", +// "payment", "Payment", +// "points", "Points"), +// Map.of("Main", Map.entry("void", +// Map.entry(Set.of(), +// 4)), +// "getHistory", Map.entry("List", +// Map.entry(Set.of(), +// 1)), +// "getTotal", Map.entry("int", +// Map.entry(Set.of(), +// 1)), +// "getPayment", Map.entry("int", +// Map.entry(Set.of(), +// 1)), +// "purchase", Map.entry("void", +// Map.entry(Set.of("int"), +// 1)), +// "getPoints", Map.entry("int", +// Map.entry(Set.of(), +// 1))))); + for (CompilationUnit cu: generatedCode) { + for (TypeDeclaration type: cu.types()) { + List fields = type.getFields(); + List methods = type.getMethods(); + // fields + if (fields.size() == 0) { + System.out.println("\t\t\texprectedStructure.put(\"" + type.getTypeName() + "\", Map.entry(Map.of(),"); + } if (fields.size() == 1) { + System.out.println("\t\t\texprectedStructure.put(\"" + type.getTypeName() + "\", Map.entry(Map.of(),"); + } else { + int i = 0; + for (FieldDeclaration field: fields) { + if (i == 0) { + System.out.println("\t\t\texprectedStructure.put(\"" + type.getTypeName() + "\", Map.entry(Map.of(\"" + field.getName() + "\", \"" + field.getType().getInterfaceTypeName() + "\","); + } else { + System.out.print("\t\t\t\t\t\t\t\t\t\t\t\t\t\t"); + for (int j = 0; j < type.getTypeName().length() / 4; j++) { + System.out.print("\t"); + } + for (int j = 0; j < type.getTypeName().length() % 4; j++) { + System.out.print(" "); + } + if (i < fields.size() - 1) { + System.out.println("\"" + field.getName() + "\", \"" + field.getType().getInterfaceTypeName() + "\","); + } else { + System.out.println("\"" + field.getName() + "\", \"" + field.getType().getInterfaceTypeName() + "\"),"); + } + } + i++; + } + } + // methods + if (methods.size() == 0) { + System.out.print("\t\t\t\t\t\t\t\t\t\t\t\t"); + for (int j = 0; j < (type.getTypeName().length() + 1) / 4; j++) { + System.out.print("\t"); + } + for (int j = 0; j < (type.getTypeName().length() + 1) % 4; j++) { + System.out.print(" "); + } + System.out.println("Map.of())));"); + } else { + int i = 0; + for (MethodDeclaration method: methods) { + // method name and return type + if (i == 0) { + System.out.print("\t\t\t\t\t\t\t\t\t\t\t\t"); + for (int j = 0; j < (type.getTypeName().length() + 1) / 4; j++) { + System.out.print("\t"); + } + for (int j = 0; j < (type.getTypeName().length() + 1) % 4; j++) { + System.out.print(" "); + } + if (method.getReturnType() == null) { + System.out.println("Map.of(\"" + method.getName() + "\", Map.entry(\"void\", "); + } else { + System.out.println("Map.of(\"" + method.getName() + "\", Map.entry(\"" + method.getReturnType().getInterfaceTypeName() + "\", "); + } + } else { + System.out.print("\t\t\t\t\t\t\t\t\t\t\t\t\t\t"); + for (int j = 0; j < type.getTypeName().length() / 4; j++) { + System.out.print("\t"); + } + for (int j = 0; j < type.getTypeName().length() % 4; j++) { + System.out.print(" "); + } + if (method.getReturnType() == null) { + System.out.println("\"" + method.getName() + "\", Map.entry(\"void\", "); + } else { + System.out.println("\"" + method.getName() + "\", Map.entry(\"" + method.getReturnType().getInterfaceTypeName() + "\", "); + } + } + // method parameters + System.out.print("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"); + for (int j = 0; j < (type.getTypeName().length() + method.getName().length() + 2) / 4; j++) { + System.out.print("\t"); + } + for (int j = 0; j < (type.getTypeName().length() + method.getName().length() + 2) % 4; j++) { + System.out.print(" "); + } + if (method.getParameters() == null || method.getParameters().size() == 0) { + System.out.println("Map.entry(Set.of(),"); + } else { + System.out.print("Map.entry(Set.of("); + String delim = ""; + for (VariableDeclaration arg: method.getParameters()) { + System.out.print(delim + "\"" + arg.getType().getInterfaceTypeName() + "\""); + delim = ","; + } + System.out.println("),"); + } + // method lines of code + System.out.print("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"); + for (int j = 0; j < (type.getTypeName().length() + method.getName().length()) / 4; j++) { + System.out.print("\t"); + } + for (int j = 0; j < (type.getTypeName().length() + method.getName().length()) % 4; j++) { + System.out.print(" "); + } + if (i < methods.size() - 1) { + System.out.println("" + method.getBody().getStatements().size() + ")),"); + } else { + System.out.println("" + method.getBody().getStatements().size() + ")))));"); + } + i++; + } + } + } + } + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/tests/UpdateConflictCheckTest.java b/AlgebraicDataflowArchitectureModel/src/tests/UpdateConflictCheckTest.java index de362c9..a7d7d6a 100644 --- a/AlgebraicDataflowArchitectureModel/src/tests/UpdateConflictCheckTest.java +++ b/AlgebraicDataflowArchitectureModel/src/tests/UpdateConflictCheckTest.java @@ -1,10 +1,14 @@ package tests; +import static org.junit.Assert.*; + import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; +import org.junit.Test; + import algorithms.*; import models.dataFlowModel.DataTransferModel; import parser.Parser; @@ -26,14 +30,16 @@ import parser.exceptions.WrongRHSExpression; public class UpdateConflictCheckTest { - public static void main(String[] args) { + + @Test + public void test() { File file = new File("models/POS2.model"); try { Parser parser = new Parser(new BufferedReader(new FileReader(file))); DataTransferModel model; try { model = parser.doParse(); - System.out.println(Validation.checkUpdateConflict(model)); + assertTrue(Validation.checkUpdateConflict(model)); } catch (ExpectedRightBracket | ExpectedChannel | ExpectedChannelName | ExpectedLeftCurlyBracket | ExpectedRightCurlyBracket | ExpectedInOrOutOrRefOrSubKeyword | ExpectedStateTransition | ExpectedEquals | ExpectedRHSExpression | WrongLHSExpression | WrongRHSExpression | ExpectedAssignment