Search in sources :

Example 1 with PrediTerm

use of nars.term.pred.PrediTerm in project narchy by automenta.

the class MatchConstraint method combineConstraints.

public static PrediTerm<Derivation> combineConstraints(AndCondition a) {
    RoaringBitmap constraints = new RoaringBitmap();
    @NotNull PrediTerm[] cond1 = a.cond;
    for (int i = 0, cl = cond1.length; i < cl; i++) {
        Term x = cond1[i];
        if (x instanceof MatchConstraint) {
            constraints.add(i);
        }
    }
    if (constraints.getCardinality() < 2) {
        return a;
    } else {
        // identify contiguous runs of constraints
        // inclusive
        List<IntIntPair> ranges = new FasterList<>(1);
        int start = -1, end = -1;
        PeekableIntIterator ii = constraints.getIntIterator();
        while (ii.hasNext()) {
            int next = ii.next();
            if (start == -1) {
                start = end = next;
            } else {
                if (next == end + 1) {
                    end++;
                } else {
                    if (end - start >= 1) {
                        // compile that range
                        ranges.add(pair(start, end));
                    }
                    // broken
                    start = -1;
                }
            }
        }
        if (end - start >= 1)
            ranges.add(pair(start, end));
        if (ranges.size() > 1)
            throw new TODO();
        IntIntPair rr = ranges.get(0);
        List<PrediTerm<Derivation>> l = new FasterList();
        int i;
        for (i = 0; i < start; i++) {
            l.add(a.cond[i]);
        }
        CompoundConstraint.the(Util.map(MatchConstraint.class::cast, MatchConstraint[]::new, ArrayUtils.subarray(a.cond, rr.getOne(), rr.getTwo() + 1))).forEach(l::add);
        i = end + 1;
        for (; i < a.cond.length; i++) {
            l.add(a.cond[i]);
        }
        return AndCondition.the((List) l);
    }
}
Also used : TODO(jcog.TODO) PrediTerm(nars.term.pred.PrediTerm) FasterList(jcog.list.FasterList) PeekableIntIterator(org.roaringbitmap.PeekableIntIterator) PrediTerm(nars.term.pred.PrediTerm) Term(nars.term.Term) Util(jcog.Util) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull) RoaringBitmap(org.roaringbitmap.RoaringBitmap) IntIntPair(org.eclipse.collections.api.tuple.primitive.IntIntPair)

Example 2 with PrediTerm

use of nars.term.pred.PrediTerm in project narchy by automenta.

the class InstrumentedDerivationPredicate method test.

@Override
public boolean test(Derivation derivation) {
    PrediTerm p;
    if (ref instanceof PrediTerm) {
        p = (PrediTerm) ref;
    } else {
        Term s0 = sub(0);
        if (s0 instanceof PrediTerm)
            p = (PrediTerm) s0;
        else {
            throw new UnsupportedOperationException();
        // ((PrediTerm)(ref.sub(0)));
        }
    }
    onEnter(p, derivation);
    Throwable thrown = null;
    boolean result = false;
    long start = System.nanoTime();
    try {
        result = p.test(derivation);
    } catch (Throwable e) {
        thrown = e;
    }
    long end = System.nanoTime();
    onExit(p, derivation, result, thrown, end - start);
    return result;
}
Also used : PrediTerm(nars.term.pred.PrediTerm) PrediTerm(nars.term.pred.PrediTerm) Term(nars.term.Term)

Example 3 with PrediTerm

use of nars.term.pred.PrediTerm in project narchy by automenta.

the class DeriveRuleProto method build.

/**
 * compiles the conditions which are necessary to activate this rule
 */
