Search in sources :

Example 1 with PatternIndex

use of nars.index.term.PatternIndex in project narchy by automenta.

the class DeriveRuleSet method rules.

public static DeriveRuleSet rules(NAR nar, Collection<String> filename) {
    PatternIndex p = new PatternIndex();
    p.nar = nar;
    return new DeriveRuleSet(filename.stream().flatMap(n -> ruleCache.apply(n).stream()), p, nar);
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) Narsese(nars.Narsese) LoggerFactory(org.slf4j.LoggerFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Memoize(jcog.memoize.Memoize) ArrayUtils(org.apache.commons.lang3.ArrayUtils) IOException(java.io.IOException) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) TrieDeriver(nars.derive.TrieDeriver) Stream(java.util.stream.Stream) DeriveRules(nars.derive.DeriveRules) NAR(nars.NAR) PatternIndex(nars.index.term.PatternIndex) Subterms(nars.subterm.Subterms) Compound(nars.term.Compound) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull) Splitter(com.google.common.base.Splitter) SoftMemoize(jcog.memoize.SoftMemoize) Term(nars.term.Term) InputStream(java.io.InputStream) PatternIndex(nars.index.term.PatternIndex)

Example 2 with PatternIndex

use of nars.index.term.PatternIndex in project narchy by automenta.

the class EllipsisTest method testCombinations.

static void testCombinations(Compound _X, Compound Y, int expect) {
    Compound X = (Compound) new PatternIndex().pattern(_X);
    for (int seed = 0; seed < 3; /*expect*5*/
    seed++) {
        Set<String> results = $.newHashSet(0);
        Random rng = new XorShift128PlusRandom(seed);
        Unify f = new Unify(VAR_PATTERN, rng, Param.UnificationStackMax, 128) {

            @Override
            public void tryMatch() {
                results.add(xy.toString());
            }
        };
        f.unify(X, Y, true);
        results.forEach(System.out::println);
        assertEquals(expect, results.size(), () -> "insufficient permutations for: " + X + " .. " + Y);
    }
}
Also used : Unify(nars.term.subst.Unify) XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) Random(java.util.Random) XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) Compound(nars.term.Compound) PatternIndex(nars.index.term.PatternIndex)

Example 3 with PatternIndex

use of nars.index.term.PatternIndex in project narchy by automenta.

the class EllipsisTest method testVarArg0.

@Disabled
@Test
public void testVarArg0() throws Narsese.NarseseException {
    // String rule = "(%S --> %M), ((|, %S, %A..+ ) --> %M) |- ((|, %A, ..) --> %M), (Belief:DecomposePositiveNegativeNegative)";
    String rule = "(%S ==> %M), ((&&,%S,%A..+) ==> %M) |- ((&&,%A..+) ==> %M), (Belief:DecomposeNegativePositivePositive, Order:ForAllSame, SequenceIntervals:FromBelief)";
    Compound _x = $.$('<' + rule + '>');
    assertTrue(_x instanceof DeriveRuleSource, _x.toString());
    DeriveRuleSource x = (DeriveRuleSource) _x;
    // System.out.println(x);
    x = new DeriveRuleProto(x, new PatternIndex());
    // System.out.println(x);
    assertEquals("(((%1==>%2),((%1&&%3..+)==>%2)),(((&&,%3..+)==>%2),((DecomposeNegativePositivePositive-->Belief),(ForAllSame-->Order),(FromBelief-->SequenceIntervals))))", x.toString());
}
Also used : Compound(nars.term.Compound) DeriveRuleSource(nars.derive.rule.DeriveRuleSource) DeriveRuleProto(nars.derive.rule.DeriveRuleProto) PatternIndex(nars.index.term.PatternIndex) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 4 with PatternIndex

use of nars.index.term.PatternIndex 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)

Example 5 with PatternIndex

use of nars.index.term.PatternIndex in project narchy by automenta.

the class TrieDeriverTest method testCompile.

public static DeriveRuleSet testCompile(boolean debug, String... rules) {
    assertNotEquals(0, rules.length);
    PatternIndex pi = new PatternIndex();
    Stream<DeriveRuleSource> parsed = DeriveRuleSet.parse(Stream.of(rules));
    DeriveRuleSet src = new DeriveRuleSet(parsed, pi, NARS.shell());
    assertNotEquals(0, src.size());
    DeriveRules d = the(src);
    if (debug) {
    // d.printRecursive();
    // PrediTerm<Derivation> dd = d.what.transform(DebugDerivationPredicate::new);
    }
    return src;
}
Also used : DeriveRules(nars.derive.DeriveRules) DeriveRuleSource(nars.derive.rule.DeriveRuleSource) DeriveRuleSet(nars.derive.rule.DeriveRuleSet) PatternIndex(nars.index.term.PatternIndex)

Aggregations

PatternIndex (nars.index.term.PatternIndex)6 DeriveRules (nars.derive.DeriveRules)3 DeriveRuleSource (nars.derive.rule.DeriveRuleSource)3 Compound (nars.term.Compound)3 Term (nars.term.Term)3 NAR (nars.NAR)2 DeriveRuleProto (nars.derive.rule.DeriveRuleProto)2 DeriveRuleSet (nars.derive.rule.DeriveRuleSet)2 Termed (nars.term.Termed)2 NotNull (org.jetbrains.annotations.NotNull)2 Test (org.junit.jupiter.api.Test)2 Splitter (com.google.common.base.Splitter)1 Streams (com.google.common.collect.Streams)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 java.util (java.util)1 List (java.util.List)1 Map (java.util.Map)1 Random (java.util.Random)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1