Search in sources :

Example 1 with MathAssessBadCasCodeException

use of uk.ac.ed.ph.qtiworks.mathassess.glue.MathAssessBadCasCodeException 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;
}
Also used : QtiMaximaProcess(uk.ac.ed.ph.qtiworks.mathassess.glue.maxima.QtiMaximaProcess) ValueWrapper(uk.ac.ed.ph.qtiworks.mathassess.glue.types.ValueWrapper) MathsContentTooComplexException(uk.ac.ed.ph.qtiworks.mathassess.glue.MathsContentTooComplexException) AttributeList(uk.ac.ed.ph.jqtiplus.attribute.AttributeList) MathAssessBadCasCodeException(uk.ac.ed.ph.qtiworks.mathassess.glue.MathAssessBadCasCodeException) QtiMaximaTypeConversionException(uk.ac.ed.ph.qtiworks.mathassess.glue.maxima.QtiMaximaTypeConversionException) BooleanValue(uk.ac.ed.ph.jqtiplus.value.BooleanValue) Value(uk.ac.ed.ph.jqtiplus.value.Value) IntegerValue(uk.ac.ed.ph.jqtiplus.value.IntegerValue) FloatValue(uk.ac.ed.ph.jqtiplus.value.FloatValue) NullValue(uk.ac.ed.ph.jqtiplus.value.NullValue) MaximaTimeoutException(uk.ac.ed.ph.jacomax.MaximaTimeoutException)

Example 2 with MathAssessBadCasCodeException

use of uk.ac.ed.ph.qtiworks.mathassess.glue.MathAssessBadCasCodeException 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;
}
Also used : QtiMaximaProcess(uk.ac.ed.ph.qtiworks.mathassess.glue.maxima.QtiMaximaProcess) ValueWrapper(uk.ac.ed.ph.qtiworks.mathassess.glue.types.ValueWrapper) MathsContentTooComplexException(uk.ac.ed.ph.qtiworks.mathassess.glue.MathsContentTooComplexException) AttributeList(uk.ac.ed.ph.jqtiplus.attribute.AttributeList) MathAssessBadCasCodeException(uk.ac.ed.ph.qtiworks.mathassess.glue.MathAssessBadCasCodeException) QtiMaximaTypeConversionException(uk.ac.ed.ph.qtiworks.mathassess.glue.maxima.QtiMaximaTypeConversionException) BooleanValue(uk.ac.ed.ph.jqtiplus.value.BooleanValue) Value(uk.ac.ed.ph.jqtiplus.value.Value) IntegerValue(uk.ac.ed.ph.jqtiplus.value.IntegerValue) FloatValue(uk.ac.ed.ph.jqtiplus.value.FloatValue) NullValue(uk.ac.ed.ph.jqtiplus.value.NullValue) MaximaTimeoutException(uk.ac.ed.ph.jacomax.MaximaTimeoutException)

Aggregations

MaximaTimeoutException (uk.ac.ed.ph.jacomax.MaximaTimeoutException)2 AttributeList (uk.ac.ed.ph.jqtiplus.attribute.AttributeList)2 BooleanValue (uk.ac.ed.ph.jqtiplus.value.BooleanValue)2 FloatValue (uk.ac.ed.ph.jqtiplus.value.FloatValue)2 IntegerValue (uk.ac.ed.ph.jqtiplus.value.IntegerValue)2 NullValue (uk.ac.ed.ph.jqtiplus.value.NullValue)2 Value (uk.ac.ed.ph.jqtiplus.value.Value)2 MathAssessBadCasCodeException (uk.ac.ed.ph.qtiworks.mathassess.glue.MathAssessBadCasCodeException)2 MathsContentTooComplexException (uk.ac.ed.ph.qtiworks.mathassess.glue.MathsContentTooComplexException)2 QtiMaximaProcess (uk.ac.ed.ph.qtiworks.mathassess.glue.maxima.QtiMaximaProcess)2 QtiMaximaTypeConversionException (uk.ac.ed.ph.qtiworks.mathassess.glue.maxima.QtiMaximaTypeConversionException)2 ValueWrapper (uk.ac.ed.ph.qtiworks.mathassess.glue.types.ValueWrapper)2