/rerun.php
PHP | 209 lines | 123 code | 45 blank | 41 comment | 29 complexity | e16b6689d47426310ce1a1f7ca067bbe MD5 | raw file
- <?php
- /*
- Message passing based simulation core engine
- Edgar Sioson, 10/29/2011
- */
-
- header("content-type: text/plain");
- if ($_GET["timeLimit"]) set_time_limit($_GET['timeLimit']);
- $time = time(); echo "\nTime: $time\n";
-
-
- /***********************************************************
- ** Parse rerun request
- ************************************************************/
- if (!$runName = $_GET['runName'] OR !$filename = $_GET['filename']) exit("Invalid runName='$runName' and/or filename='$filename'.");
-
- //the report file has gneral metadata about the run
- $report = json_decode(file_get_contents("results/$runName/report/$filename")); //print_r($report); exit();
- if (!$report) exit("Invalid report format.");
- if ($report->GETarray) $_GET = (array)$report->GETarray;
-
- $base = $report->versionInfo->base; //print_r($report->versionInfo);
- if ($changes = $report->versionInfo->changes) exit("In order to rerun $runname/$filename, the following diff must be manually applied to version $base:\n$changes");
- else if ($_GET['version']) echo "Will automatically revert to specified ". $_GET['version'] ." version.\n";
- else echo "Will automatically revert to $base version.\n";
-
-
-
- //will need to redefine configuration constants
- foreach($report->defConstants AS $param => $value) {
- if (!is_numeric($value)) $value = "\"$value\"";
- $config .= "define(\"$param\",$value);\n";
- }
-
- //the addr files have more details
- $addr = json_decode(file_get_contents("results/$runName/addr/$filename"));
- if (!$addr) exit("Invalid addr format."); //print_r($addr);
-
- if (!$report->GETarray) {
- $_GET['maxCycle'] = $report->maxCycle;
- $_GET['initBal'] = $addr->ocaup->initBal;
- if (!$_GET['onHgChange']) $_GET['onHgChange'] = "copy";
- }
-
- $runName .= "_". $filename; // .".". $time;
- if (!is_dir("results/reruns/")) mkdir("results/reruns");
-
-
- /**********************************************
- ** Load class files
- ***********************************************/
-
- eval($config);
-
- include_once "utils/Error.php";
- include_once "utils/Util.php";
- include_once "utils/msgFunc.php";
- include_once "utils/RandomInit_for_rerun.php";
-
- include_once "testbed/Person.php";
- include_once "testbed/Rsrc.php";
- include_once "testbed/Location.php";
- include_once "testbed/Waste.php";
- include_once "testbed/Producer.php";
- include_once "testbed/Consumer.php";
- include_once "testbed/Report.php";
-
- include_once "infras/Ocaup.php";
- include_once "infras/Lets.php";
- include_once "infras/Inventory.php";
- include_once "infras/BrandIndex.php";
-
- /**********************************************
- ** Set up version tracking
- ***********************************************/
-
- //catch changes using Mercurial, exit or copy -> see included Util.php
- $versionInfo = Util::onHgChange($runName,$time); //if no "&onHgChange=" value is provided, will just return and do nothing
-
-
-
- /**********************************************
- ** Prepare for simulation run
- ***********************************************/
-
- //set up infrastructure
- $Ocaup = new Ocaup("ocaup","inven");
- $Lets = new Lets("lets","inven");
- $Inven = new Inventory("inven");
- $Index = new BrandIndex("index", array("ocaup","lets"), array("inven"), 1, 50);
-
- //set up locations
- foreach($addr AS $name => $obj) {
- if ($obj->rsrc) {
- $L[ $obj->id ] = new Location($obj->id);
- $rsrcSet[] = $obj->rsrc;
- }
- else if ($obj->F) {
- $P[ $obj->id ] = new Person($obj->id);
- $pIds[] = $obj->id;
- }
- else if ($obj->brandLocs) {
- include "decision/$name.php";
- $d[$name] = new $name($name);
- $brandLocs[$name] = (array)$obj->brandLocs;
- $brandMembers[$name] = (array)$obj->members;
-
- $d[$name]->rcv(array(
- "maxCycle" => $_GET['maxCycle'], "acctant"=> $obj->acctant, "inven"=> $obj->inven, "brandIndex"=> $obj->brandIndex,
- "pQty" => $obj->pQty, "cQty" => $obj->cQty, "wQty" => $obj->wQty,
- "numP" => $name, "numLocs"=> $name
- ));
-
- $numP = 0;
- $numBrands = 0;
- foreach($d[$name]->members AS $brand => $personArr) {
- $numBrands++;
- foreach($personArr AS $personId) {
- $P[$personId]->dEng = $name;
- $numP++;
- }
- }
-
- $numLocs = 0;
- foreach($d[$name]->brandLocs AS $brand => $locInfo) {
- $numLocs += count($locInfo);
- }
-
- $dGrp[$name] = array(
- "acctant"=> $obj->acctant, "inven"=> $obj->inven, "brandIndex"=> $obj->brandIndex,
- "pQty" => $obj->pQty, "cQty" => $obj->cQty, "wQty" => $obj->wQty,
- "numLocs" => $numLocs, "numP" => $numP, "numBrands" => $numBrands
- );
- }
- }
-
-
- //set up Resource once and will just reset properties
- $Resource = new Rsrc();
-
-
- //set up report
- $Report = new Report($dGrp, $versionInfo);
-
- //Set up run parameters
- $maxTicks = CYCLE_PERIOD*$_GET['maxCycle'];
- $cycleNum = 0;
- $cyclicTick = -1;
- $linearTick = -1;
-
- if ($_GET['doMax']) {
- if ($_GET['doMax'] != "max") $_GET['maxCycle'] = $_GET['doMax'];
- }
- else {
- echo "\n\nCycle number:$cycleNum mem_usage= ". memory_get_usage() ." ";
-
- /*foreach($addr->inven->locRef AS $loc) {
- $r = str_split($loc[1]);
- for($i=0; $i<4; $i++) $rInvenDimens[$i] += $r[$i];
- }*/
- print_r($rInvenDimens); echo "\n\n";
- print_r($addr); print_r($Report); print_r($Addr_xyz); exit();
- }
-
- /**********************************************
- ** Run simulation
- ***********************************************/
-
- while ($cycleNum < $_GET['maxCycle']) {
- $cyclicTick++;
- $linearTick++;
- if ($cyclicTick == CYCLE_PERIOD) {
- $cycleNum++;
- $cyclicTick = 0;
- }
-
- //Apply changes to location qty, waste, etc.
- foreach($L AS $Loc) $Loc->wasteImg = $Loc->waste;
- foreach($L AS $Loc) $Loc->setExp($linearTick);
-
- //trigger activity
- shuffle($pIds); //print_r($pIds); print_r($Addr_xyz); exit();
- foreach($pIds as $id) {
- if (!REPLACE_DEAD OR !$P[$id]->death) $P[$id]->act($Resource, $linearTick, $cyclicTick); //print_r($P); print_r($Ocaup); print_r($Inven); exit();
- }
-
- //generate reports as needed
- if ($cycleNum == $reportCycle) {
- $Report->byDEngine($cycleNum, $P, $linearTick);
- $reportCycle += SUMMARY_PERIOD;
- }
- }
-
-
-
- /**********************************************
- ** Generate reports as needed
- ***********************************************/
- $Ocaup->genReport();
- $Lets->genReport();
-
- //$Report->viewObj($P, $L, $Lets, $Inven);
- //$Report->viewJSON($P);
- //$Report->viewJSON($L);
- print_r($Report);
- $Report->saveCSV($runName, $Addr_xyz);
- if (strpos($_GET['print'],"addr") !== false) print_r($Addr_xyz);
-
- ?>