dlvhex  2.5.0
mlp-generator/Module.cpp
Go to the documentation of this file.
00001 /* dlvhex -- Answer-Set Programming with external interfaces.
00002  * Copyright (C) 2005, 2006, 2007 Roman Schindlauer
00003  * Copyright (C) 2006, 2007, 2008, 2009, 2010 Thomas Krennwallner
00004  * Copyright (C) 2009, 2010 Peter Schüller
00005  * 
00006  * This file is part of dlvhex.
00007  *
00008  * dlvhex is free software; you can redistribute it and/or modify it
00009  * under the terms of the GNU Lesser General Public License as
00010  * published by the Free Software Foundation; either version 2.1 of
00011  * the License, or (at your option) any later version.
00012  *
00013  * dlvhex is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with dlvhex; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00021  * 02110-1301 USA
00022  */
00023 
00024 
00034 #ifdef HAVE_CONFIG_H
00035 #include "config.h"
00036 #endif // HAVE_CONFIG_H
00037 
00038 #include <iostream>
00039 #include <stdlib.h>
00040 #include <sstream>
00041 #include <fstream>
00042 #include <string>
00043 #include <vector>
00044 #include <sys/time.h>
00045 
00046 
00047 class BaseTopology
00048 {
00049   protected:
00050     std::string outputFilePrefix;
00051     int numConstantMax; 
00052     int numPredicateMax; 
00053     int sizeOfHeadMax; 
00054     int sizeOfBodyMax;
00055     int notProbability;
00056     int numRuleMax;
00057     int numModules;
00058     int maxPredArity;
00059     std::vector<int> numInputPredVector; // number of input predicates for each modules
00060     std::vector< std::vector<int> > listInputPreds;
00061     std::vector< std::vector<int> > numPredArity; // number of input predicates for each modules
00062 
00063     int* createAtom(int idxModule, int idxPredicate, std::string content, int maxToRand, std::ostream& oss);
00064     void createGroundAtom(int idxModule, int idxPredicate, std::ostream& oss);
00065     int* createNonGroundAtom(int idxModule, int idxPredicate, std::ostream& oss);
00066 
00067     void createModuleHeader(int idxModule, std::ostream& oss);
00068     void generateFacts(int idxModule, std::ostream& oss);
00069     void generateRule(int idxModule, std::ostream& ossResult);
00070     void generateRules(int idxModule, std::ostream& oss);
00071     void generateModuleCall(int idxModuleSrc, int idxModuleDest, std::ostream& oss);
00072     virtual void createMainModule(std::ostream& oss){};
00073     virtual void createLibraryModule(int idxModule, std::ostream& oss){};
00074 
00075   public:
00076     void setAll(int NumConstant, int NumPredicate, int SizeOfHead, int SizeOfBody, int NotProbability, int NumRule, int NumModules, std::string& OutputFilePrefix);
00077     void generate();
00078 };
00079 
00080 
00081 class StarTopology: public BaseTopology 
00082 {
00083   void createMainModule(std::ostream& oss);
00084   void createLibraryModule(int idxModule, std::ostream& oss);
00085 };
00086 
00087 
00088 class LineTopology: public BaseTopology 
00089 {
00090   void createMainModule(std::ostream& oss);
00091   void createLibraryModule(int idxModule, std::ostream& oss);
00092 };
00093 
00094 
00095 class RingTopology: public BaseTopology 
00096 {
00097   void createMainModule(std::ostream& oss);
00098   void createLibraryModule(int idxModule, std::ostream& oss);
00099 };
00100 
00101 class DiamondTopology: public BaseTopology 
00102 {
00103   private:
00104     void createMainModule(std::ostream& oss);
00105     void createLibraryModule(int idxModule, std::ostream& oss);
00106 };
00107 
00108 
00109 class RandomTopology: public BaseTopology 
00110 {
00111   private:
00112     int density;
00113     void createMainModule(std::ostream& oss);
00114     void createLibraryModule(int idxModule, std::ostream& oss);
00115   public:
00116     void setAll(int NumConstant, int NumPredicate, int SizeOfHead, int SizeOfBody, int NotProbability, int NumRules, int NumModules, std::string& OutputFilePrefix, int Density=50);
00117 };
00118 
00119 
00120 class TreeTopology: public BaseTopology 
00121 {
00122   private:
00123     int branch;
00124     void createMainModule(std::ostream& oss);
00125     void createLibraryModule(int idxModule, std::ostream& oss);
00126   public:
00127     void setAll(int NumConstant, int NumPredicate, int SizeOfHead, int SizeOfBody, int NotProbability, int NumRules, int NumModules, std::string& OutputFilePrefix, int Branch=3);
00128 };
00129 
00130 // set all params
00131 void BaseTopology::setAll(int NumConstant, int NumPredicate, int SizeOfHead, int SizeOfBody, int NotProbability, int NumRule, int NumModules, std::string& OutputFilePrefix)
00132 {
00133   outputFilePrefix = OutputFilePrefix;
00134   numConstantMax = NumConstant;
00135   numPredicateMax = NumPredicate;
00136   sizeOfHeadMax = SizeOfHead;
00137   sizeOfBodyMax = SizeOfBody;
00138   notProbability = NotProbability;
00139   numRuleMax = NumRule;
00140   numModules = NumModules;
00141   maxPredArity = 4;
00142   if (maxPredArity > numPredicateMax) maxPredArity = numPredicateMax;
00143   // list of arity per predicate
00144   int arrArity[22];
00145   for (int i= 0;i< 2;i++) arrArity[i]=0;  // 2
00146   for (int i= 2;i<12;i++) arrArity[i]=1;  // 10
00147   for (int i=12;i<17;i++) arrArity[i]=2;  // 5
00148   for (int i=17;i<20;i++) arrArity[i]=3;  // 3
00149   for (int i=20;i<22;i++) arrArity[i]=4;  // 2
00150 
00151   for (int i=0; i<numModules; i++)
00152     {
00153       std::vector<int> listArity;   
00154       for (int j=0; j<numPredicateMax; j++)
00155     {
00156           listArity.push_back( arrArity[rand()% 22] ); // random between 0-maxPredArity
00157 //          listArity.push_back( rand()% (maxPredArity + 1) ); // random between 0-maxPredArity
00158     }
00159       numPredArity.push_back(listArity);
00160     }
00161 
00162   // maxInputPreds for modules
00163   int maxInputPreds = numPredicateMax/3;
00164   if ( maxInputPreds == 1 && numPredicateMax > 1)
00165     {
00166       maxInputPreds = 2;
00167     }
00168   if (maxInputPreds == 0) maxInputPreds = 1;
00169 
00170   // defining input preds per module
00171   for (int i=0;i<numModules;i++)
00172     {
00173       // for randomizing input preds for each module
00174       if ( i==0 )
00175     { // for main module, input preds = 0
00176       numInputPredVector.push_back(0);  
00177       std::vector<int> listInputPred;
00178       listInputPreds.push_back(listInputPred);
00179     }
00180       else
00181     { // for library module, input preds min = 1
00182       numInputPredVector.push_back( (rand()% maxInputPreds ) + 1);
00183       std::vector<int> listInputPred;
00184       for (int j=0; j<numInputPredVector.back(); j++) 
00185         { // loop on how many input preds we have on this module
00186           listInputPred.push_back( rand() % numPredicateMax ); 
00187         }
00188       listInputPreds.push_back( listInputPred );
00189     }
00190     }
00191 }
00192 
00193 
00194 
00195 int* BaseTopology::createAtom(int idxModule, int idxPredicate, std::string content, int maxToRand, std::ostream& oss)
00196 { 
00197   int* conf = new int[maxToRand];
00198   for (int i=0; i<maxPredArity; i++) conf[i] = 0;
00199   oss << "p" << idxPredicate;
00200   for (int i=0;i<numPredArity.at(idxModule).at(idxPredicate);i++)
00201     {
00202       if ( i == 0 ) oss << "("; else oss << ",";
00203       int value = rand() % maxToRand;
00204       conf[value] = 1;
00205       oss << content << value;
00206       if ( i == numPredArity.at(idxModule).at(idxPredicate)-1 ) oss << ")";
00207     }
00208   return conf;
00209 }
00210 
00211 
00212 // create p(c0, c2, c2, c3) -> constants based on listConstant
00213 void BaseTopology::createGroundAtom(int idxModule, int idxPredicate, std::ostream& oss)
00214 {
00215   int* conf = createAtom(idxModule, idxPredicate, "c", numConstantMax, oss);
00216   delete[] conf;
00217 }
00218 
00219 
00220 // create p(X0, X2, X2, X3) -> variables based on listVars
00221 int* BaseTopology::createNonGroundAtom(int idxModule, int idxPredicate, std::ostream& oss)
00222 {
00223   int* arr = 0;
00224   arr = createAtom(idxModule, idxPredicate, "X", maxPredArity, oss);
00225   return arr;
00226 }
00227 
00228 // create module header: #module(..., [...]).
00229 void BaseTopology::createModuleHeader(int idxModule, std::ostream& oss)
00230 {
00231   oss << "#module(mod" << idxModule << ", [";
00232   for (int i=0;i<numInputPredVector.at(idxModule);i++)
00233     {
00234       if ( i>0 ) oss << ", ";
00235       oss << "p" << listInputPreds.at(idxModule).at(i) << "/" << numPredArity.at(idxModule).at( listInputPreds.at(idxModule).at(i) );
00236     }
00237   oss << "]).";  
00238 }
00239 
00240 
00241 // generate facts, automatically prefix the predicate symbol to p<idxModule>p<idPred>... 
00242 // input params: idxModule, numConstant, numPredicate, numFacts to be generated
00243 void BaseTopology::generateFacts(int idxModule, std::ostream& oss)
00244 {
00245   int idxPredicate;
00246   int numFacts = rand() % (numConstantMax*numPredicateMax/3);
00247   if (numFacts < (numConstantMax+numPredicateMax)) numFacts = numConstantMax+numPredicateMax;
00248   for (int i=0;i<numFacts;i++)
00249     {
00250       idxPredicate = rand() % numPredicateMax;
00251       createGroundAtom(idxModule, idxPredicate, oss);
00252       oss << ". " << std::endl;           
00253     }
00254 }
00255 
00256 
00257 void BaseTopology::generateRule(int idxModule, std::ostream& ossResult)
00258 {
00259   bool safe=false;
00260   std::ostringstream oss;
00261   while (safe==false) 
00262     {
00263       oss.str("");
00264       // generate head
00265       int confHead[maxPredArity];  // list the name of vars X(0-XmaxPredArity-1)
00266       for (int i=0; i<maxPredArity; i++) confHead[i] = 0;
00267       int sizeOfHead = (rand() % sizeOfHeadMax) + 1;
00268       for (int j=0;j<sizeOfHead;j++)
00269     {
00270       int idxPredicate = rand() % numPredicateMax;
00271       if ( j>0 ) oss << " v ";
00272       int* conf = 0;
00273       conf = createNonGroundAtom(idxModule, idxPredicate, oss);
00274         //rmv. std::cout<<"head: " << oss.str();
00275       for (int k=0;k<maxPredArity;k++) 
00276         {
00277         //rmv. std::cout << conf[k];    
00278         if (conf[k]==1) confHead[k]=1;
00279         }
00280       //rmv. std::cout<< std::endl;
00281       delete [] conf;
00282     } 
00283     
00284       oss << " :- ";
00285       // generate body
00286       int confBody[maxPredArity];  // list the name of vars X(0-XmaxPredArity-1)
00287       for (int i=0; i<maxPredArity; i++) confBody[i] = 0;
00288       int sizeOfBody = (rand() % sizeOfBodyMax) + 1;
00289       for (int j=0;j<sizeOfBody;j++)
00290     {
00291       int idxPredicate = rand() % numPredicateMax;
00292       int notProbs = rand() % 100;
00293       if ( j>0 ) oss << ", ";
00294       if ( notProbs < notProbability ) oss << "not ";
00295       int* conf = createNonGroundAtom(idxModule, idxPredicate, oss);
00296       if ( notProbs < notProbability ) 
00297        { for (int k=0;k<maxPredArity;k++) if (conf[k]==1) confHead[k]=1; }
00298       else 
00299        { for (int k=0;k<maxPredArity;k++) if (conf[k]==1) confBody[k]=1; }
00300       delete [] conf;
00301     } 
00302 
00303       oss << "." << std::endl;
00304 /*
00305       oss << "% [";
00306       for (int k=0;k<maxPredArity;k++) oss << confHead[k];
00307       oss << "] [";
00308       for (int k=0;k<maxPredArity;k++) oss << confBody[k];
00309       oss << "]" << std::endl;
00310 */
00311       // check
00312       safe = true;
00313       int k=0;
00314       while (k<maxPredArity && safe==true)
00315     {
00316       if (confHead[k]==1 && confBody[k]==0) safe=false;
00317       k++;
00318     }
00319     }
00320     ossResult << oss.str();  
00321 }
00322 
00323 
00324 // generate rules
00325 // input params: idxModule, numConstant, numPredicate, sizeOfHead, sizeOfBody, notProbability, numRules to be generated
00327 void BaseTopology::generateRules(int idxModule, std::ostream& oss)
00328 {
00329   // generate rules
00330   int numRule = rand() % numRuleMax;
00331   for (int i=0;i<numRule;i++)
00332     {
00333       generateRule(idxModule, oss);
00334     }
00335 }
00336 
00337 
00338 // generate module call from idxModuleSrc to idxModuleDest with numPredicate to create a random input predicate p<idxModuleSrc>p<random 0-numPredicate>
00339 void BaseTopology::generateModuleCall(int idxModuleSrc, int idxModuleDest, std::ostream& oss)
00340 {
00341   oss << "out" << idxModuleSrc << " :- @mod" << idxModuleDest << "[";
00342   int ctrNew=0;
00343   for (int i=0;i<numInputPredVector.at(idxModuleDest);i++) 
00344     {
00345       if (i > 0) oss <<",";
00346       int value = 0;
00347       // test for arity matching
00348       int ctrLoop=0;
00349       bool match = false;      
00350       while (ctrLoop<1 && match==false)
00351     {
00352       value = rand() % numPredicateMax;
00353           if (numPredArity.at(idxModuleSrc).at(value)== numPredArity.at(idxModuleDest).at( listInputPreds.at(idxModuleDest).at(i) )) match = true;
00354       ctrLoop++;
00355     }
00356       if (match==false)
00357     {
00358       oss << "pnew" << ctrNew;
00359       ctrNew++;
00360     }
00361       else oss << "p" << ( value );
00362     }
00363   oss << "]::out" << idxModuleDest << ".";
00364 }
00365 
00366 
00367 void BaseTopology::generate()
00368 {
00369       // prepare the output file 
00370       std::ofstream fileEach;
00371       std::ostringstream oss;
00372       oss.str("");
00373       oss << outputFilePrefix << 0 << ".mlp";       
00374       fileEach.open( oss.str().c_str() );
00375       // create a main module
00376       oss.str("");
00377       createMainModule(oss);
00378       oss << std::endl;
00379       // write to output files
00380       fileEach << oss.str();
00381       fileEach.close();
00382 
00383       // create library modules 
00384       for (int i=1; i<numModules; i++)
00385     {
00386       // prepare individual output files
00387           oss.str("");
00388           oss << outputFilePrefix << i << ".mlp";       
00389       fileEach.open( oss.str().c_str() );
00390           oss.str("");
00391       createLibraryModule(i, oss);
00392       oss << std::endl;
00393           // write to output files
00394           fileEach << oss.str();
00395           fileEach.close();
00396     }
00397 }
00398 
00399 
00400 
00401 /******************* 
00402  * For star topology
00403  *******************/
00404 void StarTopology::createMainModule(std::ostream& oss)
00405 {
00406   // generate fact as many as the rules
00407   createModuleHeader(0, oss);
00408   oss << std::endl;
00409   // generate facts
00410   generateFacts(0, oss);
00411   oss << std::endl;
00412 
00413   // generate rules
00414   generateRules(0, oss);
00415 
00416   // generate module calls 
00417   for (int i=1;i<numModules;i++)
00418     {
00419       generateModuleCall(0, i, oss);
00420       oss << std::endl;
00421     }
00422 } 
00423 
00424 
00425 void StarTopology::createLibraryModule(int idxModule, std::ostream& oss)
00426 {
00427   // generate fact as many as the rules
00428   createModuleHeader(idxModule, oss);
00429   oss << std::endl;
00430 
00431   // generate facts
00432   generateFacts(idxModule, oss);
00433   oss << std::endl;
00434 
00435   // generate rules
00436   generateRules(idxModule, oss);
00437 
00438   // generate module calls 
00439   // the number of input preds is according to numInputPredsVector
00440   generateModuleCall(idxModule, idxModule, oss);
00441 }
00442 
00443 
00444 
00445 /******************* 
00446  * For Line topology
00447  *******************/
00448 
00449 void LineTopology::createMainModule(std::ostream& oss)
00450 {
00451   // generate fact as many as the rules
00452   createModuleHeader(0, oss);
00453   oss << std::endl;
00454   // generate facts
00455   generateFacts(0, oss);
00456   oss << std::endl;
00457 
00458   // generate rules
00459   generateRules(0, oss);
00460 
00461   if ( numModules > 1 ) generateModuleCall(0, 1, oss);
00462 } 
00463 
00464 
00465 void LineTopology::createLibraryModule(int idxModule, std::ostream& oss)
00466 {
00467   // generate fact as many as the rules
00468   createModuleHeader(idxModule, oss);
00469   oss << std::endl;
00470 
00471   // generate facts
00472   generateFacts(idxModule, oss);
00473   oss << std::endl;
00474 
00475   // generate rules
00476   generateRules(idxModule, oss);
00477 
00478   // generate module calls 
00479   if ( idxModule == numModules-1 )
00480     generateModuleCall(idxModule, idxModule, oss);
00481   else 
00482     generateModuleCall(idxModule, idxModule+1, oss);
00483 }
00484 
00485 
00486 
00487 /******************* 
00488  * For Ring topology
00489  *******************/
00490 
00491 void RingTopology::createMainModule(std::ostream& oss)
00492 {
00493   // generate fact as many as the rules
00494   createModuleHeader(0, oss);
00495   oss << std::endl;
00496   // generate facts
00497   generateFacts(0, oss);
00498   oss << std::endl;
00499 
00500   // generate rules
00501   generateRules(0, oss);
00502 
00503   if ( numModules > 1 ) generateModuleCall(0, 1, oss);
00504 } 
00505 
00506 
00507 void RingTopology::createLibraryModule(int idxModule, std::ostream& oss)
00508 {
00509   // generate fact as many as the rules
00510   createModuleHeader(idxModule, oss);
00511   oss << std::endl;
00512 
00513   // generate facts
00514   generateFacts(idxModule, oss);
00515   oss << std::endl;
00516 
00517   // generate rules
00518   generateRules(idxModule, oss);
00519 
00520   // generate module calls 
00521   if ( idxModule == numModules-1 )
00522     generateModuleCall(idxModule, 0, oss);
00523   else 
00524     generateModuleCall(idxModule, idxModule+1, oss);
00525 }
00526 
00527 
00528 
00529 /********************** 
00530  * For Diamond topology
00531  **********************/
00532 void DiamondTopology::createMainModule(std::ostream& oss)
00533 {
00534   // generate fact as many as the rules
00535   createModuleHeader(0, oss);
00536   oss << std::endl;
00537   // generate facts
00538   generateFacts(0, oss);
00539   oss << std::endl;
00540 
00541   // generate rules
00542   generateRules(0, oss);
00543 
00544   if ( numModules > 1 ) 
00545     {
00546       generateModuleCall(0, 1, oss);
00547       oss << std::endl;
00548     }
00549   if ( numModules > 2 ) 
00550     {
00551       generateModuleCall(0, 2, oss);
00552       oss << std::endl;
00553     }
00554 } 
00555 
00556 
00557 void DiamondTopology::createLibraryModule(int idxModule, std::ostream& oss)
00558 {
00559   // generate fact as many as the rules
00560   createModuleHeader(idxModule, oss);
00561   oss << std::endl;
00562 
00563   // generate facts
00564   generateFacts(idxModule, oss);
00565   oss << std::endl;
00566 
00567   // generate rules
00568   generateRules(idxModule, oss);
00569 
00570   // generate module calls 
00571   if ( idxModule == numModules-1) generateModuleCall(idxModule, idxModule, oss);
00572   else if ( (idxModule+1)%3 == 0 ) generateModuleCall(idxModule, idxModule+1, oss);
00573   else 
00574     if ( (idxModule+2)%3 == 0 ) 
00575       {
00576         if ( idxModule+2 < (numModules) ) generateModuleCall(idxModule, idxModule+2, oss);
00577         else generateModuleCall(idxModule, idxModule, oss);
00578       }
00579   else 
00580     if ( idxModule%3 == 0 )
00581       {
00582         generateModuleCall(idxModule, idxModule+1, oss);
00583         oss << std::endl;
00584         if (idxModule+2 < numModules) generateModuleCall(idxModule, idxModule+2, oss);
00585         oss << std::endl;
00586       }
00587 }
00588 
00589 
00590 /********************* 
00591  * For Random topology
00592  *********************/
00593 // set all params
00594 void RandomTopology::setAll(int NumConstant, int NumPredicate, int SizeOfHead, int SizeOfBody, int NotProbability, int NumRules, int NumModules, std::string& OutputFilePrefix, int Density)
00595 {
00596   BaseTopology::setAll(NumConstant, NumPredicate, SizeOfHead, SizeOfBody, NotProbability, NumRules, NumModules, OutputFilePrefix);
00597   density = Density;
00598   //rmv. std::cerr << "density: " << density << std::endl;
00599 }
00600 
00601 void RandomTopology::createMainModule(std::ostream& oss)
00602 {
00603   // generate fact as many as the rules
00604   createModuleHeader(0, oss);
00605   oss << std::endl;
00606   // generate facts
00607   generateFacts(0, oss);
00608   oss << std::endl;
00609 
00610   // generate rules
00611   generateRules(0, oss);
00612 
00613   // generate module calls
00614   bool moduleCall = false;
00615   for (int i=1;i<numModules;i++)
00616     {
00617       if ( rand() % 100 < density ) 
00618         {
00619           generateModuleCall(0, i, oss);
00620           oss << std::endl;
00621       moduleCall = true;
00622         }
00623     }
00624   if ( moduleCall == false )
00625     {
00626       oss << "out0.";
00627     }
00628 } 
00629 
00630 
00631 void RandomTopology::createLibraryModule(int idxModule, std::ostream& oss)
00632 {
00633   // generate fact as many as the rules
00634   createModuleHeader(idxModule, oss);
00635   oss << std::endl;
00636 
00637   // generate facts
00638   generateFacts(idxModule, oss);
00639   oss << std::endl;
00640 
00641   // generate rules
00642   generateRules(idxModule, oss);
00643 
00644   // generate module calls
00645   bool moduleCall = false;
00646   if ( rand() % 100 < density ) 
00647     {
00648       generateModuleCall(idxModule, 0, oss);
00649       oss << std::endl;
00650       moduleCall = true;
00651     }
00652   for (int i=1;i<numModules;i++)
00653     {
00654       if ( rand() % 100 < density ) 
00655         {
00656           generateModuleCall(idxModule, i, oss);
00657           oss << std::endl;
00658           moduleCall = true;
00659         }
00660     }
00661   if ( moduleCall == false )
00662     {
00663       oss << "out" << idxModule << ".";
00664     }
00665 }
00666 
00667 
00668 /********************* 
00669  * For Tree topology
00670  *********************/
00671 // set all params
00672 void TreeTopology::setAll(int NumConstant, int NumPredicate, int SizeOfHead, int SizeOfBody, int NotProbability, int NumRules, int NumModules, std::string& OutputFilePrefix, int Branch)
00673 {
00674   BaseTopology::setAll(NumConstant, NumPredicate, SizeOfHead, SizeOfBody, NotProbability, NumRules, NumModules, OutputFilePrefix);
00675   branch = Branch;
00676 }
00677 
00678 void TreeTopology::createMainModule(std::ostream& oss)
00679 {
00680   // generate fact as many as the rules
00681   createModuleHeader(0, oss);
00682   oss << std::endl;
00683   // generate facts
00684   generateFacts(0, oss);
00685   oss << std::endl;
00686 
00687   // generate rules
00688   generateRules(0, oss);
00689 
00690   // generate module calls
00691   int numCall = branch;
00692   if (numModules-1 < branch) numCall = numModules-1;
00693   for (int i=1;i<=numCall;i++)
00694     {
00695           generateModuleCall(0, i, oss);
00696           oss << std::endl;
00697     }
00698 } 
00699 
00700 
00701 void TreeTopology::createLibraryModule(int idxModule, std::ostream& oss)
00702 {
00703   // generate fact as many as the rules
00704   createModuleHeader(idxModule, oss);
00705   oss << std::endl;
00706 
00707   // generate facts
00708   generateFacts(idxModule, oss);
00709   oss << std::endl;
00710 
00711   // generate rules
00712   generateRules(idxModule, oss);
00713 
00714   // generate module calls
00715   int lowerbound = (idxModule*branch) + 1;
00716   int upperbound = (idxModule+1) * branch;
00717   bool moduleCall = false;
00718   for (int i=lowerbound;i<=upperbound;i++)
00719     {
00720       if (i < numModules) 
00721     {
00722           generateModuleCall(idxModule, i, oss);
00723           oss << std::endl;
00724       moduleCall = true;
00725     }
00726     }
00727   if ( moduleCall == false ) generateModuleCall(idxModule, idxModule, oss);
00728 }
00729 
00730 
00731 
00732 int main(int argc, char *argv[])
00733 {
00734 
00735   // set the random seed    
00736   struct timeval tv;
00737   gettimeofday(&tv, NULL);
00738   srand(tv.tv_sec + tv.tv_usec);
00739 
00740   // read params
00741   int numParam = 9;
00742   if (argc <= numParam) 
00743     {
00744       std::cerr << "Usage: " << std::endl;  
00745       std::cerr << "Module star <numConstant> <numPredicate> <sizeOfHead> <sizeOfBody> <notProbability> <numRules> <numModules> <outputFilePrefix>" << std::endl;
00746       std::cerr << "Module line <numConstant> <numPredicate> <sizeOfHead> <sizeOfBody> <notProbability> <numRules> <numModules> <outputFilePrefix>" << std::endl;
00747       std::cerr << "Module ring <numConstant> <numPredicate> <sizeOfHead> <sizeOfBody> <notProbability> <numRules> <numModules> <outputFilePrefix>" << std::endl;
00748       std::cerr << "Module diamond <numConstant> <numPredicate> <sizeOfHead> <sizeOfBody> <notProbability> <numRules> <numModules> <outputFilePrefix>" << std::endl;
00749       std::cerr << "Module random <numConstant> <numPredicate> <sizeOfHead> <sizeOfBody> <notProbability> <numRules> <numModules> <outputFilePrefix> [density]" << std::endl;
00750       std::cerr << "Module tree <numConstant> <numPredicate> <sizeOfHead> <sizeOfBody> <notProbability> <numRules> <numModules> <outputFilePrefix> [branch]" << std::endl;
00751 
00752       return 0;
00753     }
00754   else 
00755     {
00756       int param[numParam-2]; // -2 for topology and outputFilePrefix
00757       for (int i=0;i<numParam-2;i++)
00758         {
00759           param[i] = atoi(argv[i+2]);  //+2 because [0] is for fileExecution, [1] is for topology
00760         }
00761       std::string topology = argv[1];
00762       std::string outputFilePrefix = argv[numParam];
00763 
00764       if (topology == "star")
00765     {
00766       StarTopology example;
00767       example.setAll(param[0], param[1], param[2], param[3], param[4], param[5], param[6], outputFilePrefix);
00768       example.generate();
00769     }
00770       else if (topology == "line")
00771     {
00772       LineTopology example;
00773       example.setAll(param[0], param[1], param[2], param[3], param[4], param[5], param[6], outputFilePrefix);
00774       example.generate();
00775     }
00776       else if (topology == "ring")
00777     {
00778       RingTopology example;
00779       example.setAll(param[0], param[1], param[2], param[3], param[4], param[5], param[6], outputFilePrefix);
00780       example.generate();
00781     }
00782       else if (topology == "diamond")
00783     {
00784       DiamondTopology example;
00785       example.setAll(param[0], param[1], param[2], param[3], param[4], param[5], param[6], outputFilePrefix);
00786       example.generate();
00787     }
00788       else if (topology == "random")
00789     {
00790       RandomTopology example;
00791       if (argc == numParam + 2)
00792         {
00793           example.setAll(param[0], param[1], param[2], param[3], param[4], param[5], param[6], outputFilePrefix, atoi(argv[numParam+1]));
00794         }
00795           else 
00796         {
00797           example.setAll(param[0], param[1], param[2], param[3], param[4], param[5], param[6], outputFilePrefix);
00798         }
00799       example.generate();
00800     }
00801       else if (topology == "tree")
00802     {
00803       TreeTopology example;
00804       if (argc == numParam + 2)
00805         {
00806           example.setAll(param[0], param[1], param[2], param[3], param[4], param[5], param[6], outputFilePrefix, atoi(argv[numParam+1]));
00807         }
00808           else 
00809         {
00810           example.setAll(param[0], param[1], param[2], param[3], param[4], param[5], param[6], outputFilePrefix);
00811         }
00812       example.generate();
00813     }
00814     }
00815 }
00816 
00817