3 // Die Schiffsbesatzung
4 class SimulatorPersonell extends BasePersonell {
6 private $personellList = null;
9 private $cacheList = null;
11 // A string for cached conditions
12 private $cacheCond = null;
19 private function __construct () {
20 // Call parent constructor
21 parent::constructor(__CLASS__);
23 if (((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) && (defined('DEBUG_CONSTRUCT'))) $this->getDebugInstance()->output(sprintf("[%s:] Konstruktor erreicht.<br />\n",
28 $this->setPartDescr("Simulationspersonal");
31 $this->createUniqueID();
34 $this->removeSystemArray();
38 * Magic wake-up method called when unserialize() is called. This is
39 * neccessary because in this case a personell does not need to know the
40 * min/max ages range and system classes. This would anyway use more RAM
41 * what is not required.
45 public function __wakeup () {
47 $this->removePersonellList();
48 $this->removeMinMaxAge();
50 $this->removeSystemArray();
54 * Generate a specified amount of personell and return the prepared instance
56 * @param $amountPersonell Number of personell we shall
58 * @return $personellInstance An instance of this object with a
61 public static function createSimulatorPersonell ($amountPersonell) {
62 // Make sure only integer can pass
63 $amountPersonell = (int) $amountPersonell;
66 $personellInstance = new SimulatorPersonell();
69 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->getDebugInstance()->output(sprintf("[%s:%d] Es werden <strong>%d</strong> Personal bereitgestellt.<br />\n",
75 // Initialize the personell list
76 $personellInstance->createPersonellList();
78 // Create requested amount of personell
79 for ($idx = 0; $idx < $amountPersonell; $idx++) {
80 $personellInstance->addRandomPersonell();
84 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $personellInstance->getDebugInstance()->output(sprintf("[%s:%d] <strong>%d</strong> Personal bereitgestellt.<br />\n",
91 $personellInstance->removeGender();
92 $personellInstance->removeNames();
93 $personellInstance->removeBirthday();
94 $personellInstance->removeSalary();
95 $personellInstance->removeEmployed();
96 $personellInstance->removeMarried();
97 $personellInstance->removeNumberFormaters();
98 //$personellInstance->removeCache();
99 $personellInstance->removeSystemArray();
101 // Instanz zurueckgeben
102 return $personellInstance;
106 * Create a SimulatorPersonell object by loading the specified personell
107 * list from an existing database backend
109 * @param $idNumber The ID number (only right part) of the list
110 * @return $personellInstance An instance of
111 * @throws InvalidIDFormatException If the given id number
112 * $idNumber is invalid
113 * @throws NullPointerException If a null pointer (instance)
115 * @throws NoObjectException If a non-object has been
117 * @throws MissingMethodException If a required method is missing
118 * @throws MissingSimulatorIDException If an ID number was not found
120 public final static function createSimulatorPersonellByID ($idNumber) {
121 // Add the class name if it was not found
122 if (count(explode("@", $idNumber)) < 2) {
123 // Add class name in front of the incomplete ID number
124 $tempID = sprintf("%s@%s", __CLASS__, $idNumber);
126 // Use the direct ID number
130 // Validate the ID number
131 if (!preg_match(sprintf("/%s\@([a-f0-9]){32}/i", __CLASS__), $tempID)) {
133 throw new InvalidIDFormatException(new SimulatorPersonell(), self::EXCEPTION_ID_IS_INVALID_FORMAT);
137 $personellInstance = new SimulatorPersonell(false);
139 // Get database instance
140 $dbInstance = $personellInstance->getDatabaseInstance();
142 // Is this a valid database instance?
143 if (is_null($dbInstance)) {
145 throw new NullPointerException($personellInstance, self::EXCEPTION_IS_NULL_POINTER);
146 } elseif (!is_object($dbInstance)) {
147 // Not an object! ;-(
148 throw new NoObjectException($dbInstance, self::EXCEPTION_IS_NO_OBJECT);
149 } elseif (!method_exists($dbInstance, 'isUniqueIdUsed')) {
150 // Required method not found
151 throw new MissingMethodException(array($dbInstance, 'isUniqueIdUsed'), self::EXCEPTION_MISSING_METHOD);
154 // Is the unique ID already used? Then it must be there!
155 if (!$dbInstance->isUniqueIdUsed($tempID)) {
157 throw new MissingSimulatorIDException(array($personellInstance, $idNumber), self::EXCEPTION_SIMULATOR_ID_INVALID);
160 // Load the personell list and add it to this object
161 $personellInstance->loadPersonellList($tempID);
164 $personellInstance->removeGender();
165 $personellInstance->removeNames();
166 $personellInstance->removeBirthday();
167 $personellInstance->removeSalary();
168 $personellInstance->removeEmployed();
169 $personellInstance->removeMarried();
170 $personellInstance->removeNumberFormaters();
171 //$personellInstance->removeCache();
172 $personellInstance->removeSystemArray();
175 return $personellInstance;
178 // Create personell list
179 public function createPersonellList () {
180 if (is_null($this->personellList)) {
181 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Personell-Liste erstellt.<br />\n",
185 $this->personellList = new FrameworkArrayObject();
187 throw new PersonellListAlreadyCreatedException($this, self::EXCEPTION_DIMENSION_ARRAY_INVALID);
191 // Remove the personell list
192 private function removePersonellList () {
193 if (defined('DEBUG_PERSONELL')) $this->getDebugInstance()->output(sprintf("[%s:%d] Personell-Liste entfernt.<br />\n",
197 unset($this->personellList);
200 // Add new personell object to our list
201 public function addRandomPersonell () {
203 $genders = array('M', 'F');
205 // Create new personell members
206 $personellInstance = new SimulatorPersonell();
208 // Set a randomized gender
209 $personellInstance->setGender($genders[mt_rand(0, 1)]);
211 // Set a randomized birthday (maximum age required, see const MAX_AGE)
212 $personellInstance->createBirthday();
214 // Married? Same values means: married
215 if (mt_rand(0, 5) == mt_rand(0, 5)) $personellInstance->setMarried(true);
218 $personellInstance->removePersonellList();
219 $personellInstance->removeMinMaxAge();
220 $personellInstance->removeCache();
221 $personellInstance->removeSystemArray();
223 // Add new member to the list
224 $this->personellList->append($personellInstance);
228 * Get a specifyable list of our people, null or empty string will be ignored!
230 * @return $cacheList A list of cached personells
232 function getSpecialPersonellList ($isEmployed = null, $isMarried = null, $hasGender = "") {
233 // Serialize the conditions for checking if we can take the cache
234 $serialized = serialize(array($isEmployed, $isMarried, $hasGender));
236 // The same (last) conditions?
237 if (($serialized == $this->cacheCond) && (!is_null($this->cacheCond))) {
238 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Gecachte Liste wird verwendet.<br />\n",
243 // Return cached list
244 return $this->cacheList;
247 // Output debug message
248 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Personalliste wird nach Kriterien durchsucht...<br />\n",
253 // Remember the conditions
254 $this->setCacheCond($serialized);
256 // Create cached list
257 $this->setAllCacheList(new FrameworkArrayObject());
259 // Search all unemployed personells
260 for ($idx = $this->personellList->getIterator(); $idx->valid(); $idx->next()) {
262 $el = $idx->current();
264 // Check currenylt all single conditions (combined conditions are not yet supported)
265 if ((!is_null($isEmployed)) && ($el->isEmployed() == $isEmployed)) {
266 // Add this one (employed status asked)
267 $this->cacheList->append($el);
268 } elseif ((!is_null($isMarried)) && ($el->isMarried() == $isMarried)) {
269 // Add this one (marrital status asked)
270 $this->cacheList->append($el);
271 } elseif ((!empty($hasGender)) && ($el->getGender() == $hasGender)) {
272 // Add this one (specified gender)
273 $this->cacheList->append($el);
277 // Return the completed list
278 return $this->cacheList;
282 * Get amount of unemployed personell
284 * @return $count Amount of unemployed personell
286 public final function getAllUnemployed () {
287 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Es werden alle erwerbslosen Personen gesucht.<br />\n",
292 // Get a temporary list
293 $list = $this->getSpecialPersonellList(false);
295 // Anzahl zurueckliefern
296 return $list->count();
300 * Remove cache things
304 private function removeCache () {
305 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Cache-Attribute entfernt.<br />\n",
311 unset($this->cacheList);
312 unset($this->cacheCond);
316 * Setter for cache list
318 * @param $cacheList The new cache list to set or null for initialization/reset
321 private function setAllCacheList (FrameworkArrayObject $cacheList = null) {
322 $this->cacheList = $cacheList;
326 * Setter for cache conditions
328 * @param $cacheCond The new cache conditions to set
331 private function setCacheCond ($cacheCond) {
332 $this->cacheCond = (string) $cacheCond;
340 public function resetCache () {
341 if ((defined('DEBUG_PERSONELL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Cache-Liste zurückgesetzt.<br />\n",
345 $this->setAllCacheList(null);
346 $this->setCacheCond("");
350 * Getter for surname. If no surname is set then default surnames are set
351 * for male and female personells.
353 * @return $surname The personell' surname
355 public final function getSurname () {
356 $surname = parent::getSurname();
357 // Make sure every one has a surname...
358 if (empty($surname)) {
359 if ($this->isMale()) {
363 // Typical female name
364 $surname = "Jennifer";
367 // Set typical family name
368 parent::setFamily("Smith");
374 * Saves only the personell list to the database
378 public function saveObjectToDatabase () {
380 $dbInstance = $this->getDatabaseInstance();
382 // Prepare the limitation object. We just need the personellList array object.
383 $limitInstance = ObjectLimits::createObjectLimits(array("personellList"));
385 // Limitate the saving amount
386 $dbInstance->limitObject($limitInstance);
389 $dbInstance->saveObject($this);
393 * Getter for personell list
395 * @return $personellList The list of all personells
397 public final function getPersonellList () {
398 return $this->personellList;
402 * Loads the mostly pre-cached personell list
404 * @param $idNumber The ID number we shall use for looking up
407 * @throws ContainerItemIsNullException If a container item is null
408 * @throws ContainerItemIsNoArrayException If a container item is
410 * @throws ContainerMaybeDamagedException If the container item
411 * is missing the indexes
412 * 'name' and/or 'value'
413 * @see SerializationContainer A special container class which
414 * helps storing only some attributes
417 public function loadPersonellList ($idNumber) {
418 // Get database instance
419 $dbInstance = $this->getDatabaseInstance();
421 // Get the serialization container within the personell list from
422 // the database layer
423 $containerInstance = $dbInstance->getObjectFromCachedData($idNumber);
425 // Iterate through the whole container
426 for ($idx = $containerInstance->getIterator(); $idx->valid(); $idx->next()) {
427 // Get current item from container
428 $item = $idx->current();
431 if (is_null($item)) {
433 throw new ContainerItemIsNullException($this, self::EXCEPTION_CONTAINER_ITEM_IS_NULL);
434 } elseif (!is_array($item)) {
436 throw new ContainerItemIsNoArrayException($this, self::EXCEPTION_ITEM_IS_NO_ARRAY);
437 } elseif ((!isset($item['name'])) || (!isset($item['value']))) {
439 throw new ContainerMaybeDamagedException($this, self::EXCEPTION_CONTAINER_MAYBE_DAMAGED);
442 // Okay, now we can get the item and generate a valid command for eval().
443 // We need to convert the first letter to lower-case but keep all others intact
444 $eval = sprintf("\$this->%s = \$item['value'];",
445 strtolower(substr($item['name'], 0, 1))
447 substr($item['name'], 1)
451 if ((defined('DEBUG_EVAL')) || (defined('DEBUG_ALL'))) $this->getDebugInstance()->output(sprintf("[%s:%d] Konstruierte PHP-Anweisung: <pre><em>%s</em></pre><br />\n",