Skip to content

Commit

Permalink
Handle processes with input minOccurs > 1.
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaub committed Sep 8, 2012
1 parent 61fca22 commit 19d31e1
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/main/java/org/geoscript/js/process/Process.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -109,7 +110,31 @@ public Process(Scriptable scope, InternationalString title,
@JSFunction
public Scriptable run(Scriptable inputsObj) {
Map<String, Object> inputsMap = jsObjectToMap(inputsObj);

// validate inputs
for (Entry<String, Parameter<?>> entry : inputs.entrySet()) {
String key = entry.getKey();
Parameter<?> param = entry.getValue();
if (param.minOccurs > 0) {
if (!inputsMap.containsKey(key)) {
throw ScriptRuntime.constructError("Error", "Missing required input: " + key);
}
if (param.minOccurs > 1) {
Object value = inputsMap.get(key);
if (!(value instanceof NativeArray)) {
throw ScriptRuntime.constructError("Error", "Expected an array of values for input: " + key);
} else {
int size = ((NativeArray) value).size();
if (size < param.minOccurs) {
throw ScriptRuntime.constructError("Error", "Not enough values for input: " + key);
}
}
}
}
}

Map<String, Object> outputsMap = process.execute(inputsMap, null);

Scriptable outputsObj = mapToJSObject(outputsMap);
return outputsObj;
}
Expand Down Expand Up @@ -164,15 +189,17 @@ private Parameter<?> createParameter(String name, Scriptable paramObj) {
}

Object minOccursObj = paramObj.get("minOccurs", paramObj);
int minOccurs = 1; // TODO: determine why -1 doesn't work here
int minOccurs = 1;
if (minOccursObj instanceof Number) {
minOccurs = (Integer) minOccursObj;
minOccurs = ((Number) minOccursObj).intValue();
minOccurs = minOccurs > -1 ? minOccurs : 0;
}

Object maxOccursObj = paramObj.get("maxOccurs", paramObj);
int maxOccurs = 1; // TODO: determine why -1 doesn't work here
int maxOccurs = minOccurs > 0 ? minOccurs : 1;
if (maxOccursObj instanceof Number) {
maxOccurs = (Integer) maxOccursObj;
maxOccurs = ((Number) maxOccursObj).intValue();
maxOccurs = maxOccurs >= minOccurs ? maxOccurs : Integer.MAX_VALUE;
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand Down
103 changes: 103 additions & 0 deletions src/test/resources/org/geoscript/js/tests/geoscript/test_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,88 @@ exports["test parameter binding to java.lang.Class"] = function() {

}

exports["test minOccurs"] = function() {

var process = new Process({
inputs: {
required: {type: "String", minOccurs: 1},
require2: {type: "String", minOccurs: 2},
optional: {type: "String", minOccurs: 0},
unspecified: {type: "String"}
},
outputs: {
result: {type: "Boolean"}
},
run: function(inputs) {
return {result: true};
}
});

ASSERT.strictEqual(process.inputs.required.minOccurs, 1);
ASSERT.strictEqual(process.inputs.require2.minOccurs, 2);
ASSERT.strictEqual(process.inputs.optional.minOccurs, 0);
ASSERT.strictEqual(process.inputs.unspecified.minOccurs, 1);

var inputs;

// valid inputs
inputs = {
required: "pass",
require2: ["one", "two"],
optional: "here",
unspecified: "here"
};

ASSERT.strictEqual(process.run(inputs).result, true, "minimums");

// missing required input
inputs = {
require2: ["one", "two"],
optional: "here",
unspecified: "here"
};

ASSERT.throws(function() {
process.run(inputs);
}, Error, "missing required input");

// not enough require2 input
inputs = {
required: "pass",
require2: "one",
optional: "here",
unspecified: "here"
};

ASSERT.throws(function() {
process.run(inputs);
}, Error, "too few require2 input");

// not enough require2 input (again)
inputs = {
required: "pass",
require2: ["one"],
optional: "here",
unspecified: "here"
};

ASSERT.throws(function() {
process.run(inputs);
}, Error, "too few require2 input (again)");

// missing unspecified input
inputs = {
required: "pass",
require2: ["one", "two"],
optional: "here"
};
ASSERT.throws(function() {
process.run(inputs);
}, Error, "missing unspecified input");


}

exports["test run"] = function() {
var add = new Process({
title: "Add process",
Expand Down Expand Up @@ -255,6 +337,27 @@ exports["test: Process.get('JTS:buffer')"] = function() {

}

exports["test: Process.get('JTS:union')"] = function() {

var union = Process.get("JTS:union");
var inputs = union.inputs;

ASSERT.strictEqual(typeof inputs, "object", "inputs object");
ASSERT.strictEqual(inputs.geom.type, "Geometry", "geom type");
ASSERT.strictEqual(inputs.geom.minOccurs, 2, "geom minOccurs");

var geom = require("geoscript/geom");
var poly1 = new geom.Point([0, 0]).buffer(1);
var poly2 = new geom.Point([0, 1]).buffer(1);

var outputs = union.run({geom: [poly1, poly2]});

ASSERT.ok(outputs.result instanceof geom.Polygon, "result polygon");
ASSERT.strictEqual(outputs.result.area.toFixed(3), "5.028", "correct area");

}


if (require.main == module.id) {
system.exit(require("test").run(exports));
}

0 comments on commit 19d31e1

Please sign in to comment.