source: trunk/source/intercoms/src/G4UImanager.cc @ 1256

Last change on this file since 1256 was 1256, checked in by garnier, 14 years ago

remove cycle dependency

File size: 14.6 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer                                           *
4// *                                                                  *
5// * The  Geant4 software  is  copyright of the Copyright Holders  of *
6// * the Geant4 Collaboration.  It is provided  under  the terms  and *
7// * conditions of the Geant4 Software License,  included in the file *
8// * LICENSE and available at  http://cern.ch/geant4/license .  These *
9// * include a list of copyright holders.                             *
10// *                                                                  *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work  make  any representation or  warranty, express or implied, *
14// * regarding  this  software system or assume any liability for its *
15// * use.  Please see the license in the file  LICENSE  and URL above *
16// * for the full disclaimer and the limitation of liability.         *
17// *                                                                  *
18// * This  code  implementation is the result of  the  scientific and *
19// * technical work of the GEANT4 collaboration.                      *
20// * By using,  copying,  modifying or  distributing the software (or *
21// * any work based  on the software)  you  agree  to acknowledge its *
22// * use  in  resulting  scientific  publications,  and indicate your *
23// * acceptance of all terms of the Geant4 Software license.          *
24// ********************************************************************
25//
26//
27// $Id: G4UImanager.cc,v 1.34 2010/05/19 14:50:30 lgarnier Exp $
28// GEANT4 tag $Name:  $
29//
30//
31// ---------------------------------------------------------------------
32
33#include "G4UImanager.hh"
34#include "G4UIcommandTree.hh"
35#include "G4UIcommand.hh"
36#include "G4UIsession.hh"
37#include "G4UIbatch.hh"
38#include "G4UIcontrolMessenger.hh"
39#include "G4UnitsMessenger.hh"
40#include "G4ios.hh"
41#include "G4strstreambuf.hh"
42#include "G4StateManager.hh"
43#include "G4UIaliasList.hh"
44#include "G4Tokenizer.hh"
45
46#include <sstream>
47
48
49G4UImanager * G4UImanager::fUImanager = 0;
50G4bool G4UImanager::fUImanagerHasBeenKilled = false;
51
52G4UImanager * G4UImanager::GetUIpointer()
53{
54  if(!fUImanager) 
55  {
56    if(!fUImanagerHasBeenKilled)
57    {
58      fUImanager = new G4UImanager;
59      fUImanager->CreateMessenger();
60    }
61  }
62  return fUImanager;
63}
64
65G4UImanager::G4UImanager()
66:G4VStateDependent(true)
67{
68  savedCommand = 0;
69  treeTop = new G4UIcommandTree("/");
70  aliasList = new G4UIaliasList;
71  G4String nullString;
72  savedParameters = nullString;
73  verboseLevel = 0;
74  saveHistory = false;
75  session = NULL;
76  g4UIWindow = NULL;
77  SetCoutDestination(session);
78  pauseAtBeginOfEvent = false;
79  pauseAtEndOfEvent = false;
80  maxHistSize = 20;
81}
82
83void G4UImanager::CreateMessenger()
84{
85  UImessenger = new G4UIcontrolMessenger;
86  UnitsMessenger = new G4UnitsMessenger;
87}
88
89G4UImanager::~G4UImanager()
90{
91  SetCoutDestination(NULL);
92  histVec.clear();
93  if(saveHistory) historyFile.close();
94  delete UImessenger;
95  delete UnitsMessenger;
96  delete treeTop;
97  delete aliasList;
98  fUImanagerHasBeenKilled = true;
99  fUImanager = NULL;
100}
101
102G4UImanager::G4UImanager(const G4UImanager &)
103:G4VStateDependent(true) {;}
104const G4UImanager & G4UImanager::operator=(const G4UImanager &right)
105{ return right; }
106G4int G4UImanager::operator==(const G4UImanager &right) const
107{ return (this==&right); }
108G4int G4UImanager::operator!=(const G4UImanager &right) const
109{ return (this!=&right); }
110
111G4String G4UImanager::GetCurrentValues(const char * aCommand)
112{
113  G4String theCommand = aCommand;
114  savedCommand = treeTop->FindPath( theCommand );
115  if( savedCommand == NULL ) 
116  {
117    G4cerr << "command not found" << G4endl;
118    return G4String();
119  }
120  return savedCommand->GetCurrentValue();
121}
122
123G4String G4UImanager::GetCurrentStringValue(const char * aCommand, 
124G4int parameterNumber, G4bool reGet)
125{
126  if(reGet || savedCommand == NULL)
127  {
128    savedParameters = GetCurrentValues( aCommand );
129  }
130  G4Tokenizer savedToken( savedParameters );
131  G4String token;
132  for(G4int i_thParameter=0;i_thParameter<parameterNumber;i_thParameter++)
133  {
134    token = savedToken();
135    if( token.isNull() ) return G4String();
136    if( token[(size_t)0] == '"' )
137    {
138      token.append(" ");
139      token.append(savedToken("\""));
140    }
141  }
142  return token;
143}
144
145G4String G4UImanager::GetCurrentStringValue(const char * aCommand, 
146const char * aParameterName, G4bool reGet)
147{
148  if(reGet || savedCommand == NULL)
149  {
150    G4String parameterValues = GetCurrentValues( aCommand );
151  }
152  for(G4int i=0;i<savedCommand->GetParameterEntries();i++)
153  {
154    if( aParameterName ==
155      savedCommand->GetParameter(i)->GetParameterName() )
156      return GetCurrentStringValue(aCommand,i+1,false);
157  }
158  return G4String();
159}
160
161G4int G4UImanager::GetCurrentIntValue(const char * aCommand,
162const char * aParameterName, G4bool reGet)
163{
164  G4String targetParameter = 
165     GetCurrentStringValue( aCommand, aParameterName, reGet );
166  G4int value;
167  const char* t = targetParameter;
168  std::istringstream is(t);
169  is >> value;
170  return value;
171}
172
173G4int G4UImanager::GetCurrentIntValue(const char * aCommand,
174G4int parameterNumber, G4bool reGet)
175{
176  G4String targetParameter = 
177     GetCurrentStringValue( aCommand, parameterNumber, reGet );
178  G4int value;
179  const char* t = targetParameter;
180  std::istringstream is(t);
181  is >> value;
182  return value;
183}
184
185G4double G4UImanager::GetCurrentDoubleValue(const char * aCommand,
186const char * aParameterName, G4bool reGet)
187{
188  G4String targetParameter = 
189     GetCurrentStringValue( aCommand, aParameterName, reGet );
190  G4double value;
191  const char* t = targetParameter;
192  std::istringstream is(t);
193  is >> value;
194  return value;
195}
196
197G4double G4UImanager::GetCurrentDoubleValue(const char * aCommand,
198G4int parameterNumber, G4bool reGet)
199{
200  G4String targetParameter = 
201     GetCurrentStringValue( aCommand, parameterNumber, reGet );
202  G4double value;
203  const char* t = targetParameter;
204  std::istringstream is(t);
205  is >> value;
206  return value;
207}
208
209void G4UImanager::AddNewCommand(G4UIcommand * newCommand)
210{
211  treeTop->AddNewCommand( newCommand );
212}
213
214void G4UImanager::RemoveCommand(G4UIcommand * aCommand)
215{
216  treeTop->RemoveCommand( aCommand );
217}
218
219void G4UImanager::ExecuteMacroFile(const char * fileName)
220{
221  G4UIsession* batchSession = new G4UIbatch(fileName,session);
222  session = batchSession;
223  G4UIsession* previousSession = session->SessionStart();
224  delete session;
225  session = previousSession;
226}
227
228void G4UImanager::LoopS(const char* valueList)
229{
230  G4String vl = valueList;
231  G4Tokenizer parameterToken(vl);
232  G4String mf = parameterToken();
233  G4String vn = parameterToken();
234  G4String c1 = parameterToken();
235  c1 += " ";
236  c1 += parameterToken();
237  c1 += " ";
238  c1 += parameterToken();
239  const char* t1 = c1;
240  std::istringstream is(t1);
241  G4double d1;
242  G4double d2;
243  G4double d3;
244  is >> d1 >> d2 >> d3;
245  Loop(mf,vn,d1,d2,d3);
246}
247 
248void G4UImanager::Loop(const char * macroFile,const char * variableName,
249                   G4double initialValue,G4double finalValue,G4double stepSize)
250{
251  G4String cd;
252  if (stepSize > 0) {
253    for(G4double d=initialValue;d<=finalValue;d+=stepSize)
254      {
255        std::ostringstream os;
256        os << d;
257        cd += os.str();
258        cd += " ";
259      }
260  } else {
261    for(G4double d=initialValue;d>=finalValue;d+=stepSize)
262      {
263        std::ostringstream os;
264        os << d;
265        cd += os.str();
266        cd += " ";
267      }
268  }
269  Foreach(macroFile,variableName,cd);
270}
271
272void G4UImanager::ForeachS(const char* valueList)
273{
274  G4String vl = valueList;
275  G4Tokenizer parameterToken(vl);
276  G4String mf = parameterToken();
277  G4String vn = parameterToken();
278  G4String c1 = parameterToken();
279  G4String ca;
280  while(!((ca=parameterToken()).isNull()))
281  {
282    c1 += " ";
283    c1 += ca;
284  }
285  Foreach(mf,vn,c1);
286}
287
288void G4UImanager::Foreach(const char * macroFile,const char * variableName,
289                   const char * candidates)
290{
291  G4String candidatesString = candidates;
292  G4Tokenizer parameterToken( candidatesString );
293  G4String cd;
294  while(!((cd=parameterToken()).isNull()))
295  {
296    G4String vl = variableName;
297    vl += " ";
298    vl += cd;
299    SetAlias(vl);
300    ExecuteMacroFile(macroFile);
301  }
302}
303
304
305G4String G4UImanager::SolveAlias(const char* aCmd)
306{
307  G4String aCommand = aCmd;
308  G4int ia = aCommand.index("{");
309  G4int iz = aCommand.index("#");
310  while((ia != G4int(std::string::npos))&&((iz==G4int(std::string::npos))||(ia<iz)))
311  {
312    G4int ibx = -1;
313    while(ibx<0)
314    {
315      G4int ib = aCommand.index("}");
316      if( ib == G4int(std::string::npos) )
317      {
318        G4cerr << aCommand << G4endl;
319        for(G4int iz=0;iz<ia;iz++) G4cerr << " ";
320        G4cerr << "^" << G4endl;
321        G4cerr << "Unmatched alias parenthis -- command ignored" << G4endl;
322        G4String nullStr;
323        return nullStr;
324      }
325      G4String ps = aCommand(ia+1,aCommand.length()-(ia+1));
326      G4int ic = ps.index("{");
327      G4int id = ps.index("}");
328      if(ic!=G4int(std::string::npos) && ic < id)
329      { ia+=ic+1; }
330      else
331      { ibx = ib; }
332    }
333    //--- Here ia represents the position of innermost "{"
334    //--- and ibx represents corresponding "}"
335    G4String subs;
336    if(ia>0) subs = aCommand(0,ia);
337    G4String alis = aCommand(ia+1,ibx-ia-1);
338    G4String rems = aCommand(ibx+1,aCommand.length()-ibx);
339    // G4cout << "<" << subs << "> <" << alis << "> <" << rems << ">" << G4endl;
340    G4String* alVal = aliasList->FindAlias(alis);
341    if(!alVal)
342    {
343      G4cerr << "Alias <" << alis << "> not found -- command ignored" << G4endl;
344      G4String nullStr;
345      return nullStr;
346    }
347    aCommand = subs+(*alVal)+rems;
348    ia = aCommand.index("{");
349  }
350  return aCommand;
351}
352
353G4int G4UImanager::ApplyCommand(G4String aCmd)
354{
355  return ApplyCommand(aCmd.data());
356}
357
358G4int G4UImanager::ApplyCommand(const char * aCmd)
359{
360  G4String aCommand = SolveAlias(aCmd);
361  if(aCommand.isNull()) return fAliasNotFound;
362  if(verboseLevel) G4cout << aCommand << G4endl;
363  G4String commandString;
364  G4String commandParameter;
365
366  G4int i = aCommand.index(" ");
367  if( i != G4int(std::string::npos) )
368  {
369    commandString = aCommand(0,i);
370    commandParameter = aCommand(i+1,aCommand.length()-(i+1));
371  }
372  else
373  {
374    commandString = aCommand;
375  }
376
377  // remove doubled slash
378  G4int len = commandString.length();
379  G4int ll = 0;
380  G4String a1;
381  G4String a2;
382  while(ll<len-1)
383  {
384    if(commandString(ll,2)=="//")
385    {
386      if(ll==0)
387      { commandString.remove(ll,1); }
388      else
389      {
390        a1 = commandString(0,ll);
391        a2 = commandString(ll+1,len-ll-1);
392        commandString = a1+a2;
393      }
394      len--;
395    }
396    else
397    { ll++; }
398  }
399
400  G4UIcommand * targetCommand = treeTop->FindPath( commandString );
401  if( targetCommand == NULL )
402  { return fCommandNotFound; }
403
404  if(!(targetCommand->IsAvailable())) 
405  { return fIllegalApplicationState; }
406 
407  if(saveHistory) historyFile << aCommand << G4endl; 
408  if( G4int(histVec.size()) >= maxHistSize )
409  { histVec.erase(histVec.begin()); }
410  histVec.push_back(aCommand);
411  return targetCommand->DoIt( commandParameter );
412}
413
414void G4UImanager::StoreHistory(const char* fileName)
415{ StoreHistory(true,fileName); }
416
417void G4UImanager::StoreHistory(G4bool historySwitch,const char* fileName)
418{
419  if(historySwitch)
420  {
421    if(saveHistory)
422    { historyFile.close(); }
423    historyFile.open((char*)fileName);
424    saveHistory = true;
425  }
426  else
427  {
428    historyFile.close();
429    saveHistory = false;
430  }
431  saveHistory = historySwitch;
432}
433
434void G4UImanager::PauseSession(const char* msg)
435{
436  if(session) session->PauseSessionStart(msg); 
437}
438
439void G4UImanager::ListCommands(const char* direct)
440{
441  G4UIcommandTree* comTree = FindDirectory(direct);
442  if(comTree)
443  { comTree->List(); }
444  else
445  { G4cout << direct << " is not found." << G4endl; }
446}
447
448G4UIcommandTree* G4UImanager::FindDirectory(const char* dirName)
449{
450  G4String aDirName = dirName;
451  G4String targetDir = aDirName.strip(G4String::both);
452  if( targetDir( targetDir.length()-1 ) != '/' )
453  { targetDir += "/"; }
454  G4UIcommandTree* comTree = treeTop;
455  if( targetDir == "/" )
456  { return comTree; }
457  G4int idx = 1;
458  while( idx < G4int(targetDir.length())-1 )
459  {
460    G4int i = targetDir.index("/",idx);
461    G4String targetDirString = targetDir(0,i+1);
462    comTree = comTree->GetTree(targetDirString);
463    if( comTree == NULL )
464    { return NULL; }
465    idx = i+1;
466  }
467  return comTree;
468}
469
470G4bool G4UImanager::Notify(G4ApplicationState requestedState)
471{
472  //G4cout << G4StateManager::GetStateManager()->GetStateString(requestedState) << " <--- " << G4StateManager::GetStateManager()->GetStateString(G4StateManager::GetStateManager()->GetPreviousState()) << G4endl;
473  if(pauseAtBeginOfEvent)
474  {
475    if(requestedState==G4State_EventProc &&
476       G4StateManager::GetStateManager()->GetPreviousState()==G4State_GeomClosed)
477    { PauseSession("BeginOfEvent"); }
478  }
479  if(pauseAtEndOfEvent)
480  {
481    if(requestedState==G4State_GeomClosed &&
482       G4StateManager::GetStateManager()->GetPreviousState()==G4State_EventProc)
483    { PauseSession("EndOfEvent"); }
484  }
485  return true;
486}
487
488//void G4UImanager::Interact()
489//{
490//  Interact(G4String("G4> "));
491//}
492
493//void G4UImanager::Interact(const char * pC)
494//{
495//  G4cerr << "G4UImanager::Interact() is out of date and is not used anymore." << G4endl;
496//  G4cerr << "This method will be removed shortly!!!" << G4endl;
497//  G4cerr << "In case of main() use" << G4endl;
498//  G4cerr << "    G4UIsession * session = new G4UIterminal;" << G4endl;
499//  G4cerr << "    session->SessionStart();" << G4endl;
500//  G4cerr << "In other cases use" << G4endl;
501//  G4cerr << "    G4StateManager::GetStateManager()->Pause();" << G4endl;
502//}
503
504
505
506void G4UImanager::SetCoutDestination(G4UIsession *const value)
507{
508    G4coutbuf.SetDestination(value);
509    G4cerrbuf.SetDestination(value);
510}
511
512void G4UImanager::SetAlias(const char * aliasLine)
513{
514  G4String aLine = aliasLine;
515  G4int i = aLine.index(" ");
516  G4String aliasName = aLine(0,i);
517  G4String aliasValue = aLine(i+1,aLine.length()-(i+1));
518  if(aliasValue(0)=='"')
519  { 
520    G4String strippedValue;
521    if(aliasValue(aliasValue.length()-1)=='"')
522    { strippedValue = aliasValue(1,aliasValue.length()-2); }
523    else
524    { strippedValue = aliasValue(1,aliasValue.length()-1); }
525    aliasValue = strippedValue;
526  }
527
528  aliasList->ChangeAlias(aliasName,aliasValue);
529}
530
531void G4UImanager::RemoveAlias(const char * aliasName)
532{
533  G4String aL = aliasName;
534  G4String targetAlias = aL.strip(G4String::both);
535  aliasList->RemoveAlias(targetAlias);
536}
537
538void G4UImanager::ListAlias()
539{
540  aliasList->List();
541}
542
543void G4UImanager::CreateHTML(const char* dir)
544{ 
545  G4UIcommandTree* tr = FindDirectory(dir);
546  if(tr!=0)
547  { tr->CreateHTML(); }
548  else
549  { G4cerr << "Directory <" << dir << "> is not found." << G4endl; }
550}
551
Note: See TracBrowser for help on using the repository browser.