public Pair<Set<PrediTerm<PreDerivation>>, PrediTerm<Derivation>> build(PostCondition post) {
    byte puncOverride = post.puncOverride;
    TruthOperator belief = BeliefFunction.get(post.beliefTruth);
    if ((post.beliefTruth != null) && !post.beliefTruth.equals(TruthOperator.NONE) && (belief == null)) {
        throw new RuntimeException("unknown BeliefFunction: " + post.beliefTruth);
    }
    TruthOperator goal = GoalFunction.get(post.goalTruth);
    if ((post.goalTruth != null) && !post.goalTruth.equals(TruthOperator.NONE) && (goal == null)) {
        throw new RuntimeException("unknown GoalFunction: " + post.goalTruth);
    }
    // //if (puncOverride==0) {
    // if (belief!=null && goal!=null) {
    // if (!belief.single() && !goal.single()) {
    // pre.add(TaskPolarity.belief);
    // } else if (belief.single() ^ goal.single()){
    // throw new TODO();
    // }
    // } else if (belief!=null && !belief.single()) {
    // pre.add(TaskPolarity.belief);
    // } else if (goal!=null && !goal.single()) {
    // pre.add(TaskPolarity.belief);
    // }
    // TODO add more specific conditions that also work
    // }
    String beliefLabel = belief != null ? belief.toString() : "_";
    String goalLabel = goal != null ? goal.toString() : "_";
    FasterList<Term> args = new FasterList();
    args.add($.the(beliefLabel));
    args.add($.the(goalLabel));
    if (puncOverride != 0)
        args.add($.quote(((char) puncOverride)));
    Compound ii = (Compound) $.func("truth", args.toArrayRecycled(Term[]::new));
    Solve truth = (puncOverride == 0) ? new Solve.SolvePuncFromTask(ii, belief, goal) : new Solve.SolvePuncOverride(ii, puncOverride, belief, goal);
    // PREFIX
    // for ensuring uniqueness / no duplicates
    Set<PrediTerm<PreDerivation>> precon = newHashSet(4);
    addAll(precon, PRE);
    precon.addAll(this.pre);
    // //-------------------
    // below here are predicates which affect the derivation
    // SUFFIX (order already determined for matching)
    int n = 1 + this.constraints.size() + this.post.size();
    PrediTerm[] suff = new PrediTerm[n];
    int k = 0;
    suff[k++] = truth;
    for (PrediTerm p : this.constraints) {
        suff[k++] = p;
    }
    for (PrediTerm p : this.post) {
        suff[k++] = p;
    }
    return pair(precon, (PrediTerm<Derivation>) AndCondition.the(suff));
}
Also used : FasterList(jcog.list.FasterList) PrediTerm(nars.term.pred.PrediTerm) Compound(nars.term.Compound) PrediTerm(nars.term.pred.PrediTerm) Term(nars.term.Term) nars.derive.constraint(nars.derive.constraint) TruthOperator(nars.truth.func.TruthOperator)

Example 4 with PrediTerm

use of nars.term.pred.PrediTerm in project narchy by automenta.

the class Conclude method match.

