use of uk.ac.ed.ph.qtiworks.mathassess.glue.types.ValueWrapper in project OpenOLAT by OpenOLAT.
the class MaximaOperator method evaluateSelf.
@Override
protected Value evaluateSelf(OpenOLATExtensionPackage jqtiExtensionPackage, ProcessingContext context, Value[] childValues, int depth) {
AttributeList attributes = getAttributes();
Attribute<?> attrValue = attributes.get("value");
String code = (String) attrValue.getValue();
log.debug("Performing scriptRule: code={}, simplify={}" + code);
/* Pass variables to Maxima */
final QtiMaximaProcess qtiMaximaProcess = jqtiExtensionPackage.obtainMaximaSessionForThread();
for (int i = 0; i < childValues.length; i++) {
Value childValue = childValues[i];
String val = getValue(childValue);
code = code.replace("$(" + (i + 1) + ")", val);
}
/* Run code */
log.debug("Executing scriptRule code");
try {
qtiMaximaProcess.executeScriptRule(code, true);
} catch (final MaximaTimeoutException e) {
context.fireRuntimeError(this, "A timeout occurred executing the ScriptRule logic. Not setting QTI variables and returing FALSE");
return BooleanValue.FALSE;
} catch (final RuntimeException e) {
context.fireRuntimeError(this, "An unexpected problem occurred while trying to run the scriptRule logic. Not setting QTI variables and returing FALSE");
return BooleanValue.FALSE;
}
/* Read variables back */
log.debug("Reading variables back from Maxima");
/* Run Maxima code and extract result */
log.info("Running code to determine result of MAXIMA operator");
final Class<? extends ValueWrapper> resultClass = GlueValueBinder.getCasReturnClass(ReturnTypeType.FLOAT);
ValueWrapper maximaResult;
try {
maximaResult = qtiMaximaProcess.executeCasProcess(code, true, resultClass);
} catch (final MaximaTimeoutException e) {
context.fireRuntimeError(this, "A timeout occurred executing the CasCondition logic. Returning NULL");
return NullValue.INSTANCE;
} catch (final MathsContentTooComplexException e) {
context.fireRuntimeError(this, "An unexpected problem occurred querying the result of CasProcess, so returning NULL");
return NullValue.INSTANCE;
} catch (final MathAssessBadCasCodeException e) {
context.fireRuntimeError(this, "Your CasProcess code did not work as expected. The CAS input was '" + e.getMaximaInput() + "' and the CAS output was '" + e.getMaximaOutput() + "'. The failure reason was: " + e.getReason());
return NullValue.INSTANCE;
} catch (final QtiMaximaTypeConversionException e) {
context.fireRuntimeError(this, "Your CasProcess code did not produce a result that could be converted into the required QTI type. The CAS input was '" + e.getMaximaInput() + "' and the CAS output was '" + e.getMaximaOutput() + "'");
return NullValue.INSTANCE;
} catch (final RuntimeException e) {
log.warn("Unexpected Maxima failure", e);
context.fireRuntimeError(this, "An unexpected problem occurred while executing this CasProcess");
return BooleanValue.FALSE;
}
/* Bind result */
Value result = GlueValueBinder.casToJqti(maximaResult);
if (result == null) {
context.fireRuntimeError(this, "Failed to convert result from Maxima back to a QTI variable - returning NULL");
return NullValue.INSTANCE;
}
return result;
}
use of uk.ac.ed.ph.qtiworks.mathassess.glue.types.ValueWrapper in project openolat by klemens.
the class MaximaOperator method evaluateSelf.
@Override
protected Value evaluateSelf(OpenOLATExtensionPackage jqtiExtensionPackage, ProcessingContext context, Value[] childValues, int depth) {
AttributeList attributes = getAttributes();
Attribute<?> attrValue = attributes.get("value");
String code = (String) attrValue.getValue();
log.debug("Performing scriptRule: code={}, simplify={}" + code);
/* Pass variables to Maxima */
final QtiMaximaProcess qtiMaximaProcess = jqtiExtensionPackage.obtainMaximaSessionForThread();
for (int i = 0; i < childValues.length; i++) {
Value childValue = childValues[i];
String val = getValue(childValue);
code = code.replace("$(" + (i + 1) + ")", val);
}
/* Run code */
log.debug("Executing scriptRule code");
try {
qtiMaximaProcess.executeScriptRule(code, true);
} catch (final MaximaTimeoutException e) {
context.fireRuntimeError(this, "A timeout occurred executing the ScriptRule logic. Not setting QTI variables and returing FALSE");
return BooleanValue.FALSE;
} catch (final RuntimeException e) {
context.fireRuntimeError(this, "An unexpected problem occurred while trying to run the scriptRule logic. Not setting QTI variables and returing FALSE");
return BooleanValue.FALSE;
}
/* Read variables back */
log.debug("Reading variables back from Maxima");
/* Run Maxima code and extract result */
log.info("Running code to determine result of MAXIMA operator");
final Class<? extends ValueWrapper> resultClass = GlueValueBinder.getCasReturnClass(ReturnTypeType.FLOAT);
ValueWrapper maximaResult;
try {
maximaResult = qtiMaximaProcess.executeCasProcess(code, true, resultClass);
} catch (final MaximaTimeoutException e) {
context.fireRuntimeError(this, "A timeout occurred executing the CasCondition logic. Returning NULL");
return NullValue.INSTANCE;
} catch (final MathsContentTooComplexException e) {
context.fireRuntimeError(this, "An unexpected problem occurred querying the result of CasProcess, so returning NULL");
return NullValue.INSTANCE;
} catch (final MathAssessBadCasCodeException e) {
context.fireRuntimeError(this, "Your CasProcess code did not work as expected. The CAS input was '" + e.getMaximaInput() + "' and the CAS output was '" + e.getMaximaOutput() + "'. The failure reason was: " + e.getReason());
return NullValue.INSTANCE;
} catch (final QtiMaximaTypeConversionException e) {
context.fireRuntimeError(this, "Your CasProcess code did not produce a result that could be converted into the required QTI type. The CAS input was '" + e.getMaximaInput() + "' and the CAS output was '" + e.getMaximaOutput() + "'");
return NullValue.INSTANCE;
} catch (final RuntimeException e) {
log.warn("Unexpected Maxima failure", e);
context.fireRuntimeError(this, "An unexpected problem occurred while executing this CasProcess");
return BooleanValue.FALSE;
}
/* Bind result */
Value result = GlueValueBinder.casToJqti(maximaResult);
if (result == null) {
context.fireRuntimeError(this, "Failed to convert result from Maxima back to a QTI variable - returning NULL");
return NullValue.INSTANCE;
}
return result;
}
Aggregations