Generated on Mon Aug 25 11:35:32 2008 for Gecode by doxygen 1.5.6

javascript.cc

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Guido Tack <tack@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Guido Tack, 2008
00008  *
00009  *  Last modified:
00010  *     $Date: 2008-01-31 10:48:49 +0100 (Thu, 31 Jan 2008) $ by $Author: tack $
00011  *     $Revision: 6002 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 #include "examples/support.hh"
00039 #include "gecode/serialization.hh"
00040 #include <fstream>
00041 #include <string>
00042 
00047 class FileNameOptions : public Options {
00048 protected:
00049   const char* _file; 
00050 public:
00052   FileNameOptions(const char* n);
00054   virtual void help(void);
00056   void parse(int& argc, char* argv[]);
00057 
00059   void file(const char* f);
00061   const char* file(void) const;
00062 };
00063 
00064 inline void
00065 FileNameOptions::file(const char* f) { _file = f; }
00066 
00067 inline const char*
00068 FileNameOptions::file(void) const {
00069   return _file;
00070 }
00071 
00072 FileNameOptions::FileNameOptions(const char* n) 
00073   : Options(n), _file(NULL) {}
00074 
00075 void
00076 FileNameOptions::help(void) {
00077   Options::help();
00078   std::cerr << "\t(string)" << std::endl
00079             << "\t\tJavaScript source file to parse" << std::endl;
00080 }
00081 
00082 void
00083 FileNameOptions::parse(int& argc, char* argv[]) {
00084   Options::parse(argc,argv);
00085   if (argc < 2)
00086     return;
00087   file(argv[1]);
00088 }
00089 
00099 class JavaScript : public Example {
00100 protected:
00102   VarArray<Reflection::Var> x;
00103 public:
00105   JavaScript(const FileNameOptions& opt) : x(this, 0) {
00106     if (opt.file() == NULL) {
00107       throw Exception("JavaScript", "no file given");
00108     }
00109     std::ifstream programFile(opt.file());
00110     std::string program;
00111     if (programFile.fail()) {
00112       throw Exception("JavaScript", "error reading file");      
00113     }
00114     while (!programFile.eof()) {
00115       std::string line;
00116       std::getline(programFile, line);
00117       program += line+"\n";
00118     }
00119     programFile.close();
00120     fromJavaScript(this, program);
00121     Reflection::VarMap vm;
00122     for (Reflection::ActorSpecIter si(this, vm); si(); ++si) {
00123       (void) si.actor();
00124     }
00125     for (Reflection::VarMapIter vmi(vm); vmi(); ++vmi) {
00126       x.add(this, Reflection::Var(vmi.varImpBase(), vmi.spec().vti()));
00127     }
00128   }
00129 
00131   JavaScript(bool share, JavaScript& s) : Example(share,s) {
00132     x.update(this, share, s.x);
00133   }
00134 
00136   virtual Space*
00137   copy(bool share) {
00138     return new JavaScript(share,*this);
00139   }
00140 
00142   virtual void
00143   print(std::ostream& os) {
00144     for (int i=0; i<x.size(); i++) {
00145       os << x[i] << (i < x.size() - 1 ? "," : "");
00146     }
00147     os << std::endl;
00148   }
00149 };
00150 
00154 int
00155 main(int argc, char* argv[]) {
00156   FileNameOptions opt("JavaScript");
00157   opt.parse(argc,argv);
00158   Gecode::Serialization::initRegistry();
00159   Example::run<JavaScript,DFS,FileNameOptions>(opt);
00160   return 0;
00161 }
00162 
00163 // STATISTICS: example-any