Skip to content

Commit 2c8bfe2

Browse files
committed
Simplify usages of task.outputs['result']
For retrieval, we can use the new task.result() convenience method. For assignment, we can rely on result being set automatically.
1 parent c7e8c7f commit 2c8bfe2

File tree

10 files changed

+26
-28
lines changed

10 files changed

+26
-28
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ Environment env = Appose.system();
7676
try (Service python = env.python()) {
7777
Task task = python.task("5 + 6");
7878
task.waitFor();
79-
Object result = task.outputs.get("result");
80-
assertEquals(11, result);
79+
assertEquals(11, task.result());
8180
}
8281
```
8382

src/main/java/org/apposed/appose/Appose.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@
6969
* 5 + 6
7070
* """);
7171
* task.waitFor();
72-
* Object result = task.outputs.get("result");
73-
* assertEquals(11, result);
72+
* assertEquals(11, task.result());
7473
* }</pre>
7574
* <p>
7675
* And here is an example using a few more of Appose's features:
@@ -163,7 +162,7 @@
163162
* {
164163
* "task" : "87427f91-d193-4b25-8d35-e1292a34b5c4",
165164
* "requestType" : "EXECUTE",
166-
* "script" : "task.outputs[\"result\"] = computeResult(gamma)\n",
165+
* "script" : "task.outputs[\"answer\"] = computeResult(gamma)\n",
167166
* "inputs" : {"gamma": 2.2}
168167
* }
169168
* </code></pre>

src/main/java/org/apposed/appose/Service.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public Object getVar(String name) throws IOException, InterruptedException {
286286
Syntaxes.validate(this);
287287
String script = syntax.getVar(name);
288288
Task task = task(script).waitFor();
289-
if (task.status == TaskStatus.COMPLETE) return task.outputs.get("result");
289+
if (task.status == TaskStatus.COMPLETE) return task.result();
290290
throw new IllegalStateException("Failed to get variable '" + name + "': " + task.error);
291291
}
292292

@@ -350,7 +350,7 @@ public Object call(String function, Object... args) throws IOException, Interrup
350350
}
351351
String script = syntax.call(function, varNames);
352352
Task task = task(script, inputs).waitFor();
353-
if (task.status == TaskStatus.COMPLETE) return task.outputs.get("result");
353+
if (task.status == TaskStatus.COMPLETE) return task.result();
354354
throw new IllegalStateException("Failed to call function '" + function + "': " + task.error);
355355
}
356356

src/main/java/org/apposed/appose/util/Proxies.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public static <T> T create(Service service, String var, Class<T> api) {
112112
* Each method invocation on the returned proxy generates a script of the form
113113
* {@code var.methodName(arg0, arg1, ...)} and submits it as a task to the worker.
114114
* Arguments are passed via the task's {@code inputs} map, and the return value
115-
* is retrieved from {@code task.outputs["result"]}.
115+
* is retrieved from {@code task.result()}.
116116
* </p>
117117
* <p>
118118
* <strong>Variable export requirement:</strong> The variable must have been previously
@@ -141,7 +141,7 @@ public static <T> T create(Service service, String var, Class<T> api) {
141141
* @param queue Optional queue identifier for task execution. Pass {@code "main"} to ensure
142142
* execution on the worker's main thread, or {@code null} for default behavior.
143143
* @return A proxy object implementing the specified interface. Method calls block until
144-
* the remote execution completes and return the value from {@code task.outputs["result"]}.
144+
* the remote execution completes and return the value from {@code task.result()}.
145145
* @throws RuntimeException If a proxied method call fails in the worker process.
146146
* @see Service#proxy(String, Class, String) Convenience method available on Service instances.
147147
* @see Service#task(String, String) For understanding queue behavior.
@@ -171,7 +171,7 @@ public static <T> T create(Service service, String var, Class<T> api, String que
171171
if (task.status != Service.TaskStatus.COMPLETE) {
172172
throw new RuntimeException(task.error);
173173
}
174-
return task.outputs.get("result");
174+
return task.result();
175175
});
176176
}
177177
}

src/test/java/org/apposed/appose/BuilderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ public void testCustom() throws IOException, InterruptedException {
234234
// Verify that the custom environment can execute Python tasks.
235235
try (Service service = env.python()) {
236236
maybeDebug(service);
237-
Task task = service.task("task.outputs['result'] = 2 + 2");
237+
Task task = service.task("2 + 2");
238238
task.waitFor();
239239
assertComplete(task);
240-
Number result = (Number) task.outputs.get("result");
240+
Number result = (Number) task.result();
241241
assertEquals(4, result.intValue());
242242
}
243243

src/test/java/org/apposed/appose/NDArrayExampleGroovy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ public static void main(String[] args) throws Exception {
6262
try (Service service = env.groovy()) {
6363
final Map< String, Object > inputs = new HashMap<>();
6464
inputs.put( "img", ndArray);
65-
Service.Task task = service.task(PRINT_INPUT, inputs );
65+
Service.Task task = service.task(PRINT_INPUT, inputs);
6666
task.waitFor();
67-
System.out.println( "result = " + task.outputs.get("result") );
67+
System.out.println("result = " + task.result());
6868
}
6969
ndArray.close();
7070
}

src/test/java/org/apposed/appose/NDArrayExamplePython.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static void main(String[] args) throws Exception {
5959
inputs.put( "img", ndArray);
6060
Service.Task task = service.task(PRINT_INPUT, inputs );
6161
task.waitFor();
62-
final String result = ( String ) task.outputs.get( "result" );
62+
final String result = ( String ) task.result();
6363
System.out.println( "result = \n" + result );
6464
}
6565
ndArray.close();
@@ -68,6 +68,6 @@ public static void main(String[] args) throws Exception {
6868

6969
private static final String PRINT_INPUT = "" + //
7070
"import numpy as np\n" + //
71-
"task.outputs['result'] = str(img.ndarray())";
71+
"str(img.ndarray())";
7272

7373
}

src/test/java/org/apposed/appose/ServiceTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void testScopeGroovy() throws IOException, InterruptedException {
7979
assertEquals(10, result.intValue());
8080

8181
inputs.put("age", 81);
82-
task = service.task("task.outputs['result'] = sqrt_age(age)", inputs).waitFor();
82+
task = service.task("sqrt_age(age)", inputs).waitFor();
8383
assertComplete(task);
8484
result = (Number) task.result();
8585
assertEquals(9, result.intValue());
@@ -284,7 +284,7 @@ public void testInit() throws IOException, InterruptedException {
284284
maybeDebug(service);
285285

286286
// Verify that the init script was executed and the variable is accessible.
287-
Task task = service.task("task.outputs['result'] = init_value").waitFor();
287+
Task task = service.task("init_value").waitFor();
288288
assertComplete(task);
289289

290290
String result = (String) task.result();
@@ -301,7 +301,7 @@ public void testTaskResult() throws IOException, InterruptedException {
301301
maybeDebug(service);
302302

303303
// Create a task that produces a result.
304-
Task task = service.task("task.outputs['result'] = 'success'").waitFor();
304+
Task task = service.task("'success'").waitFor();
305305
assertComplete(task);
306306

307307
// Test the result() convenience method.

src/test/java/org/apposed/appose/SyntaxTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ public void testPutVarPython() throws IOException, InterruptedException {
105105
service.putVar("my_number", 123);
106106

107107
// Verify the variable is accessible in subsequent tasks
108-
Task task = service.task("task.outputs['result'] = my_number * 2").waitFor();
108+
Task task = service.task("my_number * 2").waitFor();
109109
assertComplete(task);
110-
Number result = (Number) task.outputs.get("result");
110+
Number result = (Number) task.result();
111111
assertEquals(246, result.intValue());
112112
}
113113
}
@@ -123,9 +123,9 @@ public void testPutVarGroovy() throws IOException, InterruptedException {
123123
service.putVar("my_string", "test");
124124

125125
// Verify the variable is accessible in subsequent tasks
126-
Task task = service.task("task.outputs['result'] = my_string.toUpperCase()").waitFor();
126+
Task task = service.task("my_string.toUpperCase()").waitFor();
127127
assertComplete(task);
128-
String result = (String) task.outputs.get("result");
128+
String result = (String) task.result();
129129
assertEquals("TEST", result);
130130
}
131131
}
@@ -142,9 +142,9 @@ public void testPutVarList() throws IOException, InterruptedException {
142142
service.putVar("my_list", values);
143143

144144
// Verify the list is accessible and can be manipulated
145-
Task task = service.task("task.outputs['result'] = sum(my_list)").waitFor();
145+
Task task = service.task("sum(my_list)").waitFor();
146146
assertComplete(task);
147-
Number result = (Number) task.outputs.get("result");
147+
Number result = (Number) task.result();
148148
assertEquals(15, result.intValue());
149149
}
150150
}

src/test/java/org/apposed/appose/TestBase.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class TaskState {
113113

114114
// Validate the execution result.
115115
assertComplete(task);
116-
Number result = (Number) task.outputs.get("result");
116+
Number result = (Number) task.result();
117117
assertEquals(91, result.intValue());
118118

119119
// Validate the events received.
@@ -148,13 +148,13 @@ public void cowsayAndAssert(Environment env, String greeting)
148148
maybeDebug(service);
149149
Task task = service.task(
150150
"import cowsay\n" +
151-
"task.outputs['result'] = cowsay.get_output_string('cow', '" + greeting + "')\n"
151+
"cowsay.get_output_string('cow', '" + greeting + "')\n"
152152
);
153153
task.waitFor();
154154
assertComplete(task);
155155
// Verify cowsay output contains the greeting and key elements
156156
// (exact spacing can vary between cowsay versions).
157-
String actual = (String) task.outputs.get("result");
157+
String actual = (String) task.result();
158158
assertNotNull(actual, "Cowsay output should not be null");
159159
assertTrue(actual.contains(greeting), "Output should contain the greeting: " + greeting);
160160
assertTrue(actual.contains("^__^"), "Output should contain cow face");

0 commit comments

Comments
 (0)