<commit_before>/*
 * This file is distributed under the terms in the attached LICENSE file.
 * If you do not find this file, copies can be found by writing to:
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300,
 * Berkeley, CA, 94704.  Attention:  Intel License Inquiry.
 * Or
 * UC Berkeley EECS Computer Science Division, 387 Soda Hall #1776, 
 * Berkeley, CA,  94707. Attention: P2 Group.
 * 
 * DESCRIPTION: 
 *
 */

#include "plannerContext.h"
#include "plumber.h"
#include "reporting.h"
#include "systemTable.h"
#include "list.h"
#include "val_null.h"
#include "val_list.h"
#include "val_tuple.h"
#include "val_int64.h"
#include "oper.h"

using namespace opr;

namespace compile {
  namespace planner {

    using namespace std;
    using namespace opr;
  
    static string 
    int_list_to_str(std::vector<unsigned>& values)
    {
      ostringstream oss;
      oss << "{";
      for (std::vector<unsigned>::iterator iter = values.begin(); 
           iter != values.end(); iter++) {
        oss << *iter;
        if (iter + 1  != values.end())
          oss << ", ";
      }
      oss << "}";
      return oss.str(); 
    }
  
    DEFINE_ELEMENT_INITS_NS(Context, "PlannerContext", compile::planner)

    Context::Context(string name, string d, string isie, string isoe, string esoe)
    : compile::Context(name), 
      _mainDataflowName(d), 
      _internalStrandInputElement(isie), 
      _internalStrandOutputElement(isoe), 
      _externalStrandOutputElement(esoe),
      _nameCounter(0) { }

    Context::Context(TuplePtr args)
    : compile::Context((*args)[2]->toString()), 
      _mainDataflowName((*args)[3]->toString()), 
      _internalStrandInputElement((*args)[4]->toString()), 
      _internalStrandOutputElement((*args)[5]->toString()), 
      _externalStrandOutputElement((*args)[6]->toString()),
      _nameCounter(0) { }
  
    Context::~Context()
    {}

    int
    Context::initialize()
    {
      CommonTable::ManagerPtr catalog = Plumber::catalog();
      CommonTablePtr programTbl = catalog->table(PROGRAM);  
      CommonTable::Iterator iter = programTbl->scan();
      while (!iter->done()) {
        compile::Context::program(catalog, iter->next());
      }
      return 0;
    }

    bool 
    Context::watched(string name, string mod)
    {
      //      cout << "Asked if table " << name << " contains modifier " << mod
      //    << "\n";
      CommonTable::ManagerPtr catalog = Plumber::catalog();
      CommonTablePtr watchTbl = catalog->table(WATCH);
      TuplePtr lookup = Tuple::mk(WATCH);
      lookup->append(Val_Str::mk(name));
      lookup->append(Val_Null::mk());
      lookup->freeze();
      CommonTable::Key indexKey;
      indexKey.push_back(catalog->attribute(WATCH, "NAME"));

      CommonTable::Iterator i = 
        watchTbl->lookup(CommonTable::theKey(CommonTable::KEY2),
                         CommonTable::theKey(CommonTable::KEY4),
                         lookup); 
      while (!i->done()) {
        // Found something.
        if (mod == "") {
          //  cout << "It does by default\n";
          return true;
        } else {
          // Does it contain this explicit modifier?
          TuplePtr theWatchSpec = i->next();
          //cout << "The watch record is " << theWatchSpec->toString() << "\n";
          string theWatchModifier =
            (*theWatchSpec)[catalog->attribute(WATCH,
                                               "MOD")]->toString();
          //cout << "The watch modifieer is " << theWatchModifier << "\n";
          if ((theWatchModifier.find(mod) == theWatchModifier.npos) &&
              !(theWatchModifier == "")) {
            // Didn't find it
          } else {
            // Found it
            //cout << "It does explicitly\n";
            return true; 
          }
        }
      }
      return false;
    }

