Salome HOME
808487c80d0c15e2612f8cd6298a8504021ec229
[modules/shaper.git] / src / GeomValidators / GeomValidators_ValueOrder.cpp
1 // Copyright (C) 2017-2021  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomValidators_ValueOrder.h"
21
22 #include <Config_PropManager.h>
23 #include <Events_InfoMessage.h>
24
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeInteger.h>
27 #include <ModelAPI_Session.h>
28 #include <ModelAPI_Validator.h>
29
30 // LCOV_EXCL_START
31 static double attributeValue(AttributePtr theAttr)
32 {
33   AttributeIntegerPtr anIntAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>(theAttr);
34   if (anIntAttr)
35     return (double)anIntAttr->value();
36   AttributeDoublePtr aDoubleAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttr);
37   if (aDoubleAttr)
38     return aDoubleAttr->value();
39   return 0.0;
40 }
41
42 static bool isGreaterOrEqual(AttributePtr theFirstArg, AttributePtr theSecondArg)
43 {
44   if (theFirstArg && theFirstArg->isInitialized() &&
45       theSecondArg && theSecondArg->isInitialized())
46     return attributeValue(theFirstArg) >= attributeValue(theSecondArg);
47   return false;
48 }
49
50 static bool isLessOrEqual(AttributePtr theFirstArg, AttributePtr theSecondArg)
51 {
52   if (theFirstArg && theFirstArg->isInitialized() &&
53       theSecondArg && theSecondArg->isInitialized())
54     return attributeValue(theFirstArg) <= attributeValue(theSecondArg);
55   return false;
56 }
57
58 // Check the attributes are satisfy theCompare function
59 static bool isValidOrder(const AttributePtr& theAttribute,
60                          const std::list<std::string>& theArguments,
61                          Events_InfoMessage& theError,
62                          bool (*theCompare)(AttributePtr, AttributePtr))
63 {
64   std::string anAttrType = theAttribute->attributeType();
65   if (anAttrType != ModelAPI_AttributeDouble::typeId() &&
66       anAttrType != ModelAPI_AttributeInteger::typeId()) {
67     theError = "Unsupported attribute type (integer or double applicable)";
68     return false;
69   }
70
71   FeaturePtr anOwner = ModelAPI_Feature::feature(theAttribute->owner());
72   if (!anOwner) {
73     theError = "Attribute without owner";
74     return false;
75   }
76
77   for (std::list<std::string>::const_iterator anIt = theArguments.begin();
78        anIt != theArguments.end(); ++anIt) {
79     // check the argument links to the attribute of the current feature
80     AttributePtr aCurAttr = anOwner->attribute(*anIt);
81     if (!aCurAttr) {
82       theError = "Arguments should be names of attributes of current feature";
83       return false;
84     }
85
86     // compare values
87     if (!(*theCompare)(theAttribute, aCurAttr)) {
88       theError = "Attributes have incorrect order";
89       return false;
90     }
91   }
92
93   return true;
94 }
95 // LCOV_EXCL_STOP
96
97
98 /// Global instance for validators factory
99 GeomValidators_GreaterOrEqual MY_GEQ_INSTANCE;
100 GeomValidators_LessOrEqual MY_LEQ_INSTANCE;
101
102 GeomValidators_GreaterOrEqual::GeomValidators_GreaterOrEqual()
103 {
104   // this validator is registered in the factory on this library loading
105   SessionPtr aMgr = ModelAPI_Session::get();
106   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
107   aFactory->registerValidator("GeomValidators_GreaterOrEqual", this);
108 }
109
110 // LCOV_EXCL_START
111 bool GeomValidators_GreaterOrEqual::isValid(const AttributePtr& theAttribute,
112                                             const std::list<std::string>& theArguments,
113                                             Events_InfoMessage& theError) const
114 {
115   return isValidOrder(theAttribute, theArguments, theError, &isGreaterOrEqual);
116 }
117 // LCOV_EXCL_STOP
118
119
120 GeomValidators_LessOrEqual::GeomValidators_LessOrEqual()
121 {
122   // this validator is registered in the factory on this library loading
123   SessionPtr aMgr = ModelAPI_Session::get();
124   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
125   aFactory->registerValidator("GeomValidators_LessOrEqual", this);
126 }
127
128 // LCOV_EXCL_START
129 bool GeomValidators_LessOrEqual::isValid(const AttributePtr& theAttribute,
130                                          const std::list<std::string>& theArguments,
131                                          Events_InfoMessage& theError) const
132 {
133   return isValidOrder(theAttribute, theArguments, theError, &isLessOrEqual);
134 }
135 // LCOV_EXCL_STOP