2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ */
34 #include "boost/filesystem.hpp"
35 #include "bbtkBBPInterpreter.h"
37 namespace bf = boost::filesystem;
39 typedef std::vector<std::vector<int> > Graph;
41 typedef std::set<std::string> Dependencies;
42 typedef std::vector<Dependencies> DependenciesVector;
43 typedef std::vector<std::string> BoxesVector;
45 std::vector<bf::path> getFileList(const std::string& path);
47 bool isCycle(const Graph& g);
48 bool checkCycle(const Graph& g, const int& i, std::vector<bool>& v);
50 void setPriorities(const Graph& g, std::vector<int>& p);
51 void setPriority(const Graph& g, const int i, std::vector<int>& p);
53 int main(int argc, char **argv)
58 std::cout << "usage : bbConfigurator <path_to_bbs> <package_name> <output_path>" << std::endl;
62 std::string path_bbs(argv[1]);
63 std::string package_name(argv[2]);
64 std::string path_out(argv[3]);
66 // Get bbs files in path_bbs
67 std::vector<bf::path> files = getFileList(path_bbs);
70 std::cout << "No files to check." << std::endl;
72 // Write results to bbp file
74 std::string fname = path_out + "\\" + package_name + ".bbp";
76 std::string fname = path_out + "/" + package_name + ".bbp";
79 std::ofstream out(fname.c_str());
80 out << "#-----------------------------------------" << std::endl;
81 out << "# Include script for bbtk package '" << package_name << "'" << std::endl;
82 out << "# Automatically generated by bbpConfigurator" << std::endl;
83 out << "#-----------------------------------------" << std::endl;
84 out << "load "<< package_name << std::endl;
85 out << "#-----------------------------------------" << std::endl;
86 out << "package "<< package_name << std::endl;
87 out << "#-----------------------------------------" << std::endl;
88 out << "endpackage" << std::endl;
89 out << "#-- EOF ----------------------------------" << std::endl;
98 // Order files by dependencies
99 // Get DependenciesVector and Box Names
100 DependenciesVector deps;
104 for (int i = 0; i < (int)files.size(); ++i) {
105 bbtk::BBPInterpreter::Pointer I = bbtk::BBPInterpreter::New();
106 I->InterpretFile(files[i].string());
107 boxs.push_back( ((bbtk::BBPInterpreter*)(I.get()))->boxName );
108 deps.push_back( ((bbtk::BBPInterpreter*)(I.get()))->dependencies );
110 //print box name and dependencies
111 // std::cout << ((bbtk::BBPInterpreter*)(I.get()))->boxName << ": ";
113 // Dependencies::iterator it = ((bbtk::BBPInterpreter*)(I.get()))->dependencies.begin();
114 // it != ((bbtk::BBPInterpreter*)(I.get()))->dependencies.end();
116 // std::cout << *it << ", ";
118 // std::cout << std::endl;
122 // Only keep dependencies from package
123 Dependencies boxNamesSet(boxs.begin(), boxs.end());
125 //std::cout << "after: " << std::endl;
126 for (DependenciesVector::iterator it = deps.begin(); it != deps.end(); it++) {
128 std::set_intersection(it->begin(), it->end(), boxNamesSet.begin(), boxNamesSet.end(),std::back_inserter(tmp));
129 Dependencies tmp1(tmp.begin(),tmp.end());
131 //print clean dependencies
133 // Dependencies::iterator it1 = it->begin();
136 // std::cout << *it1 << ", ";
138 // std::cout << std::endl;
142 // Create dependencies graph
143 std::vector<std::vector<int> > g(boxs.size(), std::vector<int>());
144 std::map<std::string, int> idxs;
147 for (int i = 0; i < (int)boxs.size(); ++i)
153 for (DependenciesVector::iterator dit = deps.begin(); dit != deps.end(); dit++, boxit++)
155 for (Dependencies::iterator ddit = dit->begin(); ddit != dit->end(); ddit++)
157 g[boxit].push_back(idxs[*ddit]);
161 // Check there are no cycles in graph
164 std::cout << "there are dependency cycles, please check your scripts." << std::endl;
169 std::cout << "no cycles, we keep going." << std::endl;
170 std::vector<int> priorities(boxs.size(), -1);
171 setPriorities(g, priorities);
172 // for (int i = 0; i < (int)priorities.size(); i++)
174 // std::cout << priorities[i] << " ";
176 // std::cout << std::endl;
178 // Write results to bbp file
180 std::string fname = path_out + "\\" + package_name + ".bbp";
182 std::string fname = path_out + "/" + package_name + ".bbp";
185 std::ofstream out(fname.c_str());
186 out << "#-----------------------------------------" << std::endl;
187 out << "# Include script for bbtk package '" << package_name << "'" << std::endl;
188 out << "# Automatically generated by bbpConfigurator" << std::endl;
189 out << "#-----------------------------------------" << std::endl;
190 out << "load "<< package_name << std::endl;
191 out << "#-----------------------------------------" << std::endl;
192 out << "package "<< package_name << std::endl;
193 out << "#-----------------------------------------" << std::endl;
195 //each bbs file ordered.
196 //include [package_name]/boxes/[file_bbs]
197 //#-----------------------------------------
199 // for (int i = 0; i < (int)files.size(); ++i) {
200 // out << "include " << package_name << "/boxes/" << files[i].filename().string() << std::endl;
201 // out << "#-----------------------------------------" << std::endl;
204 // find max priority level
206 for (int i = 0; i < (int)priorities.size(); i++)
207 mx_priority = std::max(mx_priority, priorities[i]);
209 // for each priority level print scripts in that level.
210 for (int i = 0; i <= mx_priority; i++)
212 for (int j = 0; j < (int)priorities.size(); j++)
214 if(priorities[j] == i)
216 out << "include " << package_name << "/boxes/" << files[j].filename().string() << std::endl;
217 out << "#-----------------------------------------" << std::endl;
221 out << "endpackage" << std::endl;
222 out << "#-- EOF ----------------------------------" << std::endl;
228 //==========================================================================
230 // extract of the tarjan's algorithm for strongly connected components
231 bool isCycle(const Graph& g)
233 for (int it = 0; it < (int)g.size(); ++it) {
234 std::vector<bool> visited (g.size(), false);
235 if (checkCycle(g, it, visited))
237 //std::cout << "At " << it << std::endl;
244 //==========================================================================
246 // dfs search to check cycles.
247 bool checkCycle(const Graph& g, const int& i, std::vector<bool>& v)
251 for(int dit = 0; dit < (int)g[i].size(); dit++)
254 if(d < 0 || d >= (int)g.size() || v[d])
256 //std::cout << "Checking " << i << " dependency " << dit << "=" << d << std::endl;
259 if(checkCycle(g,d,v))
266 //==========================================================================
268 // find precedence in graph. 0 are the boxes that have no deps, 1 boxes that have deps from 0 or less, 2 boxes that have deps from 1 or less, etc.
269 void setPriorities(const Graph& g, std::vector<int>& p)
271 for(int i = 0; i < (int)g.size(); i++)
274 setPriority(g, i, p);
278 //==========================================================================
280 // dfs search to find dependencies
281 void setPriority(const Graph& g, const int i, std::vector<int>& p)
284 for(int j = 0; j < (int)g[i].size(); j++)
286 setPriority(g, g[i][j], p);
287 pi = std::max(pi, p[g[i][j]]);
292 //==========================================================================
294 std::vector<bf::path> getFileList(const std::string& path)
296 std::vector<bf::path> files;
298 bf::path pth(path.c_str());
299 if(bf::exists(pth) && bf::is_directory(pth))
301 bf::directory_iterator end_itr;
302 for(bf::directory_iterator itr(pth); itr != end_itr; ++itr)
304 if(!is_directory(itr->status()))
306 std::string nm(itr->path().filename().string());
307 if(nm.substr(nm.size()-4) == ".bbs")
309 //std::cout << itr->path().filename().string() << std::endl;
310 files.push_back(itr->path());
317 std::cout<< "the path to the bbs's should be a folder and not a file.";
321 // Order files by name
322 for (int i = 0; i < (int)files.size()-1; ++i) {
323 for (int j = i+1; j < (int)files.size(); ++j) {
324 if(files[j].filename().string() < files[i].filename().string())
326 bf::path tmp = files[i];