aboutsummaryrefslogtreecommitdiffstats
path: root/Model/Variable/Variable.php
diff options
context:
space:
mode:
Diffstat (limited to 'Model/Variable/Variable.php')
-rw-r--r--Model/Variable/Variable.php257
1 files changed, 118 insertions, 139 deletions
diff --git a/Model/Variable/Variable.php b/Model/Variable/Variable.php
index d50ff77..8c16410 100644
--- a/Model/Variable/Variable.php
+++ b/Model/Variable/Variable.php
@@ -8,7 +8,7 @@
*
* New BSD License
*
- * Copyright © 2007-2015, Ivan Enderlin. All rights reserved.
+ * Copyright © 2007-2015, Hoa community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -34,104 +34,87 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
-namespace {
+namespace Hoa\Praspel\Model\Variable;
-from('Hoa')
-
-/**
- * \Hoa\Praspel\Exception\Model
- */
--> import('Praspel.Exception.Model')
-
-/**
- * \Hoa\Visitor\Element
- */
--> import('Visitor.Element')
-
-/**
- * \Hoa\Realdom\IRealdom\Holder
- */
--> import('Realdom.I~.Holder');
-
-}
-
-namespace Hoa\Praspel\Model\Variable {
+use Hoa\Core;
+use Hoa\Math;
+use Hoa\Praspel;
+use Hoa\Realdom;
+use Hoa\Visitor;
/**
* Class \Hoa\Praspel\Model\Variable.
*
* Represent a variable.
*
- * @author Ivan Enderlin <ivan.enderlin@hoa-project.net>
- * @copyright Copyright © 2007-2015 Ivan Enderlin.
+ * @copyright Copyright © 2007-2015 Hoa community
* @license New BSD License
*/
-
class Variable
- implements \Hoa\Visitor\Element,
- \Hoa\Realdom\IRealdom\Holder {
-
+ implements Visitor\Element,
+ Realdom\IRealdom\Holder
+{
/**
* Variable name.
*
- * @var \Hoa\Praspel\Model\Variable string
+ * @var string
*/
protected $_name = null;
/**
* Local (let) or not.
*
- * @var \Hoa\Praspel\Model\Variable bool
+ * @var bool
*/
protected $_local = false;
/**
* Clause that contains this variable.
*
- * @var \Hoa\Praspel\Model\Clause object
+ * @var \Hoa\Praspel\Model\Clause
*/
protected $_clause = null;
/**
* Variable value.
*
- * @var \Hoa\Praspel\Model\Variable mixed
+ * @var mixed
*/
protected $_value = null;
/**
* Domains that describe the variable.
*
- * @var \Hoa\Realdom\Disjunction object
+ * @var \Hoa\Realdom\Disjunction
*/
protected $_domains = null;
/**
* References domains.
*
- * @var \Hoa\Realdom\Disjunction object
+ * @var \Hoa\Realdom\Disjunction
*/
protected $_refDomains = null;
/**
* Alias variable (please, see “domainof”).
*
- * @var \Hoa\Praspel\Model\Variable object
+ * @var \Hoa\Praspel\Model\Variable
*/
protected $_alias = null;
/**
* Constraints.
*
- * @var \Hoa\Praspel\Model\Variable array
+ * @var array
*/
- protected $_constraints = array();
+ protected $_constraints = [];
/**
* Temporary constraints type.
* Useful when propagate new constraints.
*
- * @var \Hoa\Praspel\Model\Variable string
+ * @var string
*/
protected $_tmpConstraintsType = null;
@@ -139,7 +122,7 @@ class Variable
* Temporary constraints index.
* Useful when propagate new constraints.
*
- * @var \Hoa\Praspel\Model\Variable string
+ * @var string
*/
protected $_tmpConstraintsIndex = null;
@@ -148,23 +131,27 @@ class Variable
/**
* Build a variable.
*
- * @access public
* @param string $name Name.
* @param bool $local Local.
* @param \Hoa\Praspel\Model\Clause $clause Clause.
* @return void
- * @throw \Hoa\Praspel\Exception\Model
+ * @throws \Hoa\Praspel\Exception\Model
*/
- public function __construct ( $name, $local,
- \Hoa\Praspel\Model\Clause $clause = null ) {
-
- if( ('\old' === substr($name, 0, 4)
- || '\result' === $name)
- && !($clause instanceof \Hoa\Praspel\Model\Ensures))
- throw new \Hoa\Praspel\Exception\Model(
+ public function __construct(
+ $name,
+ $local,
+ Praspel\Model\Clause $clause = null
+ ) {
+ if (('\old' === substr($name, 0, 4) ||
+ '\result' === $name) &&
+ !($clause instanceof Praspel\Model\Ensures)) {
+ throw new Praspel\Exception\Model(
'\old(…) and \result are only allowed in @ensures, ' .
'given %s in @%s.',
- 0, array($name, $clause->getName()));
+ 0,
+ [$name, $clause->getName()]
+ );
+ }
$this->_name = $name;
$this->_local = $local;
@@ -177,12 +164,11 @@ class Variable
/**
* Set a value to the variable.
*
- * @access public
* @param mixed $value Value.
* @return mixed
*/
- public function setValue ( $value ) {
-
+ public function setValue($value)
+ {
$old = $this->_value;
$this->_value = $value;
@@ -192,11 +178,10 @@ class Variable
/**
* Get value of the variable.
*
- * @access public
* @return mixed
*/
- public function &getValue ( ) {
-
+ public function &getValue()
+ {
return $this->_value;
}
@@ -204,16 +189,14 @@ class Variable
* Allow to write $variable->in = … to define domains (if $name is not equal
* to "in", then it is a normal behavior).
*
- * @access public
* @param string $name Name.
* @param mixed $value Value.
* @return void
- * @throw \Hoa\Praspel\Exception\Model
+ * @throws \Hoa\Praspel\Exception\Model
*/
- public function __set ( $name, $value ) {
-
- if('in' !== $name) {
-
+ public function __set($name, $value)
+ {
+ if ('in' !== $name) {
$this->$name = $value;
return;
@@ -221,19 +204,22 @@ class Variable
$onDomains = $this->_domains === $this->_refDomains;
- if( true === $onDomains
- && !empty($this->_domains))
- throw new \Hoa\Praspel\Exception\Model(
+ if (true === $onDomains &&
+ !empty($this->_domains)) {
+ throw new Praspel\Exception\Model(
'Variable $%s has already declared domains.',
- 1, $this->getName());
+ 1,
+ $this->getName()
+ );
+ }
- if(!($value instanceof \Hoa\Realdom\Disjunction))
+ if (!($value instanceof Realdom\Disjunction)) {
$value = realdom()->const($value);
+ }
$this->_refDomains = $value;
- if(false === $onDomains) {
-
+ if (false === $onDomains) {
$this->_domains->propagateConstraints(
$this->_tmpConstraintsType,
$this->_tmpConstraintsIndex
@@ -248,8 +234,9 @@ class Variable
$this->_domains->setHolder($this);
- foreach($this->_domains as $domain)
+ foreach ($this->_domains as $domain) {
$domain->setConstraints($this->_constraints);
+ }
return;
}
@@ -261,10 +248,11 @@ class Variable
* @param mixed $q Sampled value.
* @return boolean
*/
- public function predicate ( $q = null ) {
-
- if(null === $q)
+ public function predicate($q = null)
+ {
+ if (null === $q) {
$q = $this->getValue();
+ }
return $this->getDomains()->predicate($q);
}
@@ -272,43 +260,41 @@ class Variable
/**
* Call the sample() method on realistic domains.
*
- * @access public
* @param \Hoa\Math\Sampler $sampler Sampler.
* @return mixed
- * @throw \Hoa\Realdom\Exception
+ * @throws \Hoa\Realdom\Exception
*/
- public function sample ( \Hoa\Math\Sampler $sampler = null ) {
-
+ public function sample(Math\Sampler $sampler = null)
+ {
return $this->getDomains()->sample($sampler);
}
/**
* Call the reset() method on realistic domains.
*
- * @access public
* @return void
*/
- public function reset ( ) {
-
+ public function reset()
+ {
return $this->getDomains()->reset();
}
/**
* Define a “key” constraint. Use $variable->key(…)->in = …;
*
- * @access public
* @param mixed $scalar Value.
* @return \Hoa\Praspel\Model\Variable
*/
- public function key ( $scalar ) {
-
- if(!isset($this->_constraints['key']))
- $this->_constraints['key'] = array();
+ public function key($scalar)
+ {
+ if (!isset($this->_constraints['key'])) {
+ $this->_constraints['key'] = [];
+ }
unset($this->_refDomains);
- $handle = &$this->_constraints['key'][];
- $handle[0] = realdom()->const($scalar);
- $this->_refDomains = &$handle[1];
+ $handle = &$this->_constraints['key'][];
+ $handle[0] = realdom()->const($scalar);
+ $this->_refDomains = &$handle[1];
end($this->_constraints['key']);
$this->_tmpConstraintsType = 'key';
@@ -320,14 +306,14 @@ class Variable
/**
* Define a “contains” constraint.
*
- * @access public
* @param mixed $scalar Value.
* @return \Hoa\Praspel\Model\Variable
*/
- public function contains ( $scalar ) {
-
- if(!isset($this->_constraints['contains']))
- $this->_constraints['contains'] = array();
+ public function contains($scalar)
+ {
+ if (!isset($this->_constraints['contains'])) {
+ $this->_constraints['contains'] = [];
+ }
$this->_constraints['contains'][] = realdom()->const($scalar);
@@ -337,14 +323,14 @@ class Variable
/**
* Add an “is” constraint.
*
- * @access public
* @param string ... Keywords.
* @return \Hoa\Praspel\Model\Variable
*/
- public function is ( ) {
-
- if(!isset($this->_constraints['is']))
- $this->_constraints['is'] = array();
+ public function is()
+ {
+ if (!isset($this->_constraints['is'])) {
+ $this->_constraints['is'] = [];
+ }
$this->_constraints['is'] = array_merge(
$this->_constraints['is'],
@@ -357,25 +343,30 @@ class Variable
/**
* Declare a “domainof” (alias).
*
- * @access public
* @param \Hoa\Praspel\Model\Variable $variable Variable.
* @return \Hoa\Praspel\Model\Variable
- * @throw \Hoa\Realdom\Exception
+ * @throws \Hoa\Realdom\Exception
*/
- public function domainof ( $variable ) {
-
+ public function domainof($variable)
+ {
$variables = $this->getClause()->getLocalVariables();
- if(!isset($variables[$variable]))
- throw new \Hoa\Praspel\Exception\Model(
+ if (!isset($variables[$variable])) {
+ throw new Praspel\Exception\Model(
'Variable $%s does not exist, cannot alias domains to $%s.',
- 2, array($variable, $this->getName()));
+ 2,
+ [$variable, $this->getName()]
+ );
+ }
- if(!empty($this->_domains))
- throw new \Hoa\Praspel\Exception\Model(
+ if (!empty($this->_domains)) {
+ throw new Praspel\Exception\Model(
'Variable $%s already has domains, cannot alias new domains ' .
'from $%s.',
- 3, array($this->getName(), $variable));
+ 3,
+ [$this->getName(), $variable]
+ );
+ }
$this->_alias = $variable;
$this->_domains = &$variables[$variable]->getDomains();
@@ -386,103 +377,91 @@ class Variable
/**
* Get domains.
*
- * @access public
* @return \Hoa\Realdom\Disjunction
*/
- public function &getDomains ( ) {
-
+ public function &getDomains()
+ {
return $this->_domains;
}
/**
* Get held realdoms.
*
- * @access public
* @return \Hoa\Realdom\Disjunction
*/
- public function &getHeld ( ) {
-
+ public function &getHeld()
+ {
return $this->getDomains();
}
/**
* Get variable name.
*
- * @access public
* @return string
*/
- public function getName ( ) {
-
+ public function getName()
+ {
return $this->_name;
}
/**
* Check if the variable is local (let) or not.
*
- * @access public
* @return bool
*/
- public function isLocal ( ) {
-
+ public function isLocal()
+ {
return $this->_local;
}
/**
* Get constraints.
*
- * @access public
* @return array
*/
- public function getConstraints ( ) {
-
+ public function getConstraints()
+ {
return $this->_constraints;
}
/**
* Get alias.
*
- * @access public
* @return \Hoa\Praspel\Model\Variable
*/
- public function getAlias ( ) {
-
+ public function getAlias()
+ {
return $this->_alias;
}
/**
* Get parent clause.
*
- * @access public
* @return \Hoa\Praspel\Model\Clause
*/
- public function getClause ( ) {
-
+ public function getClause()
+ {
return $this->_clause;
}
/**
* 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 ) {
-
+ public function accept(
+ Visitor\Visit $visitor,
+ &$handle = null,
+ $eldnah = null
+ ) {
return $visitor->visit($this, $handle, $eldnah);
}
}
-}
-
-namespace {
-
/**
* Flex entity.
*/
-Hoa\Core\Consistency::flexEntity('Hoa\Praspel\Model\Variable\Variable');
-
-}
+Core\Consistency::flexEntity('Hoa\Praspel\Model\Variable\Variable');