    TuplePtr 
    Context::program(CommonTable::ManagerPtr catalog, TuplePtr program) 
    {
      programEvents.clear();
      ostringstream stage_oss;

      CommonTable::Key indexKey;
      CommonTable::Iterator iter;
      ElementPtr demux = 
        Plumber::dataflow(_mainDataflowName)->find(_internalStrandInputElement)->element();

      indexKey.push_back(catalog->attribute(STAGE, "PID"));
      iter =<commit_msg>Cleaned up decision tree for when watches are considered requested and when not<commit_after>/*
 * This file is distributed under the terms in the attached LICENSE file.
 * If you do not find this file, copies can be found by writing to:
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300,
 * Berkeley, CA, 94704.  Attention:  Intel License Inquiry.
 * Or
 * UC Berkeley EECS Computer Science Division, 387 Soda Hall #1776, 
 * Berkeley, CA,  94707. Attention: P2 Group.
 * 
 * DESCRIPTION: 
 *
 */

#include "plannerContext.h"
#include "plumber.h"
#include "reporting.h"
#include "systemTable.h"
#include "list.h"
#include "val_null.h"
#include "val_list.h"
#include "val_tuple.h"
#include "val_int64.h"
#include "oper.h"

using namespace opr;

namespace compile {
  namespace planner {

    using namespace std;
    using namespace opr;
  
    static string 
    int_list_to_str(std::vector<unsigned>& values)
    {
      ostringstream oss;
      oss << "{";
      for (std::vector<unsigned>::iterator iter = values.begin(); 
           iter != values.end(); iter++) {
        oss << *iter;
        if (iter + 1  != values.end())
          oss << ", ";
      }
      oss << "}";
      return oss.str(); 
    }
  
    DEFINE_ELEMENT_INITS_NS(Context, "PlannerContext", compile::planner)

    Context::Context(string name, string d, string isie, string isoe, string esoe)
    : compile::Context(name), 
      _mainDataflowName(d), 
      _internalStrandInputElement(isie), 
      _internalStrandOutputElement(isoe), 
      _externalStrandOutputElement(esoe),
      _nameCounter(0) { }

    Context::Context(TuplePtr args)
    : compile::Context((*args)[2]->toString()), 
      _mainDataflowName((*args)[3]->toString()), 
      _internalStrandInputElement((*args)[4]->toString()), 
      _internalStrandOutputElement((*args)[5]->toString()), 
      _externalStrandOutputElement((*args)[6]->toString()),
      _nameCounter(0) { }
  
    Context::~Context()
    {}

    int
    Context::initialize()
    {
      CommonTable::ManagerPtr catalog = Plumber::catalog();
      CommonTablePtr programTbl = catalog->table(PROGRAM);  
      CommonTable::Iterator iter = programTbl->scan();
      while (!iter->done()) {
        compile::Context::program(catalog, iter->next());
      }
      return 0;
    }

    bool 
    Context::watched(string name, string mod)
    {
      TELL_INFO << "Planner: Asked if table "
                << name
                << " is watched with modifier '"
                << mod
                << "'...";
      CommonTable::ManagerPtr catalog = Plumber::catalog();
      CommonTablePtr watchTbl = catalog->table(WATCH);
      TuplePtr lookup = Tuple::mk(WATCH);
      lookup->append(Val_Str::mk(name));
      lookup->append(Val_Null::mk());
      lookup->freeze();
      CommonTable::Key indexKey;
      indexKey.push_back(catalog->attribute(WATCH, "NAME"));

      CommonTable::Iterator i = 
        watchTbl->lookup(CommonTable::theKey(CommonTable::KEY2),
                         CommonTable::theKey(CommonTable::KEY4),
                         lookup); 
      while (!i->done()) {
        // Found something.
        if (mod == "") {
          TELL_INFO << "Yes (universal modifier)\n";
          return true;
        } else {
          // Does it contain this explicit modifier?
          TuplePtr theWatchSpec = i->next();
          string theWatchModifier =
            (*theWatchSpec)[catalog->attribute(WATCH,
                                               "MOD")]->toString();
          if (theWatchModifier == "") {
            // The watch is universal, so whatever my modifier is, it's
            // included
            TELL_INFO << "Yes (all modifiers are watched)\n";
            return true;
          } else if (theWatchModifier.find(mod) !=
                     theWatchModifier.npos) {
            // The watch is not universal, and it doesn't contain my modifier
            TELL_INFO << "No (only '"
                 << theWatchModifier
                 << "' is being watched)\n";
            return false;
          } else {
            // The watch is not universal and I found my modifier in it
            TELL_INFO << "Yes (explicitly)\n";
            return true; 
          }
        }
      }
      TELL_INFO << "No (no watch on this table)\n";
      return false;
    }

    TuplePtr 
    Context::program(CommonTable::ManagerPtr catalog, TuplePtr program) 
    {
      programEvents.clear();
      ostringstream stage_oss;

      CommonTable::Key indexKey;
      CommonTable::Iterator iter;
      ElementPtr demux = 
        Plumber::dataflow(_mainDataflowName)->find(_internalStrandInputElement)->element();

      indexKey.push_back(catalog->attribute(STAGE, "PID"));
      iter =
{'fraction_non_alphanumeric': 0.13314101852879173, 'fraction_numerical': 0.008654166204371464, 'mean_word_length': 2.37856071964018, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5369476', 'n_tokens_mistral': 2686, 'n_tokens_neox': 2542, 'n_words': 601}