<commit_before>/*
 * OpenPsiRules.cc
 *
 * Copyright (C) 2017 MindCloud
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License v3 as
 * published by the Free Software Foundation and including the exceptions
 * at http://opencog.org/wiki/Licenses
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, write to:
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <opencog/atoms/base/Node.h>
#include <opencog/atoms/base/Link.h>

#include "OpenPsiRules.h"

using namespace opencog;

std::map<Handle, OpenPsiRules::PsiTuple> OpenPsiRules::_psi_rules = {};
Handle OpenPsiRules::_psi_action = \
  Handle(createNode(CONCEPT_NODE, "OpenPsi: action"));
Handle OpenPsiRules::_psi_goal = \
  Handle(createNode(CONCEPT_NODE, "OpenPsi: goal"));
Handle OpenPsiRules::_psi_demand = \
  Handle(createNode(CONCEPT_NODE, "OpenPsi: demand"));

OpenPsiRules::OpenPsiRules(AtomSpace* as): _as(as)
{}

Handle OpenPsiRules::add_rule(const HandleSeq& context, const Handle& action,
  const Handle& goal, const TruthValuePtr stv, const Handle& demand)
{
  // Declare as an openpsi action if it hasn't already been declared.
  _as->add_link(MEMBER_LINK, action, _psi_action);

  // Add a SequentialAndLink of context and action which forms the
  // implicant of the psi-rule.
  HandleSeq temp_c =  context;
  temp_c.push_back(action);
  Handle hca = _as->add_link(SEQUENTIAL_AND_LINK, temp_c);

  // Add the psi-rule, set the truthvalue and add to a demand.
  Handle rule = _as->add_link(IMPLICATION_LINK, hca, goal);
  rule->setTruthValue(stv);
  _as->add_link(MEMBER_LINK, rule, demand);

  // Construct the query atom that is used to check satisfiablity
  Handle query_body = Handle(createLink(context, AND_LINK)) ;

  // Add to the index of rules.
  _psi_rules[rule] = std::make_tuple(context, action, goal, query_body);

  return rule;
}

Handle OpenPsiRules::add_tag(const Handle tag_type_node,
                             const std::string& name)
{
  Handle tag = _as->add_node(CONCEPT_NODE, name);<commit_msg>No need to copy<commit_after>/*
 * OpenPsiRules.cc
 *
 * Copyright (C) 2017 MindCloud
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License v3 as
 * published by the Free Software Foundation and including the exceptions
 * at http://opencog.org/wiki/Licenses
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, write to:
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <opencog/atoms/base/Node.h>
#include <opencog/atoms/base/Link.h>

#include "OpenPsiRules.h"

using namespace opencog;

std::map<Handle, OpenPsiRules::PsiTuple> OpenPsiRules::_psi_rules = {};
Handle OpenPsiRules::_psi_action = \
  Handle(createNode(CONCEPT_NODE, "OpenPsi: action"));
Handle OpenPsiRules::_psi_goal = \
  Handle(createNode(CONCEPT_NODE, "OpenPsi: goal"));
Handle OpenPsiRules::_psi_demand = \
  Handle(createNode(CONCEPT_NODE, "OpenPsi: demand"));

OpenPsiRules::OpenPsiRules(AtomSpace* as): _as(as)
{}

Handle OpenPsiRules::add_rule(const HandleSeq& context, const Handle& action,
  const Handle& goal, const TruthValuePtr stv, const Handle& demand)
{
  // Declare as an openpsi action if it hasn't already been declared.
  _as->add_link(MEMBER_LINK, action, _psi_action);

  // Add a SequentialAndLink of context and action which forms the
  // implicant of the psi-rule.
  HandleSeq temp_c =  context;
  temp_c.push_back(action);
  Handle hca = _as->add_link(SEQUENTIAL_AND_LINK, temp_c);

  // Add the psi-rule, set the truthvalue and add to a demand.
  Handle rule = _as->add_link(IMPLICATION_LINK, hca, goal);
  rule->setTruthValue(stv);
  _as->add_link(MEMBER_LINK, rule, demand);

  // Construct the query atom that is used to check satisfiablity
  Handle query_body(createLink(context, AND_LINK)) ;

  // Add to the index of rules.
  _psi_rules[rule] = std::make_tuple(context, action, goal, query_body);

  return rule;
}

Handle OpenPsiRules::add_tag(const Handle tag_type_node,
                             const std::string& name)
{
  Handle tag = _as->add_node(CONCEPT_NODE, name);
{'fraction_non_alphanumeric': 0.0984646878198567, 'fraction_numerical': 0.0065506653019447286, 'mean_word_length': 4.392935982339956, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4715927', 'n_tokens_mistral': 1542, 'n_tokens_neox': 1499, 'n_words': 509}