aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2013-04-16 16:15:55 +0200
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2013-04-16 16:15:55 +0200
commita47252b81b7f6cb207b27fb4ae3f290d0496c3ea (patch)
treef5a1675595d9a04224376c2d67ca00dad0b58420
parentc2c368e07a224f7817c033e9a9761ff0241d76ae (diff)
downloadPraspel-a47252b81b7f6cb207b27fb4ae3f290d0496c3ea.zip
Praspel-a47252b81b7f6cb207b27fb4ae3f290d0496c3ea.tar.gz
Praspel-a47252b81b7f6cb207b27fb4ae3f290d0496c3ea.tar.bz2
Introduce collection of clauses.
Clauses can have variables, so that they extend Declaration. But a specification or a behavior can have a collection of other clauses, such as a collection of behavior, represented by an identifier. To build a collection of clauses, we need to instance the Collection class with a preset clause and post-clone function. For each new offset in the collection, the associated value is a clone from the preset clause with the post-clone function applied on this clone. This mechanism allows us to build modular collection of clauses.
-rw-r--r--Model/Behavior.php18
-rw-r--r--Model/Collection.php229
-rw-r--r--Model/Specification.php10
-rw-r--r--Visitor/Compiler.php7
-rw-r--r--Visitor/Interpreter.php9
5 files changed, 263 insertions, 10 deletions
diff --git a/Model/Behavior.php b/Model/Behavior.php
index c31da7f..852d1a7 100644
--- a/Model/Behavior.php
+++ b/Model/Behavior.php
@@ -66,7 +66,12 @@ from('Hoa')
/**
* \Hoa\Praspel\Model\Invariant
*/
--> import('Praspel.Model.Invariant');
+-> import('Praspel.Model.Invariant')
+
+/**
+ * \Hoa\Praspel\Model\Collection
+ */
+-> import('Praspel.Model.Collection');
}
@@ -141,7 +146,15 @@ class Behavior extends Clause {
break;
case 'behavior':
- $handle = new self($this);
+ $handle = new Collection(
+ new self($this),
+ function ( self $clause, $identifier ) {
+
+ $clause->setIdentifier($identifier);
+
+ return;
+ }
+ );
break;
default:
@@ -169,7 +182,6 @@ class Behavior extends Clause {
* Set identifier.
*
* @access public
- * @param string $identifier Identifier.
* @return string
*/
public function setIdentifier ( $identifier ) {
diff --git a/Model/Collection.php b/Model/Collection.php
new file mode 100644
index 0000000..72df0ca
--- /dev/null
+++ b/Model/Collection.php
@@ -0,0 +1,229 @@
+<?php
+
+/**
+ * Hoa
+ *
+ *
+ * @license
+ *
+ * New BSD License
+ *
+ * Copyright © 2007-2013, Ivan Enderlin. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Hoa nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+namespace {
+
+from('Hoa')
+
+/**
+ * \Hoa\Praspel\Exception\Model
+ */
+-> import('Praspel.Exception.Model')
+
+/**
+ * \Hoa\Visitor\Element
+ */
+-> import('Visitor.Element');
+
+}
+
+namespace Hoa\Praspel\Model {
+
+/**
+ * Class \Hoa\Praspel\Model\Collection.
+ *
+ * Represent a collection of clauses.
+ *
+ * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
+ * @copyright Copyright © 2007-2013 Ivan Enderlin.
+ * @license New BSD License
+ */
+
+class Collection
+ implements \Hoa\Visitor\Element,
+ \ArrayAccess,
+ \IteratorAggregate {
+
+ /**
+ * Clauses.
+ *
+ * @var \Hoa\Praspel\Model\Collection array
+ */
+ protected $_clauses = array();
+
+ /**
+ * Reference clause.
+ *
+ * @var \Hoa\Praspel\Model\Clause object
+ */
+ protected $_clause = null;
+
+ /**
+ * Post-clone function.
+ *
+ * @var \Closure closure
+ */
+ protected $_postClone = null;
+
+
+
+ /**
+ * Build a collection of clauses.
+ *
+ * @access public
+ * @param \Hoa\Praspel\Model\Clause $clause Clause.
+ * @param \Closure $postClone Post-clone function.
+ * @return void
+ */
+ public function __construct ( Clause $clause,
+ \Closure $postClone = null ) {
+
+ $this->_clause = $clause;
+ $this->_postClone = $postClone;
+
+ return;
+ }
+
+ /**
+ * Check whether an offset exists.
+ *
+ * @access public
+ * @param string $offset Offset.
+ * @return bool
+ */
+ public function offsetExists ( $offset ) {
+
+ return false !== array_key_exists($offset, $this->_clauses);
+ }
+
+ /**
+ * Get a clause.
+ *
+ * @access public
+ * @param string $offset Offset.
+ * @return \Hoa\Praspel\Model\Clause
+ */
+ public function offsetGet ( $offset ) {
+
+ if(false === $this->offsetExists($offset)) {
+
+ $clause = $this->getClause();
+ $this->_clauses[$offset] = clone $clause;
+ $postClone = $this->getPostClone();
+
+ if(null !== $postClone)
+ $postClone($this->_clauses[$offset], $offset);
+ }
+
+ return $this->_clauses[$offset];
+ }
+
+ /**
+ * Alias of $this->offsetGet($offset).
+ *
+ * @access public
+ * @param string $offset Offset.
+ * @return \Hoa\Praspel\Model\Clause
+ */
+ public function get ( $offset ) {
+
+ return $this->offsetGet($offset);
+ }
+
+ /**
+ * Disabled.
+ *
+ * @access public
+ * @return void
+ * @throw \Hoa\Praspel\Exception\Model
+ */
+ public function offsetSet ( $offset, $value ) {
+
+ throw new \Hoa\Praspel\Exception\Model('Operation denied.', 0);
+ }
+
+ /**
+ * Disabled.
+ *
+ * @access public
+ * @return void
+ * @throw \Hoa\Praspel\Exception\Model
+ */
+ public function offsetUnset ( $offset ) {
+
+ throw new \Hoa\Praspel\Exception\Model('Operation denied.', 0);
+ }
+
+ /**
+ * Get reference clause.
+ *
+ * @access protected
+ * @return \Hoa\Praspel\Model\Clause
+ */
+ protected function getClause ( ) {
+
+ return $this->_clause;
+ }
+
+ /**
+ * Get post-clone function.
+ *
+ * @access protected
+ * @return \Closure
+ */
+ protected function getPostClone ( ) {
+
+ return $this->_postClone;
+ }
+
+ /**
+ * Iterate over all clauses.
+ *
+ * @access public
+ * @return \ArrayIterator
+ */
+ public function getIterator ( ) {
+
+ return new \ArrayIterator($this->_clauses);
+ }
+
+ /**
+ * Accept a visitor.
+ *
+ * @access public
+ * @param \Hoa\Visitor\Visit $visitor Visitor.
+ * @param mixed &$handle Handle (reference).
+ * @param mixed $eldnah Handle (no reference).
+ * @return mixed
+ */
+ public function accept ( \Hoa\Visitor\Visit $visitor,
+ &$handle = null, $eldnah = null ) {
+
+ return $visitor->visit($this, $handle, $eldnah);
+ }
+}
+
+}
diff --git a/Model/Specification.php b/Model/Specification.php
index 351d3e1..b5ac837 100644
--- a/Model/Specification.php
+++ b/Model/Specification.php
@@ -132,7 +132,15 @@ class Specification extends Behavior {
break;
case 'behavior':
- $handle = new Behavior($this);
+ $handle = new Collection(
+ new Behavior($this),
+ function ( Behavior $clause, $identifier ) {
+
+ $clause->setIdentifier($identifier);
+
+ return;
+ }
+ );
break;
case 'description':
diff --git a/Visitor/Compiler.php b/Visitor/Compiler.php
index 2d606d8..852bd1f 100644
--- a/Visitor/Compiler.php
+++ b/Visitor/Compiler.php
@@ -164,8 +164,8 @@ class Compiler implements \Hoa\Visitor\Visit {
$identifier = $element->getIdentifier();
$out = "\n" .
$variable . ' = ' . $parent .
- '->getClause(\'behavior\');' . "\n" .
- $variable . '->setIdentifier(\'' . $identifier . '\');' . "\n";
+ '->getClause(\'behavior\')' .
+ '->get(\'' . $identifier . '\');' . "\n";
$clauses = array(
'invariant',
'requires',
@@ -195,6 +195,9 @@ class Compiler implements \Hoa\Visitor\Visit {
preg_replace('#(?<!\\\)\'#', '\\\'', $example) .
'\';' . "\n";
}
+ elseif($element instanceof \Hoa\Praspel\Model\Collection)
+ foreach($element as $el)
+ $out .= $el->accept($this, $handle, $eldnah);
else
throw new \Hoa\Core\Exception(
'%s is not yet implemented.', 0, get_class($element));
diff --git a/Visitor/Interpreter.php b/Visitor/Interpreter.php
index fb3948e..22a03b6 100644
--- a/Visitor/Interpreter.php
+++ b/Visitor/Interpreter.php
@@ -149,12 +149,13 @@ class Interpreter implements \Hoa\Visitor\Visit {
case '#behavior':
$children = $element->getChildren();
$child0 = array_shift($children);
+ $identifier = $child0->accept($this, $handle, false);
$previous = $this->_current;
+
$this->_clause = $this->_current
- = $this->_current->getClause('behavior');
- $this->_clause->setIdentifier(
- $child0->accept($this, $handle, false)
- );
+ = $this->_current
+ ->getClause('behavior')
+ ->get($identifier);
foreach($children as $child)
$child->accept($this, $handle, $eldnah);