public static void match(final DeriveRuleProto rule, List<PrediTerm<PreDerivation>> pre, List<PrediTerm<Derivation>> post, PatternIndex index, NAR nar) {
    Term pattern = rule.conclusion().sub(0);
    // TODO may interfere with constraints, functors, etc or other features, ie.
    // if the pattern is a product for example?
    // pattern = pattern.replace(ta, Derivation._taskTerm);
    // determine if any cases where a shortcut like this can work (ie. no constraints, not a product etc)
    // //substitute compound occurrences of the exact task and belief terms with the short-cut
    // Term ta = rule.getTask();
    // if (!ta.op().var) {
    // if (pattern.equals(ta))
    // pattern = Derivation.TaskTerm;
    // }
    // Term tb = rule.getBelief();
    // if (!tb.op().var) {
    // //pattern = pattern.replace(tb, Derivation._beliefTerm);
    // if (pattern.equals(tb))
    // pattern = Derivation.BeliefTerm;
    // }
    // HACK unwrap varIntro so we can apply it at the end of the derivation process, not before like other functors
    boolean introVars1;
    Pair<Termed, Term> outerFunctor = Op.functor(pattern, (i) -> i.equals(VAR_INTRO) ? VAR_INTRO : null);
    if (outerFunctor != null) {
        introVars1 = true;
        pattern = outerFunctor.getTwo().sub(0);
    } else {
        introVars1 = false;
    }
    pattern = index.get(pattern, true).term();
    Taskify taskify = new Taskify(nar.newCause((s) -> new RuleCause(rule, s)));
    PrediTerm<Derivation> conc = AndCondition.the(new Termify($.func("derive", pattern), pattern, rule), introVars1 ? AndCondition.the(introVars, taskify) : taskify);
    final Term taskPattern = rule.getTask();
    final Term beliefPattern = rule.getBelief();
    Op to = taskPattern.op();
    boolean taskIsPatVar = to == Op.VAR_PATTERN;
    Op bo = beliefPattern.op();
    boolean belIsPatVar = bo == Op.VAR_PATTERN;
    if (!taskIsPatVar) {
        pre.add(new TaskBeliefOp(to, true, false));
        pre.addAll(SubTermStructure.get(0, taskPattern.structure()));
    }
    if (!belIsPatVar) {
        if (to == bo) {
            pre.add(new AbstractPatternOp.TaskBeliefOpEqual());
        } else {
            pre.add(new TaskBeliefOp(bo, false, true));
            pre.addAll(SubTermStructure.get(1, beliefPattern.structure()));
        }
    }
    if (taskPattern.equals(beliefPattern)) {
        post.add(new UnifyTerm.UnifySubtermThenConclude(0, taskPattern, conc));
    }
    if (taskFirst(taskPattern, beliefPattern)) {
        // task first
        post.add(new UnifyTerm.UnifySubterm(0, taskPattern));
        post.add(new UnifyTerm.UnifySubtermThenConclude(1, beliefPattern, conc));
    } else {
        // belief first
        post.add(new UnifyTerm.UnifySubterm(1, beliefPattern));
        post.add(new UnifyTerm.UnifySubtermThenConclude(0, taskPattern, conc));
    }
// Term beliefPattern = pattern.term(1);
// if (Global.DEBUG) {
// if (beliefPattern.structure() == 0) {
// if nothing else in the rule involves this term
// which will be a singular VAR_PATTERN variable
// then allow null
// if (beliefPattern.op() != Op.VAR_PATTERN)
// throw new RuntimeException("not what was expected");
// }
// }
/*System.out.println( Long.toBinaryString(
                        pStructure) + " " + pattern
        );*/
}
Also used : DepIndepVarIntroduction(nars.op.DepIndepVarIntroduction) PrediTerm(nars.term.pred.PrediTerm) AbstractPatternOp(nars.derive.op.AbstractPatternOp) nars.$(nars.$) DeriveRuleSource(nars.derive.rule.DeriveRuleSource) AndCondition(nars.term.pred.AndCondition) UnifyTerm(nars.derive.op.UnifyTerm) AbstractPred(nars.term.pred.AbstractPred) Nullable(org.jetbrains.annotations.Nullable) VAR_INTRO(nars.derive.Conclude.IntroVars.VAR_INTRO) Task(nars.Task) List(java.util.List) TaskBeliefOp(nars.derive.op.TaskBeliefOp) NAR(nars.NAR) Op(nars.Op) DeriveRuleProto(nars.derive.rule.DeriveRuleProto) PatternIndex(nars.index.term.PatternIndex) Map(java.util.Map) SubTermStructure(nars.derive.op.SubTermStructure) Termed(nars.term.Termed) Cause(nars.control.Cause) Pair(org.eclipse.collections.api.tuple.Pair) Term(nars.term.Term) AbstractPatternOp(nars.derive.op.AbstractPatternOp) TaskBeliefOp(nars.derive.op.TaskBeliefOp) Op(nars.Op) TaskBeliefOp(nars.derive.op.TaskBeliefOp) AbstractPatternOp(nars.derive.op.AbstractPatternOp) UnifyTerm(nars.derive.op.UnifyTerm) PrediTerm(nars.term.pred.PrediTerm) UnifyTerm(nars.derive.op.UnifyTerm) Term(nars.term.Term) Termed(nars.term.Termed)

Aggregations

Term (nars.term.Term)4 PrediTerm (nars.term.pred.PrediTerm)4 FasterList (jcog.list.FasterList)2 List (java.util.List)1 Map (java.util.Map)1 TODO (jcog.TODO)1 Util (jcog.Util)1 nars.$ (nars.$)1 NAR (nars.NAR)1 Op (nars.Op)1 Task (nars.Task)1 Cause (nars.control.Cause)1 VAR_INTRO (nars.derive.Conclude.IntroVars.VAR_INTRO)1 nars.derive.constraint (nars.derive.constraint)1 AbstractPatternOp (nars.derive.op.AbstractPatternOp)1 SubTermStructure (nars.derive.op.SubTermStructure)1 TaskBeliefOp (nars.derive.op.TaskBeliefOp)1 UnifyTerm (nars.derive.op.UnifyTerm)1 DeriveRuleProto (nars.derive.rule.DeriveRuleProto)1 DeriveRuleSource (nars.derive.rule.DeriveRuleSource)1