Search in sources :

Example 1 with InvalidTheoryException

use of alice.tuprolog.InvalidTheoryException in project Osmand by osmandapp.

the class AbstractPrologCommandPlayer method init.

private void init(String voiceProvider, OsmandSettings settings, String configFile) throws CommandPlayerException {
    prologSystem.clearTheory();
    voiceDir = null;
    if (voiceProvider != null) {
        File parent = ctx.getAppPath(IndexConstants.VOICE_INDEX_DIR);
        voiceDir = new File(parent, voiceProvider);
        if (!voiceDir.exists()) {
            voiceDir = null;
            throw new CommandPlayerException(ctx.getString(R.string.voice_data_unavailable));
        }
    }
    // voiceZipFile = null;
    if (voiceDir != null) {
        long time = System.currentTimeMillis();
        boolean wrong = false;
        try {
            InputStream config;
            // if (voiceDir.getName().endsWith(".zip")) { //$NON-NLS-1$
            // voiceZipFile = new ZipFile(voiceDir);
            // config = voiceZipFile.getInputStream(voiceZipFile.getEntry("_config.p")); //$NON-NLS-1$
            // } else {
            // $NON-NLS-1$
            config = new FileInputStream(new File(voiceDir, configFile));
            // }
            MetricsConstants mc = settings.METRIC_SYSTEM.get();
            ApplicationMode m = settings.getApplicationMode();
            if (m.getParent() != null) {
                m = m.getParent();
            }
            settings.APPLICATION_MODE.addListener(this);
            prologSystem.getTheoryManager().assertA(new Struct("appMode", new Struct(ctx.getSettings().APPLICATION_MODE.get().getStringKey().toLowerCase())), true, "", true);
            prologSystem.addTheory(new Theory("measure('" + mc.toTTSString() + "')."));
            prologSystem.addTheory(new Theory(config));
            config.close();
        } catch (InvalidTheoryException e) {
            // $NON-NLS-1$
            log.error("Loading voice config exception " + voiceProvider, e);
            wrong = true;
        } catch (IOException e) {
            // $NON-NLS-1$
            log.error("Loading voice config exception " + voiceProvider, e);
            wrong = true;
        }
        if (wrong) {
            throw new CommandPlayerException(ctx.getString(R.string.voice_data_corrupted));
        } else {
            Term val = solveSimplePredicate(P_VERSION);
            if (!(val instanceof Number) || Arrays.binarySearch(sortedVoiceVersions, ((Number) val).intValue()) < 0) {
                throw new CommandPlayerException(ctx.getString(R.string.voice_data_not_supported));
            }
            currentVersion = ((Number) val).intValue();
        }
        if (log.isInfoEnabled()) {
            // $NON-NLS-1$ //$NON-NLS-2$
            log.info("Initializing voice subsystem  " + voiceProvider + " : " + (System.currentTimeMillis() - time));
        }
    }
}
Also used : Theory(alice.tuprolog.Theory) InvalidTheoryException(alice.tuprolog.InvalidTheoryException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ApplicationMode(net.osmand.plus.ApplicationMode) IOException(java.io.IOException) Term(alice.tuprolog.Term) FileInputStream(java.io.FileInputStream) Struct(alice.tuprolog.Struct) Number(alice.tuprolog.Number) MetricsConstants(net.osmand.plus.OsmandSettings.MetricsConstants) File(java.io.File)

Example 2 with InvalidTheoryException

use of alice.tuprolog.InvalidTheoryException in project narchy by automenta.

the class ProNALTest method test1.

@Test
public void test1() throws InvalidTheoryException, Narsese.NarseseException, IOException, URISyntaxException {
    // Theory t = Theory.string(
    // "add(0,X,X).",
    // "add(s(X),Y,s(Z)):-add(X,Y,Z).\n",
    // "goal(R):-add(s(s(0)),s(s(0)),R)."
    // );
    Theory t = Theory.resource("../../../resources/prolog/furniture.pl");
    Param.DEBUG = true;
    NAR n = NARS.tmp(6);
    n.questPriDefault.set(1f);
    n.beliefPriDefault.set(0.5f);
    // n.log();
    Set<String> answers = new TreeSet();
    for (nars.term.Term xx : PrologToNAL.N(t)) {
        if (Op.functor(xx, (xt) -> xt.equals(PrologToNAL.QUESTION_GOAL) ? xt : null) != null) {
            Term qTerm = Operator.args(xx).sub(0).normalize();
            // n.question(q);
            n.question(qTerm, ETERNAL, (q, a) -> {
                if (answers.add(a.term().toString())) {
                    System.err.println(q + " " + a);
                    System.err.println(a.proof());
                }
            /*else {
                        System.err.println("dup");
                    }*/
            });
        } else {
            n.believe(xx.normalize());
        }
    }
    n.run(2500);
    // ?- made_of(your_chair,X), colour(X,Colour).
    assertTrue(answers.contains("(colour(wood,brown)&&made_of(your_chair,wood))"));
// n.concepts().forEach(c -> {
// c.print();
// });
/*
        [0] *** ANSWER=goal(s(s(s(s(0)))))
        TOTAL ANSWERS=1
        */
}
Also used : nars(nars) PrologToNAL(nars.op.prolog.PrologToNAL) URISyntaxException(java.net.URISyntaxException) Set(java.util.Set) IOException(java.io.IOException) InvalidTheoryException(alice.tuprolog.InvalidTheoryException) ETERNAL(nars.time.Tense.ETERNAL) Theory(alice.tuprolog.Theory) TreeSet(java.util.TreeSet) Test(org.junit.jupiter.api.Test) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Operator(nars.concept.Operator) Term(nars.term.Term) Theory(alice.tuprolog.Theory) TreeSet(java.util.TreeSet) Term(nars.term.Term) Term(nars.term.Term) Test(org.junit.jupiter.api.Test)

Example 3 with InvalidTheoryException

use of alice.tuprolog.InvalidTheoryException in project narchy by automenta.

the class PrologCoreTest method testPrologShell.

@Test
public void testPrologShell() throws MalformedGoalException {
    NAR n = NARS.tmp();
    PrologCore p = new PrologCore(n);
    try {
        p.input(new Theory(PrologCoreTest.class.getClassLoader().getResource("shell.prolog").openStream()));
    } catch (InvalidTheoryException | IOException e) {
        e.printStackTrace();
    }
    p.solve("do(help).");
}
Also used : PrologCore(nars.op.prolog.PrologCore) Theory(alice.tuprolog.Theory) InvalidTheoryException(alice.tuprolog.InvalidTheoryException) IOException(java.io.IOException) NAR(nars.NAR) Test(org.junit.jupiter.api.Test)

Aggregations

InvalidTheoryException (alice.tuprolog.InvalidTheoryException)3 Theory (alice.tuprolog.Theory)3 IOException (java.io.IOException)3 Test (org.junit.jupiter.api.Test)2 Number (alice.tuprolog.Number)1 Struct (alice.tuprolog.Struct)1 Term (alice.tuprolog.Term)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 nars (nars)1 NAR (nars.NAR)1 Operator (nars.concept.Operator)1 PrologCore (nars.op.prolog.PrologCore)1 PrologToNAL (nars.op.prolog.PrologToNAL)1 Term (nars.term.Term)1 ETERNAL (nars.time.Tense.ETERNAL)1