This function is currently geared very much towards writing
        STIX/CybOX objects to a dictionary. That should not be the case -- the function
        needs to be generic just as the from_dict function.
        TODO: make function generic.

def to_dict(self,include_node_id=False,no_attributes=False,track_namespaces=False):
        """
        This function is currently geared very much towards writing
        STIX/CybOX objects to a dictionary. That should not be the case -- the function
        needs to be generic just as the from_dict function.
        TODO: make function generic.
        """
        flat_result = []

        def make_ns_slug(name_counter,slug='n'):
            while "%s%s" % (slug,name_counter['counter']) in namespace_mapping.values():
                name_counter['counter']  = name_counter['counter']+1

            return "%s%s" % (slug,name_counter['counter'])

        name_counter = {'counter':0}

        if track_namespaces:

            fact_thrus = self.fact_thru.all().prefetch_related(
                'fact__fact_term',
                'fact__fact_values',
                'fact__fact_values__fact_data_type',
                'fact__fact_values__fact_data_type__namespace',
                'fact__value_iobject_id',
                'fact__value_iobject_id__namespace',
                'namespace_map__namespaces_thru__namespace',
                'node_id')
        else:
            fact_thrus = self.fact_thru.all().prefetch_related(
                'fact__fact_term',
                'fact__fact_values',
                'fact__fact_values__fact_data_type',
                'fact__fact_values__fact_data_type__namespace',
                'fact__value_iobject_id',
                'fact__value_iobject_id__namespace',
                'node_id')

        export_ns_dict = {}

        namespace_mapping=  {"%s-%s" % (self.iobject_type.namespace.uri,self.iobject_type_revision): 'n0'}
        #fact_thrus = self.fact_thru.all()
        for fact_thru in fact_thrus:
            #print fact_thru.node_id
            #print fact_thru.fact.fact_term
            #for positional_namespace in fact_thru.namespace_map.namespaces_thru.all():
            #    print positional_namespace

            value_list = []
            first = True
            fact_datatype_name = None
            fact_datatype_ns = None

            fact_dict = {'node_id': fact_thru.node_id.name,
                         'term': fact_thru.fact.fact_term.term,
                         'attribute' : fact_thru.fact.fact_term.attribute,
                         '@@namespace_map' : fact_thru.namespace_map,
                         }

            for fact_value in fact_thru.fact.fact_values.all():
                if first:
                    first=False
                    fact_datatype_name = fact_value.fact_data_type.name
                    fact_datatype_ns = fact_value.fact_data_type.namespace.uri
                    if (fact_datatype_name == DINGOS_DEFAULT_FACT_DATATYPE and
                                fact_datatype_ns == DINGOS_NAMESPACE_URI) or fact_thru.fact.value_iobject_id:
                        pass
                    else:
                        if not fact_datatype_ns in namespace_mapping:
                            namespace_slug = make_ns_slug(name_counter)
                            namespace_mapping[fact_datatype_ns] = namespace_slug
                        else:
                            namespace_slug= namespace_mapping[fact_datatype_ns]
                        fact_dict['@@type'] = '%s:%s' % (namespace_slug,fact_datatype_name)

                value_list.append(fact_value.value)




            fact_dict['value_list'] = value_list


            if fact_thru.fact.value_iobject_id:
                value_iobject_id_ns = fact_thru.fact.value_iobject_id.namespace.uri
                if not value_iobject_id_ns in namespace_mapping:
                    namespace_slug = make_ns_slug(name_counter)
                    namespace_mapping[value_iobject_id_ns] = namespace_slug
                else:
                    namespace_slug= namespace_mapping[value_iobject_id_ns]

                value_iobject_id  =fact_thru.fact.value_iobject_id.uid
                if fact_dict['attribute']:
                    # Here we treat the case that the reference is part of an attribute such as
                    # 'phase_id'
                    fact_dict['value_list'] = ["%s:%s" % (namespace_slug,value_iobject_id)]
                else:
                    # Otherwise, we sneak in an idref attribute. Because the code that
                    # generates the dictionary simply dumps all untreated key-value paris
                    # into the generated dictionary, this works... but is a bit of a hack, really.

                    fact_dict['@idref'] = "%s:%s" % (namespace_slug,value_iobject_id)

            flat_result.append(fact_dict)

        result = DingoObjDict()


        result.from_flat_repr(flat_result,
            include_node_id=include_node_id,
            no_attributes=no_attributes,
            track_namespaces=track_namespaces,
            namespace_mapping=namespace_mapping
            )


        if not no_attributes:
            if not track_namespaces:
                result['@@iobject_type'] = self.iobject_type.name
                result['@@iobject_type_ns'] = self.iobject_type.namespace.uri
                return result
            else:
                result['@ns'] = namespace_mapping["%s-%s" % (self.iobject_type.namespace.uri,self.iobject_type_revision)]
                #result['@@iobject_type'] = self.iobject_type.name
                return {'namespaces': dict(map(lambda x : (x[1],x[0]), namespace_mapping.items())),
                        'objects' : [result]
                }
        else:
            return result