1 | #include "sectionToExecute.h" |
---|
2 | #include "softwareUnknown.h" |
---|
3 | #include "softwareParmela.h" |
---|
4 | #include "softwareTransport.h" |
---|
5 | #include "softwareGenerator.h" |
---|
6 | #include "softwareGenerator.h" |
---|
7 | #include "softwareUsersprogram.h" |
---|
8 | #include "softwareTest.h" |
---|
9 | #include "softwareMadx.h" //xx |
---|
10 | |
---|
11 | |
---|
12 | sectionToExecute::sectionToExecute(abstractElement* f, abstractSoftware* s,dataManager* data,sector* sect) { |
---|
13 | dataManager_ = data; |
---|
14 | sector_ = sect; |
---|
15 | |
---|
16 | elements_.push_back(f); |
---|
17 | if (s == NULL) { |
---|
18 | software_ = new softwareUnknown(); |
---|
19 | } else { |
---|
20 | software_ = s; |
---|
21 | } |
---|
22 | } |
---|
23 | |
---|
24 | |
---|
25 | bool sectionToExecute::insertAfter(abstractElement* previousElement,abstractElement* currentElement) { |
---|
26 | |
---|
27 | std::vector<abstractElement*>::iterator it; |
---|
28 | for (it = elements_.begin(); it < elements_.end(); it++) { |
---|
29 | if (*it == previousElement ) { |
---|
30 | elements_.insert (it+1,currentElement); |
---|
31 | return true; |
---|
32 | } |
---|
33 | } |
---|
34 | return false; |
---|
35 | } |
---|
36 | |
---|
37 | |
---|
38 | void sectionToExecute::setSoftware(std::string logiciel) { |
---|
39 | |
---|
40 | abstractSoftware* prog; |
---|
41 | string inputFileName; |
---|
42 | if(logiciel == "parmela") { |
---|
43 | inputFileName = "parmin"; |
---|
44 | prog = new softwareParmela(inputFileName, this,dataManager_ ); |
---|
45 | } else if (logiciel == "transport") { |
---|
46 | inputFileName = "transport.input"; |
---|
47 | prog = new softwareTransport(inputFileName, this,dataManager_ ); |
---|
48 | } else if (logiciel == "generator") { |
---|
49 | inputFileName = "generator.in"; |
---|
50 | prog = new softwareGenerator(inputFileName, this,dataManager_ ); |
---|
51 | } else if (logiciel == "madx") { |
---|
52 | inputFileName = "madx.input"; |
---|
53 | prog = new softwareMadx(inputFileName,this,dataManager_ ); |
---|
54 | } else if (logiciel == "usersprogram") { |
---|
55 | inputFileName = "dummy"; |
---|
56 | prog = new softwareUsersprogram(inputFileName, this,dataManager_ ); |
---|
57 | } else if (logiciel == "test") { |
---|
58 | prog = new softwareTest(inputFileName, this,dataManager_ ); |
---|
59 | } else { |
---|
60 | prog = new softwareUnknown(); |
---|
61 | } |
---|
62 | |
---|
63 | setSoftware(prog); |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | void sectionToExecute::setFirstElement(int index) { |
---|
68 | // This will set the beginning of this section to the element with the given name |
---|
69 | |
---|
70 | // Possible cases : |
---|
71 | // |
---|
72 | // | section 1 | section 2 | section 3 | section 4 | section 5 | |
---|
73 | // | a b c d | e f g h | i j k l | m n o p | q r | Before (example) |
---|
74 | |
---|
75 | // Moved in a previous section : |
---|
76 | // | a b c d | e f | _g_ h i j k l | m n o p | q r | Case 1 : move beginning of section 3 to "g" |
---|
77 | // | a b c | | _d_ e f g h i j k l | m n o p | q r | Case 2 : move beginning of section 3 to "d" => section 2 will be removed ! |
---|
78 | // => all elements from "d"/"g" to "l" will be moved in the 3rd section |
---|
79 | |
---|
80 | // Moved in the same section : |
---|
81 | // | a b c d | e f g h i j | _k_ l | m n o p | q r | Case 3 : move beginning of section 3 to "k" |
---|
82 | // => all elements from "i"(begin of 3rd section) to before "k" will be moved in the 2nd (3-1) section |
---|
83 | |
---|
84 | // Moved in a next section : |
---|
85 | // | a b c d | e f g h i j k l m | _n_ | o p | q r | Case 4 : move beginning of section 3 to "n" |
---|
86 | // | a b c d | e f g h i j k l m n o p | _q_ | | r | Case 5 : move beginning of section 3 to "q" => remove section 4 ! |
---|
87 | // => all elements from "i"(begin of 3rd section) to before "n"/"q" will be moved in the 2nd (3-1) section |
---|
88 | // => "n"/"q" element is set to be alone in 3rd section |
---|
89 | |
---|
90 | std::vector <sectionToExecute*> sectVect = sector_->getSectionsToExecute(); |
---|
91 | unsigned int sectionNumber = 0; |
---|
92 | int newBeginSectionNumber = 0; |
---|
93 | unsigned long indexOfNewBeginSectionNumber = 0; |
---|
94 | |
---|
95 | // get this section number |
---|
96 | // get the section number of the new begin element |
---|
97 | unsigned int n = 0; |
---|
98 | bool newFound = false; |
---|
99 | |
---|
100 | for (unsigned int a=0; a< sectVect.size(); a++) { |
---|
101 | if (sectVect[a] == this) { |
---|
102 | sectionNumber = a; |
---|
103 | } |
---|
104 | n += sectVect[a]->getElements().size(); |
---|
105 | if ((n >index ) && (!newFound)) { |
---|
106 | newBeginSectionNumber = a; |
---|
107 | indexOfNewBeginSectionNumber = index - (n-sectVect[a]->getElements().size()); |
---|
108 | newFound = true; |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | if (!newFound ) { |
---|
113 | return; // Impossible to find this index |
---|
114 | } |
---|
115 | |
---|
116 | // Moved in a previous section |
---|
117 | if ( newBeginSectionNumber < sectionNumber ) { |
---|
118 | for (int a = sectionNumber-1 ; a >= newBeginSectionNumber; a--) { |
---|
119 | std::vector< abstractElement* > elemsVect = sectVect[a]->getElements(); |
---|
120 | long stop = 0; |
---|
121 | long start = elemsVect.size(); |
---|
122 | if (a == newBeginSectionNumber) { |
---|
123 | stop = indexOfNewBeginSectionNumber; |
---|
124 | } |
---|
125 | for (long elemIndex = start; elemIndex > stop; elemIndex--) { |
---|
126 | insertAtFirst(elemsVect.back()); |
---|
127 | elemsVect.pop_back(); |
---|
128 | |
---|
129 | // remove elements |
---|
130 | sectVect[a]->removeLastElement(); |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | // Moved in the same section : |
---|
135 | } else if (( newBeginSectionNumber == sectionNumber ) && (sectionNumber > 0)){ |
---|
136 | for (long elemIndex = 0; elemIndex < indexOfNewBeginSectionNumber; elemIndex++) { |
---|
137 | sectVect[sectionNumber-1]->insertAtLast(elements_.front()); |
---|
138 | removeFirstElement(); |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | // Moved in a next section : |
---|
143 | } else if ( newBeginSectionNumber > sectionNumber ) { |
---|
144 | |
---|
145 | |
---|
146 | } |
---|
147 | |
---|
148 | // Suppress the empty sections |
---|
149 | for (unsigned int a=0; a< sectVect.size(); a++) { |
---|
150 | if (sectVect[a]->getElements().size() == 0 ) { |
---|
151 | sector_->clearSectionToExecute(a); |
---|
152 | } |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | bool sectionToExecute::isInside(abstractElement* previousElement) { |
---|
158 | for (unsigned int a=0; a< elements_.size(); a++) { |
---|
159 | if (elements_[a] == previousElement ) { |
---|
160 | return true; |
---|
161 | } |
---|
162 | } |
---|
163 | return false; |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | void sectionToExecute::removeElement(abstractElement* previousElement){ |
---|
168 | std::vector<abstractElement*>::iterator it; |
---|
169 | for (it = elements_.begin(); it < elements_.end(); it++) { |
---|
170 | if (*it == previousElement ) { |
---|
171 | elements_.erase(it); |
---|
172 | return; |
---|
173 | } |
---|
174 | } |
---|
175 | } |
---|