/*****************************************************************************
* First character of command id should be second char of line
* Lexer returns a full line of data, needs to be parsed if .ce
* In version 1.0, valid commands are:
* .* (comment) followed by text to '\n'
* .br (new line), nothing else allowed on line
* .im (include file) 'filename'
* .nameit (define text macro) symbol=[a-zA-Z0-9]+ (10 max) text='text string'
*     text may contain entity references, nameit references and tags
* .ce (center) no tags, but text and both entity types
* Version 2.0 only supports .*, .br, .im
*/
Lexer::Token Document::processCommand( Lexer* lexer, Tag* parent )
{
    if( lexer->cmdId() == Lexer::COMMENT )
        ;//do nothing
    else if( lexer->cmdId() == Lexer::BREAK )
        parent->appendChild( new BrCmd( this, parent, dataName(), dataLine(), dataCol() ) );
    else if( lexer->cmdId() == Lexer::CENTER ) {
        CeCmd* cecmd( new CeCmd( this, parent, dataName(), dataLine(), dataCol() ) );
        parent->appendChild( cecmd );
        return cecmd->parse( lexer );
    }
    else if( lexer->cmdId() == Lexer::IMBED ) {
        std::string env( Environment.value( "IPFCIMBED" ) );
        std::vector< std::wstring > paths;
        std::wstring cwd;   //empty string for current directory
        paths.push_back( cwd );
#ifdef __UNIX__
        std::string separators( ":;" );
        char slash( '/' );
#else
        std::string separators( ";" );
        char slash( '\\' );
#endif
        std::string::size_type idx1( 0 );
        std::string::size_type idx2( env.find_first_of( separators, idx1 ) );
        std::wstring fbuffer;
        mbtowstring( env.substr( idx1, idx2 - idx1 ), fbuffer );
        paths.push_back( fbuffer );
        while( idx2 != std::string::npos ) {
            idx1 = idx2 + 1;
            idx2 = env.find_first_of( separators, idx1 );
            fbuffer.clear();
            mbtowstring( env.substr( idx1, idx2 - idx1 ), fbuffer );
            paths.push_back( fbuffer );
        }
        for( size_t count = 0; count < paths.size(); ++count ) {
            std::wstring* fname( new std::wstring( paths[ count ] ) );
            if( !fname->empty() )
                *fname += slash;
            *fname += lexer->text();
#ifndef __UNIX__
            if( fname->size() > PATH_MAX ) {
                throw FatalError( ERR_PATH_MAX );
            }
#endif
            try {
                IpfFile* ipff( new IpfFile( fname ) );
                fname = addFileName( fname );
                pushInput( ipff );
                break;
            }
            catch( FatalError& e ) {
                delete fname;
                if( count == paths.size() - 1 )
                    throw e;
            }
            catch( FatalIOError& e ) {
                delete fname;
                if( count == paths.size() - 1 )
                    throw e;
            }
        }
    }
    else if( lexer->cmdId() == Lexer::NAMEIT ) {
        std::wstring::size_type idx1( lexer->text().find( L"symbol=" ) );
        std::wstring::size_type idx2( lexer->text().find( L' ', idx1 ) );
        std::wstring sym( lexer->text().substr( idx1 + 7, idx2 - idx1 - 7 ) );
        killQuotes( sym );
        sym.insert( sym.begin(), L'&' );
        sym += L'.';
        std::wstring::size_type idx3( lexer->text().find( L"text=" ) );
        //check for single quotes
        std::wstring::size_type idx4( lexer->text()[ idx3 + 5 ] == L'\'' ? \
            lexer->text().find( L'\'', idx3  + 6 ) : \
            lexer->text().find( L' ', idx3 + 5 ) );
        std::wstring txt( lexer->text().substr( idx3 + 5, idx4 - idx3 - 5 ) );
        killQuotes( txt );
        if( !nls->isEntity( sym ) && nameIts.find( sym ) == nameIts.end() ) //add it to the list
            nameIts.insert( std::map< std::wstring, std::wstring >::value_type( sym, txt ) );
        else
            printError( ERR3_DUPSYMBOL );
    }
    else
        printError( ERR1_CMDNOTDEF );
    return getNextToken();
}
Exemple #2
0
int main(int argc, char **argv)
	 
