Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / Model / Model_Validator.cpp
1 // File:        Model_Validator.cpp
2 // Created:     2 Jul 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Model_Validator.h>
6 #include <ModelAPI_Feature.h>
7 #include <Events_Error.h>
8
9 using namespace std;
10
11 void Model_ValidatorsFactory::registerValidator(
12   const string& theID, ModelAPI_Validator* theValidator)
13 {
14   if (myIDs.find(theID) != myIDs.end()) {
15     Events_Error::send(string("Validator ") + theID + " is already registered");
16   } else {
17     myIDs[theID] = theValidator;
18   }
19 }
20
21 void Model_ValidatorsFactory::assignValidator(const string& theID, const string& theFeatureID)
22 {
23   bool isError = false;
24   map<string, ModelAPI_Validator*>::iterator aVal = myIDs.find(theID);
25   if (aVal == myIDs.end()) {
26     Events_Error::send(
27       string("Validator ") + theID + " for feature " + theFeatureID + " was not registered");
28     isError = true;
29   }
30   if (myFeatures.find(theFeatureID) != myFeatures.end()) {
31       Events_Error::send(
32         string("Validator for feature ") + theFeatureID + " is already registered");
33     isError = true;
34   }
35   if (!isError)
36     myFeatures[theFeatureID] = aVal->second;
37 }
38
39 void Model_ValidatorsFactory::assignValidator(const string& theID, 
40   const string& theFeatureID, const string& theAttrID, const list<string>& theArguments)
41 {
42   bool isError = false;
43   map<string, ModelAPI_Validator*>::iterator aVal = myIDs.find(theID);
44   if (aVal == myIDs.end()) {
45     Events_Error::send(
46       string("Validator ") + theID + " for feature " + theFeatureID + " was not registered");
47     isError = true;
48   }
49   // create feature-structures if not exist
50   map<string, map<string, pair<ModelAPI_Validator*, list<string> > > >::iterator
51     aFeature = myAttrs.find(theFeatureID);
52   if (aFeature == myAttrs.end()) {
53     myAttrs[theFeatureID] = map<string, pair<ModelAPI_Validator*, list<string> > >();
54     aFeature = myAttrs.find(theFeatureID);
55   }
56   // add attr-structure if not exist, or generate error if already exist
57   map<string, pair<ModelAPI_Validator*, list<string> > >::iterator 
58     anAttr = aFeature->second.find(theAttrID);
59   if (anAttr == aFeature->second.end()) {
60     if (!isError) {
61       aFeature->second[theAttrID] = 
62         pair<ModelAPI_Validator*, list<string> >(aVal->second, theArguments);
63     }
64   } else {
65     Events_Error::send(
66       string("Validator ") + theID + " for feature " + theFeatureID +
67               "attribute " + theAttrID + " is already registered");
68     isError = true;
69   }
70 }
71
72 const ModelAPI_Validator* Model_ValidatorsFactory::validator(const string& theFeatureID) const
73 {
74   map<string, ModelAPI_Validator*>::const_iterator aFeature = myFeatures.find(theFeatureID);
75   if (aFeature != myFeatures.cend())
76     return aFeature->second;
77   return NULL; // not found
78 }
79
80 /*bool Model_ValidatorsFactory::validate(
81   const boost::shared_ptr<ModelAPI_Feature>& theFeature, const string& theAttrID ) const
82 {
83   map<string, map<string, pair<ModelAPI_Validator*, list<string> > > >::const_iterator
84     aFeature = myAttrs.find(theFeature->getKind());
85   if (aFeature == myAttrs.cend()) return true; // feature is not found
86   map<string, pair<ModelAPI_Validator*, list<string> > >::const_iterator 
87     anAttr = aFeature->second.find(theAttrID);
88   if (anAttr == aFeature->second.cend()) return true; // attribute is not found
89   return anAttr->second.first->validate(theFeature, theAttrID, anAttr->second.second);
90 }*/
91
92 Model_ValidatorsFactory::Model_ValidatorsFactory() : ModelAPI_ValidatorsFactory()
93 {
94 }