Exemplo n.º 1
0
    def read_translate(self, query, bindings_set=[{}], reqd_bound_vars=[]):
        query_triples = self.parser.parse(query)
        ret_evals = []
        bindings_set = explode_bindings_set(bindings_set)
        if len(reqd_bound_vars) == 0:
            reqd_bound_vars = find_vars(query_triples, is_lit_var)
            if len(reqd_bound_vars) == 0:
                p('Warning: no required bound variables.  Are there any _vars?'
                  )

        #for triples in sub_var_bindings_set(query_triples, bindings_set) :
        for bindings in bindings_set:
            #p('bindings',bindings)
            #p('reqd_bound_vars',reqd_bound_vars)
            new_reqd_bound_vars = []
            provided_bindings = []
            for var in reqd_bound_vars:
                if var in bindings:
                    provided_bindings.append(var)
                else:
                    new_reqd_bound_vars.append(var)
            reqd_bound_vars = new_reqd_bound_vars

            #p('reqd_bound_vars',reqd_bound_vars)
            #p('provided_bindings',provided_bindings)
            triples = sub_var_bindings(query_triples, bindings)
            begin_compile = time.time()
            ret_comp = self.compiler.compile(triples, reqd_bound_vars)
            end_compile = time.time()
            #p('ret_comp',ret_comp)
            if 'time' in self.options:
                print 'compile time:', end_compile - begin_compile
                self.cum_comp_time += end_compile - begin_compile
            if ret_comp == False:
                raise CompilerException(
                    "Couldn't compile ... sorry I don't have more here")
            begin_eval = time.time()
            #for i in range(100) :
            ret_eval = self.evaluator.evaluate(ret_comp)
            end_eval = time.time()
            if 'time' in self.options:
                print 'eval time:', end_eval - begin_eval
                self.cum_eval_time += end_eval - begin_eval
            for ret in ret_eval:
                for var in provided_bindings:
                    ret[var] = bindings[var]
            #p('ret_eval',ret_eval)
            ret_evals.extend(ret_eval)

        return ret_evals
Exemplo n.º 2
0
 def read_translate(self, query, bindings_set = [{}], reqd_bound_vars = []) :
   query_triples = self.parser.parse(query)
   ret_evals = []
   bindings_set = explode_bindings_set(bindings_set)
   if len(reqd_bound_vars) == 0 :
     reqd_bound_vars = find_vars(query_triples, is_lit_var)
     if len(reqd_bound_vars) == 0 :
       p('Warning: no required bound variables.  Are there any _vars?')
   
   #for triples in sub_var_bindings_set(query_triples, bindings_set) :
   for bindings in bindings_set :
     #p('bindings',bindings)
     #p('reqd_bound_vars',reqd_bound_vars)
     new_reqd_bound_vars = []
     provided_bindings = []
     for var in reqd_bound_vars :
       if var in bindings :
         provided_bindings.append(var)
       else :
         new_reqd_bound_vars.append(var)
     reqd_bound_vars = new_reqd_bound_vars
     
     #p('reqd_bound_vars',reqd_bound_vars)
     #p('provided_bindings',provided_bindings)
     triples = sub_var_bindings(query_triples, bindings)
     begin_compile = time.time()
     ret_comp = self.compiler.compile(triples, reqd_bound_vars)
     end_compile = time.time()
     #p('ret_comp',ret_comp)
     if 'time' in self.options :
       print 'compile time:',end_compile-begin_compile
       self.cum_comp_time += end_compile-begin_compile
     if ret_comp == False :
       raise CompilerException("Couldn't compile ... sorry I don't have more here")
     begin_eval = time.time()
     #for i in range(100) :
     ret_eval = self.evaluator.evaluate(ret_comp)
     end_eval = time.time()
     if 'time' in self.options :
       print 'eval time:',end_eval-begin_eval
       self.cum_eval_time += end_eval-begin_eval
     for ret in ret_eval :
       for var in provided_bindings :
         ret[var] = bindings[var]
     #p('ret_eval',ret_eval)
     ret_evals.extend(ret_eval)
     
   return ret_evals
Exemplo n.º 3
0
  def write_sparql(self, query, bindings_set = [{}]) :
    """
    write triples into sparql database.
    NOTE: any URI which is_var will be converted to a fresh bnode URI
    """
    if not self.sparql :
      raise Exception("sparql not initialized")

    query_triples = self.parser.parse(query)
    bindings_set = explode_bindings_set(bindings_set)
    for bindings in bindings_set :
      triples = sub_var_bindings(query_triples, bindings)
      missing_vars = find_vars(triples)
      if len(missing_vars) is not 0 :
        new_bindings = dict([(var, self.urigen()) for var in missing_vars])
        triples = sub_var_bindings(triples, new_bindings)
        bindings.update(new_bindings)
      self.sparql.write(triples)
    return bindings_set
