A DSL Object Graph Traversal

I choose this somewhat dry title for a presentation I did at TW UK the the other week. It was about an external DSL, I wrote some time back, that allows you to specify traversals of object graphs. These specifications might be used to perform the following operations with object graphs:

  • Serialization
  • Visualization
  • Comparison
  • Cloning

The slides and the the sourcecode are available. Feedback is appreciated.


Posted

in

by

Tags:

Comments

7 responses to “A DSL Object Graph Traversal”

  1. Sai Avatar
    Sai

    The presentation about the DSL is really interesting. A working demo will also help.

  2. Ben Hughes Avatar

    You could also try Intype (http://www.intype.info) – still in beta (and therefore free) – also uses bundles.

  3. Nirav Thaker Avatar

    Cool stuff. I would have, at the very least, added visitation support in Node, otherwise there’s no reason to reinvent DOM.

  4. JoshG Avatar

    Nice. Your serialisation would be easy to produce in JSON format, making the node names easily derivable too. The DSL + serialisation is almost a “static type” JSON.

    I like the idea of additional metadata like predicates… feels like it’s heading towards RelaxNG.

    @Nirav: for anything other than basic purposes, there may be plenty of reasons to reinvent DOM (as has occurred a couple of times already, even just for marked up textual documents).

  5. felix Avatar
    felix

    @Sai: It should be possible to make the code in the zip run. For the nice graphs you will need to download the graphviz binaries.

  6. felix Avatar
    felix

    @Nirav: This is actually not so much about reinventing DOM. The same principles could be used not to traverse (Java-)Objects but DOM nodes. It’s more about specifying a subset of nodes and it has a certain a lot of similarity to xpath, but has a simpler Syntax and allows to specify where to stop traversing(if I am correct selecting a node in xpath means you also select all the subnodes).

    I actually included several iterators, mostly because I am not sure which one is best. One of them is SAX Style, while others allow to transform the tree, while traversing.

  7. felix Avatar
    felix

    @Josh

    I just implemented a JSON renderer. It is pretty straight-forward, even though it reads a bit ugly:

    public class JSONSerializer {
        private BottomUpTransformer transformer;
    
        public JSONSerializer(ClassNode classNode) {
            transformer = new BottomUpTransformer(classNode, new JsonMapper(), true);
        }
    
        public Object serialize(Object object){
            return transformer.transform(object);
        }
    
        private class JsonMapper implements BottomUpMapper {
            public Object mapObject(ClassNode nodeDescriptor, List mappedProperties) {
                return "{"+
                    StringUtils.join(
                            CollectionUtils.collect(mappedProperties,new Transformer() {
                                public Object transform(Object o) {
                                    final PropertyValue propertyValue = (PropertyValue) o;
                                    return propertyValue.getMeta().getName()+":"+propertyValue.getValue();
                                 }}).iterator(),
                            ", ")
                +"}";
            }
    
            public Object mapCollection(CollectionRef collection, Collection mappedElements) {
                return "["+StringUtils.join(mappedElements.iterator(),", ")+"]";
            }
    
            public Object mapFlatProperty(FlatProperty property, Object value)
            {
                final String stringValue = new DefaultStringMapper().toString(property, value);
                return property.getDescriptor().getPropertyType()==String.class? 
                        """+stringValue+"""
                        :stringValue;
            }
    
        }
    }
    

Leave a Reply to JoshG Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.