 
   builder =
 
-    init: (state) ->
-      state.result = []
-      #top = {}
-      #result.current = top
-      #result.graph = [top]
-      #result.context = {}
+    init: (doc, base, profile) ->
+      resolver = doc.createElement('a')
+      resolveURI = (ref) ->
+        resolver.href = ref
+        return resolver.href
+      state = new State(base, profile, resolveURI)
+      docObj = {"@id": base}
+      all = {}
+      all[base] = docObj
+      #context = {}
+      state.current = docObj
+      state.result = {all: all}
+      return state
 
     visit: (el, state) ->
       desc = new Description(el, state)
-      state.result.push(desc)
+      res = state.result
+      current = state.current
+      nextObj = current
+
+      s = desc.getSubject()
+      if s
+        current = res.all[s]
+        unless current
+          current = res.all[s] = {"@id": s}
+
+      o = desc.getReference()
+      if o
+        nextObj = res.all[o]
+        unless nextObj
+          nextObj = res.all[o] = {"@id": o}
+
+        for link in desc.getLinks()
+          refs = current[link]
+          unless refs
+            refs = current[link] = []
+          refs.push({"@id": o})
+
+      value = desc.getValue()
+      if value
+        for prop in desc.getValueProperties()
+          values = current[prop]
+          unless values
+            values = current[prop] = []
+          values.push(value)
+
       subState = desc.getChildState()
-      subState.result = desc.items = []
-      delete desc.state
+      subState.current = nextObj
       return subState
 
     complete: (state) ->
-      state.result
-      #result = state.result
-      #{'@context': result.context, '@graph': result.graph}
+      items = []
+      for s, obj of state.result.all
+        add = true
+        if obj["@id"]
+          add = false
+          for key of obj
+            if key != "@id"
+              add = true
+              break
+        if add
+          items.push(obj)
+      items
+      #{'@context': state.result.context, '@graph': items}
 
 
   class State
-    constructor: (base=null, @resolveURI) ->
+    constructor: (base=null, @profile, @resolveURI) ->
       @mappings = {base: base, lang: null, vocab: null, prefixes: {}}
       @lists = {}
       @hanging = {rels: [], revs: [], lists: []}
       @result = null
+      @current = null
 
     newLocal: (base, lang, vocab, prefixes) ->
       # TODO: new State with updated members
       ctor = () ->
       ctor.prototype = this
       return new ctor
+
+
+  class Mappings
 
 
   ##


    getAbout: ->
      if @attrs.about? #and not parent
        next = {'@id': @state.resolveURI(@attrs.about.value)}

    getResource: ->
      if @attrs.resource?
        next = {'@id': @state.resolveURI(@attrs.resource.value)}
      else if @attrs.href?
        next = {'@id': @el.href}
      else if @attrs.src?
        next = {'@id': @state.resolveURI(@attrs.src.value)}

    getTypes: ->
      @attrs.typeof?.value.split(/\s+/)

    getContent: ->
      if @attrs.content?
        return @attrs.content.value
      else if @state.profile is 'html' and @tagName is 'time'
        if @attrs.datetime?
          return @attrs.datetime.value
      return @el.textContent
