Skip to content

Commit

Permalink
fix minor bugs in retrieving output from python dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanetteclark committed Sep 25, 2024
1 parent eba8b21 commit 39bb629
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
24 changes: 15 additions & 9 deletions src/main/java/edu/ucsb/nceas/mdqengine/dispatch/Dispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public Result dispatch(Map<String, Object> variables, String code) throws Script
Object out_py = null;
Object out_ids = null;
Object out_type = null;
Object out_status = null;
// do we have a result object from an R check?
try {
var_r = engine.get("mdq_result");
Expand Down Expand Up @@ -169,8 +170,8 @@ public Result dispatch(Map<String, Object> variables, String code) throws Script
}
// save the output
if (out_py != null && !out_py.toString().equals("<unbound>")) {
if (out_py instanceof ArrayList){

if (out_py instanceof ArrayList) {
ArrayList<Output> outputList = new ArrayList<>();

ArrayList<?> out_py_l = (ArrayList<?>) out_py;
Expand All @@ -179,32 +180,37 @@ public Result dispatch(Map<String, Object> variables, String code) throws Script

for (int i = 0; i < out_py_l.size(); i++) {
Output o = new Output(out_py_l.get(i).toString());

String id = out_ids_l.get(i).toString();
String type = out_type_l.get(i).toString();

o.setIdentifier(id);
o.setType(type);
outputList.add(o);
}
dr.setOutput(outputList);
dr.setOutput(outputList);
} else {
Output o = new Output(out_py.toString());
dr.setOutput(o);
}

}
// if we didn't get any "normal" output from python grab whatever got returned
if (out != null & out_py == null) {
Output o = new Output(out.toString());
dr.setOutput(o);
}
// try to get the global status variable from python
try {
out_py = engine.get("status");
out_status = engine.get("status");
} catch (Exception e) {
// catch this silently since we are just fishing
// the no result case is handled later
log.trace("No result found for python check variable variable status.");
}
// save the status
if (out_py != null && !out_py.toString().equals("<unbound>")) {
dr.setStatus(Status.valueOf(out_py.toString()));
if (out_status != null && !out_status.toString().equals("<unbound>")) {
dr.setStatus(Status.valueOf(out_status.toString()));
} else {
// if we haven't found anything at this point it probably failed
dr.setStatus(Status.FAILURE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public class PythonDispatcherTest {
public static void setupOnce() {
try {
Dispatcher.setupJep();
} catch (MetadigException me){
} catch (MetadigException me) {
fail("Setup failed with MetadigException: " + me.getMessage());
}
}

@Before
public void init(){
public void init() {
dispatcher = Dispatcher.getDispatcher("python");
}

Expand All @@ -48,7 +48,7 @@ public void testTypes() {

names.put("myInt", XMLDialect.retypeObject("2"));
names.put("myFloat", XMLDialect.retypeObject("1.5"));
names.put("myBool", XMLDialect.retypeObject("true"));
names.put("myBool", XMLDialect.retypeObject("True"));
names.put("myStr", XMLDialect.retypeObject("hello"));

String code = "output = (type(myInt) is int) and (type(myFloat) is float) and (type(myBool) is bool)";
Expand All @@ -60,7 +60,7 @@ public void testTypes() {
e.printStackTrace();
fail(e.getMessage());
}
assertEquals("true", result.getOutput().get(0).getValue());
assertEquals("True", result.getOutput().get(0).getValue());
}

@Test
Expand Down Expand Up @@ -92,7 +92,7 @@ public void testEquality() {
Map<String, Object> names = new HashMap<String, Object>();
names.put("x", 2);
names.put("y", 2);
String code = "x == y";
String code = "def call(): return(x == y)\n";
Result result = null;
try {
result = dispatcher.dispatch(names, code);
Expand Down Expand Up @@ -146,7 +146,8 @@ public void testCache() {
names.put("y", 2);
InputStream library = this.getClass().getResourceAsStream("/code/mdq-cache.py");

String code = "def call(): \n"
String code = "global output \n"
+ "def call(): \n"
+ " return get('" + dataUrl + "') \n";

Result result = null;
Expand All @@ -158,6 +159,7 @@ public void testCache() {
e.printStackTrace();
fail(e.getMessage());
}
result.getOutput();
// make sure the file is named as expected
assertTrue(result.getOutput().get(0).getValue().endsWith(DigestUtils.md5Hex(dataUrl)));
}
Expand Down

0 comments on commit 39bb629

Please sign in to comment.