int SizeOf_(Element const& element)
        {
            bool isA = false;
            int out = 0;
            switch(element.Type())
            {
                case Types::STRING: 
                {
                    String in  = CastToString(element, isA);
                    out = static_cast<int>(in.Value().size());
                    break;
                }

                case Types::SUPERSTRING: 
                {
                    SuperString in  = CastToSuperString(element, isA);
                    out = static_cast<int>(in.Value().size());
                    break;
                }

                case Types::CONTAINER:
                {
                    Container in = CastToContainer(element, isA);
                    out = in.NumberOfElements();
                    break;
                }
            };
            return out;  
        }
Element LanguageBuiltinImplementation::Interpret_( Environment& environment
                                                 , std::vector<Element> const& parmsIn
                                                 , Element const& // additional_parameters
                                                 )
{
    Element out;

    if (false == parmsIn.empty())
    {
        Element parm1 = parmsIn[0];
        std::vector<Element> parms(parmsIn.begin() + 1, parmsIn.end());
        
        bool isSuperString = false;
        SuperString SS = CastToSuperString(parm1, isSuperString);

        bool isString = false;
        String S = CastToString(parm1, isString);
        
        std::string id;
        if (isSuperString)
        {
            id = SS.Value();
        }

        if (isString)
        { 
            id = S.Value();
        }

        Translator& translator = this->translators.Get(id);
        out = Translate_(environment, parms, translator);
    }

    return out;
}
Beispiel #3
0
void LitterNetwork::create(istream& in) {
  SuperString line;
  map<int, LitterNode*> name_map;
  while (getline(in, line, '\n')) {
    vector<SuperString> parts = line.split("\t");
    int a = atoi(parts[0].c_str());
    int b = atoi(parts[1].c_str());

    if (a == b) continue;

    LitterNode* nodea = name_map[a];
    LitterNode* nodeb = name_map[b];

    if (nodea == NULL) {
      nodea = new LitterNode(a);
      name_map[a] = nodea;
    }

    if (nodeb == NULL) {
      nodeb = new LitterNode(b);
      name_map[b] = nodeb;
    }

    if(add(Edge(nodea, nodeb))) {
      nodea->stats.degree++;
      nodeb->stats.degree++;
    }
  }

  auto_ptr<NodeIterator> ni(getNodeIterator());
  while (ni->moveNext()) {
    LitterNode* node = dynamic_cast<LitterNode*>(ni->current());
    node->setNeighborIT(getNeighborIterator(node));
  }
}
 Element InterpretSuperString_(SuperString str)
 {
     Element out = Nil();
     if (false == str.Value().empty())
     {
         out = SuperString(str.Value().substr(0, 1)); 
     }
     return out;
 }
Beispiel #5
0
/* 
INPUT:  Single line '\n' terminated.  	
	Login:mtenniswood;Password:Tobart;Device name:motorola XT1565;\n
	
OUTPUT: 
	m_login_name
	m_login_password
*/
bool ServerHandler::parse_credentials( string mCredentials )
{
	bool retval = true;
	SuperString str = mCredentials;
	
	int count = str.split(';');
	str.trim_spaces_in_splits();		
	if (count < 3) {
		form_response( "Sorry there was garbled text in your credentials." );
		return false;		
	}
	//	[0] is "Login;"
	SuperString name   = str.m_split_words[1];	// UserID
	SuperString passwd = str.m_split_words[2];	// Password
	SuperString hostname = str.m_split_words[3];	// Password
		
	name.split(':');
	m_login_name = name.m_split_words[1];
	passwd.split(':'); 
	m_login_password = passwd.m_split_words[1];
			
	hostname.split(':');
	m_login_hostname = hostname.m_split_words[1];
	//printf("\tdevname=%s\n", m_login_hostname.c_str() );
	//printf("Login=%s\t\tpasswd=%s\n", m_login_name.c_str(), m_login_password.c_str() );
	return retval;
}
 Element InterpretSuperString_(SuperString str)
 {
     Element out = Nil();
     std::string const INPUT(str.Value());
     if (false == INPUT.empty())
     {
         StringHolder temp = str.GetStringHolder();
         out = SuperString(StringHolder(temp, 1, INPUT.size() - 1));
     }
     return out;
 }
Beispiel #7
0
  void CatName(std::string& _return, const std::string& doc) {
      float score=knn.Predict(doc,_return);
      SuperString SS;

      if(_return.length()==0)
          _return="Null";

      string strScore;
      strScore=SS.FtoS(score);
      _return+="\t";
      _return+=strScore;
  }
