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;
75 // Order files by dependencies
76 // Get DependenciesVector and Box Names
77 DependenciesVector deps;
81 for (int i = 0; i < (int)files.size(); ++i) {
82 bbtk::BBPInterpreter::Pointer I = bbtk::BBPInterpreter::New();
83 I->InterpretFile(files[i].string());
84 boxs.push_back( ((bbtk::BBPInterpreter*)(I.get()))->boxName );
85 deps.push_back( ((bbtk::BBPInterpreter*)(I.get()))->dependencies );
87 //print box name and dependencies
88 // std::cout << ((bbtk::BBPInterpreter*)(I.get()))->boxName << ": ";
90 // Dependencies::iterator it = ((bbtk::BBPInterpreter*)(I.get()))->dependencies.begin();
91 // it != ((bbtk::BBPInterpreter*)(I.get()))->dependencies.end();
93 // std::cout << *it << ", ";
95 // std::cout << std::endl;
99 // Only keep dependencies from package
100 Dependencies boxNamesSet(boxs.begin(), boxs.end());
102 //std::cout << "after: " << std::endl;
103 for (DependenciesVector::iterator it = deps.begin(); it != deps.end(); it++) {
105 std::set_intersection(it->begin(), it->end(), boxNamesSet.begin(), boxNamesSet.end(),std::back_inserter(tmp));
106 Dependencies tmp1(tmp.begin(),tmp.end());
108 //print clean dependencies
110 // Dependencies::iterator it1 = it->begin();
113 // std::cout << *it1 << ", ";
115 // std::cout << std::endl;
119 // Create dependencies graph
120 std::vector<std::vector<int> > g(boxs.size(), std::vector<int>());
121 std::map<std::string, int> idxs;
124 for (int i = 0; i < (int)boxs.size(); ++i)
130 for (DependenciesVector::iterator dit = deps.begin(); dit != deps.end(); dit++, boxit++)
132 for (Dependencies::iterator ddit = dit->begin(); ddit != dit->end(); ddit++)
134 g[boxit].push_back(idxs[*ddit]);
138 // Check there are no cycles in graph
141 std::cout << "there are dependency cycles, please check your scripts." << std::endl;
146 std::cout << "no cycles, we keep going." << std::endl;
147 std::vector<int> priorities(boxs.size(), -1);
148 setPriorities(g, priorities);
149 // for (int i = 0; i < (int)priorities.size(); i++)
151 // std::cout << priorities[i] << " ";
153 // std::cout << std::endl;
155 // Write results to bbp file
157 std::string fname = path_out + "\\" + package_name + ".bbp";
159 std::string fname = path_out + "/" + package_name + ".bbp";
162 std::ofstream out(fname.c_str());
163 out << "#-----------------------------------------" << std::endl;
164 out << "# Include script for bbtk package '" << package_name << "'" << std::endl;
165 out << "# Automatically generated by bbpConfigurator" << std::endl;
166 out << "#-----------------------------------------" << std::endl;
167 out << "load "<< package_name << std::endl;
168 out << "#-----------------------------------------" << std::endl;
169 out << "package "<< package_name << std::endl;
170 out << "#-----------------------------------------" << std::endl;
172 //each bbs file ordered.
173 //include [package_name]/boxes/[file_bbs]
174 //#-----------------------------------------
176 // for (int i = 0; i < (int)files.size(); ++i) {
177 // out << "include " << package_name << "/boxes/" << files[i].filename().string() << std::endl;
178 // out << "#-----------------------------------------" << std::endl;
181 // find max priority level
183 for (int i = 0; i < (int)priorities.size(); i++)
184 mx_priority = std::max(mx_priority, priorities[i]);
186 // for each priority level print scripts in that level.
187 for (int i = 0; i <= mx_priority; i++)
189 for (int j = 0; j < (int)priorities.size(); j++)
191 if(priorities[j] == i)
193 out << "include " << package_name << "/boxes/" << files[j].filename().string() << std::endl;
194 out << "#-----------------------------------------" << std::endl;
198 out << "endpackage" << std::endl;
199 out << "#-- EOF ----------------------------------" << std::endl;
205 //==========================================================================
207 // extract of the tarjan's algorithm for strongly connected components
208 bool isCycle(const Graph& g)
210 for (int it = 0; it < (int)g.size(); ++it) {
211 std::vector<bool> visited (g.size(), false);
212 if (checkCycle(g, it, visited))
214 //std::cout << "At " << it << std::endl;
221 //==========================================================================
223 // dfs search to check cycles.
224 bool checkCycle(const Graph& g, const int& i, std::vector<bool>& v)
228 for(int dit = 0; dit < (int)g[i].size(); dit++)
231 if(d < 0 || d >= (int)g.size() || v[d])
233 //std::cout << "Checking " << i << " dependency " << dit << "=" << d << std::endl;
236 if(checkCycle(g,d,v))
243 //==========================================================================
245 // 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.
246 void setPriorities(const Graph& g, std::vector<int>& p)
248 for(int i = 0; i < (int)g.size(); i++)
251 setPriority(g, i, p);
255 //==========================================================================
257 // dfs search to find dependencies
258 void setPriority(const Graph& g, const int i, std::vector<int>& p)
261 for(int j = 0; j < (int)g[i].size(); j++)
263 setPriority(g, g[i][j], p);
264 pi = std::max(pi, p[g[i][j]]);
269 //==========================================================================
271 std::vector<bf::path> getFileList(const std::string& path)
273 std::vector<bf::path> files;
275 bf::path pth(path.c_str());
276 if(bf::exists(pth) && bf::is_directory(pth))
278 bf::directory_iterator end_itr;
279 for(bf::directory_iterator itr(pth); itr != end_itr; ++itr)
281 if(!is_directory(itr->status()))
283 std::string nm(itr->path().filename().string());
284 if(nm.substr(nm.size()-4) == ".bbs")
286 //std::cout << itr->path().filename().string() << std::endl;
287 files.push_back(itr->path());
294 std::cout<< "the path to the bbs's should be a folder and not a file.";
298 // Order files by name
299 for (int i = 0; i < (int)files.size()-1; ++i) {
300 for (int j = i+1; j < (int)files.size(); ++j) {
301 if(files[j].filename().string() < files[i].filename().string())
303 bf::path tmp = files[i];