Search in sources :

Example 26 with Compound

use of nars.term.Compound 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 27 with Compound

use of nars.term.Compound in project narchy by automenta.

the class UnifyTest method test.

Unify test(/**/
Op type, String s1, String s2, boolean shouldSub) {
    // NAR nar = NARS.shell();
    try {
        Compound t2 = (Compound) Narsese.the().term(s2, true);
        assertNotNull(t2);
        Compound t1;
        if (type == Op.VAR_PATTERN) {
            // special handling for ellipsis
            t1 = pattern(s1);
            assertNotNull(t1);
        } else {
            t1 = (Compound) Narsese.the().term(s1, true);
        }
        // nar.question(s2);
        // nar.run(cycles);
        Set<Term> t1u = t1.recurseTermsToSet(type);
        // assertTrue(t1u.size() > 0);
        Set<Term> t2u = t2.recurseTermsToSet(type);
        // assertTrue(t2u.size() == 0);
        // Sets.difference(t1u, t2u).size();
        int n1 = t1u.size();
        // int n2 = Sets.difference(t2u, t1u).size();
        // a somewhat strict lower bound
        // int power = 4 * (1 + t1.volume() * t2.volume());
        // power*=power;
        AtomicBoolean subbed = new AtomicBoolean(false);
        Unify sub = new Unify(type, new XorShift128PlusRandom(1), Param.UnificationStackMax, INITIAL_TTL) {

            @Override
            public void tryMatch() {
                if (shouldSub) {
                    this.xy.forEachVersioned((k, v) -> {
                        if (matchType(k.op()))
                            assertNotNull(v);
                        return true;
                    });
                    if (/*((n2) <= (yx.size())*/
                    (n1) <= (xy.size())) {
                        subbed.set(true);
                    }
                /*else {
                            System.out.println("incomplete:\n\t" + xy);
                        }*/
                // assertFalse("incomplete: " + toString(), this.isEmpty());
                } else {
                    // HACK there should be incomplete assignments even though this says it matched
                    // || (n2) <= (yx.size()));
                    assertTrue((n1) > (xy.size()), "why matched?: " + xy);
                // assertFalse("match found but should not have", true);
                }
            }
        };
        sub.unify(t1.term(), t2.term(), true);
        // for testing
        sub.revert(0);
        assertEquals(shouldSub, subbed.get());
        return sub;
    } catch (Narsese.NarseseException e) {
        throw new RuntimeException(e);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Unify(nars.term.subst.Unify) XorShift128PlusRandom(jcog.math.random.XorShift128PlusRandom) Compound(nars.term.Compound) Term(nars.term.Term)

Example 28 with Compound

use of nars.term.Compound in project narchy by automenta.

the class KIFInput method impl.

// private Variable nextVar(Op v) {
// return $.v(v, nextVar());
// }
// private final AtomicInteger serial = new AtomicInteger(0);
// private String nextVar() {
// return Integer.toString(Math.abs(serial.incrementAndGet()), 36);
// }
// public final Set<Twin<Term>> impl = new HashSet();
public Term impl(Term a, Term b, boolean implOrEquiv) {
    // reduce as implication first
    Term tmp = IMPL.the(a, b);
    if (tmp.unneg().op() != IMPL) {
        logger.warn("un-impl: {} ==> {} ", a, b);
        return null;
    }
    tmp = tmp.unneg();
    a = tmp.sub(0);
    b = tmp.sub(1);
    MutableSet<Term> aVars = new VarOnlySet();
    if (a instanceof Compound)
        ((Compound) a).recurseTermsToSet(Op.VariableBits, aVars, true);
    else if (a.op().var)
        aVars.add(a);
    MutableSet<Term> bVars = new VarOnlySet();
    if (b instanceof Compound)
        ((Compound) b).recurseTermsToSet(Op.VariableBits, bVars, true);
    else if (b.op().var)
        bVars.add(b);
    Map<Term, Term> remap = new HashMap();
    MutableSet<Term> common = aVars.intersect(bVars);
    if (!common.isEmpty()) {
        common.forEach(t -> {
            Variable u = $.v(Op.VAR_INDEP, // Op.VAR_PATTERN,
            t.toString().substring(1));
            if (!t.equals(u))
                remap.put(t, u);
        });
    }
    for (MutableSet<Term> ab : new MutableSet[] { aVars, bVars }) {
        ab.forEach(aa -> {
            if (aa.op() == VAR_INDEP && !common.contains(aa)) {
                remap.put(aa, $.v(Op.VAR_DEP, aa.toString().substring(1)));
            }
        });
    }
    if (!remap.isEmpty()) {
        a = a.replace(remap);
        if (a == null)
            throw new NullPointerException("transform failure");
        b = b.replace(remap);
        if (b == null)
            throw new NullPointerException("transform failure");
    }
    try {
        return implOrEquiv ? IMPL.the(a, b) : equi(a, b);
    } catch (Exception ignore) {
        ignore.printStackTrace();
    }
    return null;
}
Also used : MutableSet(org.eclipse.collections.api.set.MutableSet) Variable(nars.term.var.Variable) IntObjectHashMap(org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap) Compound(nars.term.Compound) Term(nars.term.Term) FileNotFoundException(java.io.FileNotFoundException)

Example 29 with Compound

use of nars.term.Compound in project narchy by automenta.

the class PrologCore method pterm.

// NARS term -> Prolog term
public static alice.tuprolog.Term pterm(final Term term) {
    if (term instanceof Compound) {
        Op op = term.op();
        alice.tuprolog.Term[] st = psubterms(((Compound) term));
        switch(op) {
            case IMPL:
                return new Struct(":-", st[1], st[0]);
            case CONJ:
                return new Struct(",", st);
            case NEG:
                return new Struct(/*"\\="*/
                "not", st);
            case PROD:
                // list
                return new Struct(st);
            case INH:
                Term pred = term.sub(1);
                if (pred.op() == ATOM) {
                    Term subj = term.sub(0);
                    if (subj.op() == PROD) {
                        alice.tuprolog.Term args = st[0];
                        return new Struct(wrapAtom(pred.toString()), args instanceof alice.tuprolog.Term ? Iterators.toArray(((Struct) st[0]).listIterator(), alice.tuprolog.Term.class) : new alice.tuprolog.Term[] { args });
                    }
                }
                break;
        }
        return new Struct(op.str, st);
    } else if (term instanceof NormalizedVariable) {
        switch(term.op()) {
            case VAR_QUERY:
            case VAR_PATTERN:
            // ?? as if atomic
            case VAR_DEP:
            case // ??
            VAR_INDEP:
                return new Var("_" + (((NormalizedVariable) term).anonNum()));
        }
    } else if (term instanceof Atomic) {
        return new Struct(wrapAtom(term.toString()));
    }
    throw new UnsupportedOperationException();
// //CharSequence s = termString(term);
// if (term instanceof Statement) {
// Statement i = (Statement)term;
// String predicate = classPredicate(i.getClass());
// alice.tuprolog.Term  subj = alice.tuprolog.Term (i.getSubject());
// alice.tuprolog.Term  obj = alice.tuprolog.Term (i.getPredicate());
// if ((subj!=null) && (obj!=null))
// return new Struct(predicate, subj, obj);
// }
// else if ((term instanceof SetTensional) || (term instanceof Product) /* conjunction */) {
// Compound s = (Compound)term;
// String predicate = classPredicate(s.getClass());
// alice.tuprolog.Term [] args = alice.tuprolog.Term s(s.term);
// if (args!=null)
// return new Struct(predicate, args);
// }
// //Image...
// //Conjunction...
// else if (term instanceof Negation) {
// alice.tuprolog.Term  np = alice.tuprolog.Term (((Negation)term).term[0]);
// if (np == null) return null;
// return new Struct("negation", np);
// }
// else if (term.getClass().equals(Variable.class)) {
// return getVariable((Variable)term);
// }
// else if (term.getClass().equals(Atom.class)) {
// return new Struct(pescape(term.toString()));
// }
// else if (term instanceof Compound) {
// //unhandled type of compound term, store as an atomic string
// //NOT ready yet
// if (allTerms) {
// return new Struct('_' + pescape(term.toString()));
// }
// }
// 
// return null;
}
Also used : Op(nars.Op) alice.tuprolog(alice.tuprolog) Compound(nars.term.Compound) NormalizedVariable(nars.term.var.NormalizedVariable) Atomic(nars.term.atom.Atomic) Term(nars.term.Term)

Example 30 with Compound

use of nars.term.Compound in project narchy by automenta.

the class VisionRay method inputVisionFreq.

private void inputVisionFreq(float dist, String material) {
    float freq = 0.5f + 0.5f * dist;
    // TODO move to constructor
    if (thisAngle == null)
        thisAngle = Atom.the(angleTerm);
    Compound tt = Inheritance.make(Product.make(thisAngle, Atom.the(material)), Atom.the("see"));
    abstractPolygonBot.nar.input(abstractPolygonBot.nar.task(tt).belief().present().truth(freq, conf).normalized());
}
Also used : Compound(nars.term.Compound)

Aggregations

Compound (nars.term.Compound)58 Test (org.junit.jupiter.api.Test)38 Term (nars.term.Term)20 Unify (nars.term.subst.Unify)3 FasterList (jcog.list.FasterList)2 XorShift128PlusRandom (jcog.math.random.XorShift128PlusRandom)2 Op (nars.Op)2 PatternCompound (nars.derive.PatternCompound)2 PatternIndex (nars.index.term.PatternIndex)2 Atomic (nars.term.atom.Atomic)2 Variable (nars.term.var.Variable)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 alice.tuprolog (alice.tuprolog)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Prioritized (jcog.pri.Prioritized)1 Narsese (nars.Narsese)1