blob: 96bb6e203bfff1fd28248f1159a68eaa5c2279d9 [file] [log] [blame]
Yang Nicc1ca482015-03-16 15:53:18 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yang Ni281c3252014-10-24 08:52:24 -070017package android.renderscript;
18
19import android.util.Log;
20import android.util.Pair;
21import java.util.ArrayList;
22import java.util.HashMap;
23import java.util.List;
24import java.util.Map;
25
26/**
Yang Ni4470d68a2015-03-16 16:23:50 -070027 * ScriptGroup2 is a new, enhanced API for script groups.
28 * A script group is a collection of kernels or invocable functions, with
29 * data dependencies defined among them. A script group is launched for
30 * execution as a whole, rather than launching each kernel or invocable function
31 * separately. Once created, a script group can be repeatedly used with
32 * different inputs.
33 * <p>
34 * In the new ScriptGroup2 API, a script group is modeled using closures.
35 * A closure, in this context, is defined as a function call to a kernel or
36 * invocable function. Each function argument or global variable accessed inside
37 * the function is bound to 1) a known value, 2) a script group input, or 3) a
38 * future. A future is the output of a closure, i.e., the return value of the
39 * function or a global variable written by that function.
40 * <p>
41 * A script group is a directed acyclic graph (DAG), in which closures are the
42 * vertices and the dependencies among them are the edges.
43 * The way the ScriptGroup2 API is designed makes cycles impossible in a script
44 * group. For example, it is impossible to make forward references to futures,
45 * i.e., it is impossible to set as input to a closure the future from itself or
46 * a future from another closure that directly or indirectly depends on it.
47 * <p>
48 * Grouping kernels and invocable functions together allows to execute them more
49 * efficiently. Runtime and compiler optimizations are applied to script
50 * groups, to reduce computation or communication overhead, and to make more
51 * efficient use of the CPU and the GPU.
52 */
Yang Ni281c3252014-10-24 08:52:24 -070053
Yang Ni281c3252014-10-24 08:52:24 -070054public class ScriptGroup2 extends BaseObj {
55
Yang Ni4470d68a2015-03-16 16:23:50 -070056 /**
57 * An opaque class for closures
58 */
59
Yang Nicc1ca482015-03-16 15:53:18 -070060 public static class Closure extends BaseObj {
Yang Ni4c93c8c2015-03-26 14:35:22 -070061 private Object[] mArgs;
Yang Nicc1ca482015-03-16 15:53:18 -070062 private Allocation mReturnValue;
63 private Map<Script.FieldID, Object> mBindings;
Yang Ni281c3252014-10-24 08:52:24 -070064
Yang Nicc1ca482015-03-16 15:53:18 -070065 private Future mReturnFuture;
66 private Map<Script.FieldID, Future> mGlobalFuture;
Yang Ni281c3252014-10-24 08:52:24 -070067
Yang Nicc1ca482015-03-16 15:53:18 -070068 private FieldPacker mFP;
Yang Nibe392ad2015-01-23 17:16:02 -080069
Yang Nicc1ca482015-03-16 15:53:18 -070070 private static final String TAG = "Closure";
Yang Ni281c3252014-10-24 08:52:24 -070071
Yang Nicc1ca482015-03-16 15:53:18 -070072 public Closure(long id, RenderScript rs) {
73 super(id, rs);
Yang Ni281c3252014-10-24 08:52:24 -070074 }
Yang Ni281c3252014-10-24 08:52:24 -070075
Yang Nicc1ca482015-03-16 15:53:18 -070076 public Closure(RenderScript rs, Script.KernelID kernelID, Type returnType,
77 Object[] args, Map<Script.FieldID, Object> globals) {
78 super(0, rs);
79
Yang Ni4c93c8c2015-03-26 14:35:22 -070080 mArgs = args;
Yang Nicc1ca482015-03-16 15:53:18 -070081 mReturnValue = Allocation.createTyped(rs, returnType);
Yang Ni4c93c8c2015-03-26 14:35:22 -070082 mBindings = globals;
Yang Nicc1ca482015-03-16 15:53:18 -070083 mGlobalFuture = new HashMap<Script.FieldID, Future>();
84
85 int numValues = args.length + globals.size();
86
87 long[] fieldIDs = new long[numValues];
88 long[] values = new long[numValues];
89 int[] sizes = new int[numValues];
90 long[] depClosures = new long[numValues];
91 long[] depFieldIDs = new long[numValues];
92
93 int i;
94 for (i = 0; i < args.length; i++) {
95 Object obj = args[i];
96 fieldIDs[i] = 0;
97 if (obj instanceof UnboundValue) {
98 UnboundValue unbound = (UnboundValue)obj;
99 unbound.addReference(this, i);
100 } else {
101 retrieveValueAndDependenceInfo(rs, i, args[i], values, sizes,
102 depClosures, depFieldIDs);
103 }
104 }
105
106 for (Map.Entry<Script.FieldID, Object> entry : globals.entrySet()) {
107 Object obj = entry.getValue();
108 Script.FieldID fieldID = entry.getKey();
109 fieldIDs[i] = fieldID.getID(rs);
110 if (obj instanceof UnboundValue) {
111 UnboundValue unbound = (UnboundValue)obj;
112 unbound.addReference(this, fieldID);
113 } else {
114 retrieveValueAndDependenceInfo(rs, i, obj, values,
115 sizes, depClosures, depFieldIDs);
116 }
117 i++;
118 }
119
120 long id = rs.nClosureCreate(kernelID.getID(rs), mReturnValue.getID(rs),
121 fieldIDs, values, sizes, depClosures, depFieldIDs);
122
123 setID(id);
Yang Ni281c3252014-10-24 08:52:24 -0700124 }
Yang Ni281c3252014-10-24 08:52:24 -0700125
Yang Nicc1ca482015-03-16 15:53:18 -0700126 public Closure(RenderScript rs, Script.InvokeID invokeID,
127 Object[] args, Map<Script.FieldID, Object> globals) {
128 super(0, rs);
Yang Ni8bcbf472015-04-01 17:29:14 -0700129 mFP = FieldPacker.createFromArray(args);
Yang Ni281c3252014-10-24 08:52:24 -0700130
Yang Ni4c93c8c2015-03-26 14:35:22 -0700131 mArgs = args;
132 mBindings = globals;
Yang Nicc1ca482015-03-16 15:53:18 -0700133 mGlobalFuture = new HashMap<Script.FieldID, Future>();
Yang Ni281c3252014-10-24 08:52:24 -0700134
Yang Nicc1ca482015-03-16 15:53:18 -0700135 int numValues = globals.size();
Yang Nibe392ad2015-01-23 17:16:02 -0800136
Yang Nicc1ca482015-03-16 15:53:18 -0700137 long[] fieldIDs = new long[numValues];
138 long[] values = new long[numValues];
139 int[] sizes = new int[numValues];
140 long[] depClosures = new long[numValues];
141 long[] depFieldIDs = new long[numValues];
Yang Nibe392ad2015-01-23 17:16:02 -0800142
Yang Nicc1ca482015-03-16 15:53:18 -0700143 int i = 0;
144 for (Map.Entry<Script.FieldID, Object> entry : globals.entrySet()) {
145 Object obj = entry.getValue();
146 Script.FieldID fieldID = entry.getKey();
147 fieldIDs[i] = fieldID.getID(rs);
148 if (obj instanceof UnboundValue) {
149 UnboundValue unbound = (UnboundValue)obj;
150 unbound.addReference(this, fieldID);
151 } else {
Yang Nicc1ca482015-03-16 15:53:18 -0700152 retrieveValueAndDependenceInfo(rs, i, obj, values,
153 sizes, depClosures, depFieldIDs);
154 }
155 i++;
156 }
Yang Nibe392ad2015-01-23 17:16:02 -0800157
Yang Nicc1ca482015-03-16 15:53:18 -0700158 long id = rs.nInvokeClosureCreate(invokeID.getID(rs), mFP.getData(), fieldIDs,
159 values, sizes);
Yang Nibe392ad2015-01-23 17:16:02 -0800160
Yang Nicc1ca482015-03-16 15:53:18 -0700161 setID(id);
Yang Nibe392ad2015-01-23 17:16:02 -0800162 }
Yang Nibe392ad2015-01-23 17:16:02 -0800163
Yang Nicc1ca482015-03-16 15:53:18 -0700164 private static
165 void retrieveValueAndDependenceInfo(RenderScript rs,
166 int index, Object obj,
167 long[] values, int[] sizes,
168 long[] depClosures,
169 long[] depFieldIDs) {
Yang Nibe392ad2015-01-23 17:16:02 -0800170
Yang Nicc1ca482015-03-16 15:53:18 -0700171 if (obj instanceof Future) {
172 Future f = (Future)obj;
173 obj = f.getValue();
174 depClosures[index] = f.getClosure().getID(rs);
175 Script.FieldID fieldID = f.getFieldID();
176 depFieldIDs[index] = fieldID != null ? fieldID.getID(rs) : 0;
177 if (obj == null) {
178 // Value is originally created by the owner closure
179 values[index] = 0;
180 sizes[index] = 0;
181 return;
182 }
183 } else {
184 depClosures[index] = 0;
185 depFieldIDs[index] = 0;
186 }
Yang Nibe392ad2015-01-23 17:16:02 -0800187
Yang Nicc1ca482015-03-16 15:53:18 -0700188 ValueAndSize vs = new ValueAndSize(rs, obj);
189 values[index] = vs.value;
190 sizes[index] = vs.size;
Yang Nibe392ad2015-01-23 17:16:02 -0800191 }
Yang Ni281c3252014-10-24 08:52:24 -0700192
Yang Ni4470d68a2015-03-16 16:23:50 -0700193 /**
194 * Returns the future for the return value
195 *
196 * @return a future
197 */
198
Yang Nicc1ca482015-03-16 15:53:18 -0700199 public Future getReturn() {
200 if (mReturnFuture == null) {
201 mReturnFuture = new Future(this, null, mReturnValue);
202 }
Yang Ni281c3252014-10-24 08:52:24 -0700203
Yang Nicc1ca482015-03-16 15:53:18 -0700204 return mReturnFuture;
Yang Ni281c3252014-10-24 08:52:24 -0700205 }
Yang Ni281c3252014-10-24 08:52:24 -0700206
Yang Ni4470d68a2015-03-16 16:23:50 -0700207 /**
208 * Returns the future for a global variable
209 *
210 * @param field the field ID for the global variable
211 * @return a future
212 */
213
Yang Nicc1ca482015-03-16 15:53:18 -0700214 public Future getGlobal(Script.FieldID field) {
215 Future f = mGlobalFuture.get(field);
Yang Ni281c3252014-10-24 08:52:24 -0700216
Yang Nicc1ca482015-03-16 15:53:18 -0700217 if (f == null) {
218 // If the field is not bound to this closure, this will return a future
219 // without an associated value (reference). So this is not working for
220 // cross-module (cross-script) linking in this case where a field not
221 // explicitly bound.
222 f = new Future(this, field, mBindings.get(field));
223 mGlobalFuture.put(field, f);
224 }
225
226 return f;
227 }
228
229 void setArg(int index, Object obj) {
Yang Ni4c93c8c2015-03-26 14:35:22 -0700230 mArgs[index] = obj;
Yang Nicc1ca482015-03-16 15:53:18 -0700231 ValueAndSize vs = new ValueAndSize(mRS, obj);
232 mRS.nClosureSetArg(getID(mRS), index, vs.value, vs.size);
233 }
234
235 void setGlobal(Script.FieldID fieldID, Object obj) {
Yang Ni4c93c8c2015-03-26 14:35:22 -0700236 mBindings.put(fieldID, obj);
Yang Nicc1ca482015-03-16 15:53:18 -0700237 ValueAndSize vs = new ValueAndSize(mRS, obj);
238 mRS.nClosureSetGlobal(getID(mRS), fieldID.getID(mRS), vs.value, vs.size);
239 }
240
241 private static final class ValueAndSize {
242 public ValueAndSize(RenderScript rs, Object obj) {
243 if (obj instanceof Allocation) {
244 value = ((Allocation)obj).getID(rs);
245 size = -1;
246 } else if (obj instanceof Boolean) {
247 value = ((Boolean)obj).booleanValue() ? 1 : 0;
248 size = 4;
249 } else if (obj instanceof Integer) {
250 value = ((Integer)obj).longValue();
251 size = 4;
252 } else if (obj instanceof Long) {
253 value = ((Long)obj).longValue();
254 size = 8;
255 } else if (obj instanceof Float) {
256 value = ((Float)obj).longValue();
257 size = 4;
258 } else if (obj instanceof Double) {
259 value = ((Double)obj).longValue();
260 size = 8;
261 }
262 }
263 public long value;
264 public int size;
265 }
Yang Ni281c3252014-10-24 08:52:24 -0700266 }
267
Yang Ni4470d68a2015-03-16 16:23:50 -0700268 /**
269 * An opaque class for futures
270 */
271
Yang Nicc1ca482015-03-16 15:53:18 -0700272 public static class Future {
273 Closure mClosure;
274 Script.FieldID mFieldID;
275 Object mValue;
Yang Ni281c3252014-10-24 08:52:24 -0700276
Yang Nicc1ca482015-03-16 15:53:18 -0700277 Future(Closure closure, Script.FieldID fieldID, Object value) {
278 mClosure = closure;
279 mFieldID = fieldID;
280 mValue = value;
281 }
Yang Ni281c3252014-10-24 08:52:24 -0700282
Yang Nicc1ca482015-03-16 15:53:18 -0700283 Closure getClosure() { return mClosure; }
284 Script.FieldID getFieldID() { return mFieldID; }
285 Object getValue() { return mValue; }
Yang Ni281c3252014-10-24 08:52:24 -0700286 }
287
Yang Ni4470d68a2015-03-16 16:23:50 -0700288 /**
289 * An opaque class for unbound values (a.k.a. script group inputs)
290 */
291
Yang Nicc1ca482015-03-16 15:53:18 -0700292 public static class UnboundValue {
293 // Either mFieldID or mArgIndex should be set but not both.
294 List<Pair<Closure, Script.FieldID>> mFieldID;
295 // -1 means unset. Legal values are 0 .. n-1, where n is the number of
296 // arguments for the referencing closure.
297 List<Pair<Closure, Integer>> mArgIndex;
298
299 UnboundValue() {
300 mFieldID = new ArrayList<Pair<Closure, Script.FieldID>>();
301 mArgIndex = new ArrayList<Pair<Closure, Integer>>();
302 }
303
304 void addReference(Closure closure, int index) {
305 mArgIndex.add(Pair.create(closure, Integer.valueOf(index)));
306 }
307
308 void addReference(Closure closure, Script.FieldID fieldID) {
309 mFieldID.add(Pair.create(closure, fieldID));
310 }
311
312 void set(Object value) {
313 for (Pair<Closure, Integer> p : mArgIndex) {
314 Closure closure = p.first;
315 int index = p.second.intValue();
316 closure.setArg(index, value);
317 }
318 for (Pair<Closure, Script.FieldID> p : mFieldID) {
319 Closure closure = p.first;
320 Script.FieldID fieldID = p.second;
321 closure.setGlobal(fieldID, value);
322 }
323 }
Yang Ni281c3252014-10-24 08:52:24 -0700324 }
325
Yang Ni35be56c2015-04-02 17:47:56 -0700326 String mName;
Yang Ni281c3252014-10-24 08:52:24 -0700327 List<Closure> mClosures;
328 List<UnboundValue> mInputs;
Yang Nicc1ca482015-03-16 15:53:18 -0700329 Future[] mOutputs;
Yang Ni281c3252014-10-24 08:52:24 -0700330
Yang Nicc1ca482015-03-16 15:53:18 -0700331 private static final String TAG = "ScriptGroup2";
Yang Ni281c3252014-10-24 08:52:24 -0700332
Yang Nicc1ca482015-03-16 15:53:18 -0700333 public ScriptGroup2(long id, RenderScript rs) {
334 super(id, rs);
Yang Ni281c3252014-10-24 08:52:24 -0700335 }
336
Yang Ni35be56c2015-04-02 17:47:56 -0700337 ScriptGroup2(RenderScript rs, String name, List<Closure> closures,
Yang Nicc1ca482015-03-16 15:53:18 -0700338 List<UnboundValue> inputs, Future[] outputs) {
339 super(0, rs);
Yang Ni35be56c2015-04-02 17:47:56 -0700340 mName = name;
Yang Nicc1ca482015-03-16 15:53:18 -0700341 mClosures = closures;
342 mInputs = inputs;
343 mOutputs = outputs;
344
345 long[] closureIDs = new long[closures.size()];
346 for (int i = 0; i < closureIDs.length; i++) {
347 closureIDs[i] = closures.get(i).getID(rs);
348 }
Yang Ni35be56c2015-04-02 17:47:56 -0700349 long id = rs.nScriptGroup2Create(name, ScriptC.mCachePath, closureIDs);
Yang Nicc1ca482015-03-16 15:53:18 -0700350 setID(id);
Yang Ni281c3252014-10-24 08:52:24 -0700351 }
352
Yang Ni4470d68a2015-03-16 16:23:50 -0700353 /**
354 * Executes a script group
355 *
356 * @param inputs inputs to the script group
357 * @return outputs of the script group as an array of objects
358 */
359
Yang Nicc1ca482015-03-16 15:53:18 -0700360 public Object[] execute(Object... inputs) {
361 if (inputs.length < mInputs.size()) {
362 Log.e(TAG, this.toString() + " receives " + inputs.length + " inputs, " +
363 "less than expected " + mInputs.size());
364 return null;
365 }
366
367 if (inputs.length > mInputs.size()) {
368 Log.i(TAG, this.toString() + " receives " + inputs.length + " inputs, " +
369 "more than expected " + mInputs.size());
370 }
371
372 for (int i = 0; i < mInputs.size(); i++) {
373 Object obj = inputs[i];
374 if (obj instanceof Future || obj instanceof UnboundValue) {
375 Log.e(TAG, this.toString() + ": input " + i +
376 " is a future or unbound value");
377 return null;
378 }
379 UnboundValue unbound = mInputs.get(i);
380 unbound.set(obj);
381 }
382
383 mRS.nScriptGroup2Execute(getID(mRS));
384
385 Object[] outputObjs = new Object[mOutputs.length];
386 int i = 0;
387 for (Future f : mOutputs) {
388 outputObjs[i++] = f.getValue();
389 }
390 return outputObjs;
Yang Nibe392ad2015-01-23 17:16:02 -0800391 }
392
Yang Nicc1ca482015-03-16 15:53:18 -0700393 /**
Yang Ni4470d68a2015-03-16 16:23:50 -0700394 * A class representing a binding of a value to a global variable in a
395 * kernel or invocable function. Such a binding can be used to create a
396 * closure.
397 */
398
Yang Ni8ff29802015-03-11 16:25:37 -0700399 public static final class Binding {
Yang Ni4470d68a2015-03-16 16:23:50 -0700400 private Script.FieldID mField;
401 private Object mValue;
402
403 /**
404 * Returns a Binding object that binds value to field
405 *
406 * @param field the Script.FieldID of the global variable
407 * @param value the value
408 */
409
Yang Ni8ff29802015-03-11 16:25:37 -0700410 public Binding(Script.FieldID field, Object value) {
411 mField = field;
412 mValue = value;
413 }
Yang Ni4470d68a2015-03-16 16:23:50 -0700414
415 /**
416 * Returns the field ID
417 */
418
419 public Script.FieldID getField() { return mField; }
420
421 /**
422 * Returns the value
423 */
424
425 public Object getValue() { return mValue; }
Yang Ni8ff29802015-03-11 16:25:37 -0700426 }
427
428 /**
Yang Ni4470d68a2015-03-16 16:23:50 -0700429 * The builder class to create a script group.
430 * <p>
431 * Closures are created using the {@link #addKernel} or {@link #addInvoke}
432 * methods.
433 * When a closure is created, futures from previously created closures
434 * can be used as inputs.
435 * Unbound values can be used as inputs to create closures as well.
436 * An unbound value is created using the {@link #addInput} method.
437 * Unbound values become inputs to the script group to be created,
438 * in the order that they are added.
439 * A script group is created by a call to the {@link #create} method, which
440 * accepts an array of futures as the outputs for the script group.
441 * <p>
442 * Closures in a script group can be evaluated in any order as long as the
443 * following conditions are met.
444 * First, a closure must be evaluated before any other closures that take its
445 * futures as inputs.
446 * Second, all closures added before an invoke closure must be evaluated
447 * before it.
448 * Third, all closures added after an invoke closure must be evaluated after
449 * it.
450 * <p>
451 * As a special case, the order that the closures are added is a legal
452 * evaluation order. However, other evaluation orders are allowed, including
453 * concurrently evaluating independent closures.
454 */
455
Yang Nicc1ca482015-03-16 15:53:18 -0700456 public static final class Builder {
457 RenderScript mRS;
458 List<Closure> mClosures;
459 List<UnboundValue> mInputs;
Yang Nicc1ca482015-03-16 15:53:18 -0700460 private static final String TAG = "ScriptGroup2.Builder";
Yang Ni281c3252014-10-24 08:52:24 -0700461
Yang Ni4470d68a2015-03-16 16:23:50 -0700462 /**
463 * Returns a Builder object
464 *
465 * @param rs the RenderScript context
466 */
Yang Nicc1ca482015-03-16 15:53:18 -0700467 public Builder(RenderScript rs) {
468 mRS = rs;
469 mClosures = new ArrayList<Closure>();
470 mInputs = new ArrayList<UnboundValue>();
471 }
472
Yang Ni4470d68a2015-03-16 16:23:50 -0700473 /**
474 * Adds a closure for a kernel
475 *
476 * @param k Kernel ID for the kernel function
477 * @param returnType Allocation type for the return value
478 * @param args arguments to the kernel function
479 * @param globalBindings bindings for global variables
480 * @return a closure
481 */
482
Yang Nicc1ca482015-03-16 15:53:18 -0700483 public Closure addKernel(Script.KernelID k, Type returnType, Object[] args,
484 Map<Script.FieldID, Object> globalBindings) {
485 Closure c = new Closure(mRS, k, returnType, args, globalBindings);
486 mClosures.add(c);
487 return c;
488 }
489
Yang Ni4470d68a2015-03-16 16:23:50 -0700490 /**
491 * Adds a closure for an invocable function
492 *
493 * @param invoke Invoke ID for the invocable function
494 * @param args arguments to the invocable function
495 * @param globalBindings bindings for global variables
496 * @return a closure
497 */
498
Yang Nicc1ca482015-03-16 15:53:18 -0700499 public Closure addInvoke(Script.InvokeID invoke, Object[] args,
500 Map<Script.FieldID, Object> globalBindings) {
501 Closure c = new Closure(mRS, invoke, args, globalBindings);
502 mClosures.add(c);
503 return c;
504 }
505
Yang Ni4470d68a2015-03-16 16:23:50 -0700506 /**
507 * Adds a script group input
508 *
509 * @return a unbound value that can be used to create a closure
510 */
Yang Nicc1ca482015-03-16 15:53:18 -0700511 public UnboundValue addInput() {
512 UnboundValue unbound = new UnboundValue();
513 mInputs.add(unbound);
514 return unbound;
515 }
516
Yang Ni4470d68a2015-03-16 16:23:50 -0700517 /**
518 * Adds a closure for a kernel
519 *
520 * @param k Kernel ID for the kernel function
521 * @param argsAndBindings arguments followed by bindings for global variables
522 * @return a closure
523 */
524
Yang Ni8ff29802015-03-11 16:25:37 -0700525 public Closure addKernel(Script.KernelID k, Type returnType, Object... argsAndBindings) {
526 ArrayList<Object> args = new ArrayList<Object>();
527 Map<Script.FieldID, Object> bindingMap = new HashMap<Script.FieldID, Object>();
528 if (!seperateArgsAndBindings(argsAndBindings, args, bindingMap)) {
529 return null;
530 }
531 return addKernel(k, returnType, args.toArray(), bindingMap);
532 }
533
Yang Ni4470d68a2015-03-16 16:23:50 -0700534 /**
535 * Adds a closure for an invocable function
536 *
537 * @param invoke Invoke ID for the invocable function
538 * @param argsAndBindings arguments followed by bindings for global variables
539 * @return a closure
540 */
541
Yang Ni8ff29802015-03-11 16:25:37 -0700542 public Closure addInvoke(Script.InvokeID invoke, Object... argsAndBindings) {
543 ArrayList<Object> args = new ArrayList<Object>();
544 Map<Script.FieldID, Object> bindingMap = new HashMap<Script.FieldID, Object>();
545 if (!seperateArgsAndBindings(argsAndBindings, args, bindingMap)) {
546 return null;
547 }
548 return addInvoke(invoke, args.toArray(), bindingMap);
549 }
550
Yang Ni4470d68a2015-03-16 16:23:50 -0700551 /**
552 * Creates a script group
553 *
Yang Ni17098562015-04-06 16:58:57 -0700554 * @param name name for the script group. Legal names can only contain letters, digits,
555 * '-', or '_'. The name can be no longer than 100 characters.
Yang Ni4470d68a2015-03-16 16:23:50 -0700556 * @param outputs futures intended as outputs of the script group
557 * @return a script group
558 */
559
Yang Ni17098562015-04-06 16:58:57 -0700560 public ScriptGroup2 create(String name, Future... outputs) {
561 if (name == null || name.isEmpty() || name.length() > 100 ||
562 !name.equals(name.replaceAll("[^a-zA-Z0-9-]", "_"))) {
563 throw new RSIllegalArgumentException("invalid script group name");
564 }
Yang Ni35be56c2015-04-02 17:47:56 -0700565 ScriptGroup2 ret = new ScriptGroup2(mRS, name, mClosures, mInputs, outputs);
Yang Nicc1ca482015-03-16 15:53:18 -0700566 return ret;
567 }
568
Yang Ni8ff29802015-03-11 16:25:37 -0700569 private boolean seperateArgsAndBindings(Object[] argsAndBindings,
570 ArrayList<Object> args,
571 Map<Script.FieldID, Object> bindingMap) {
572 int i;
573 for (i = 0; i < argsAndBindings.length; i++) {
574 if (argsAndBindings[i] instanceof Binding) {
575 break;
576 }
577 args.add(argsAndBindings[i]);
578 }
579
580 for (; i < argsAndBindings.length; i++) {
581 if (!(argsAndBindings[i] instanceof Binding)) {
582 return false;
583 }
584 Binding b = (Binding)argsAndBindings[i];
Yang Ni4470d68a2015-03-16 16:23:50 -0700585 bindingMap.put(b.getField(), b.getValue());
Yang Ni8ff29802015-03-11 16:25:37 -0700586 }
587
588 return true;
589 }
590
Yang Nicc1ca482015-03-16 15:53:18 -0700591 }
Yang Ni281c3252014-10-24 08:52:24 -0700592}