2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2015 - Scilab Enterprises - Calixte DENIZET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 #include "gvn/ConstraintManager.hxx"
14 #include "data/FunctionBlock.hxx"
18 std::vector<std::shared_ptr<InferenceConstraint>> ConstraintManager::generalConstraints = init();
20 std::vector<std::shared_ptr<InferenceConstraint>> ConstraintManager::init()
22 std::vector<std::shared_ptr<InferenceConstraint>> v;
23 v.reserve(Kind::COUNT);
24 // Same dims => equality of 2 pairs of values
25 v.emplace_back(new SameDimsConstraint());
27 v.emplace_back(new EqualConstraint());
28 // Positivity of a value
29 v.emplace_back(new PositiveConstraint());
30 // Strict positivity of a value
31 v.emplace_back(new StrictPositiveConstraint());
32 // Is a value greater than an other ?
33 v.emplace_back(new GreaterConstraint());
34 // Is a value strict greater than an other ?
35 v.emplace_back(new StrictGreaterConstraint());
37 v.emplace_back(new ValidIndexConstraint());
39 v.emplace_back(new ValidRangeConstraint());
44 ConstraintManager::ConstraintManager(FunctionBlock * _function, FunctionBlock * _parent) : parent(_parent ? & _parent->getConstraintManager() : nullptr), function(_function) { }
46 bool ConstraintManager::check(const MPolyConstraintSet & set, const std::vector<GVN::Value *> & values)
48 /*std::wcerr << set.constraints.begin()->poly << "::" << set.constraints.begin()->kind << std::endl;
49 for (const auto & v : values)
51 std::wcerr << "DEBUG1=" << *v << std::endl;
53 InferenceConstraint::Result res = set.check((parent && parent->function) ? parent->function->getGVN() : function->getGVN(), values);
56 case InferenceConstraint::Result::RESULT_TRUE:
58 mpConstraints.add(set);
59 set.applyConstraints(values);
62 case InferenceConstraint::Result::RESULT_FALSE:
64 case InferenceConstraint::Result::RESULT_DUNNO:
66 if (parent && parent->function)
68 const bool ret = parent->check(set.getMPConstraints(values), parent->function->getInValues());
71 mpConstraints.add(set);
72 set.applyConstraints(values);
84 bool ConstraintManager::check(Kind kind, const std::vector<GVN::Value *> & values)
88 const InferenceConstraint & ic = *generalConstraints[kind];
89 InferenceConstraint::Result res = ic.check(function->getGVN(), values);
90 //std::wcerr << "DEBUG2=" << res << std::endl;
94 case InferenceConstraint::Result::RESULT_TRUE:
96 mpConstraints.add(ic.getMPConstraints(values));
97 ic.applyConstraints(values);
100 case InferenceConstraint::Result::RESULT_FALSE:
102 case InferenceConstraint::Result::RESULT_DUNNO:
104 MPolyConstraintSet set = ic.getMPConstraints(values);
105 const bool ret = check(set, function->getInValues());
108 mpConstraints.add(set);
109 ic.applyConstraints(values);
119 bool ConstraintManager::checkGlobalConstant(const symbol::Symbol & sym)
121 if (constantConstraints.find(sym) == constantConstraints.end())
123 // TODO: fix that !!!
124 const bool ret = true; //symbol::Context::getInstance()->isOriginalSymbol(sym);
127 ConstraintManager * cm = this;
130 cm->constantConstraints.emplace(sym);
142 bool ConstraintManager::checkGlobalConstants(const std::set<symbol::Symbol> & gc)
144 for (const auto sym : gc)
146 if (!symbol::Context::getInstance()->isOriginalSymbol(sym))