Search in sources :

Example 1 with TextOutputHandler

use of nars.io.events.TextOutputHandler in project opennars by opennars.

the class NALTest method testNAL.

protected double testNAL(final String path) {
    Memory.resetStatic();
    final List<OutputCondition> expects = new ArrayList();
    NAR n = null;
    boolean error = false;
    try {
        n = newNAR();
        String example = getExample(path);
        if (showOutput) {
            System.out.println(example);
            System.out.println();
        }
        List<OutputCondition> extractedExpects = OutputCondition.getConditions(n, example, similarsToSave);
        for (OutputCondition e1 : extractedExpects) expects.add(e1);
        if (showOutput)
            new TextOutputHandler(n, System.out);
        if (showTrace) {
            new InferenceLogger(n, System.out);
        }
        n.addInputFile(path);
        n.cycles(minCycles);
    } catch (Throwable e) {
        System.err.println(e);
        if (Parameters.DEBUG) {
            e.printStackTrace();
        }
        error = true;
    }
    System.err.flush();
    System.out.flush();
    boolean success = expects.size() > 0 && (!error);
    for (OutputCondition e : expects) {
        if (!e.succeeded)
            success = false;
    }
    double score = Double.POSITIVE_INFINITY;
    if (success) {
        long lastSuccess = -1;
        for (OutputCondition e : expects) {
            if (e.getTrueTime() != -1) {
                if (lastSuccess < e.getTrueTime())
                    lastSuccess = e.getTrueTime();
            }
        }
        if (lastSuccess != -1) {
            // score = 1.0 + 1.0 / (1+lastSuccess);
            score = lastSuccess;
            scores.put(path, score);
        }
    } else {
        scores.put(path, Double.POSITIVE_INFINITY);
    }
    if ((!success & showFail) || (success && showSuccess)) {
        System.err.println('\n' + path + " @" + n.memory.time());
        for (OutputCondition e : expects) {
            System.err.println("  " + e);
        }
    }
    // System.err.println("Status: " + success + " total=" + expects.size() + " " + expects);
    if (requireSuccess)
        assertTrue(path, success);
    return score;
}
Also used : InferenceLogger(nars.gui.InferenceLogger) OutputCondition(nars.lab.testutils.OutputCondition) ArrayList(java.util.ArrayList) TextOutputHandler(nars.io.events.TextOutputHandler) NAR(nars.main.NAR)

Example 2 with TextOutputHandler

use of nars.io.events.TextOutputHandler in project opennars by opennars.

the class TestMultistepEdited method testMultistepEndState.

@Test
public void testMultistepEndState() {
    NAR n = new NAR();
    n.addInputFile("nal/Examples/Example-MultiStep-edited.txt");
    new TextOutputHandler(n, System.out);
    /*InferenceLogger logger = new InferenceLogger(System.out);
        n.memory.setRecorder(logger);*/
    n.cycles(1000);
// System.out.println(n.memory.concepts);
}
Also used : TextOutputHandler(nars.io.events.TextOutputHandler) NAR(nars.main.NAR) Test(org.junit.Test)

Example 3 with TextOutputHandler

use of nars.io.events.TextOutputHandler in project opennars by opennars.

the class Shell method run.

/**
 * non-static equivalent to {@link #main(String[])} : finish to completion from
 * an addInput file
 */
public void run(String[] args) {
    TextOutputHandler output = new TextOutputHandler(nar, new PrintWriter(out, true));
    output.setErrors(true);
    output.setErrorStackTrace(true);
    InputThread it;
    int sleep = -1;
    boolean noFile = false;
    if (args.length > 0) {
        try {
            nar.addInputFile(args[0]);
        } catch (Exception ex) {
            noFile = true;
            // Integer.valueOf(args[0]);
            sleep = Integer.valueOf(args[0]);
        // System.err.println("NARRun.init: " + ex);
        }
    }
    if (args.length == 0 || noFile) {
        it = new InputThread(System.in, nar);
        it.start();
    // nar.addInput(new TextInput(new BufferedReader(new InputStreamReader(System.in))));
    }
    while (true) {
        if (logging)
            log("NARSBatch.run():" + " step " + nar.time());
        nar.cycles(1);
        try {
            if (sleep > -1) {
                Thread.sleep(sleep);
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(Shell.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (logging)
            log("NARSBatch.run(): after tick" + " step " + nar.time());
        if (maxTime > 0) {
            if (nar.time() == maxTime) {
                break;
            }
        }
    }
    System.exit(0);
}
Also used : TextOutputHandler(nars.io.events.TextOutputHandler) PrintWriter(java.io.PrintWriter)

Aggregations

TextOutputHandler (nars.io.events.TextOutputHandler)3 NAR (nars.main.NAR)2 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 InferenceLogger (nars.gui.InferenceLogger)1 OutputCondition (nars.lab.testutils.OutputCondition)1 Test (org.junit.Test)1