예제 #1
0
 def read_sparql(self, query, bindings_set = [{}], keep_old_bindings = False) :
   """
   read from the sparql database
   @arg query the query in one long string, a list of string or triples_set
   @arg bindings_set is a set of bindings to apply on the way in
   @arg keep_old_bindings if True will keep variable bound in the incoming 
   bindings_set in the new bindings_set
   @return a new set of bindings
   """
   if not self.sparql :
     raise Exception("sparql not initialized")
   
   results = []
   query_triples = self.parser.parse(query)
   #p('query_triples',query_triples)
   #for triples in sub_var_bindings_set(query_triples, bindings_set) :
   for bindings in explode_bindings_set(bindings_set) :
     triples = sub_var_bindings(query_triples, bindings)
     #self.sanitize_vars(triples)
     read_bindings_set = self.sparql.read(triples, outvarnamespace = LitVar)
     if keep_old_bindings :
       #p('bindings',bindings)
       #p('read_bindings_set',read_bindings_set)
       for read_bindings in read_bindings_set :
         new_bindings = copy.copy(bindings)
         new_bindings.update(read_bindings)
         results.append(new_bindings)
     else :
       results.extend(read_bindings_set)
   return results
예제 #2
0
    def read_sparql(self, query, bindings_set=[{}], keep_old_bindings=False):
        """
    read from the sparql database
    @arg query the query in one long string, a list of string or triples_set
    @arg bindings_set is a set of bindings to apply on the way in
    @arg keep_old_bindings if True will keep variable bound in the incoming 
    bindings_set in the new bindings_set
    @return a new set of bindings
    """
        if not self.sparql:
            raise Exception("sparql not initialized")

        results = []
        query_triples = self.parser.parse(query)
        #p('query_triples',query_triples)
        #for triples in sub_var_bindings_set(query_triples, bindings_set) :
        for bindings in explode_bindings_set(bindings_set):
            triples = sub_var_bindings(query_triples, bindings)
            #self.sanitize_vars(triples)
            read_bindings_set = self.sparql.read(triples,
                                                 outvarnamespace=LitVar)
            if keep_old_bindings:
                #p('bindings',bindings)
                #p('read_bindings_set',read_bindings_set)
                for read_bindings in read_bindings_set:
                    new_bindings = copy.copy(bindings)
                    new_bindings.update(read_bindings)
                    results.append(new_bindings)
            else:
                results.extend(read_bindings_set)
        return results
예제 #3
0
 def python(self, query, bindings_set=[{}]):
     new_bindings_set = []
     for bindings in bindings_set:
         # TODO don't allow people to break in!  Not sure how good this is ...
         bindings['__builtins__'] = None
         bindings['bindings'] = bindings
         bindings['str'] = str
         exec query in bindings
         del bindings['__builtins__']
         del bindings['bindings']
         del bindings['str']
         new_bindings_set.extend(explode_bindings_set(bindings))
     return new_bindings_set
예제 #4
0
 def python(self, query, bindings_set = [{}]) :
   new_bindings_set = []
   for bindings in bindings_set :
     # TODO don't allow people to break in!  Not sure how good this is ...
     bindings['__builtins__'] = None
     bindings['bindings'] = bindings
     bindings['str'] = str
     exec query in bindings
     del bindings['__builtins__']
     del bindings['bindings']
     del bindings['str']
     new_bindings_set.extend(explode_bindings_set(bindings))
   return new_bindings_set
예제 #5
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
예제 #6
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
예제 #7
0
    def write_sparql_delete(self, query, bindings_set=[{}]):
        """
    delete triples from 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 triples in sub_var_bindings_set(query_triples, bindings_set):
            # replace any kind of var with a 'standard' var for SimpleSPARQL
            for triple in triples:
                for i, v in enumerate(triple):
                    if is_any_var(v):
                        triple[i] = Var(v.name)
            self.sparql.delete(triples)
예제 #8
0
  def write_sparql_delete(self, query, bindings_set = [{}]) :
    """
    delete triples from 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 triples in sub_var_bindings_set(query_triples, bindings_set) :
      # replace any kind of var with a 'standard' var for SimpleSPARQL
      for triple in triples :
        for i, v in enumerate(triple) :
          if is_any_var(v) :
            triple[i] = Var(v.name)
      self.sparql.delete(triples)
예제 #9
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
예제 #10
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
예제 #11
0
  def evaluate_step_with_bindings_set(self, step, q_in_bs) :
    """
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !! the trick here I think is that I am trying to reuse code between
    !! function and multifunction cases that aren't the same.  Its hard to
    !! make the code more complex and repetitive at this point, but I think
    !! there are bugs in the code as it is now due to its false simplicity
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    """
    # descriptions of variables
    # q_in_bs      : query space to value
    # t_to_q_in_b  : translation to query space
    # t_in_bs      : translation space to value
    # t_out_bs     : translation space to value
    # t_to_q_out_b : translation to query space
    # q_out_bs     : query space to value
    
    t_to_q_in_b  = step['input_bindings']
    t_to_q_out_b = step['output_bindings']
    
    t_in_bs = self.map_q_to_t(q_in_bs, t_to_q_in_b)

    # NOTE that if t_out_b == [] aka. the input binding set is no longer valid
    # we want that to be added to the ret, not the input bindings
    if 'function' in step['translation'] :
      q_out_bs = []
      for t_in_b, q_in_b in izip(t_in_bs, q_in_bs) :
        t_out_b = step['translation']['function'](t_in_b)
        if t_out_b == None :
          t_out_b = t_in_b

        self.t_out_b_valid(step, t_out_b, t_to_q_out_b)

        for t_out_b in self.flatten(t_out_b) :
          # need to make a copy so that we don't mess up code
          # somewhere else that expects to be able to use q_in_b later
          q_out_b = copy.copy(q_in_b)
          q_out_b.update(self.map_t_to_q(t_out_b, t_to_q_out_b, t_in_b))
          q_out_bs.append(q_out_b)
    elif 'multi_function' in step['translation'] :
      # a multi_function takes in the entire t_in_bs and returns
      # an entire new one, rather than the normal function which is fed one
      # at a time (and can return any number of results)
      t_out_bs = step['translation']['multi_function'](t_in_bs)
      if t_out_bs == None :
        t_out_bs = t_in_bs

      self.t_out_b_valid(step, t_out_bs, t_to_q_out_b)

      # allow multi_function to return a single dict, if that is the
      # only binding in the result set
      if isinstance(t_out_bs, dict) :
        t_out_bs = [t_out_bs]
      
      q_out_bs = [self.map_t_to_q(t_out_b, t_to_q_out_b)
                  for t_out_b in t_out_bs]
    else :
      raise Exception("translation doesn't have a function ...")
  
    # handle values which are lists, which were really short hand for many
    # possibilities (see glob.glob)
    q_out_bs = explode_bindings_set(q_out_bs)

    return q_out_bs