Salome HOME
Merge branch 'Dev_GroupsRevision'
[modules/shaper.git] / src / GeomValidators / GeomValidators_ValueOrder.cpp
1 // Copyright (C) 2017-20xx  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomValidators_ValueOrder.h"
22
23 #include <Config_PropManager.h>
24 #include <Events_InfoMessage.h>
25
26 #include <ModelAPI_AttributeDouble.h>
27 #include <ModelAPI_AttributeInteger.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Validator.h>
30
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
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 bool GeomValidators_GreaterOrEqual::isValid(const AttributePtr& theAttribute,
111                                             const std::list<std::string>& theArguments,
112                                             Events_InfoMessage& theError) const
113 {
114   return isValidOrder(theAttribute, theArguments, theError, &isGreaterOrEqual);
115 }
116
117
118
119 GeomValidators_LessOrEqual::GeomValidators_LessOrEqual()
120 {
121   // this validator is registered in the factory on this library loading
122   SessionPtr aMgr = ModelAPI_Session::get();
123   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
124   aFactory->registerValidator("GeomValidators_LessOrEqual", this);
125 }
126
127 bool GeomValidators_LessOrEqual::isValid(const AttributePtr& theAttribute,
128                                          const std::list<std::string>& theArguments,
129                                          Events_InfoMessage& theError) const
130 {
131   return isValidOrder(theAttribute, theArguments, theError, &isLessOrEqual);
132 }