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");
}
}
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;
}
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;
}
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;
}
Aggregations