From dacb5605d7799aa93899edeb9fefa4fdc1564895 Mon Sep 17 00:00:00 2001 From: rahimiankeanu Date: Tue, 2 Nov 2021 21:21:52 +0100 Subject: [PATCH] executor 2 change --- .../adapter/out/web/GetAssignmentAdapter.java | 4 ++-- .../executor/domain/ExecutorBase.java | 6 +++--- .../executorBase/executor/domain/Task.java | 6 +++++- .../executor2/executor/domain/Executor.java | 21 +++++++++++++------ 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/executor-base/src/main/java/ch/unisg/executorBase/executor/adapter/out/web/GetAssignmentAdapter.java b/executor-base/src/main/java/ch/unisg/executorBase/executor/adapter/out/web/GetAssignmentAdapter.java index 05852fa..411073b 100644 --- a/executor-base/src/main/java/ch/unisg/executorBase/executor/adapter/out/web/GetAssignmentAdapter.java +++ b/executor-base/src/main/java/ch/unisg/executorBase/executor/adapter/out/web/GetAssignmentAdapter.java @@ -46,8 +46,8 @@ public class GetAssignmentAdapter implements GetAssignmentPort { if (response.body().equals("")) { return null; } - - return new Task(new JSONObject(response.body()).getString("taskID")); + JSONObject responseBody = new JSONObject(response.body()); + return new Task(responseBody.getString("taskID"), responseBody.getString("input")); } catch (IOException | InterruptedException e) { logger.log(Level.SEVERE, e.getLocalizedMessage(), e); diff --git a/executor-base/src/main/java/ch/unisg/executorBase/executor/domain/ExecutorBase.java b/executor-base/src/main/java/ch/unisg/executorBase/executor/domain/ExecutorBase.java index c9df1a8..83ab862 100644 --- a/executor-base/src/main/java/ch/unisg/executorBase/executor/domain/ExecutorBase.java +++ b/executor-base/src/main/java/ch/unisg/executorBase/executor/domain/ExecutorBase.java @@ -61,7 +61,7 @@ public abstract class ExecutorBase { System.out.println("Starting execution"); this.status = ExecutorStatus.EXECUTING; - task.setResult(execution()); + task.setResult(execution(task.getInput())); executionFinishedEventPort.publishExecutionFinishedEvent( new ExecutionFinishedEvent(task.getTaskID(), task.getResult(), "SUCCESS")); @@ -70,6 +70,6 @@ public abstract class ExecutorBase { getAssignment(); } - protected abstract String execution(); - + protected abstract String execution(String... input); + } diff --git a/executor-base/src/main/java/ch/unisg/executorBase/executor/domain/Task.java b/executor-base/src/main/java/ch/unisg/executorBase/executor/domain/Task.java index fec330f..f455dcd 100644 --- a/executor-base/src/main/java/ch/unisg/executorBase/executor/domain/Task.java +++ b/executor-base/src/main/java/ch/unisg/executorBase/executor/domain/Task.java @@ -12,8 +12,12 @@ public class Task { @Setter private String result; - public Task(String taskID) { + @Getter + private String[] input; + + public Task(String taskID, String... input) { this.taskID = taskID; + this.input = input; } } diff --git a/executor2/src/main/java/ch/unisg/executor2/executor/domain/Executor.java b/executor2/src/main/java/ch/unisg/executor2/executor/domain/Executor.java index 4d022b5..f2021b8 100644 --- a/executor2/src/main/java/ch/unisg/executor2/executor/domain/Executor.java +++ b/executor2/src/main/java/ch/unisg/executor2/executor/domain/Executor.java @@ -18,19 +18,28 @@ public class Executor extends ExecutorBase { @Override protected - String execution() { + String execution(String... input) { + + double result = Double.NaN; + int a = Integer.parseInt(input[0]); + int b = Integer.parseInt(input[2]); + String operation = input[1]; - int a = 20; - int b = 20; try { TimeUnit.SECONDS.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } - int result = a + b; + if (operation == "+") { + result = a + b; + } else if (operation == "*") { + result = a * b; + } else if (operation == "-") { + result = a - b; + } - return Integer.toString(result); + return Double.toString(result); } -} +} \ No newline at end of file