Search in sources :

Example 1 with ObjectGraph

use of jcog.data.graph.ObjectGraph in project narchy by automenta.

the class NodeGraphTest method testObjectGraph.

@Test
public void testObjectGraph() {
    MapNodeGraph<Object, Object> h = new MapNodeGraph<>();
    h.addEdge(h.addNode("y"), "yx", h.addNode("x"));
    ObjectGraph o = new ObjectGraph(3, h) {

        @Override
        protected boolean access(Object root, FasterList<Pair<Class, ObjectGraph.Accessor>> path, Object target) {
            System.out.println(root + " -> " + target + "\n\t" + path);
            return true;
        }

        @Override
        public boolean includeValue(Object value) {
            return true;
        }

        @Override
        public boolean includeClass(Class<?> c) {
            return !c.isPrimitive();
        }

        @Override
        public boolean includeField(Field f) {
            return true;
        }
    };
    o.print();
}
Also used : Field(java.lang.reflect.Field) FasterList(jcog.list.FasterList) ObjectGraph(jcog.data.graph.ObjectGraph) MapNodeGraph(jcog.data.graph.MapNodeGraph) Test(org.junit.jupiter.api.Test)

Example 2 with ObjectGraph

use of jcog.data.graph.ObjectGraph in project narchy by automenta.

the class Tweaks method discover.

/**
 * auto discovers tweaks by reflecting a sample of the subject
 */
public Tweaks<X> discover(Discovery<X> each) {
    // sample instance
    X x = (this.subjects.get());
    ObjectGraph o = new ObjectGraph() {

        @Override
        protected boolean access(Object root, FasterList<Pair<Class, Accessor>> path, Object target) {
            if (this.nodes.containsKey(target))
                // prevent cycle
                return false;
            Class<?> targetType = target.getClass();
            if (!Tweaks.this.includeClass(targetType))
                return false;
            if (tweakable(targetType)) {
                each.discovered((X) root, path.clone(), targetType);
            }
            return !Primitives.unwrap(target.getClass()).isPrimitive();
        }

        @Override
        public boolean recurse(Object x) {
            Class<?> xc = x.getClass();
            return Tweaks.this.includeClass(xc) && !tweakable(xc);
        }

        @Override
        public boolean includeValue(Object v) {
            return Tweaks.this.includeClass(v.getClass());
        }

        @Override
        public boolean includeClass(Class<?> c) {
            return Tweaks.this.includeClass(c);
        }

        @Override
        public boolean includeField(Field f) {
            int m = f.getModifiers();
            if (!Modifier.isPublic(m) || !Tweaks.this.includeField(f))
                return false;
            Class<?> t = Primitives.wrap(f.getType());
            boolean primitive = Primitives.unwrap(f.getType()).isPrimitive();
            if (tweakable(t)) {
                return (!primitive || !Modifier.isFinal(m));
            } else
                // explore further into Object's, final or not
                return !primitive;
        }
    };
    o.add(DEFAULT_DEPTH, x);
    return this;
}
Also used : Field(java.lang.reflect.Field) FasterList(jcog.list.FasterList) ObjectGraph(jcog.data.graph.ObjectGraph)

Aggregations

Field (java.lang.reflect.Field)2 ObjectGraph (jcog.data.graph.ObjectGraph)2 FasterList (jcog.list.FasterList)2 MapNodeGraph (jcog.data.graph.MapNodeGraph)1 Test (org.junit.jupiter.api.Test)1