Ejemplo n.º 1
0
bool KnowledgeBase::IsSatisfiable(const Argument& arg)
{
  // get answer
  Answer *ans = GetAnswer(arg, VariableMap(), std::set<size_t>());
  // return if any valid substitution exists
  bool res = ans->next();
  delete ans;
  return res;
}
Ejemplo n.º 2
0
bool KnowledgeBase::IsSatisfiable(const std::string& s_arg)
{
  Argument arg(s_arg, symbol_table, true, log);
  // get answer
  Answer *ans = GetAnswer(arg, VariableMap(), std::set<size_t>());
  // return if any valid substitution exists
  bool res = ans->next();
  delete ans;
  return res;
}
Ejemplo n.º 3
0
std::list<Argument*> KnowledgeBase::Ask(const Argument& arg,
                                        bool checkForDoubles)
{
  std::list<Argument*> out;

  // get answer
  Answer *ans = GetAnswer(arg, VariableMap(), std::set<size_t>());
  // get all the valid substitution and add them to list

  if(!checkForDoubles)
  {
    while(ans->next())
    {
      Argument* temp = Unify::GetSubstitutedArgument(&arg, ans->GetVariableMap());
      out.push_back(temp);
    }
  }
  else
  {
    std::set<std::string> str_ans;
    while(ans->next())
    {
      Argument* temp = Unify::GetSubstitutedArgument(&arg, ans->GetVariableMap());
      std::stringstream stream;
      stream << *temp;

      //SymbolDecodeStream sds(symbol_table);
      //sds << *temp << endl;

      if(str_ans.find(stream.str()) == str_ans.end())
      {
        out.push_back(temp);
        str_ans.insert(stream.str());
      }
      else delete temp;
    }
  }
  // delete answer
  delete ans;

  return out;
}
Ejemplo n.º 4
0
void KIFFlattener::FlattenRelation(const DGraphNode* n,
                                   const KnowledgeBase& all_kb,
                                   const std::set<size_t>& state_independent,
                                   KnowledgeBase& m_kb,
                                   std::list<Clause>& f_clauses,
                                   std::list<Fact>& f_facts)
{
  //compute signature of relation
  const size_t& sig = n->id;

  // get all the facts and clauses associated with this signature
  const KnowledgeBase::FactList* facts = all_kb.GetFacts(sig);
  const KnowledgeBase::ClauseList* p_clauses = all_kb.GetClauses(sig);

  if(p_clauses == NULL)
  {
    if(facts != NULL)
      for(KnowledgeBase::FactList::const_iterator it = facts->begin();
                                                      it != facts->end();it++)
      {
        f_facts.push_back(*it);
        m_kb.Tell(*it);
      }
    return;
  }

  const KnowledgeBase::ClauseList& clauses = *p_clauses;

  // to store the heads of the flattened clauses
  // these will be added later to temporary knowledge base
  list<Argument*> f_heads;

  // start flattening clauses
  for(list<Clause>::const_iterator it = clauses.begin();it != clauses.end();it++)
  {
    // if the clause is already ground add it directly
    // add its head to heads list
    if((*it).IsGround())
    {
      Clause* to_add = new Clause(*it);
      f_clauses.push_back(*to_add);
      f_heads.push_back(to_add->head);
      to_add->head = NULL;
      delete to_add;
      continue;
    }

    // pre-process the clause
    // adjust the extra variables which are present in body but not head
    // adjust 'not' relation appropriately
    Clause* p_clause = ProcessClause(*it, state_independent);

    // add the processed clause temporarily to knowledge base
    size_t c_index = m_kb.Tell(*p_clause);

    VariableMap h_v_map;
    Argument* question = SpecialArgCopy2(p_clause->head, h_v_map);

    // after adding the head of the clause will be the question to ask
    Answer* ans = m_kb.GetAnswer(*question, VariableMap(), set<size_t>());


    while(ans->next())
    {
      VariableMap ans_v_map = ans->GetVariableMap();
      for(auto va : h_v_map)
        ans_v_map.insert(va);

      // compute the answer with substitution
      Clause* to_add = Unify::GetSubstitutedClause(&(*it), ans_v_map);

      // remove all the occurrences of data relations
      Clause* temp = RemoveDataFromClause(to_add, state_independent);

      // check whether clause can be converted to fact after data relations removal
      if(temp == NULL)
      {
        Argument* h = to_add->head;
        to_add->head = NULL;
        delete to_add;
        f_facts.push_back(*h);
        f_facts.back().loc = (*it).loc;
        f_facts.back().isLocation = (*it).isLocation;
        f_heads.push_back(h);
      }
      else
      {
        f_clauses.push_back(*temp);
        f_clauses.back().loc = (*it).loc;
        f_clauses.back().isLocation = (*it).isLocation;
        f_heads.push_back(temp->head);
        temp->head = NULL;
        delete temp;
      }
    }

    // delete answer
    delete ans;

    // erase the temporary knowledge from knowledge base
    m_kb.Erase(*p_clause, c_index);

    // delete the processed clause(without deleting the variables
    SpecialClauseDelete(p_clause);

    delete question;
  }

  // add all the facts related to this relation to knowledge base
  if(facts != NULL)
    for(list<Fact>::const_iterator it = facts->begin();it != facts->end();it++)
    {
      f_facts.push_back(*it);
      m_kb.Tell(*it);
    }

  // compute signature and add to appropriate FactVec
  size_t command = n->id;
  KnowledgeBase::FactList& fl = m_kb.m_facts[command];

  // add heads of all the clauses to knowledge base
  for(list<Argument*>::iterator it = f_heads.begin();it != f_heads.end();it++)
  {
    Fact f;
    f.arg = *it;

    fl.push_back(std::move(f));
  }
}