Beispiel #8
0
/* The caller may select which columns will be filled with data.
	ie. mFields = "(timestamp, firstname, lastname, username, password )"
	    mValues = "(NOW(), 'Steve', 'Tenniswood', 'stenniswood', 'Buggsbunny' )"
*/	
int	SQL_Devices::sql_add	( std::string mFields, std::string mValues  )
{
	SuperString values = mValues;
	values.convert_to_lower_case();	
	query_string =  "INSERT INTO ";
	query_string += table_name;
	query_string += " ";
	query_string += mFields;
	query_string += " VALUES "+ values + ";";
	printf("sql_add device %s\n", query_string.c_str() );
	return query(false);		
}
Beispiel #9
0
void Network::readFrom(istream& in) {

  SuperString line;
  map<string, Node*> name_map;
  while( getline(in, line, '\n') ) {
    if( line[0] == '#' ) { //This is a comment
      continue;
    }
    vector<SuperString> result = line.split(" : ");
    Node* first = 0;
    Node* second = 0;
    if( name_map.find(result[0]) == name_map.end() ) {
      //This is a new node:
      if( result[0] != "" ) {
        first = new Node(result[0]);
        name_map[ result[0] ] = first;
        add( first );
      }
    }
    else {
      first = name_map[ result[0] ];
    }
    if( result.size() == 1 ) { //There was no neighbors
      continue;
    }
    result = result[1].split(" ");
    vector<SuperString>::iterator sit;
    for(sit = result.begin();
	sit != result.end();
	sit++) {
      second = 0;
      if( name_map.find( *sit ) == name_map.end() ) {
        //This is a new node:
	if( *sit != "" ) {
          second = new Node( *sit );
          name_map[ *sit ] = second;
	  add( second );
	}
      }
      else {
        second = name_map[ *sit ];
      }
      //Add the edge:
      if( first && second ) {
        add( Edge(first, second) );
      }
    }
  }
	
}
 /**
  * Interpret an "at" with a given super string.
  * @param container Container to look through.
  * @param index The index in the container.
  * @return the element at an index in the container.
  */
 Element InterpretSuperString_( SuperString str, Number index)
 {
     int const SIZE = static_cast<int>(str.Value().size());
     if (index.IntValue() < SIZE && index.IntValue() >= 0)
     {
          return SuperString(str.Value().substr(index.IntValue(), 1)); 
     }
     else
     {
          std::stringstream ss;
          ss << "the index given is out of range: index=" << index.IntValue() << ", max=" << SIZE;
          return Error(ss.str());
     }
 }
