Ejemplo n.º 1
0
 def extend(self, goals, variables):
   """
   Extends the current example by using the fol_bc_ask algorithm to determine
   values for the new variables.
   
   @param goals: The goals by which the extension of the example is to be 
       calculated.
   @type goals: A list of Term objects.
   @param variables: The new variables in the extension.
   @type variables: A list of Var objects.
   @return: The number of examples created from extending.
   """
   logger.debug("extend( " + str(goals) + ", " + str(variables) + " )")
   extension_count = 0
   examples_count = 0
   for node, bindings in self.enumerate_nodes_bindings():
     extended = False
     logger.debug("calling " + str(fol_bc_ask) + " with '" + str(goals) 
                   + "' '" + str(bindings) + "'")
     for answer in fol_bc_ask(goals, bindings):
       if answer != None:
         examples_count += 1
         extended = True
         value = []
         for var in variables:
           value.append(answer[var])
         node.children.append(ExampleTree.Node(variables, value, node))
     if extended:
       extension_count += 1
   self.levels += 1
   self._count_list.append(examples_count)
   logger.debug("Performed " + str(extension_count) + " extensions.")
   return extension_count
Ejemplo n.º 2
0
 def assertNotFollows(self, arg0, arg1=None):
   msg, term = None, None
   if isinstance(arg0, str):
     msg = arg0
     term = arg1
   else:
     term = arg0
   for x in fol_bc_ask([term], {}):
     if msg:
       self.fail(msg)
     else:
       self.fail()
Ejemplo n.º 3
0
 def prune_covered(self):
   logger.debug("Examples to consider for pruning: ")
   logger.debug(str(self._examples))
   for ex in self._examples[:]:
     prune = False
     logger.debug("Calling fol_bc_ask(" + str(self.predicate(*ex.root.values)) + ", {})")
     for answer in fol_bc_ask([self.predicate(*ex.root.values)], {}):
       logger.debug("fol_bc_ask(" + str(self.predicate(*ex.root.values)) 
                    + ", {}) -> " + str(answer))
       if answer != None and answer != False:
         self._examples.remove(ex)
         prune = True
         logger.debug("Pruned '%s'." % ex)
       break
     if not prune: logger.debug("Did not prune '%s'." % ex)
Ejemplo n.º 4
0
def determine_param_orderings(predicate):
  logger.debug("Determining ordering for: " + str(predicate))
  
  def establish_relationship(value_pair, index_pair, op, cmp_map):
    x,y = value_pair
    i,k = index_pair
    if not(cmp_map.has_key((i,k)) and cmp_map[(i,k)] == None):
      if op(x,y):
        if cmp_map.has_key((i,k)): 
          if op != cmp_map[(i,k)]:
            cmp_map[(i,k)] = None
        else:
          cmp_map[(i,k)] = op
      elif cmp_map.has_key((i,k)) and op == cmp_map[(i,k)]:
        cmp_map[(i,k)] = None
      logger.debug(str(cmp_map))
  # end establish_relationship
      
  v = UniqueVariableFactory()
  type_map = {}
  types = predicate.param_types
  for i, type in zip(range(0, len(types)), types):
    if not type_map.has_key(type):
      type_map[type] = []
    type_map[type].append(i)
  pairs = []
  for type in type_map.keys():
    for x in choose(type_map[type], 2):
      pairs.append(list(x))
  cmp_map = {}
  variables = v.next_variable_sequence(predicate.arity)
  logger.debug("Calling: fol_bc_ask( %s )" % predicate(*variables))
  for answer in fol_bc_ask([predicate(*variables)], {}):
    logger.debug("Answer: " + str(answer))
    for pair in pairs:
      i,k = pair
      x,y = answer[variables[i]], answer[variables[k]]
      logger.debug("Comparing %s and %s." % (x, y))
      try:
        for op in [operator.lt, operator.gt, operator.eq]:
          establish_relationship((x,y), (i,k), op, cmp_map)
      except:
        pass
  return cmp_map
Ejemplo n.º 5
0
def determine_covered(predicate, tuple):
  for x in fol_bc_ask([predicate(*tuple)], {}):
    return True
  return False
Ejemplo n.º 6
0
def consult(query):
  print str(query)
  for answer in fol_bc_ask(query, {}): print "Answer: " + str(answer)