Search in sources :

Example 1 with UnifySubst

use of nars.term.subst.UnifySubst in project narchy by automenta.

the class Premise method match.

/**
 * resolve the most relevant belief of a given term/concept
 * <p>
 * patham9 project-eternalize
 * patham9 depending on 4 cases
 * patham9 https://github.com/opennars/opennars2/blob/a143162a559e55c456381a95530d00fee57037c4/src/nal/deriver/projection_eternalization.clj
 * sseehh__ ok ill add that in a bit
 * patham9 you need  project-eternalize-to
 * sseehh__ btw i disabled immediate eternalization entirely
 * patham9 so https://github.com/opennars/opennars2/blob/a143162a559e55c456381a95530d00fee57037c4/src/nal/deriver/projection_eternalization.clj#L31
 * patham9 especially try to understand the "temporal temporal" case
 * patham9 its using the result of higher confidence
 * <p>
 * returns ttl used, -1 if failed before starting
 *
 * @param matchTime - temporal focus control: determines when a matching belief or answer should be projected to
 */
@Nullable
public Derivation match(Derivation d, long[] focus, int matchTTL) {
    NAR n = d.nar;
    if (task == null || task.isDeleted()) {
        // }
        return null;
    }
    // n.conceptualize(task.term(), (c)->{});
    int dur = d.dur;
    Term beliefTerm = term();
    Term taskTerm = task.term();
    Term taskConcept = task.term().concept();
    final boolean[] beliefConceptCanAnswerTaskConcept = { false };
    boolean unifiedBelief = false;
    Op to = taskTerm.op();
    Op bo = beliefTerm.op();
    if (to == bo) {
        if (taskConcept.equals(beliefTerm.concept())) {
            beliefConceptCanAnswerTaskConcept[0] = true;
        } else {
            if ((bo.conceptualizable) && (beliefTerm.hasAny(var) || taskTerm.hasAny(var))) {
                Term _beliefTerm = beliefTerm;
                final Term[] unifiedBeliefTerm = new Term[] { null };
                UnifySubst u = new UnifySubst(/*null*/
                VAR_QUERY, n, (y) -> {
                    if (y.op().conceptualizable) {
                        y = y.normalize();
                        beliefConceptCanAnswerTaskConcept[0] = true;
                        if (!y.equals(_beliefTerm)) {
                            unifiedBeliefTerm[0] = y;
                            // stop
                            return false;
                        }
                    }
                    // keep going
                    return true;
                }, matchTTL);
                u.varSymmetric = true;
                u.varCommonalize = true;
                u.unify(taskTerm, beliefTerm, true);
                if (unifiedBeliefTerm[0] != null) {
                    beliefTerm = unifiedBeliefTerm[0];
                    unifiedBelief = true;
                }
            }
        }
    }
    // HACK ?? assert(beliefTerm.op()!=NEG);
    beliefTerm = beliefTerm.unneg();
    // QUESTION ANSWERING and TERMLINK -> TEMPORALIZED BELIEF TERM projection
    Task belief = null;
    // float timeFocus = n.timeFocus.floatValue();
    // int fRad = Math.round(Math.max(1,dur * timeFocus));
    final Concept beliefConcept = beliefTerm.op().conceptualizable ? // conceptualize in case of dynamic concepts
    n.conceptualize(beliefTerm) : null;
    if (beliefConcept != null) {
        if (!beliefTerm.hasVarQuery()) {
            // doesnt make sense to look for a belief in a term with query var, it will have none
            final BeliefTable bb = beliefConcept.beliefs();
            if (task.isQuestOrQuestion()) {
                if (beliefConceptCanAnswerTaskConcept[0]) {
                    final BeliefTable answerTable = (task.isGoal() || task.isQuest()) ? beliefConcept.goals() : bb;
                    // //see if belief unifies with task (in reverse of previous unify)
                    // if (questionTerm.varQuery() == 0 || (unify((Compound)beliefConcept.term(), questionTerm, nar) == null)) {
                    // 
                    // } else {
                    // 
                    // }
                    Task match = answerTable.answer(task.start(), task.end(), dur, task, beliefTerm, n, d::add);
                    if (match != null) {
                        assert (task.isQuest() || match.punc() == BELIEF) : "quest answered with a belief but should be a goal";
                        @Nullable Task answered = task.onAnswered(match, n);
                        if (answered != null) {
                            n.emotion.onAnswer(task, answered);
                        }
                        if (match.isBelief()) {
                            belief = match;
                        }
                    }
                }
            }
            if ((belief == null) && !bb.isEmpty()) {
                belief = bb.match(focus[0], focus[1], beliefTerm, n, taskConcept.equals(beliefConcept.term()) ? x -> !x.equals(task) : null);
            }
        }
        if (unifiedBelief) {
            Concept originalBeliefConcept = n.conceptualize(term());
            if (originalBeliefConcept != null)
                linkVariable(originalBeliefConcept, beliefConcept);
        }
    }
    if (belief != null) {
        // use the belief's actual possibly-temporalized term
        beliefTerm = belief.term().unneg();
    // if (belief.equals(task)) { //do not repeat the same task for belief
    // belief = null; //force structural transform; also prevents potential inductive feedback loop
    // }
    }
    if (beliefTerm instanceof Bool) {
        // logger.warn("{} produced Bool beliefTerm", this);
        return null;
    }
    if (!d.reset().proto(task, belief, beliefTerm))
        return null;
    return d;
}
Also used : Concept(nars.concept.Concept) Op(nars.Op) Task(nars.Task) Term(nars.term.Term) Bool(nars.term.atom.Bool) BeliefTable(nars.table.BeliefTable) UnifySubst(nars.term.subst.UnifySubst) NAR(nars.NAR) Nullable(org.jetbrains.annotations.Nullable) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with UnifySubst

use of nars.term.subst.UnifySubst in project narchy by automenta.

the class QuerySpider method starting.

@Override
protected void starting(NAR nar) {
    nar.onTask(t -> {
        if (t.isQuestOrQuestion() && t.term().hasVarQuery()) {
            Term tt = t.term();
            AdjGraph<Term, Float> g = spider(nar, t, 3);
            g.nodes.keysView().takeWhile(r -> {
                new UnifySubst(null, nar, (z) -> {
                    // TODO
                    return true;
                }, Param.TTL_MIN()).unify(tt, r.v, true);
                return true;
            });
        }
    });
}
Also used : AdjGraph(jcog.data.graph.AdjGraph) Param(nars.Param) TermGraph(nars.util.graph.TermGraph) UnifySubst(nars.term.subst.UnifySubst) Bag(jcog.bag.Bag) NAR(nars.NAR) Termed(nars.term.Termed) PriReference(jcog.pri.PriReference) Concept(nars.concept.Concept) NARService(nars.control.NARService) Term(nars.term.Term) Term(nars.term.Term) UnifySubst(nars.term.subst.UnifySubst)

Aggregations

NAR (nars.NAR)2 Concept (nars.concept.Concept)2 Term (nars.term.Term)2 UnifySubst (nars.term.subst.UnifySubst)2 Bag (jcog.bag.Bag)1 AdjGraph (jcog.data.graph.AdjGraph)1 PriReference (jcog.pri.PriReference)1 Op (nars.Op)1 Param (nars.Param)1 Task (nars.Task)1 NARService (nars.control.NARService)1 BeliefTable (nars.table.BeliefTable)1 Termed (nars.term.Termed)1 Bool (nars.term.atom.Bool)1 TermGraph (nars.util.graph.TermGraph)1 Nullable (org.jetbrains.annotations.Nullable)1