Exemple #1
0
/*++
********************************************************************************
                                                                            
 FUNCTION:

 DESCRIPTION:
    Check if file or folder exists

 ARGUMENTS:

 RETURNS:

********************************************************************************
--*/
int fExists(char *fidname)
{
FILE *fHand;
int retVal;
    if ((fHand = fOpen(fidname, "rb")))
    {   retVal = 1;
        fClose(fHand);
    }
    else {
        retVal = 0;
    }
    return retVal;
}
bool LexicalReorderingTableTree::Create(std::istream& inFile,
                                        const std::string& outFileName)
{
  std::string line;
  //TRACE_ERR("Entering Create...\n");
  std::string
  ofn(outFileName+".binlexr.srctree"),
      oft(outFileName+".binlexr.tgtdata"),
      ofi(outFileName+".binlexr.idx"),
      ofsv(outFileName+".binlexr.voc0"),
      oftv(outFileName+".binlexr.voc1");


  FILE *os = fOpen(ofn.c_str(),"wb");
  FILE *ot = fOpen(oft.c_str(),"wb");

  //TRACE_ERR("opend files....\n");

  typedef PrefixTreeSA<LabelId,OFF_T> PSA;
  PSA *psa = new PSA;
  PSA::setDefault(InvalidOffT);
  WordVoc* voc[3];

  LabelId currFirstWord = InvalidLabelId;
  IPhrase currKey;

  Candidates         cands;
  std::vector<OFF_T> vo;
  size_t lnc = 0;
  size_t numTokens    = 0;
  size_t numKeyTokens = 0;
  while(getline(inFile, line)) {
    ++lnc;
    if(0 == lnc % 10000) {
      TRACE_ERR(".");
    }
    IPhrase key;
    Scores   score;

    std::vector<std::string> tokens = TokenizeMultiCharSeparator(line, "|||");
    std::string w;
    if(1 == lnc) {
      //do some init stuff in the first line
      numTokens = tokens.size();
      if(tokens.size() == 2) { //f ||| score
        numKeyTokens = 1;
        voc[0] = new WordVoc();
        voc[1] = 0;
      } else if(3 == tokens.size() || 4 == tokens.size()) { //either f ||| e ||| score or f ||| e ||| c ||| score
        numKeyTokens = 2;
        voc[0] = new WordVoc(); //f voc
        voc[1] = new WordVoc(); //e voc
        voc[2] = voc[1];        //c & e share voc
      }
    } else {
      //sanity check ALL lines must have same number of tokens
      CHECK(numTokens == tokens.size());
    }
    size_t phrase = 0;
    for(; phrase < numKeyTokens; ++phrase) {
      //conditioned on more than just f... need |||
      if(phrase >=1) {
        key.push_back(PrefixTreeMap::MagicWord);
      }
      std::istringstream is(tokens[phrase]);
      while(is >> w) {
        key.push_back(voc[phrase]->add(w));
      }
    }
    //collect all non key phrases, i.e. c
    std::vector<IPhrase> tgt_phrases;
    tgt_phrases.resize(numTokens - numKeyTokens - 1);
    for(size_t j = 0; j < tgt_phrases.size(); ++j, ++phrase) {
      std::istringstream is(tokens[numKeyTokens + j]);
      while(is >> w) {
        tgt_phrases[j].push_back(voc[phrase]->add(w));
      }
    }
    //last token is score
    std::istringstream is(tokens[numTokens-1]);
    while(is >> w) {
      score.push_back(atof(w.c_str()));
    }
    //transform score now...
    std::transform(score.begin(),score.end(),score.begin(),TransformScore);
    std::transform(score.begin(),score.end(),score.begin(),FloorScore);
    std::vector<Scores> scores;
    scores.push_back(score);

    if(key.empty()) {
      TRACE_ERR("WARNING: empty source phrase in line '"<<line<<"'\n");
      continue;
    }
    //first time inits
    if(currFirstWord == InvalidLabelId) {
      currFirstWord = key[0];
    }
    if(currKey.empty()) {
      currKey = key;
      //insert key into tree
      CHECK(psa);
      PSA::Data& d = psa->insert(key);
      if(d == InvalidOffT) {
        d = fTell(ot);
      } else {
        TRACE_ERR("ERROR: source phrase already inserted (A)!\nline(" << lnc << "): '" << line << "\n");
        return false;
      }
    }
    if(currKey != key) {
      //ok new key
      currKey = key;
      //a) write cands for old key
      cands.writeBin(ot);
      cands.clear();
      //b) check if we need to move on to new tree root
      if(key[0] != currFirstWord) {
        // write key prefix tree to file and clear
        PTF pf;
        if(currFirstWord >= vo.size()) {
          vo.resize(currFirstWord+1,InvalidOffT);
        }
        vo[currFirstWord] = fTell(os);
        pf.create(*psa, os);
        // clear
        delete psa;
        psa = new PSA;
        currFirstWord = key[0];
      }
      //c) insert key into tree
      CHECK(psa);
      PSA::Data& d = psa->insert(key);
      if(d == InvalidOffT) {
        d = fTell(ot);
      } else {
        TRACE_ERR("ERROR: source phrase already inserted (A)!\nline(" << lnc << "): '" << line << "\n");
        return false;
      }
    }
    cands.push_back(GenericCandidate(tgt_phrases, scores));
  }
  if (lnc == 0) {
    TRACE_ERR("ERROR: empty lexicalised reordering file\n" << std::endl);
    return false;
  }
  //flush remainders
  cands.writeBin(ot);
  cands.clear();
  //process last currFirstWord
  PTF pf;
  if(currFirstWord >= vo.size()) {
    vo.resize(currFirstWord+1,InvalidOffT);
  }
  vo[currFirstWord] = fTell(os);
  pf.create(*psa,os);
  delete psa;
  psa=0;

  fClose(os);
  fClose(ot);
  /*
  std::vector<size_t> inv;
  for(size_t i = 0; i < vo.size(); ++i){
    if(vo[i] == InvalidOffT){
      inv.push_back(i);
    }
  }
  if(inv.size()) {
    TRACE_ERR("WARNING: there are src voc entries with no phrase "
        "translation: count "<<inv.size()<<"\n"
        "There exists phrase translations for "<<vo.size()-inv.size()
        <<" entries\n");
  }
  */
  FILE *oi = fOpen(ofi.c_str(),"wb");
  fWriteVector(oi,vo);
  fClose(oi);

  if(voc[0]) {
    voc[0]->Write(ofsv);
    delete voc[0];
  }
  if(voc[1]) {
    voc[1]->Write(oftv);
    delete voc[1];
  }
  return true;
}
Exemple #3
0
void CTestCBotDoc::OnTest()
{
    CBotProgram::DefineNum("WingedGrabber", 1);
    CBotProgram::DefineNum("TrackedGrabber", 2);
    CBotProgram::DefineNum("WheeledGrabber", 3);
    CBotProgram::DefineNum("LeggedGrabber", 4);
    CBotProgram::DefineNum("WingedShooter", 5);
    CBotProgram::DefineNum("TrackedShooter", 6);
    CBotProgram::DefineNum("WheeledShooter", 7);
    CBotProgram::DefineNum("LeggedShooter", 8);
    CBotProgram::DefineNum("WingedOrgaShooter", 9);
    CBotProgram::DefineNum("TrackedOrgaShooter", 10);
    CBotProgram::DefineNum("WheeledOrgaShooter", 11);
    CBotProgram::DefineNum("LeggedOrgaShooter", 12);
    CBotProgram::DefineNum("WingedSniffer", 13);
    CBotProgram::DefineNum("TrackedSniffer", 14);
    CBotProgram::DefineNum("WheeledSniffer", 14);
    CBotProgram::DefineNum("LeggedSniffer", 15);
    CBotProgram::DefineNum("Thumper", 16);
    CBotProgram::DefineNum("PhazerShooter", 17);
    CBotProgram::DefineNum("Recycler", 18);
    CBotProgram::DefineNum("Shielder", 19);
    CBotProgram::DefineNum("Subber", 20);
    CBotProgram::DefineNum("Me", 21);

    CBotProgram::DefineNum("TypeMarkPath", 111);

    OnFileSave();

//  CPerformDlg dlg;
//  dlg.m_Script = m_DocText;
//  dlg.DoModal();

    // défini la routine RetObject
    CBotProgram::AddFunction( "Radar", rRetObject, cRetObject );

    // ajoute une routine pour cette classe
    CBotProgram::AddFunction("Space", rSpace, cSpace);

    // défini la routine Test
    CBotProgram::AddFunction( "TEST", rTEST, cTEST );
    CBotProgram::AddFunction( "F", rF, cF );

    CBotProgram::AddFunction( "goto", rMove, cMove );
    CBotProgram::AddFunction( "fire", rTurn, cTurn );
    CBotProgram::AddFunction( "radar", rRadar, cRadar );

    // crée une instance de la classe "Bot" pour ce robot
    CBotVar*    pThisRobot = CBotVar::Create( "", CBotTypResult(CBotTypClass, "object") );
    pThisRobot->SetUserPtr( (void*)1 );
    pThisRobot->SetIdent( 1234 );

    delete m_pProg;
    // crée un objet programme associé à cette instance
    m_pProg = new CBotProgram(pThisRobot);

    // compile le programme
    CString         TextError;
    int             code, start, end;

    m_pEdit->GetWindowText(m_DocText);
    if (!m_pProg->Compile(m_DocText, m_Liste, (void*) 44))
    {
        m_pProg->GetError(code, start, end);
        delete m_pProg;
        m_pProg = NULL;

        delete pThisRobot;

        m_pEdit->SetSel( start, end );
        m_pEdit->SetFocus();                // met en évidence la partie avec problème

        TextError = CBotProgram::GivErrorText( code );
        AfxMessageBox( TextError );

        m_pEdit->SetFocus();
        return;
    }

    // exécute pour voir
    m_pProg->Start(m_Liste[0]);

    int mode = -1;

    if ( mode >= 0 ) {

    // sauve et restore à chaque pas possible
    while (!m_pProg->Run(NULL, 1))
    {
        const char* FunctionName;
        int         start1, end1;
        m_pProg->GetRunPos(FunctionName, start1, end1);
        if ( end1 <= 0 )
            m_pProg->GetRunPos(FunctionName, start1, end1);
        m_pEdit->SetSel(start1, end1);

if ( mode == 0 ) continue;

        FILE*   pf;
        pf =    fOpen( "TEST.CBO", "wb" );
        CBotClass::SaveStaticState(pf);
        m_pProg->SaveState(pf);
        fClose(pf);

if ( mode == 2 ) if (!m_pProg->Compile(m_DocText, m_Liste, (void*) 44))
    {
        m_pProg->GetError(code, start, end);
        delete m_pProg;
        m_pProg = NULL;

        delete pThisRobot;

        m_pEdit->SetSel( start, end );
        m_pEdit->SetFocus();                // met en évidence la partie avec problème

        TextError = CBotProgram::GivErrorText( code );
        AfxMessageBox( TextError );

        m_pEdit->SetFocus();
        return;
    }

        pf =    fOpen( "TEST.CBO", "rb" );
        CBotClass::RestoreStaticState(pf);
        m_pProg->RestoreState(pf);
        fClose(pf);

        int         start2, end2;
        m_pProg->GetRunPos(FunctionName, start2, end2);
        if ( end2 <= 0 )
            m_pProg->GetRunPos(FunctionName, start2, end2);

        if ( start1 != start2 || end1 != end2 )
            m_pProg->GetRunPos(FunctionName, start2, end2);
        m_pEdit->SetSel(start2, end2);
    }

    if (m_pProg->GetError(code, start, end))
    {
        m_pEdit->SetSel(start, end);
        TextError = CBotProgram::GivErrorText(code);
        AfxMessageBox(TextError);
    }
    return;}

    while (!m_pProg->Run(NULL, 0))
    {
        const char* FunctionName;
        int         start, end;
        m_pProg->GetRunPos(FunctionName, start, end);
        m_pEdit->SetSel(start, end);

        if ( FunctionName == NULL ) continue;
        CString info (FunctionName);
        CString sep  (":\n");

        int level = 0;
        const char* Name;
        while ( TRUE )
        {
            CBotVar* pVar = m_pProg->GivStackVars(Name, level--);
            if ( Name != FunctionName ) break;
            if ( pVar == NULL ) continue;
//          pVar->Maj(NULL, FALSE);
            while ( pVar != NULL )
            {
                info += sep;
                info += pVar->GivName() + CBotString(" = ") + pVar->GivValString();
                sep = ", ";
                pVar = pVar->GivNext();
            }
            sep = "\n";
        }
        if ( IDOK != AfxMessageBox(info, MB_OKCANCEL) ) break;

        if ( test == 1 )
        {
            test = 0;
            FILE*   pf;
            pf =    fOpen( "TEST.CBO", "wb" );
            m_pProg->SaveState(pf);
            fClose(pf);
        }

        if ( test == 2 )
        {
            test = 0;
            FILE*   pf;
            pf =    fOpen( "TEST.CBO", "rb" );
            m_pProg->RestoreState(pf);
            fClose(pf);
        }

        if ( test == 12 )
        {
            test = 0;
            FILE*   pf;
            pf =    fOpen( "TEST.CBO", "wb" );
            m_pProg->SaveState(pf);
            fClose(pf);

            pf =    fOpen( "TEST.CBO", "rb" );
            m_pProg->RestoreState(pf);
            fClose(pf);

            test = 13;
        }
    }

    if (m_pProg->GetError(code, start, end))
    {
        m_pEdit->SetSel(start, end);
        TextError = CBotProgram::GivErrorText(code);
        AfxMessageBox(TextError);
    }

    delete pThisRobot;
}