Execute

def execute(self, triple_map, output, **kwargs):
        """Execute """
        subjects = []
        if NS_MGR.ql.JSON.rdflib in \
                triple_map.logicalSource.reference_formulations:
            output_format = "json"
        else:
            output_format = "xml"
        if 'limit' not in kwargs:
            kwargs['limit'] = self.limit
        if 'offset' not in kwargs:
            kwargs['offset'] = self.offset
        # log.debug("triple_map.logicalSource: \n%s",
                  # pprint.pformat(triple_map.logicalSource.__dict__))
        iterator = str(triple_map.logicalSource.iterator)
        start = datetime.datetime.now()
        key, json_query = None, None
        # pdb.set_trace()
        if hasattr(triple_map.logicalSource, 'json_query') \
                and self.use_json_qry:
            key = kwargs.get(str(triple_map.logicalSource.json_key))
            if not key:
                key =[val for val in kwargs.values() \
                      if isinstance(val, rdflib.URIRef)][0]
            json_query = triple_map.logicalSource.json_query
            bindings = kwargs['dataset'].json_qry(json_query, {'$': key})
        else:
            sparql = PREFIX + triple_map.logicalSource.query.format(**kwargs)
            bindings = self.__get_bindings__(sparql, output_format)
        for binding in bindings:
            if key:
                try:
                    entity_raw = binding.subject.rdflib
                except AttributeError:
                    entity_raw = binding
            else:
                entity_raw = binding.get(iterator)
            if isinstance(entity_raw, (rdflib.URIRef,
                                       rdflib.BNode,
                                       BaseRdfDataType)):
                entity = entity_raw
            else:
                raw_value = entity_raw.get('value')
                if entity_raw.get('type').startswith('bnode'):
                    entity = BlankNode(raw_value)
                else:
                    entity = Uri(raw_value)
            if triple_map.subjectMap.class_ is not None:
                sub = entity
                if isinstance(entity, BaseRdfDataType):
                    sub = entity.rdflib
                output.add((sub,
                                 NS_MGR.rdf.type.rdflib,
                                 triple_map.subjectMap.class_))
            # pdb.set_trace()
            for pred_obj_map in triple_map.predicateObjectMap:
                predicate = pred_obj_map.predicate
                kwargs[iterator] = entity

                if pred_obj_map.parentTriplesMap is not None:
                    self.__handle_parents__(
                            output=output,
                            parent_map=pred_obj_map.parentTriplesMap,
                            subject=entity,
                            predicate=predicate,
                            **kwargs)
                    continue
                if pred_obj_map.reference is not None:
                    ref_key = str(pred_obj_map.reference)
                    if pred_obj_map.json_query:
                        # if pred_obj_map.json_query =="$.schema_logo":
                        # pdb.set_trace()
                        if ref_key in binding:
                            for item in binding[ref_key]:
                                output.add((entity,
                                                 predicate,
                                                 item.rdflib))
                            continue
                    else:
                        if ref_key in binding:
                            object_ = __get_object__(
                                binding[ref_key])
                            output.add((entity, predicate, object_))
                        continue
                if pred_obj_map.constant is not None:
                    if isinstance(entity, BaseRdfDataType):
                        entity = entity.rdflib
                    output.add(
                        (entity, predicate, pred_obj_map.constant))
                    continue

                json_query = None
                if pred_obj_map.json_query and self.use_json_qry:
                    json_query = pred_obj_map.json_query
                    start = datetime.datetime.now()
                    # pdb.set_trace()
                    # if str(pred_obj_map.predicate) == "http://purl.org/dc/terms/creator":
                    #     pdb.set_trace()
                    pre_obj_bindings = kwargs['dataset'].json_qry(json_query,
                                                                  {'$': entity})
                else:
                    sparql_query = PREFIX + pred_obj_map.query.format(**kwargs)
                    pre_obj_bindings = self.__get_bindings__(sparql_query,
                                                             output_format)

                for row in pre_obj_bindings:
                    if json_query and self.use_json_qry:
                        if isinstance(entity, BaseRdfDataType):
                            entity = entity.rdflib
                        output.add((entity, predicate, row.rdflib))
                    else:
                        object_ = __get_object__(row)
                        if object_ is None:
                            continue
                        if isinstance(entity, BaseRdfDataType):
                            entity = entity.rdflib
                        output.add((entity, predicate, object_))
            subjects.append(entity)
        return subjects