{
  unsigned long	i = 1;
  char		Err[BUFSIZ];
#ifdef PVM
  unsigned int	j = 0L;
#endif

  
  /* There is no signal handling under parix. */
  if(((int)signal(SIGINT, (void (*)(int)) myInterrupt) == -1) ||
	 ((int)signal(SIGFPE, (void (*)(int)) Arithmetic) == -1))
	panic(A_FATAL, "child-main", "signal call went wrong :%s : %d",
		  __FILE__, __LINE__);
  
  strcpy(hlp, argv[0]);
  program_name = hlp;
  
  initDef(&eps);				/* initialize structure */
  ipOptPrc(&eps, argc, argv);	/* process options. */
  
#ifdef PVM
  eps.tid  = pvm_mytid();        /* tid of process. */
  strcpy(eps.Group, eps.Suffix); /* Create Group name. */
  if((eps.inst = pvm_joingroup(eps.Group)) < 0)
	panic(A_FATAL, "child-main", 
		  "pvm grouping for group %s failed : %s : %d\n",
		  eps.Group, __FILE__, __LINE__);

  printf("Slave with tid %d and inst %d started\n", eps.tid, eps.inst);

  /* To get more diversity, each Population gets other external parameters.*/
  
  if(eps.SigDim > 0)
	eps.SigStart += (0.1 * (double) eps.inst);

  /*
  if(eps.SigDim > 0) {
	if(eps.SigDim == 1)
	  eps.TauOne *= (0.1 * (double) eps.inst);
	else
	  eps.TauLcl *= (0.1 * (double) eps.inst);
  }
  
  if(eps.PDim > 0) {
	if(eps.PDim == 1) {
	  eps.GamOne *= (double) eps.inst;
	  eps.PStart *= (double) eps.inst;
	}
	else
	  eps.GamLcl *= (double) eps.inst;
  }
  
#ifdef DEBUG
  fprintf(stderr, "%d:Tau = %g, Gamma = %g\n",
          eps.inst, eps.TauLcl, eps.GamOne);
#endif 
  */

  sprintf(&hlp[strlen(hlp)], ".%d", eps.tid);
  if(pvm_barrier(eps.Group, eps.Tasks+1) < 0)
	panic(A_WARN, "child-main", "pvm barrier error : %s : %d\n",
		  __FILE__, __LINE__);
  printf("\n%s as instance %d activ.\n", program_name, eps.inst);
  
#endif /* PVM */
  
  if(ipOptChk(&eps, Err)) {	  /* check parameter consistency */
	fprintf(stderr,"%s",Err); /* print warning message */
	panic(A_FATAL, "ipOptPrc", "Parameter inconsistency :\n%s\n%s : %d", 
		  Err, __FILE__, __LINE__);
  }
  
#ifndef PVM
  printf("\n%s activ.\n", program_name);
#endif	
  
#ifdef PVM
  if(NULL == (MigrationBuffer = (individual_tPtr *)
			  calloc(eps.Neighbours, sizeof(individual_tPtr))))
	panic(E_FATAL, "main-child", "not enough memory : %s : %d",
		  __FILE__, __LINE__);
  
  for(j = 0; j < eps.Neighbours; j++)
	MigrationBuffer[j]= inNewIndividual(eps.XDim, eps.DDim, eps.SigDim,
										eps.AlpDim, eps.PDim);
#endif

  initSuffix(&eps);			/* initialize suffix,
							   here string from ES.in*/
  parsFormatString(&eps);	/* pars format string */
  
  pop = poNewPopulation(&eps);
  
  for (i = 1; i <= eps.TotExp; i++) {  /* experiment loop */
	
	ExpNbr = i;
	
	initPop(&eps, pop, i);

#ifdef PVM
	/* Initialization of MigrationBuffer. */
	for(j = 0; j < eps.Neighbours; j++) {
	  if(j < pop->mu)
		inCopyIndividual(poGetIndividual(j+1, pop),
						 MigrationBuffer[j]);
	  else
		inCopyIndividual(poGetIndividual(pop->mu, pop),
						 MigrationBuffer[j]);
	}
#endif
	
	dataParLog(&eps);		/* dump parameters */
	fioTimeOut(&eps);		/* get start time */
	
	PopulationEVAL(&eps, pop, "parents");
	dataCol(&eps, pop, i);
	if (eps.GfxFlg)
	  gnInitGnuPipe(&eps);
	
#ifdef PVM
	send_Best(&eps, pop);
#endif
	
	while (!termExp(&eps)) { /* ES main loop */
	  
	  eps.GenCnt++;
	  PopulationCREATE(&eps, pop);
	  /* recombination and mutation including the
		 concept of letal mutations. */
	  PopulationEVAL(&eps, pop, "offspring");
	  PopulationSELECT(&eps, pop);
	  dataCol(&eps, pop, i);
	  if(eps.GfxFlg && (eps.GenCnt % eps.GfxIvl == 0))
		gnPlotGnuPipe(&eps);     /* plot data */

#ifdef PVM
	  send_Best(&eps, pop);
	  if(eps.GenCnt % eps.IsolationTime == 0) {
		send_Neighbours(&eps, pop);
		if(eps.Communication == COMMUNICATION_SYNCHRON)
		  recv_sync(&eps, MigrationBuffer);
	  }
	  if(eps.Communication == COMMUNICATION_ASYNCHRON)
		recv_async(&eps, MigrationBuffer);
	  for(j = 0; j < eps.Neighbours && j < pop->mu; j++)
		inCopyIndividual(MigrationBuffer[j],
					     poGetIndividual(j+1, pop));
#endif

	}  /* end ES main loop */
	
	dataBst(&eps, pop, i); /* dump best ind. */
	fioTimeOut(&eps);	   /* get end time */
	
  } /* end experiment loop */
  
  if(eps.GfxFlg)
	gnQuitGnuPipe(&eps);	/* stop plotting */
  
  reportExperiments(&eps);
  
  pop = poDeletePopulation(pop);
  
#ifdef PVM
  if(pvm_initsend(PvmDataRaw) < 0)
	panic(A_FATAL,"child-main", "initsend for group %d failed : %s : %d",
		  eps.inst, __FILE__, __LINE__);
  
  if((ptid = pvm_parent()) == PvmNoParent)
	panic(A_FATAL,"child-main", "pvm_parent for group %d failed : %s : %d",
		  eps.inst, __FILE__, __LINE__);
  
  if(pvm_pkint(&(eps.tid), 1, 1) < 0)
	panic(A_FATAL,"child-main", "packing of tid failed : %s : %d",
		  __FILE__, __LINE__);
  
  if(pvm_send(ptid, MSGTAG_END) < 0)
	panic(A_WARN, "child-main", 
		  "sending end message to master failed : %s : %d",
		  __FILE__, __LINE__);
  pvm_barrier(eps.Group, eps.Tasks+1);
  pvm_lvgroup(eps.Group);
	
  for(j = 0; j < eps.Neighbours; j++)
	inDeleteIndividual(MigrationBuffer[j]);
  free(MigrationBuffer);
#endif
	
  utGlobalTermination(&eps);
	
  printf("\n%s 1.0 terminated.\n", program_name);

#ifdef PVM
  pvm_exit();
#endif

  exit(0);
}