Exemplo n.º 4
0
    def write_sparql(self, query, bindings_set=[{}]):
        """
    write triples into sparql database.
    NOTE: any URI which is_var will be converted to a fresh bnode URI
    """
        if not self.sparql:
            raise Exception("sparql not initialized")

        query_triples = self.parser.parse(query)
        bindings_set = explode_bindings_set(bindings_set)
        for bindings in bindings_set:
            triples = sub_var_bindings(query_triples, bindings)
            missing_vars = find_vars(triples)
            if len(missing_vars) is not 0:
                new_bindings = dict([(var, self.urigen())
                                     for var in missing_vars])
                triples = sub_var_bindings(triples, new_bindings)
                bindings.update(new_bindings)
            self.sparql.write(triples)
        return bindings_set
Exemplo n.º 5
0
	def new_read(self, query, expected_vars = [], varnamespace = None, outvarnamespace = None) :
		#print('query',query)
		# TODO: extract out the result bindings as in self.read
		# TODO: allow object notation as root query.
		#   TODO: depricate read, and others
		#   TODO: more cleaning
		# TODO: figure out some way to avoid having resets everywhere.  Should this
		#   be some other class to do each translation?
		if varnamespace == None :
			varnamespace = self.n.var
		
		# for now, these need to come in the triples set in the same order as 
		# required by SPARQL.  LIMIT after ORDER and OFFSET after LIMIT
		modifiers = []
		new_query = []
		count = False
		for triple in query :
			modified = False
			if triple[0] == self.n.query.query :
				if triple[1] == self.n.query.limit :
					modifiers.append('LIMIT %d' % int(triple[2]))
					modified = True
				if triple[1] == self.n.query.offset :
					modifiers.append('OFFSET %d' % int(triple[2]))
					modified = True
				if triple[1] == self.n.query.sort_ascending :
					modifiers.append('ORDER BY ?%s' % var_name(triple[2]))
					modified = True
				if triple[1] == self.n.query.sort_descending :
					modifiers.append('ORDER BY DESC(?%s)' % var_name(triple[2]))
					modified = True
				if triple[1] == self.n.query['count'] :
					count = triple[2]
					if not is_any_var(count) :
						raise Exception('When query.query[query.count] = count. count must be a variable')
					count = var_name(count)
					modified = True

			
			if not modified :
				new_query.append(triple)
		query = new_query
		
		# determine what the output vars are before they are sanitized for 
		# triplelist_to_sparql
		if count :
			output_vars_list = 'COUNT(*)'
		else :
			output_vars_list = '*'
			if outvarnamespace :
				output_vars = find_vars(query, lambda var: var.find(outvarnamespace) == 0)
				if len(output_vars) :
					output_vars_list = ' '.join(map(lambda x:'?'+x, output_vars))
					self.sanitize_vars(query, outvarnamespace, varnamespace)
				else :
					print("couldn't find them")
		#print('output_vars_list',output_vars_list)
		
		self.reset_py_to_SPARQL_bnode()
		self._reset_SPARQL_variables()
		query_str = self.triplelist_to_sparql(query, varnamespace)
		#print('query',query)
		query = "SELECT %s WHERE { %s } %s" % (output_vars_list, self.wrapGraph(query_str), '\n'.join(modifiers))
		ret = self.doQuery(query)
		if count :
			# NOTE: This is most likely implementation dependant.  Works for Joseki-3.3.0
			yield {count : int(ret['results']['bindings'][0][u'.1']['value'])}
		else :
			for binding in ret['results']['bindings'] :
				newbinding = {}
				for var, value in binding.iteritems() :
					if value['type'] == 'typed-literal' :
						if value['datatype'] == 'http://www.w3.org/2001/XMLSchema#integer' :
							newbinding[var] = int(value['value'])
						elif value['datatype'] == 'http://www.w3.org/2001/XMLSchema#decimal' :
							newbinding[var] = float(value['value'])
					elif value['type'] == 'literal' :
						newbinding[var] = value['value']
					elif value['type'] == 'uri' :
						newbinding[var] = URIRef(value['value'])
					elif value['type'] == 'bnode' :
						raise Exception('cant do bnodes')
				
				yield newbinding
Exemplo n.º 6
0
	def bound_vars_general(self, g):
		triples = self.parser.parse(g.group(1), reset_bnodes = False)
		return find_vars(triples), triples