Model_AttributeBoolean.h
Model_Events.h
Model_Update.h
+ Model_Validator.h
)
SET(PROJECT_SOURCES
Model_AttributeBoolean.cpp
Model_Events.cpp
Model_Update.cpp
+ Model_Validator.cpp
)
SET(PROJECT_LIBRARIES
#include <Model_Document.h>
#include <Model_Application.h>
#include <Model_Events.h>
+#include <Model_Validator.h>
#include <Events_Loop.h>
#include <Events_Error.h>
#include <Config_FeatureMessage.h>
{
myPluginsInfoLoaded = false;
myCheckTransactions = true;
- ModelAPI_PluginManager::SetPluginManager(boost::shared_ptr<ModelAPI_PluginManager>(this));
+ ModelAPI_PluginManager::setPluginManager(boost::shared_ptr<ModelAPI_PluginManager>(this));
// register the configuration reading listener
Events_Loop* aLoop = Events_Loop::loop();
static Events_ID FeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
{
myPluginObjs[myCurrentPluginName] = thePlugin;
}
+
+ModelAPI_ValidatorsFactory* Model_PluginManager::validators()
+{
+ static Model_ValidatorsFactory* aFactory = new Model_ValidatorsFactory;
+ return aFactory;
+}
MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_Document> copy(
boost::shared_ptr<ModelAPI_Document> theSource, std::string theID);
+ /// Returns the validators factory: the only one instance per application
+ MODEL_EXPORT virtual ModelAPI_ValidatorsFactory* validators();
+
void setCheckTransactions(const bool theCheck) {myCheckTransactions = theCheck;}
/// Is called only once, on startup of the application
--- /dev/null
+// File: Model_Validator.cpp
+// Created: 2 Jul 2014
+// Author: Mikhail PONIKAROV
+
+#include <Model_Validator.h>
+#include <ModelAPI_Feature.h>
+#include <Events_Error.h>
+
+using namespace std;
+
+void Model_ValidatorsFactory::registerValidator(
+ const string& theID, ModelAPI_Validator* theValidator)
+{
+ if (myIDs.find(theID) != myIDs.end()) {
+ Events_Error::send(string("Validator ") + theID + " is already registered");
+ } else {
+ myIDs[theID] = theValidator;
+ }
+}
+
+void Model_ValidatorsFactory::assignValidator(const string& theID, const string& theFeatureID)
+{
+ bool isError = false;
+ map<string, ModelAPI_Validator*>::iterator aVal = myIDs.find(theID);
+ if (aVal == myIDs.end()) {
+ Events_Error::send(
+ string("Validator ") + theID + " for feature " + theFeatureID + " was not registered");
+ isError = true;
+ }
+ if (myFeatures.find(theFeatureID) != myFeatures.end()) {
+ Events_Error::send(
+ string("Validator for feature ") + theFeatureID + " is already registered");
+ isError = true;
+ }
+ if (!isError)
+ myFeatures[theFeatureID] = aVal->second;
+}
+
+void Model_ValidatorsFactory::assignValidator(const string& theID,
+ const string& theFeatureID, const string& theAttrID, const list<string>& theArguments)
+{
+ bool isError = false;
+ map<string, ModelAPI_Validator*>::iterator aVal = myIDs.find(theID);
+ if (aVal == myIDs.end()) {
+ Events_Error::send(
+ string("Validator ") + theID + " for feature " + theFeatureID + " was not registered");
+ isError = true;
+ }
+ // create feature-structures if not exist
+ map<string, map<string, pair<ModelAPI_Validator*, list<string> > > >::iterator
+ aFeature = myAttrs.find(theFeatureID);
+ if (aFeature == myAttrs.end()) {
+ myAttrs[theFeatureID] = map<string, pair<ModelAPI_Validator*, list<string> > >();
+ aFeature = myAttrs.find(theFeatureID);
+ }
+ // add attr-structure if not exist, or generate error if already exist
+ map<string, pair<ModelAPI_Validator*, list<string> > >::iterator
+ anAttr = aFeature->second.find(theAttrID);
+ if (anAttr == aFeature->second.end()) {
+ if (!isError) {
+ aFeature->second[theAttrID] =
+ pair<ModelAPI_Validator*, list<string> >(aVal->second, theArguments);
+ }
+ } else {
+ Events_Error::send(
+ string("Validator ") + theID + " for feature " + theFeatureID +
+ "attribute " + theAttrID + " is already registered");
+ isError = true;
+ }
+}
+
+const ModelAPI_Validator* Model_ValidatorsFactory::validator(const string& theFeatureID) const
+{
+ map<string, ModelAPI_Validator*>::const_iterator aFeature = myFeatures.find(theFeatureID);
+ if (aFeature != myFeatures.cend())
+ return aFeature->second;
+ return NULL; // not found
+}
+
+bool Model_ValidatorsFactory::validate(
+ const boost::shared_ptr<ModelAPI_Feature>& theFeature, const string& theAttrID ) const
+{
+ map<string, map<string, pair<ModelAPI_Validator*, list<string> > > >::const_iterator
+ aFeature = myAttrs.find(theFeature->getKind());
+ if (aFeature == myAttrs.cend()) return true; // feature is not found
+ map<string, pair<ModelAPI_Validator*, list<string> > >::const_iterator
+ anAttr = aFeature->second.find(theAttrID);
+ if (anAttr == aFeature->second.cend()) return true; // attribute is not found
+ return anAttr->second.first->validate(theFeature, theAttrID, anAttr->second.second);
+}
+
+Model_ValidatorsFactory::Model_ValidatorsFactory() : ModelAPI_ValidatorsFactory()
+{
+}
--- /dev/null
+// File: Model_Validator.hxx
+// Created: 2 Jul 2014
+// Author: Mikhail PONIKAROV
+
+#ifndef Model_Validator_HeaderFile
+#define Model_Validator_HeaderFile
+
+#include <Model.h>
+#include <ModelAPI_Validator.h>
+#include <map>
+
+/**\class Model_ValidatorsFactory
+ * \ingroup DataModel
+ * \breif Manages the registered validators
+ *
+ * Allows to get a validator by the feature identifier and
+ * the attribute identifier (if attribute is validated).
+ * All accessible validators mustbe registered by the ID string first.
+ * The instance of this factory can be get in the PluginManager.
+ * Keeps the validator objects alive and just returns one of it by request.
+ * All the needed information is provided to the validator as an argument,
+ * this allows to work with them independently from the feature specific object.
+ */
+class Model_ValidatorsFactory: public ModelAPI_ValidatorsFactory
+{
+ std::map<std::string, ModelAPI_Validator*> myIDs; ///< map from ID to registered validator
+ std::map<std::string, ModelAPI_Validator*> myFeatures; ///< validators by feature ID
+ std::map<std::string, std::map<std::string, std::pair<ModelAPI_Validator*,
+ std::list<std::string> > > > myAttrs; ///< validators and arguments by feature and attribute IDs
+public:
+ /// Registers the instance of the validator by the ID
+ MODEL_EXPORT virtual void registerValidator(
+ const std::string& theID, ModelAPI_Validator* theValidator);
+
+ /// Assigns validator to the feature
+ MODEL_EXPORT virtual void assignValidator(
+ const std::string& theID, const std::string& theFeatureID);
+
+ /// Assigns validator to the attribute of the feature
+ MODEL_EXPORT virtual void assignValidator(const std::string& theID,
+ const std::string& theFeatureID, const std::string& theAttrID,
+ const std::list<std::string>& theArguments);
+
+ /// Provides a validator for the feature, returns NULL if no validator
+ MODEL_EXPORT virtual const ModelAPI_Validator* validator(const std::string& theFeatureID) const;
+
+ /// Returns the result of "validate" method for attribute of validator.
+ /// If validator is not exists, returns true: everything is valid by default.
+ MODEL_EXPORT virtual bool validate(
+ const boost::shared_ptr<ModelAPI_Feature>& theFeature, const std::string& theAttrID) const;
+
+protected:
+ /// Get instance from PluginManager
+ Model_ValidatorsFactory();
+
+ friend class Model_PluginManager;
+};
+
+#endif
ModelAPI_AttributeRefList.h
ModelAPI_AttributeBoolean.h
ModelAPI_Events.h
+ ModelAPI_Validator.h
)
SET(PROJECT_SOURCES
#include "ModelAPI_AttributeDouble.h"
#include "ModelAPI_AttributeReference.h"
#include "ModelAPI_AttributeRefAttr.h"
+ #include "ModelAPI_Validator.h"
%}
// to avoid error on this
%include "ModelAPI_AttributeDouble.h"
%include "ModelAPI_AttributeReference.h"
%include "ModelAPI_AttributeRefAttr.h"
+%include "ModelAPI_Validator.h"
\ No newline at end of file
#include <ModelAPI_AttributeRefAttr.h>
#include <ModelAPI_AttributeRefList.h>
#include <ModelAPI_Events.h>
+#include <ModelAPI_Validator.h>
#include <Config_ModuleReader.h>
{
}
-void ModelAPI_PluginManager::SetPluginManager(
+void ModelAPI_PluginManager::setPluginManager(
boost::shared_ptr<ModelAPI_PluginManager> theManager)
{
MY_MANAGER = theManager;
class ModelAPI_Feature;
class ModelAPI_Plugin;
class ModelAPI_Document;
+class ModelAPI_ValidatorsFactory;
/**\class ModelAPI_PluginManager
* \ingroup DataModel
virtual boost::shared_ptr<ModelAPI_Document> copy(
boost::shared_ptr<ModelAPI_Document> theSource, std::string theID) = 0;
+ /// Returns the validators factory: the only one instance per application
+ virtual ModelAPI_ValidatorsFactory* validators() = 0;
+
/// Is needed for python wrapping by swig, call Get to get an instance
ModelAPI_PluginManager();
/// Creates the feature object using plugins functionality
virtual boost::shared_ptr<ModelAPI_Feature> createFeature(std::string theFeatureID) = 0;
- static void SetPluginManager(boost::shared_ptr<ModelAPI_PluginManager> theManager);
+ static void setPluginManager(boost::shared_ptr<ModelAPI_PluginManager> theManager);
friend class Model_Document;
};
typedef boost::shared_ptr<ModelAPI_PluginManager> PluginManagerPtr;
-
#endif
--- /dev/null
+// File: ModelAPI_Validator.hxx
+// Created: 2 Jul 2014
+// Author: Mikhail PONIKAROV
+
+#ifndef ModelAPI_Validator_HeaderFile
+#define ModelAPI_Validator_HeaderFile
+
+#include <ModelAPI.h>
+#include <boost/shared_ptr.hpp>
+#include <list>
+
+class ModelAPI_Feature;
+
+/**\class ModelAPI_Validator
+ * \ingroup DataModel
+ * \brief Allows to validate the attribute value of a feature or the whole feature.
+ *
+ * This object is assigned by the name
+ * in the XML file to the specific attribute or to the whole feature.
+ * If validator returns "false", it is signalized in user interface
+ * and feature is not executed.
+ * Validators must be registered in the validators factory to be
+ * correctly identified by the XML string-ID.
+ */
+class MODELAPI_EXPORT ModelAPI_Validator
+{
+public:
+ /// Returns true if feature and/or attributes are valid
+ /// \param theFeature the validated feature
+ /// \param theAttr the validated attribute ID, empty string of feature is validated
+ /// \param theArguments list of string, feature attribute names: dependent attributes
+ virtual bool validate(const boost::shared_ptr<ModelAPI_Feature>& theFeature,
+ const std::string theAttr, std::list<std::string> theArguments) const = 0;
+};
+
+typedef boost::shared_ptr<ModelAPI_Validator> ValidatorPtr;
+
+/**\class ModelAPI_ValidatorsFactory
+ * \ingroup DataModel
+ * \breif Manages the registered validators
+ *
+ * Allows to get a validator by the feature identifier and
+ * the attribute identifier (if attribute is validated).
+ * All accessible validators mustbe registered by the ID string first.
+ * The instance of this factory can be get in the PluginManager.
+ * Keeps the validator objects alive and just returns one of it by request.
+ * All the needed information is provided to the validator as an argument,
+ * this allows to work with them independently from the feature specific object.
+ */
+class MODELAPI_EXPORT ModelAPI_ValidatorsFactory
+{
+public:
+ /// Registers the instance of the validator by the ID
+ virtual void registerValidator(const std::string& theID, ModelAPI_Validator* theValidator) = 0;
+
+ /// Assigns validator to the feature
+ virtual void assignValidator(const std::string& theID, const std::string& theFeatureID) = 0;
+
+ /// Assigns validator to the attribute of the feature
+ virtual void assignValidator(const std::string& theID,
+ const std::string& theFeatureID, const std::string& theAttrID,
+ const std::list<std::string>& theArguments) = 0;
+
+ /// Provides a validator for the feature, returns NULL if no validator
+ virtual const ModelAPI_Validator* validator(const std::string& theFeatureID) const = 0;
+
+ /// Returns the result of "validate" method for attribute of validator.
+ /// If validator is not exists, returns true: everything is valid by default.
+ virtual bool validate(
+ const boost::shared_ptr<ModelAPI_Feature>& theFeature, const std::string& theAttrID) const = 0;
+
+protected:
+ /// Get instance from PluginManager
+ ModelAPI_ValidatorsFactory() {}
+};
+
+#endif