<commit_before>/* ---------------------------------------------------------------------
 * Numenta Platform for Intelligent Computing (NuPIC)
 * Copyright (C) 2013, Numenta, Inc.  Unless you have an agreement
 * with Numenta, Inc., for a separate license for this software code, the
 * following terms and conditions apply:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero Public License version 3 as
 * published by the Free Software Foundation.
 *
 * 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 Affero Public License for more details.
 *
 * You should have received a copy of the GNU Affero Public License
 * along with this program.  If not, see http://www.gnu.org/licenses.
 *
 * http://numenta.org/licenses/
 * ---------------------------------------------------------------------
 */

/** @file
Implementation of the Region class

Methods related to parameters are in Region_parameters.cpp
Methods related to inputs and outputs are in Region_io.cpp

*/

#include <iostream>
#include <stdexcept>
#include <set>
#include <string>
#include <nupic/engine/Region.hpp>
#include <nupic/engine/RegionImpl.hpp>
#include <nupic/engine/RegionImplFactory.hpp>
#include <nupic/engine/Spec.hpp>
#include <nupic/utils/Log.hpp>
#include <nupic/engine/Input.hpp>
#include <nupic/engine/Output.hpp>
#include <nupic/engine/Link.hpp>
#include <nupic/ntypes/NodeSet.hpp>
#include <nupic/os/Timer.hpp>
#include <nupic/proto/RegionProto.capnp.h>

namespace nupic
{

  class GenericRegisteredRegionImpl;

  // Create region from parameter spec
  Region::Region(std::string name,
                 const std::string& nodeType,
                 const std::string& nodeParams,
                 Network * network) :
    name_(std::move(name)),
    type_(nodeType),
    initialized_(false),
    enabledNodes_(nullptr),
    network_(network)
  {
    // Set region info before creating the RegionImpl so that the
    // Impl has access to the region info in its constructor.
    RegionImplFactory & factory = RegionImplFactory::getInstance();
    spec_ = factory.getSpec(nodeType);

    // Dimensions start off as unspecified, but if
    // the RegionImpl only supports a single node, we
    // can immediately set the dimensions.
    if (spec_->singleNodeOnly)
      dims_.push_back(1);
    // else dims_ = []

    impl_ = factory.createRegionImpl(nodeType, nodeParams, this);
    createInputsAndOutputs_();

  }

  // Deserialize region
  Region::Region(std::string name,
                 const std::string& nodeType,
                 const Dimensions& dimensions,
                 BundleIO& bundle,
                 Network * network) :
    name_(std::move(name)),
    type_(nodeType),
    initialized_(false),
    enabledNodes_(nullptr),
    network_(network)
  {
    // Set region info before creating the RegionImpl so that the
    // Impl has access to the region info in its constructor.
    RegionImplFactory & factory = RegionImplFactory::getInstance();
    spec_ = factory.getSpec(nodeType);

    // Dimensions start off as unspecified, but if
    // the RegionImpl only supports a single node, we
    // can immediately set the dimensions.
    if (spec_->singleNodeOnly)
      if (!dimensions.isDontcare() && !dimensions.isUnspecified() &&
          !dimensions.isOnes())
        NTA_THROW << "Attempt to deserialize region of type " << nodeType
                  << " with dimensions " << dimensions
                  << " but region supports exactly one node.";

    dims_ = dimensions;

    impl_ = factory.deserializeRegionImpl(nodeType, bundle, this);
    createInputsAndOutputs_();
  }

  Region::Region(std::string name, RegionProto::Reader& proto,
                 Network* network) :
    name_(std::move(name)),
    type_(proto.getNodeType().cStr()),
    initialized_(false),
    enabledNodes_(nullptr),
    network_(network)
  {
    read(proto);
    createInputsAndOutputs_();
  }