Beispiel #11
0
int MenuItem::file_results(Sentence& mSentence)
{
    SuperString tmp;
    //for (auto x:mSentence.m_reduced_sentence.regex_matches)
    for (int m=1; m<mSentence.m_reduced_sentence.regex_matches.size(); m++)
    {
        tmp = mSentence.m_reduced_sentence.regex_matches[m].str();
        
        if (mSentence.m_reduced_sentence.regex_matches.size())
            m_quantity = 1;
        // is first match a number?  file as Quantity
        tmp.trim_spaces();
        tmp.split(' ');
        if (tmp.is_nth_word_a_number(0))
        {
            m_quantity = stof( tmp.c_str() );
        }
        
        // is match a required option?
        if (m_required_options.size())
        {
            string exp;
            for (int ro=0; ro<m_required_options.size(); ro++)
            {
                exp += m_required_options[ro].name;
                tmp.regex_find( exp );
                size_t result = tmp.regex_matches.size();           // Select the item, if info provided in sentence.
                if (result)                                         // (we'll not have to ask the user)
                    m_required_options[ro].selected_item = tmp.regex_matches[0];
            }
        }
        
        // Is the match an additional topping?
        for (int at=0; at<m_additional_toppings.size(); at++)
        {
            string str = m_additional_toppings[at].name.c_str();    //
            tmp.regex_find( str );
            if ( tmp.regex_matches.size() )
                m_additional_toppings[at].requested_indices.push_back( tmp.c_str() );
        }
        string tmps;
        // is match a removed topping?
        for (int st=0; st<m_standard_toppings.size(); st++)
        {
            string str = m_standard_toppings[st].name.c_str();    //
            tmp.regex_find( str );
            //tmps = tmp;
            if ( tmp.regex_matches.size() )
                m_standard_toppings[st].requested_indices.push_back( tmp );
        }
    }
    return m_quantity;
}
Beispiel #12
0
bool Parameter::parse_unit( SuperString& mSentence )
{
    int result = mSentence.regex_find(m_permitted_units);
    if (result) {
        m_unit_specified = mSentence.regex_matches[0];
        return true;
    }
    return false;
}
Beispiel #13
0
Network* NetworkFactory::create(istream& in)
{
  Network* net = new Network();
  SuperString line;
  while( getline(in, line, '\n') ) {
    if( line[0] == '#' ) { //This is a comment
      continue;
    }
    vector<SuperString> result = line.split(" : ");
    Node* first = 0;
    Node* second = 0;
    first = _nf->create(result[0]);
    net->add( first ); 
    if( result.size() == 1 ) { //There were no neighbors
      continue;
    }
    //There is a list of second nodes:
    auto_ptr< Iterator<SuperString> > si( result[1].spliterator(" ") );
    while( si->moveNext() ) {
      second = 0;
      second = _nf->create( si->current() );
      net->add( second );
      Edge* e = 0;
      if( result.size() > 2 ) {
        e = _ef->create(first, second, result[2]);
      }
      else {
        e = _ef->create(first, second);
      }
      //Add the edge:
      if( e ) {
	/**
	 * Remember: we have to add references to edges, 
	 * not pointers.  This is because we don't want to allow
	 * multiple edges between nodes (for now).
	 */
        net->add( *e );
      }
      delete e;
    }
  }
  return net;
}
Beispiel #14
0
void Parameter::file_results    ()
{
    SuperString tmp;
    int    junk;
    size_t size = m_matches.size();
    for (int x=1; x<size; x++)
    {
        tmp = m_matches[x].str();
        SuperString exp = get_numeral_regexpression( m_type );
        int matches = tmp.regex_find( exp );
        if (matches)
            parse_value( tmp, junk );
        else
            parse_unit( tmp );
        matches = tmp.regex_find( m_name );
        if (matches)
            m_name_found = tmp;
    }
}
Beispiel #15
0
bool Parameter::parse( SuperString& mSentence )
{
    bool retval = false;
    int  result = mSentence.regex_find(m_regexpression);
    m_matches  = mSentence.regex_matches;
    if (result) {
        retval = true;
    }
    file_results();
    return retval;
}
Beispiel #16
0
int Menu::how_much_is( Sentence& mSentence, ServerHandler* mh )
{
    int result =0;
    RestaurantOrder tmpOrder;
    if (mSentence.are_found_in_sentence("how much"))
    {
        result = Parse_Menu( mSentence, mh, tmpOrder);
        if (tmpOrder.m_order.size()==0)
            mh->form_response("I could not find that on the menu.");
        else {
            SuperString tmp = "Okay, ";
            tmpOrder.read_back_items( tmp );
            tmp += " costs $";
            float total = tmpOrder.get_total();
            tmp.append_float ( total );
            mh->form_response( tmp.c_str() );
        }
        return 1;
    }
    return 0;
}
Beispiel #17
0
bool Parameter::parse_value  ( SuperString& mSentence, int& mWordIndex )
{
    for (int w=0; w < mSentence.m_split_words.size(); w++)
    {
        bool result = mSentence.is_nth_word_a_number(w);
        if (result) {
            m_value = mSentence.m_split_words[w];
            if (m_type==FLOAT)
                m_f_value  = stof( mSentence.m_split_words[w] );
            else if (m_type==INTEGER)
                m_f_value  = stoi( mSentence.m_split_words[w] );
            mWordIndex = w;
            return true;
        }
    }
    return false;
}
        Element Interpret_( Environment& 
                          , std::vector<Element> const& parms
                          , Element const&)
        {
            if (parms.size() != 3)
            {
                std::stringstream ss;
                ss << "subseq requires 3 arguments, a container/string and start/end values";
                return Error(ss.str());
            }             

            bool gotANumber1 = false;
            bool gotANumber2 = false;
            bool gotAContainer = false;
            bool gotAString = false;
            bool gotASuperString = false;

            Number N1 = CastToNumber(parms[1], gotANumber1);
            Number N2 = CastToNumber(parms[2], gotANumber2);
            Container C = CastToContainer(parms[0], gotAContainer);
            String S = CastToString(parms[0], gotAString);
            SuperString SS = CastToSuperString(parms[0], gotASuperString);
             
            if (gotANumber1 && gotANumber2 && (gotAContainer || gotAString || gotASuperString))
            {
                int index1 = N1.IntValue();
                int index2 = N2.IntValue();
 
                if (index1 < 0 || index2 < 0)
                {
                    std::stringstream ss;
                    ss << "subseq requires a positive range, received: " << index1 << " and " << index2;
                    return Error(ss.str());
                }

                if (gotAContainer)
                {
                    if (C.NumberOfElements() <= index1 && C.NumberOfElements() <= index2)
                    {
                         return Container();
                    }

                    std::vector<Element> elements;
                    C.RetrieveVector(elements);
                    elements.assign(elements.begin() + N1.IntValue(), elements.begin() + N2.IntValue());
                    Container out;
                    out.SetVector(elements);
                    return out;
                }
                if (gotAString)
                {
                    std::string value = S.Value();
                    if (index1 >= static_cast<int>(value.size()))
                    {
                         return String("");
                    }

                    value = value.substr(N1.IntValue(), N2.IntValue());
                    return String(value);
                }

                if (gotASuperString)
                {
                    StringHolder temp = SS.GetStringHolder();
                    return SuperString(StringHolder(temp, N1.IntValue(), N2.IntValue()));
                }

                return Error("Something unexpected happened in subseq");
            }
            else
            {
                std::stringstream ss;
                ss << "subseq requires 3 arguments, a container/string and start/end numeric values.\n"
                   << "Received: " << parms[0].ToString() << ", " << parms[1].ToString() << ", " << parms[2].ToString();
                return Error(ss.str());
            }
        }
