Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4UIcommandTree.cc
Go to the documentation of this file.
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// G4UIcommandTree
27//
28// Author: Makoto Asai (SLAC), 1998
29// --------------------------------------------------------------------
30
31#include "G4UIcommandTree.hh"
32#include "G4StateManager.hh"
33#include <fstream>
34#include "G4ios.hh"
35
36// --------------------------------------------------------------------
38{
39}
40
41// --------------------------------------------------------------------
42G4UIcommandTree::G4UIcommandTree(const char* thePathName)
43{
44 pathName = thePathName;
45}
46
47// --------------------------------------------------------------------
49{
50 G4int i;
51 G4int n_treeEntry = tree.size();
52 for(i = 0; i < n_treeEntry; ++i)
53 {
54 delete tree[i];
55 }
56}
57
58// --------------------------------------------------------------------
60{
61 return (pathName == right.GetPathName());
62}
63
64// --------------------------------------------------------------------
66{
67 return (pathName != right.GetPathName());
68}
69
70// --------------------------------------------------------------------
72 G4bool workerThreadOnly)
73{
74 G4String commandPath = newCommand->GetCommandPath();
75 G4String remainingPath = commandPath;
76 remainingPath.remove(0, pathName.length());
77 if(remainingPath.isNull())
78 {
79 if(!guidance)
80 {
81 guidance = newCommand;
82 if(!(newCommand->ToBeBroadcasted()))
83 broadcastCommands = false;
84 if(workerThreadOnly)
85 newCommand->SetWorkerThreadOnly();
86 }
87 return;
88 }
89 G4int i = remainingPath.first('/');
90 if(i == G4int(std::string::npos))
91 {
92 // Find command
93 G4int n_commandEntry = command.size();
94 for(G4int i_thCommand = 0; i_thCommand < n_commandEntry; ++i_thCommand)
95 {
96 if(remainingPath == command[i_thCommand]->GetCommandName())
97 {
98 return;
99 }
100 }
101 if(!broadcastCommands)
102 newCommand->SetToBeBroadcasted(false);
103 if(workerThreadOnly)
104 newCommand->SetWorkerThreadOnly();
105 command.push_back(newCommand);
106 return;
107 }
108 else
109 {
110 // Find path
111 G4String nextPath = pathName;
112 nextPath.append(remainingPath(0, i + 1));
113 G4int n_treeEntry = tree.size();
114 for(G4int i_thTree = 0; i_thTree < n_treeEntry; ++i_thTree)
115 {
116 if(nextPath == tree[i_thTree]->GetPathName())
117 {
118 if(!broadcastCommands)
119 newCommand->SetToBeBroadcasted(false);
120 tree[i_thTree]->AddNewCommand(newCommand, workerThreadOnly);
121 return;
122 }
123 }
124 G4UIcommandTree* newTree = new G4UIcommandTree(nextPath);
125 tree.push_back(newTree);
126 if(!broadcastCommands)
127 newCommand->SetToBeBroadcasted(false);
128 newTree->AddNewCommand(newCommand, workerThreadOnly);
129 return;
130 }
131}
132
133// --------------------------------------------------------------------
135 G4bool workerThreadOnly)
136{
137 if(workerThreadOnly && !(aCommand->IsWorkerThreadOnly()))
138 return;
139 G4String commandPath = aCommand->GetCommandPath();
140 G4String remainingPath = commandPath;
141 remainingPath.remove(0, pathName.length());
142 if(remainingPath.isNull())
143 {
144 guidance = nullptr;
145 }
146 else
147 {
148 G4int i = remainingPath.first('/');
149 if(i == G4int(std::string::npos))
150 {
151 // Find command
152 G4int n_commandEntry = command.size();
153 for(G4int i_thCommand = 0; i_thCommand < n_commandEntry; ++i_thCommand)
154 {
155 if(remainingPath == command[i_thCommand]->GetCommandName())
156 {
157 command.erase(command.begin() + i_thCommand);
158 break;
159 }
160 }
161 }
162 else
163 {
164 // Find path
165 G4String nextPath = pathName;
166 nextPath.append(remainingPath(0, i + 1));
167 G4int n_treeEntry = tree.size();
168 for(G4int i_thTree = 0; i_thTree < n_treeEntry; ++i_thTree)
169 {
170 if(nextPath == tree[i_thTree]->GetPathName())
171 {
172 tree[i_thTree]->RemoveCommand(aCommand);
173 G4int n_commandRemain = tree[i_thTree]->GetCommandEntry();
174 G4int n_treeRemain = tree[i_thTree]->GetTreeEntry();
175 if(n_commandRemain == 0 && n_treeRemain == 0)
176 {
177 G4UIcommandTree* emptyTree = tree[i_thTree];
178 tree.erase(tree.begin() + i_thTree);
179 delete emptyTree;
180 }
181 break;
182 }
183 }
184 }
185 }
186}
187
188// --------------------------------------------------------------------
189G4UIcommand* G4UIcommandTree::FindPath(const char* commandPath) const
190{
191 // This function tries to match a command name
192
193 G4String remainingPath = commandPath;
194 if(remainingPath.index(pathName) == std::string::npos)
195 {
196 return nullptr;
197 }
198 remainingPath.remove(0, pathName.length());
199 G4int i = remainingPath.first('/');
200 if(i == G4int(std::string::npos))
201 {
202 // Find command
203 G4int n_commandEntry = command.size();
204 for(G4int i_thCommand = 0; i_thCommand < n_commandEntry; ++i_thCommand)
205 {
206 if(remainingPath == command[i_thCommand]->GetCommandName())
207 {
208 return command[i_thCommand];
209 }
210 }
211 }
212 else
213 {
214 // Find path
215 G4String nextPath = pathName;
216 nextPath.append(remainingPath(0, i + 1));
217 G4int n_treeEntry = tree.size();
218 for(G4int i_thTree = 0; i_thTree < n_treeEntry; ++i_thTree)
219 {
220 if(nextPath == tree[i_thTree]->GetPathName())
221 {
222 return tree[i_thTree]->FindPath(commandPath);
223 }
224 }
225 }
226 return nullptr;
227}
228
229// --------------------------------------------------------------------
231{
232 // Try to match a command or a path with the one given.
233 // @commandPath : command or path to match
234 // @return the commandTree found or nullptr if not
235
236 G4String remainingPath = commandPath;
237 if(remainingPath.index(pathName) == std::string::npos)
238 {
239 return nullptr;
240 }
241 remainingPath.remove(0, pathName.length());
242 G4int i = remainingPath.first('/');
243 if(i != G4int(std::string::npos))
244 {
245 // Find path
246 G4String nextPath = pathName;
247 nextPath.append(remainingPath(0, i + 1));
248 G4int n_treeEntry = tree.size();
249 for(G4int i_thTree = 0; i_thTree < n_treeEntry; ++i_thTree)
250 {
251 if(tree[i_thTree]->GetPathName() == commandPath)
252 {
253 return tree[i_thTree];
254 }
255 else if(nextPath == tree[i_thTree]->GetPathName())
256 {
257 return tree[i_thTree]->FindCommandTree(commandPath);
258 }
259 }
260 }
261 else
262 {
263 return this;
264 }
265 return nullptr;
266}
267
268// --------------------------------------------------------------------
270{
271 G4String pName = aCommandPath;
272 G4String remainingPath = aCommandPath;
273 G4String empty = "";
274 G4String matchingPath = empty;
275
276 // find the tree
277 G4int jpre = pName.last('/');
278 if(jpre != G4int(G4String::npos))
279 pName.remove(jpre + 1);
280 G4UIcommandTree* aTree = FindCommandTree(pName);
281
282 if(!aTree)
283 {
284 return empty;
285 }
286
287 if(pName.index(pName) == std::string::npos)
288 return empty;
289
290 std::vector<G4String> paths;
291
292 // list matched directories/commands
293 G4String strtmp;
294 G4int nMatch = 0;
295
296 G4int Ndir = aTree->GetTreeEntry();
297 G4int Ncmd = aTree->GetCommandEntry();
298
299 // directory ...
300 for(G4int idir = 1; idir <= Ndir; ++idir)
301 {
302 G4String fpdir = aTree->GetTree(idir)->GetPathName();
303 // matching test
304 if(fpdir.index(remainingPath, 0) == 0)
305 {
306 if(nMatch == 0)
307 {
308 matchingPath = fpdir;
309 }
310 else
311 {
312 matchingPath = GetFirstMatchedString(fpdir, matchingPath);
313 }
314 ++nMatch;
315 paths.push_back(fpdir);
316 }
317 }
318
319 if(paths.size() >= 2)
320 {
321 G4cout << "Matching directories :" << G4endl;
322 for(unsigned int i_thCommand = 0; i_thCommand < paths.size(); ++i_thCommand)
323 {
324 G4cout << paths[i_thCommand] << G4endl;
325 }
326 }
327
328 // command ...
329 std::vector<G4String> commands;
330
331 for(G4int icmd = 1; icmd <= Ncmd; ++icmd)
332 {
333 G4String fpcmd =
334 aTree->GetPathName() + aTree->GetCommand(icmd)->GetCommandName();
335 // matching test
336 if(fpcmd.index(remainingPath, 0) == 0)
337 {
338 if(nMatch == 0)
339 {
340 matchingPath = fpcmd + " ";
341 }
342 else
343 {
344 strtmp = fpcmd + " ";
345 matchingPath = GetFirstMatchedString(matchingPath, strtmp);
346 }
347 nMatch++;
348 commands.push_back(fpcmd + " ");
349 }
350 }
351
352 if(commands.size() >= 2)
353 {
354 G4cout << "Matching commands :" << G4endl;
355 for(unsigned int i_thCommand = 0; i_thCommand < commands.size();
356 ++i_thCommand)
357 {
358 G4cout << commands[i_thCommand] << G4endl;
359 }
360 }
361
362 return matchingPath;
363}
364
365// --------------------------------------------------------------------
367 const G4String& str2) const
368{
369 G4int nlen1 = str1.length();
370 G4int nlen2 = str2.length();
371
372 G4int nmin = nlen1 < nlen2 ? nlen1 : nlen2;
373
374 G4String strMatched;
375 for(size_t i = 0; G4int(i) < nmin; ++i)
376 {
377 if(str1[i] == str2[i])
378 {
379 strMatched += str1[i];
380 }
381 else
382 {
383 break;
384 }
385 }
386
387 return strMatched;
388}
389
390// --------------------------------------------------------------------
392{
393 G4cout << "Command directory path : " << pathName << G4endl;
394 if(guidance != nullptr)
395 guidance->List();
396 G4cout << " Sub-directories : " << G4endl;
397 G4int n_treeEntry = tree.size();
398 for(G4int i_thTree = 0; i_thTree < n_treeEntry; ++i_thTree)
399 {
400 G4cout << " " << tree[i_thTree]->GetPathName();
401 if(tree[i_thTree]->GetGuidance() &&
402 tree[i_thTree]->GetGuidance()->IsWorkerThreadOnly())
403 {
404 G4cout << " @ ";
405 }
406 else
407 {
408 G4cout << " ";
409 }
410 G4cout << tree[i_thTree]->GetTitle() << G4endl;
411 }
412 G4cout << " Commands : " << G4endl;
413 G4int n_commandEntry = command.size();
414 for(G4int i_thCommand = 0; i_thCommand < n_commandEntry; ++i_thCommand)
415 {
416 G4cout << " " << command[i_thCommand]->GetCommandName();
417 if(command[i_thCommand]->IsWorkerThreadOnly())
418 {
419 G4cout << " @ ";
420 }
421 else
422 {
423 G4cout << " * ";
424 }
425 G4cout << command[i_thCommand]->GetTitle() << G4endl;
426 }
427}
428
429// --------------------------------------------------------------------
431{
432 G4cout << "Command directory path : " << pathName << G4endl;
433 if(guidance != nullptr)
434 guidance->List();
435 G4int i = 0;
436 G4cout << " Sub-directories : " << G4endl;
437 G4int n_treeEntry = tree.size();
438 for(G4int i_thTree = 0; i_thTree < n_treeEntry; ++i_thTree)
439 {
440 ++i;
441 G4cout << " " << i << ") " << tree[i_thTree]->GetPathName() << " "
442 << tree[i_thTree]->GetTitle() << G4endl;
443 }
444 G4cout << " Commands : " << G4endl;
445 G4int n_commandEntry = command.size();
446 for(G4int i_thCommand = 0; i_thCommand < n_commandEntry; ++i_thCommand)
447 {
448 ++i;
449 G4cout << " " << i << ") " << command[i_thCommand]->GetCommandName()
450 << " * " << command[i_thCommand]->GetTitle() << G4endl;
451 }
452}
453
454// --------------------------------------------------------------------
456{
457 ListCurrent();
458 G4int n_commandEntry = command.size();
459 for(G4int i_thCommand = 0; i_thCommand < n_commandEntry; ++i_thCommand)
460 {
461 command[i_thCommand]->List();
462 }
463 G4int n_treeEntry = tree.size();
464 for(G4int i_thTree = 0; i_thTree < n_treeEntry; ++i_thTree)
465 {
466 tree[i_thTree]->List();
467 }
468}
469
470// --------------------------------------------------------------------
471G4String G4UIcommandTree::CreateFileName(const char* pName)
472{
473 G4String fn = pName;
474 G4int idxs;
475 while((idxs = fn.index("/")) != G4int(std::string::npos))
476 {
477 fn(idxs) = '_';
478 }
479 fn += ".html";
480 return fn;
481}
482
483// --------------------------------------------------------------------
484G4String G4UIcommandTree::ModStr(const char* strS)
485{
486 G4String sx;
487 G4String str = strS;
488 for(G4int i = 0; i < G4int(str.length()); ++i)
489 {
490 char c = str(i);
491 switch(c)
492 {
493 case '<':
494 sx += "&lt;";
495 break;
496 case '>':
497 sx += "&gt;";
498 break;
499 case '&':
500 sx += "&amp;";
501 break;
502 default:
503 sx += c;
504 }
505 }
506 return sx;
507}
508
509// --------------------------------------------------------------------
511{
512 G4String ofileName = CreateFileName(pathName);
513 std::ofstream oF(ofileName, std::ios::out);
514
515 oF << "<html><head><title>Commands in " << ModStr(pathName)
516 << "</title></head>" << G4endl;
517 oF << "<body bgcolor=\"#ffffff\"><h2>" << ModStr(pathName) << "</h2><p>"
518 << G4endl;
519
520 if(guidance != nullptr)
521 {
522 for(std::size_t i = 0; i < guidance->GetGuidanceEntries(); ++i)
523 {
524 oF << ModStr(guidance->GetGuidanceLine(i)) << "<br>" << G4endl;
525 }
526 }
527
528 oF << "<p><hr><p>" << G4endl;
529
530 oF << "<h2>Sub-directories : </h2><dl>" << G4endl;
531 for(std::size_t i_thTree = 0; i_thTree < tree.size(); ++i_thTree)
532 {
533 oF << "<p><br><p><dt><a href=\""
534 << CreateFileName(tree[i_thTree]->GetPathName()) << "\">"
535 << ModStr(tree[i_thTree]->GetPathName()) << "</a>" << G4endl;
536 oF << "<p><dd>" << ModStr(tree[i_thTree]->GetTitle()) << G4endl;
537 tree[i_thTree]->CreateHTML();
538 }
539
540 oF << "</dl><p><hr><p>" << G4endl;
541
542 oF << "<h2>Commands : </h2><dl>" << G4endl;
543 for(std::size_t i_thCommand = 0; i_thCommand < command.size(); ++i_thCommand)
544 {
545 G4UIcommand* cmd = command[i_thCommand];
546 oF << "<p><br><p><dt><b>" << ModStr(cmd->GetCommandName());
547 if(cmd->GetParameterEntries() > 0)
548 {
549 for(std::size_t i_thParam = 0; i_thParam < cmd->GetParameterEntries();
550 ++i_thParam)
551 {
552 oF << " [<i>"
553 << ModStr(cmd->GetParameter(i_thParam)->GetParameterName())
554 << "</i>]";
555 }
556 }
557 oF << "</b>" << G4endl;
558 oF << "<p><dd>" << G4endl;
559 for(std::size_t i = 0; i < cmd->GetGuidanceEntries(); ++i)
560 {
561 oF << ModStr(cmd->GetGuidanceLine(i)) << "<br>" << G4endl;
562 }
563 if(!(cmd->GetRange()).isNull())
564 {
565 oF << "<p><dd>Range : " << ModStr(cmd->GetRange()) << G4endl;
566 }
567 std::vector<G4ApplicationState>* availabelStateList = cmd->GetStateList();
568 if(availabelStateList->size() == 6)
569 {
570 oF << "<p><dd>Available at all Geant4 states." << G4endl;
571 }
572 else
573 {
574 oF << "<p><dd>Available Geant4 state(s) : ";
575 for(std::size_t ias = 0; ias < availabelStateList->size(); ++ias)
576 {
578 (*availabelStateList)[ias])
579 << " " << G4endl;
580 }
581 }
582 if(cmd->GetParameterEntries() > 0)
583 {
584 oF << "<p><dd>Parameters<table border=1>" << G4endl;
585 for(std::size_t i_thParam = 0; i_thParam < cmd->GetParameterEntries();
586 ++i_thParam)
587 {
588 G4UIparameter* prm = cmd->GetParameter(i_thParam);
589 oF << "<tr><td>" << ModStr(prm->GetParameterName()) << G4endl;
590 oF << "<td>type " << prm->GetParameterType() << G4endl;
591 oF << "<td>";
592 if(prm->IsOmittable())
593 {
594 oF << "Omittable : ";
595 if(prm->GetCurrentAsDefault())
596 {
597 oF << "current value is used as the default value." << G4endl;
598 }
599 else
600 {
601 oF << "default value = " << prm->GetDefaultValue() << G4endl;
602 }
603 }
604 oF << "<td>";
605 if(!(prm->GetParameterRange()).isNull())
606 {
607 oF << "Parameter range : " << ModStr(prm->GetParameterRange())
608 << G4endl;
609 }
610 else if(!(prm->GetParameterCandidates()).isNull())
611 {
612 oF << "Parameter candidates : "
613 << ModStr(prm->GetParameterCandidates()) << G4endl;
614 }
615 }
616 oF << "</table>" << G4endl;
617 }
618 }
619
620 oF << "</dl></body></html>" << G4endl;
621 oF.close();
622}
623
624// --------------------------------------------------------------------
626{
627 G4String comName = comNameC;
628 for(std::size_t i = 0; i < tree.size(); ++i)
629 {
630 if(comName == tree[i]->GetPathName())
631 {
632 return tree[i];
633 }
634 }
635 return nullptr;
636}
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
G4String GetStateString(const G4ApplicationState &aState) const
static G4StateManager * GetStateManager()
G4String & remove(str_size)
G4String & append(const G4String &)
str_size index(const char *, G4int pos=0) const
G4bool isNull() const
std::size_t last(char) const
std::size_t first(char) const
G4bool operator!=(const G4UIcommandTree &right) const
G4int GetCommandEntry() const
const G4UIcommand * GetGuidance() const
G4UIcommand * GetCommand(G4int i)
const G4String & GetPathName() const
G4int GetTreeEntry() const
G4UIcommandTree * GetTree(G4int i)
void List() const
G4bool operator==(const G4UIcommandTree &right) const
void AddNewCommand(G4UIcommand *newCommand, G4bool workerThreadOnly=false)
G4UIcommandTree * FindCommandTree(const char *commandPath)
void ListCurrentWithNum() const
const G4String GetTitle() const
G4String CompleteCommandPath(const G4String &commandPath)
G4String GetFirstMatchedString(const G4String &, const G4String &) const
G4UIcommand * FindPath(const char *commandPath) const
void ListCurrent() const
void RemoveCommand(G4UIcommand *aCommand, G4bool workerThreadOnly=false)
void SetToBeBroadcasted(G4bool val)
Definition: G4UIcommand.hh:172
G4bool IsWorkerThreadOnly() const
Definition: G4UIcommand.hh:177
std::size_t GetParameterEntries() const
Definition: G4UIcommand.hh:138
const G4String & GetGuidanceLine(G4int i) const
Definition: G4UIcommand.hh:132
G4UIparameter * GetParameter(G4int i) const
Definition: G4UIcommand.hh:139
G4bool ToBeBroadcasted() const
Definition: G4UIcommand.hh:173
const G4String & GetCommandPath() const
Definition: G4UIcommand.hh:136
std::size_t GetGuidanceEntries() const
Definition: G4UIcommand.hh:128
std::vector< G4ApplicationState > * GetStateList()
Definition: G4UIcommand.hh:140
void SetWorkerThreadOnly(G4bool val=true)
Definition: G4UIcommand.hh:176
virtual void List()
Definition: G4UIcommand.cc:395
const G4String & GetCommandName() const
Definition: G4UIcommand.hh:137
const G4String & GetRange() const
Definition: G4UIcommand.hh:127
const G4String & GetParameterCandidates() const
G4bool IsOmittable() const
const G4String & GetParameterRange() const
G4bool GetCurrentAsDefault() const
char GetParameterType() const
const G4String & GetParameterName() const
const G4String & GetDefaultValue() const