Search in sources :

Example 1 with ObjectByteHashMap

use of org.eclipse.collections.impl.map.mutable.primitive.ObjectByteHashMap in project narchy by automenta.

the class FastCompound method get.

public static FastCompound get(Op o, int subs, Iterable<Term> subterms) {
    ObjectByteHashMap<Term> atoms = new ObjectByteHashMap();
    DynBytes shadow = new DynBytes(256);
    // UncheckedBytes shadow = new UncheckedBytes(Bytes.wrapForWrite(new byte[256]));
    shadow.writeUnsignedByte(o.ordinal());
    shadow.writeUnsignedByte(subs);
    final byte[] numAtoms = { 0 };
    ByteFunction0 nextUniqueAtom = () -> numAtoms[0]++;
    int structure = o.bit, hashCode = 1;
    byte volume = 1;
    for (Term x : subterms) {
        x.recurseTerms((child) -> {
            shadow.writeUnsignedByte((byte) child.op().ordinal());
            if (child.op().atomic) {
                int aid = atoms.getIfAbsentPut(child, nextUniqueAtom);
                shadow.writeUnsignedByte((byte) aid);
            } else {
                shadow.writeUnsignedByte(child.subs());
            // TODO use last bit of the subs byte to indicate presence or absence of subsequent 'dt' value (32 bits)
            }
        });
        structure |= x.structure();
        hashCode = Util.hashCombine(hashCode, x.hashCode());
        volume += x.volume();
    }
    hashCode = Util.hashCombine(hashCode, o.id);
    assert (volume < 127);
    // TODO calculate normalized by the encountered sequence of variable id's - whether it is monotonically increasing by 1 each time the max value does increase or something
    boolean normalized = false;
    // TODO sort atoms to canonicalize its dictionary for sharing with other terms
    FastCompound y;
    // {
    // byte[][] a = new byte[atoms.size()][];
    // for (ObjectBytePair<Term> p : atoms.keyValuesView()) {
    // a[p.getTwo()] = IO.termToBytes(p.getOne());
    // }
    // y = new FastCompoundSerializedAtoms(a, shadow.toByteArray(), x.hashCode(), x.hashCodeSubTerms(), (byte) x.volume(), x.isNormalized());
    // }
    {
        Term[] a = new Term[atoms.size()];
        for (ObjectBytePair<Term> p : atoms.keyValuesView()) {
            a[p.getTwo()] = p.getOne();
        }
        y = new FastCompoundInstancedAtoms(a, shadow.toByteArray(), structure, hashCode, volume, normalized);
    }
    return y;
}
Also used : DynBytes(jcog.data.byt.DynBytes) ObjectBytePair(org.eclipse.collections.api.tuple.primitive.ObjectBytePair) ByteFunction0(org.eclipse.collections.api.block.function.primitive.ByteFunction0) Term(nars.term.Term) ObjectByteHashMap(org.eclipse.collections.impl.map.mutable.primitive.ObjectByteHashMap)

Example 2 with ObjectByteHashMap

use of org.eclipse.collections.impl.map.mutable.primitive.ObjectByteHashMap in project narchy by automenta.

the class DepIndepVarIntroduction method introduce.

@Nullable
@Override
protected Term introduce(Term input, Term selected, byte order) {
    if (selected.equals(Imdex))
        return null;
    Op inOp = input.op();
    List<ByteList> paths = $.newArrayList(1);
    int minPathLength = inOp.statement ? 2 : 0;
    input.pathsTo(selected, (path, t) -> {
        if (path.size() >= minPathLength)
            paths.add(path.toImmutable());
        // TODO may be able to terminate early if we know this is the last one
        return true;
    });
    int pSize = paths.size();
    // if (pSize == 0)
    if (pSize <= 1)
        return null;
    // byte[][] paths = pp.toArray(new byte[pSize][]);
    // //detect an invalid top-level indep var substitution
    // if (inOp.statement) {
    // Iterator<byte[]> pp = paths.iterator();
    // while (pp.hasNext()) {
    // byte[] p = pp.next();
    // if (p.length < 2)
    // pp.remove();;
    // for (byte[] r : paths)
    // if (r.length < 2)
    // return null; //substitution would replace something at the top level of a statement}
    // }
    // }
    // use the innermost common parent to decide the type of variable.
    // in case there is no other common parent besides input itself, use that.
    Term commonParent = input.commonParent(paths);
    Op commonParentOp = commonParent.op();
    // decide what kind of variable can be introduced according to the input operator
    boolean depOrIndep;
    switch(commonParentOp) {
        case CONJ:
            depOrIndep = true;
            break;
        case IMPL:
            depOrIndep = false;
            break;
        default:
            // ????
            return null;
    }
    ObjectByteHashMap<Term> m = new ObjectByteHashMap<>(0);
    for (int path = 0; path < pSize; path++) {
        ByteList p = paths.get(path);
        // root
        Term t = null;
        int pathLength = p.size();
        for (int i = -1; i < pathLength - 1; /* dont include the selected term itself */
        i++) {
            t = (i == -1) ? input : t.sub(p.get(i));
            Op o = t.op();
            if (!depOrIndep && validIndepVarSuperterm(o)) {
                byte inside = (byte) (1 << p.get(i + 1));
                m.updateValue(t, inside, (previous) -> (byte) ((previous) | inside));
            } else if (depOrIndep && validDepVarSuperterm(o)) {
                m.addToValue(t, (byte) 1);
            }
        }
    }
    if (!depOrIndep) {
        // at least one impl/equiv must have both sides covered
        return (m.anySatisfy(b -> b == 0b11)) ? $.v(VAR_INDEP, order) : /*varIndep(order)*/
        null;
    } else {
        // at least one conjunction must contain >=2 path instances
        return m.anySatisfy(b -> b >= 2) ? $.v(VAR_DEP, order) : /* $.varDep(order) */
        null;
    }
}
Also used : Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ByteList(org.eclipse.collections.api.list.primitive.ByteList) Op(nars.Op) Map(java.util.Map) ToIntFunction(java.util.function.ToIntFunction) Random(java.util.Random) nars.$(nars.$) ObjectByteHashMap(org.eclipse.collections.impl.map.mutable.primitive.ObjectByteHashMap) Pair(org.eclipse.collections.api.tuple.Pair) Terms(nars.term.Terms) Term(nars.term.Term) Op(nars.Op) ByteList(org.eclipse.collections.api.list.primitive.ByteList) Term(nars.term.Term) ObjectByteHashMap(org.eclipse.collections.impl.map.mutable.primitive.ObjectByteHashMap) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Term (nars.term.Term)2 ObjectByteHashMap (org.eclipse.collections.impl.map.mutable.primitive.ObjectByteHashMap)2 List (java.util.List)1 Map (java.util.Map)1 Random (java.util.Random)1 ToIntFunction (java.util.function.ToIntFunction)1 DynBytes (jcog.data.byt.DynBytes)1 nars.$ (nars.$)1 Op (nars.Op)1 Terms (nars.term.Terms)1 ByteFunction0 (org.eclipse.collections.api.block.function.primitive.ByteFunction0)1 ByteList (org.eclipse.collections.api.list.primitive.ByteList)1 Pair (org.eclipse.collections.api.tuple.Pair)1 ObjectBytePair (org.eclipse.collections.api.tuple.primitive.ObjectBytePair)1 Nullable (org.jetbrains.annotations.Nullable)1