Beispiel #19
0
void Network::readFromGDL(std::istream& in){
  SuperString line;
  map<string, Node*> name_map;
  list<string> edge_src;
  list<string> edge_dst;
  int node=0, edge=0;
  Node* temp1 = 0;
  Node* temp2 = 0;

  while( getline(in, line, '\n') ) 
  {
    if( line[0] == '/' && line[1] == '/' ) continue;

    vector<SuperString> result = line.split(" ");
    vector<SuperString>::iterator sit;
    string nodename="", source="", target="";

    for (sit = result.begin(); sit != result.end(); sit++)
    {
      // note: should be able to handle node/edge definitions that span multiple lines now
      // note: doesn't handle multiword titles/sources/destinations, but they shouldn't exist anyway

      if (*sit == "node:")
        node = 1;
      else if (*sit == "edge:" || *sit == "nearedge:")
        edge = 1;
      else if (node == 1 && *sit == "title:")
      {
        sit++;
        nodename = *sit;

        if (name_map.find(nodename) == name_map.end()) // if new node...
        {
          temp1 = new Node(nodename);
          name_map[nodename] = temp1;
          add( temp1 );
          //cout<<"Created node "<<nodename<<endl;
        }

        nodename = "";
        node = 0;
      }
      else if (edge == 1 && (*sit == "targetname:" || *sit == "target:"))
      {
        sit++;
        target = *sit;
      }
      else if (edge == 1 && (*sit == "sourcename:" || *sit == "source:"))
      {
        sit++;
        source = *sit;
      }

      if (edge == 1 && source != "" && target != "")
      {
        edge_src.push_front(source);
        edge_dst.push_front(target);
        source = "";
        target = "";
        edge = 0;
      }

    }
  }

  list<string>::iterator i = edge_src.begin(), j = edge_dst.begin();

  while (i != edge_src.end())
  {
    //cout<<"Created edge from "<<*i<<" to "<<*j<<endl;
    temp1 = name_map[*i];
    temp2 = name_map[*j];
    add( Edge(temp1, temp2) );
    i++;
    j++;
  }

	
}
Beispiel #20
0
int main(int argc, char* argv[]) {

  vector<string> reqs;
  reqs.push_back("input");
  reqs.push_back("subgraph_nodes");
  reqs.push_back("out-prefix");
 
  vector<string> opts;
  opts.push_back("weighted");
  opts.push_back("first_is_name");
  OptionParser op(reqs,opts);
  try {
    op.parse(argc, argv);
  }
  catch (exception x) {
    cerr << "usage: " << argv[0] << op.getUsageString() << endl;
    return -1;
  }
  NetworkFactory* nf;
  NodeFactory* node_f = new NamedNodeFactory();
  bool weighted = op.getBoolOpt("weighted", false);
  if( weighted ) {
    nf = new WeightedNetworkFactory(node_f,
                                    new WeightedEdgeFactory());
  }
  else {
    //Here is unweighted:
    nf = new NetworkFactory(node_f,
                            new EdgeFactory());
  }
  Network* net;
  if( op.getStringOpt("input", "-") == "-" ) {
    net = nf->create(cin);
  }
  else {
    ifstream input( op.getStringOpt("input","").c_str() );
    net = nf->create(input);
  }
  //Subgraph
  ifstream sg( op.getStringOpt("subgraph_nodes","").c_str() );
  bool first_is_name = op.getBoolOpt("first_is_name", false);  

  SuperString line;
  int line_cnt = 0;
  while(getline(sg, line, '\n')) {
    if( line[0] == '#' ) { //This is a comment
      continue;
    }
    vector<SuperString> parts = line.split(" ");
    string name;
    int idx = 0;
    if( first_is_name ) {
      name = parts[0]; 
      idx = 1;
    }
    else {
      stringstream ss;
      ss << line_cnt;
      name = ss.str();
    }
    Network* sg_net;
    if( weighted ) { sg_net = new WeightedNetwork(); }
    else { sg_net = new Network(); }
    for(int i = idx; i < parts.size(); i++) {
      sg_net->add( node_f->create(parts[i]) );
    }
    sg_net->addJoiningEdgesFrom(net);
    ofstream out((op.getStringOpt("out-prefix","") + name).c_str());
    sg_net->printTo(out);
    delete sg_net; 
    line_cnt += 1;
  }
  
}