  Network * Region::getNetwork()
  {<commit_msg>Initialize timer variable in 'Regioncpp' file<commit_after>/* ---------------------------------------------------------------------
 * Numenta Platform for Intelligent Computing (NuPIC)
 * Copyright (C) 2013, Numenta, Inc.  Unless you have an agreement
 * with Numenta, Inc., for a separate license for this software code, the
 * following terms and conditions apply:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero Public License version 3 as
 * published by the Free Software Foundation.
 *
 * 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 Affero Public License for more details.
 *
 * You should have received a copy of the GNU Affero Public License
 * along with this program.  If not, see http://www.gnu.org/licenses.
 *
 * http://numenta.org/licenses/
 * ---------------------------------------------------------------------
 */

/** @file
Implementation of the Region class

Methods related to parameters are in Region_parameters.cpp
Methods related to inputs and outputs are in Region_io.cpp

*/

#include <iostream>
#include <stdexcept>
#include <set>
#include <string>
#include <nupic/engine/Region.hpp>
#include <nupic/engine/RegionImpl.hpp>
#include <nupic/engine/RegionImplFactory.hpp>
#include <nupic/engine/Spec.hpp>
#include <nupic/utils/Log.hpp>
#include <nupic/engine/Input.hpp>
#include <nupic/engine/Output.hpp>
#include <nupic/engine/Link.hpp>
#include <nupic/ntypes/NodeSet.hpp>
#include <nupic/os/Timer.hpp>
#include <nupic/proto/RegionProto.capnp.h>

namespace nupic
{

  class GenericRegisteredRegionImpl;

  // Create region from parameter spec
  Region::Region(std::string name,
                 const std::string& nodeType,
                 const std::string& nodeParams,
                 Network * network) :
    name_(std::move(name)),
    type_(nodeType),
    initialized_(false),
    enabledNodes_(nullptr),
    network_(network),
    profilingEnabled_(false)
  {
    // Set region info before creating the RegionImpl so that the
    // Impl has access to the region info in its constructor.
    RegionImplFactory & factory = RegionImplFactory::getInstance();
    spec_ = factory.getSpec(nodeType);

    // Dimensions start off as unspecified, but if
    // the RegionImpl only supports a single node, we
    // can immediately set the dimensions.
    if (spec_->singleNodeOnly)
      dims_.push_back(1);
    // else dims_ = []

    impl_ = factory.createRegionImpl(nodeType, nodeParams, this);
    createInputsAndOutputs_();

  }

  // Deserialize region
  Region::Region(std::string name,
                 const std::string& nodeType,
                 const Dimensions& dimensions,
                 BundleIO& bundle,
                 Network * network) :
    name_(std::move(name)),
    type_(nodeType),
    initialized_(false),
    enabledNodes_(nullptr),
    network_(network),
    profilingEnabled_(false)
  {
    // Set region info before creating the RegionImpl so that the
    // Impl has access to the region info in its constructor.
    RegionImplFactory & factory = RegionImplFactory::getInstance();
    spec_ = factory.getSpec(nodeType);

    // Dimensions start off as unspecified, but if
    // the RegionImpl only supports a single node, we
    // can immediately set the dimensions.
    if (spec_->singleNodeOnly)
      if (!dimensions.isDontcare() && !dimensions.isUnspecified() &&
          !dimensions.isOnes())
        NTA_THROW << "Attempt to deserialize region of type " << nodeType
                  << " with dimensions " << dimensions
                  << " but region supports exactly one node.";

    dims_ = dimensions;

    impl_ = factory.deserializeRegionImpl(nodeType, bundle, this);
    createInputsAndOutputs_();
  }

  Region::Region(std::string name, RegionProto::Reader& proto,
                 Network* network) :
    name_(std::move(name)),
    type_(proto.getNodeType().cStr()),
    initialized_(false),
    enabledNodes_(nullptr),
    network_(network),
    profilingEnabled_(false)
  {
    read(proto);
    createInputsAndOutputs_();
  }

  Network * Region::getNetwork()
  {
{'fraction_non_alphanumeric': 0.13028503562945368, 'fraction_numerical': 0.0014251781472684087, 'mean_word_length': 2.798376184032476, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4863862', 'n_tokens_mistral': 2307, 'n_tokens_neox': 2275, 'n_words': 734}