Filesystem.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Artificial Neural Network - Version 2.2
  4. *
  5. * For updates and changes visit the project page at http://ann.thwien.de/
  6. *
  7. *
  8. *
  9. * <b>LICENCE</b>
  10. *
  11. * The BSD 2-Clause License
  12. *
  13. * http://opensource.org/licenses/bsd-license.php
  14. *
  15. * Copyright (c) 2007 - 2012, Thomas Wien
  16. * All rights reserved.
  17. *
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted provided that the following conditions
  20. * are met:
  21. *
  22. * 1. Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. *
  25. * 2. Redistributions in binary form must reproduce the above copyright
  26. * notice, this list of conditions and the following disclaimer in the
  27. * documentation and/or other materials provided with the distribution.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  32. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  33. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  34. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  35. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  36. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  37. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  39. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGE.
  41. *
  42. * @author Thomas Wien <info_at_thwien_dot_de>
  43. * @version ANN Version 2.2 by Thomas Wien
  44. * @copyright Copyright (c) 2007-2012 by Thomas Wien
  45. * @package ANN
  46. */
  47. namespace ANN;
  48. /**
  49. * @package ANN
  50. * @access private
  51. */
  52. abstract class Filesystem
  53. {
  54. /**
  55. * @param string $strFilename (Default: null)
  56. * @uses Exception::__construct()
  57. * @throws Exception
  58. */
  59. public function saveToFile($strFilename = null)
  60. {
  61. if(!($this instanceof InterfaceLoadable))
  62. throw new Exception('Current object not instance of Interface \\ANN\\InterfaceLoadable');
  63. settype($strFilename, 'string');
  64. if(empty($strFilename))
  65. throw new Exception('Paramter $strFilename should be a filename');
  66. $strDir = dirname($strFilename);
  67. if(empty($strDir))
  68. $strDir = '.';
  69. if(!is_dir($strDir))
  70. throw new Exception("Directory $strDir does not exist");
  71. if(!is_writable($strDir))
  72. throw new Exception("Directory $strDir has no writing permission");
  73. if(is_file($strFilename) && !is_writable($strFilename))
  74. throw new Exception("File $strFilename does exist but has no writing permission");
  75. try
  76. {
  77. $strSerialized = serialize($this);
  78. file_put_contents($strFilename, $strSerialized);
  79. }
  80. catch(Exception $e)
  81. {
  82. throw new Exception("Could not open or create $strFilename!");
  83. }
  84. }
  85. /**
  86. * @param string $strFilename (Default: null)
  87. * @return Network|InputValue|OutputValue|Values|StringValue|Classification|InterfaceLoadable
  88. * @uses Exception::__construct()
  89. * @throws Exception
  90. */
  91. public static function loadFromFile($strFilename = null)
  92. {
  93. if(is_file($strFilename) && is_readable($strFilename))
  94. {
  95. $strSerialized = file_get_contents($strFilename);
  96. if (empty($strSerialized))
  97. throw new Exception('File '. basename($strFilename) .' could not be loaded (file has no object information stored)');
  98. $objInstance = unserialize($strSerialized);
  99. if(!($objInstance instanceof InterfaceLoadable))
  100. throw new Exception('File '. basename($strFilename) .' could not be opened (no ANN format)');
  101. return $objInstance;
  102. }
  103. throw new Exception('File '. basename($strFilename) .' could not be opened');
  104. }
  105. }