Cognitive Tutor Authoring Tools 2.6 > Using the Tools > Generalizing an example-tracing tutor with formulas > Writing your own function

5.6. Writing your own function

If you have some experience with Java and you have an idea for a matching function, you can implement it and access it from CTAT. To do so, create a new Java class that is a member of the CTAT Functions package:

package edu.cmu.pact.BehaviorRecorder.ProblemModel.Matcher.Functions;

Your new class should have a constructor that returns the result of the function.

Below is the content of the sum class:

package edu.cmu.pact.BehaviorRecorder.ProblemModel.Matcher.Functions;

public class sum {
    /**
     *  Add all values and return the result.
     */
    public Double sum(double... values) {
        double total = 0;

        for (double value : values)
            total += value;

        return new Double(total);
    }
}

Return type and arguments

The constructor of your function class must return one of the following Object types:

  • String

  • Boolean

  • Double

The arguments to the constructor must be of the following primitive types:

  • string

  • double

  • int

Packaging the function class

To make your function accessible to CTAT, add the compiled class to the archive DorminWidgets.jar (located in {CTAT}/lib) in the path specified in the package statement.

In the example below, the jar command is used to add a new function class, located in the directory structure matching the package, to DorminWidgets.jar:

/cygdrive/d/apps/CTAT
$ jar uf  \
     lib/DorminWidgets.jar  \
     edu/cmu/pact/BehaviorRecorder/ProblemModel/Matcher/Functions/function.class

We can then access this function from the Edit Student Input Matching dialog by specifying the function name:

function(argument1, argument2)