Search in sources :

Example 1 with QtiMaximaProcess

use of uk.ac.ed.ph.qtiworks.mathassess.glue.maxima.QtiMaximaProcess in project OpenOLAT by OpenOLAT.

the class OpenOLATExtensionPackage method obtainMaximaSessionForThread.

// ------------------------------------------------------------------------
public QtiMaximaProcess obtainMaximaSessionForThread() {
    QtiMaximaProcess maximaSession = sessionThreadLocal.get();
    if (maximaSession == null) {
        if (qtiMaximaProcessPoolManager != null) {
            log.debug("Obtaining new maxima process from pool for this request");
            /* Need to get a new process from pool */
            maximaSession = qtiMaximaProcessPoolManager.obtainProcess();
            sessionThreadLocal.set(maximaSession);
        } else {
            throw new QtiLogicException("The MathAssess extensions package could not be configured to communicate with Maxima. This package should not have been used in this case");
        }
    }
    return maximaSession;
}
Also used : QtiMaximaProcess(uk.ac.ed.ph.qtiworks.mathassess.glue.maxima.QtiMaximaProcess) QtiLogicException(uk.ac.ed.ph.jqtiplus.exception.QtiLogicException)

Example 2 with QtiMaximaProcess

use of uk.ac.ed.ph.qtiworks.mathassess.glue.maxima.QtiMaximaProcess in project openolat by klemens.

the class OpenOLATExtensionPackage method releaseMaximaSessionForThread.

private void releaseMaximaSessionForThread() {
    final QtiMaximaProcess maximaSession = sessionThreadLocal.get();
    if (maximaSession != null && qtiMaximaProcessPoolManager != null) {
        log.debug("Finished with maxima process for this request - returning to pool");
        qtiMaximaProcessPoolManager.returnProcess(maximaSession);
        sessionThreadLocal.set(null);
    }
}
Also used : QtiMaximaProcess(uk.ac.ed.ph.qtiworks.mathassess.glue.maxima.QtiMaximaProcess)

Example 3 with QtiMaximaProcess

use of uk.ac.ed.ph.qtiworks.mathassess.glue.maxima.QtiMaximaProcess in project OpenOLAT by OpenOLAT.

the class OpenOLATExtensionPackage method releaseMaximaSessionForThread.

private void releaseMaximaSessionForThread() {
    final QtiMaximaProcess maximaSession = sessionThreadLocal.get();
    if (maximaSession != null && qtiMaximaProcessPoolManager != null) {
        log.debug("Finished with maxima process for this request - returning to pool");
        qtiMaximaProcessPoolManager.returnProcess(maximaSession);
        sessionThreadLocal.set(null);
    }
}
Also used : QtiMaximaProcess(uk.ac.ed.ph.qtiworks.mathassess.glue.maxima.QtiMaximaProcess)

Example 4 with QtiMaximaProcess

use of uk.ac.ed.ph.qtiworks.mathassess.glue.maxima.QtiMaximaProcess 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 5 with QtiMaximaProcess

use of uk.ac.ed.ph.qtiworks.mathassess.glue.maxima.QtiMaximaProcess in project openolat by klemens.

the class OpenOLATExtensionPackage method obtainMaximaSessionForThread.

// ------------------------------------------------------------------------
public QtiMaximaProcess obtainMaximaSessionForThread() {
    QtiMaximaProcess maximaSession = sessionThreadLocal.get();
    if (maximaSession == null) {
        if (qtiMaximaProcessPoolManager != null) {
            log.debug("Obtaining new maxima process from pool for this request");
            /* Need to get a new process from pool */
            maximaSession = qtiMaximaProcessPoolManager.obtainProcess();
            sessionThreadLocal.set(maximaSession);
        } else {
            throw new QtiLogicException("The MathAssess extensions package could not be configured to communicate with Maxima. This package should not have been used in this case");
        }
    }
    return maximaSession;
}
Also used : QtiMaximaProcess(uk.ac.ed.ph.qtiworks.mathassess.glue.maxima.QtiMaximaProcess) QtiLogicException(uk.ac.ed.ph.jqtiplus.exception.QtiLogicException)

Aggregations

QtiMaximaProcess (uk.ac.ed.ph.qtiworks.mathassess.glue.maxima.QtiMaximaProcess)6 MaximaTimeoutException (uk.ac.ed.ph.jacomax.MaximaTimeoutException)2 AttributeList (uk.ac.ed.ph.jqtiplus.attribute.AttributeList)2 QtiLogicException (uk.ac.ed.ph.jqtiplus.exception.QtiLogicException)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 QtiMaximaTypeConversionException (uk.ac.ed.ph.qtiworks.mathassess.glue.maxima.QtiMaximaTypeConversionException)2 ValueWrapper (uk.ac.ed.ph.qtiworks.mathassess.glue.types.ValueWrapper)2