Search in sources :

Example 6 with Struct

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

the class PrologToNAL method N.

private static nars.term.Term N(alice.tuprolog.Term t) {
    if (t instanceof alice.tuprolog.Term) {
        Struct s = (Struct) t;
        String name = s.name();
        switch(name) {
            case ":-":
                assert (s.subs() == 2);
                // reverse, prolog is backwards
                nars.term.Term pre = N(s.sub(1));
                nars.term.Term post = N(s.sub(0));
                // convert to implication first, then promote variables on the resulting pre/post
                Term impl = $.impl(pre, post);
                pre = impl.sub(0);
                post = impl.sub(1);
                if (pre.varQuery() > 0 && post.varQuery() > 0) {
                    MutableSet<nars.term.var.Variable> prev = new UnifiedSet();
                    pre.recurseTerms(Termlike::hasVarQuery, (a) -> {
                        if (a.op() == Op.VAR_QUERY)
                            prev.add((Variable) a);
                        return true;
                    }, null);
                    MutableSet<nars.term.var.Variable> posv = new UnifiedSet();
                    post.recurseTerms(Termlike::hasVarQuery, (a) -> {
                        if (a.op() == Op.VAR_QUERY)
                            posv.add((Variable) a);
                        return true;
                    }, null);
                    MutableSet<nars.term.var.Variable> common = prev.intersect(posv);
                    int cs = common.size();
                    if (cs > 0) {
                        Map<nars.term.Term, nars.term.Term> x = new UnifiedMap(cs);
                        for (nars.term.var.Variable c : common) {
                            x.put(c, $.varIndep(c.toString().substring(1)));
                        }
                        impl = impl.replace(x);
                    }
                }
                return impl;
            case ",":
                return CONJ.the(N(s.sub(0)), N(s.sub(1)));
            default:
                nars.term.Term atom = $.the(name);
                int arity = s.subs();
                if (arity == 0) {
                    return atom;
                } else {
                    return $.inh($.p((nars.term.Term[]) Util.map(0, arity, i -> N(s.sub(i)), nars.term.Term[]::new)), atom);
                }
        }
    } else if (t instanceof Var) {
        return $.varQuery(((Var) t).name());
    // throw new RuntimeException(t + " untranslated");
    } else if (t instanceof NumberTerm.Int) {
        return $.the(((NumberTerm.Int) t).intValue());
    } else {
        throw new TODO(t + " (" + t.getClass() + ") untranslatable");
    }
}
Also used : Struct(alice.tuprolog.Struct) NumberTerm(alice.tuprolog.NumberTerm) Iterables(com.google.common.collect.Iterables) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) CONJ(nars.Op.CONJ) Var(alice.tuprolog.Var) nars.$(nars.$) Theory(alice.tuprolog.Theory) Util(jcog.Util) Variable(nars.term.var.Variable) MutableSet(org.eclipse.collections.api.set.MutableSet) TODO(jcog.TODO) Op(nars.Op) Termlike(nars.term.Termlike) Map(java.util.Map) Term(nars.term.Term) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet) TODO(jcog.TODO) Variable(nars.term.var.Variable) NumberTerm(alice.tuprolog.NumberTerm) UnifiedMap(org.eclipse.collections.impl.map.mutable.UnifiedMap) Var(alice.tuprolog.Var) NumberTerm(alice.tuprolog.NumberTerm) Term(nars.term.Term) Struct(alice.tuprolog.Struct) Termlike(nars.term.Termlike) Variable(nars.term.var.Variable) UnifiedSet(org.eclipse.collections.impl.set.mutable.UnifiedSet) Term(nars.term.Term)

Example 7 with Struct

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

the class AbstractPrologCommandPlayer method execute.

@Override
public List<String> execute(List<Struct> listCmd) {
    Struct list = new Struct(listCmd.toArray(new Term[listCmd.size()]));
    // $NON-NLS-1$
    Var result = new Var("RESULT");
    List<String> files = new ArrayList<String>();
    if (prologSystem == null) {
        return files;
    }
    if (log.isInfoEnabled()) {
        log.info("Query speak files " + listCmd);
    }
    SolveInfo res = prologSystem.solve(new Struct(P_RESOLVE, list, result));
    if (res.isSuccess()) {
        try {
            prologSystem.solveEnd();
            Term solution = res.getVarValue(result.getName());
            Iterator<?> listIterator = ((Struct) solution).listIterator();
            while (listIterator.hasNext()) {
                Object term = listIterator.next();
                if (term instanceof Struct) {
                    files.add(((Struct) term).getName());
                }
            }
        } catch (NoSolutionException e) {
        }
    }
    if (log.isInfoEnabled()) {
        log.info("Speak files " + files);
    }
    return files;
}
Also used : NoSolutionException(alice.tuprolog.NoSolutionException) Var(alice.tuprolog.Var) ArrayList(java.util.ArrayList) Term(alice.tuprolog.Term) SolveInfo(alice.tuprolog.SolveInfo) Struct(alice.tuprolog.Struct)

Example 8 with Struct

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

the class AbstractPrologCommandPlayer method solveSimplePredicate.

protected Term solveSimplePredicate(String predicate) {
    Term val = null;
    // $NON-NLS-1$
    Var v = new Var("MyVariable");
    SolveInfo s = prologSystem.solve(new Struct(predicate, v));
    if (s.isSuccess()) {
        prologSystem.solveEnd();
        try {
            val = s.getVarValue(v.getName());
        } catch (NoSolutionException e) {
        }
    }
    return val;
}
Also used : NoSolutionException(alice.tuprolog.NoSolutionException) Var(alice.tuprolog.Var) Term(alice.tuprolog.Term) SolveInfo(alice.tuprolog.SolveInfo) Struct(alice.tuprolog.Struct)

Example 9 with Struct

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

the class CommandBuilder method addCommand.

private CommandBuilder addCommand(String name, Object... args) {
    Struct struct = prepareStruct(name, args);
    listStruct.add(struct);
    return this;
}
Also used : Struct(alice.tuprolog.Struct)

Aggregations

Struct (alice.tuprolog.Struct)9 Term (alice.tuprolog.Term)6 Var (alice.tuprolog.Var)4 NoSolutionException (alice.tuprolog.NoSolutionException)2 SolveInfo (alice.tuprolog.SolveInfo)2 Theory (alice.tuprolog.Theory)2 InvalidTheoryException (alice.tuprolog.InvalidTheoryException)1 Number (alice.tuprolog.Number)1 NumberTerm (alice.tuprolog.NumberTerm)1 Iterables (com.google.common.collect.Iterables)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 TODO (jcog.TODO)1 Util (jcog.Util)1 nars.$ (nars.$)1 Op (nars.Op)1