|
| 1 | +/* --------------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# L-Py: L-systems in Python |
| 4 | +# |
| 5 | +# Copyright 2003-2008 UMR Cirad/Inria/Inra Dap - Virtual Plant Team |
| 6 | +# |
| 7 | +# File author(s): F. Boudon (frederic.boudon@cirad.fr) |
| 8 | +# |
| 9 | +# --------------------------------------------------------------------------- |
| 10 | +# |
| 11 | +# GNU General Public Licence |
| 12 | +# |
| 13 | +# This program is free software; you can redistribute it and/or |
| 14 | +# modify it under the terms of the GNU General Public License as |
| 15 | +# published by the Free Software Foundation; either version 2 of |
| 16 | +# the License, or (at your option) any later version. |
| 17 | +# |
| 18 | +# This program is distributed in the hope that it will be useful, |
| 19 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | +# MERCHANTABILITY or FITNESS For A PARTICULAR PURPOSE. See the |
| 21 | +# GNU General Public License for more details. |
| 22 | +# |
| 23 | +# You should have received a copy of the GNU General Public |
| 24 | +# License along with this program; see the file COPYING. If not, |
| 25 | +# write to the Free Software Foundation, Inc., 59 |
| 26 | +# Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 27 | +# |
| 28 | +# --------------------------------------------------------------------------- |
| 29 | +*/ |
| 30 | + |
| 31 | +#include "lstringmatcher.h" |
| 32 | +#include "matching.h" |
| 33 | + |
| 34 | +LPY_USING_NAMESPACE |
| 35 | + |
| 36 | +/*---------------------------------------------------------------------------*/ |
| 37 | + |
| 38 | +LstringMatcher::LstringMatcher(AxialTree::const_iterator _begin, |
| 39 | + AxialTree::const_iterator _end, |
| 40 | + AxialTree::const_iterator _leftpos, |
| 41 | + AxialTree::const_iterator _rightpos, |
| 42 | + AxialTree::const_iterator _rightlastmatch): |
| 43 | + begin(_begin), end(_end), leftpos(_leftpos), |
| 44 | + rightpos(_rightpos), rightlastmatch(_rightlastmatch) |
| 45 | +{ |
| 46 | +} |
| 47 | + |
| 48 | +/*---------------------------------------------------------------------------*/ |
| 49 | + |
| 50 | +void |
| 51 | +LstringMatcher::set(AxialTree::const_iterator _begin, |
| 52 | + AxialTree::const_iterator _end, |
| 53 | + AxialTree::const_iterator _leftpos, |
| 54 | + AxialTree::const_iterator _rightpos, |
| 55 | + AxialTree::const_iterator _rightlastmatch) |
| 56 | +{ |
| 57 | + begin = _begin ; end = _end; |
| 58 | + leftpos = _leftpos ; |
| 59 | + rightpos = _rightpos ; rightlastmatch = _rightlastmatch ; |
| 60 | +} |
| 61 | + |
| 62 | +/*---------------------------------------------------------------------------*/ |
| 63 | +void LstringMatcher::update_returned_args(boost::python::dict& args, const std::vector<std::string>& varnames, const ArgList& values) const |
| 64 | +{ |
| 65 | + std::vector<std::string>::const_iterator itVar = varnames.begin(); |
| 66 | + ArgList::const_iterator itValue = values.begin(); |
| 67 | + for(;itVar != varnames.end() && itValue != values.end(); ++itVar, ++itValue) |
| 68 | + args[*itVar] = *itValue; |
| 69 | +} |
| 70 | + |
| 71 | +bool LstringMatcher::pInLeftContext(size_t patternid, boost::python::dict& args) |
| 72 | +{ |
| 73 | + PatternString leftcontext = PatternStringManager::get().get_pattern(patternid); |
| 74 | + return inLeftContext(leftcontext,args); |
| 75 | +} |
| 76 | + |
| 77 | +bool LstringMatcher::inLeftContext(const PatternString& pattern, boost::python::dict& args) |
| 78 | +{ |
| 79 | + AxialTree::const_iterator endposLeft; |
| 80 | + ArgList values; |
| 81 | + if(!pattern.empty()){ |
| 82 | + if(!MatchingEngine::left_match(leftpos,begin,end, |
| 83 | + pattern.const_rbegin(),pattern.const_rend(), |
| 84 | + endposLeft,values)) return false; |
| 85 | + leftpos = endposLeft; |
| 86 | + update_returned_args(args, pattern.getVarNames(), values); |
| 87 | + } |
| 88 | + return true; |
| 89 | +} |
| 90 | + |
| 91 | +/*---------------------------------------------------------------------------*/ |
| 92 | + |
| 93 | + |
| 94 | +bool LstringMatcher::pInRightContext(size_t patternid, boost::python::dict& args) |
| 95 | +{ |
| 96 | + PatternString pattern = PatternStringManager::get().get_pattern(patternid); |
| 97 | + return inLeftContext(pattern,args); |
| 98 | +} |
| 99 | + |
| 100 | +bool LstringMatcher::inRightContext(const PatternString& pattern, boost::python::dict& args) |
| 101 | +{ |
| 102 | + AxialTree::const_iterator endposRigth; |
| 103 | + ArgList values; |
| 104 | + if(!pattern.empty()){ |
| 105 | + if(!MatchingEngine::right_match(rightpos,begin,end, |
| 106 | + pattern.const_begin(),pattern.const_end(), |
| 107 | + rightlastmatch, endposRigth,values)) |
| 108 | + return false; |
| 109 | + rightpos = endposRigth; |
| 110 | + update_returned_args(args, pattern.getVarNames(), values); |
| 111 | + } |
| 112 | + return true; |
| 113 | +} |
| 114 | + |
| 115 | +/*---------------------------------------------------------------------------*/ |
| 116 | + |
| 117 | +LstringMatcherMaintainer::LstringMatcherMaintainer(LstringMatcherPtr lmatcher, LsysContext * _context) : |
| 118 | + context(_context?_context:LsysContext::current()) |
| 119 | + { context->registerLstringMatcher(lmatcher); } |
| 120 | + |
| 121 | +LstringMatcherMaintainer::~LstringMatcherMaintainer() { context->registerLstringMatcher(); } |
| 122 | + |
| 123 | +/*---------------------------------------------------------------------------*/ |
0 commit comments