]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_Update.cpp
Salome HOME
Helper methods, aliases for data()->method()
[modules/shaper.git] / src / Model / Model_Update.cpp
1 // File:        Model_Update.cxx
2 // Created:     25 Jun 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Model_Update.h>
6 #include <Model_Document.h>
7 #include <Model_Data.h>
8 #include <ModelAPI_Feature.h>
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_Events.h>
12 #include <ModelAPI_AttributeReference.h>
13 #include <ModelAPI_AttributeRefList.h>
14 #include <ModelAPI_AttributeRefAttr.h>
15 #include <ModelAPI_AttributeSelection.h>
16 #include <ModelAPI_AttributeSelectionList.h>
17 #include <ModelAPI_Result.h>
18 #include <ModelAPI_Validator.h>
19 #include <ModelAPI_CompositeFeature.h>
20 #include <ModelAPI_Session.h>
21 #include <Events_Loop.h>
22 #include <Events_LongOp.h>
23 #include <Events_Error.h>
24 #include <Config_PropManager.h>
25
26 using namespace std;
27
28 Model_Update MY_UPDATER_INSTANCE;  /// the only one instance initialized on load of the library
29
30 Model_Update::Model_Update()
31 {
32   Events_Loop* aLoop = Events_Loop::loop();
33   static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
34   aLoop->registerListener(this, kChangedEvent);
35   static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
36   aLoop->registerListener(this, kRebuildEvent);
37   static const Events_ID kCreatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_CREATED);
38   aLoop->registerListener(this, kCreatedEvent);
39   static const Events_ID kUpdatedEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_UPDATED);
40   aLoop->registerListener(this, kUpdatedEvent);
41   static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
42   aLoop->registerListener(this, kOpFinishEvent);
43   static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
44   aLoop->registerListener(this, kOpAbortEvent);
45
46   Config_PropManager::registerProp("Model update", "automatic_rebuild", "Rebuild automatically",
47                                    Config_Prop::Bool, "false");
48   isAutomatic = Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
49 }
50
51 void Model_Update::processEvent(const boost::shared_ptr<Events_Message>& theMessage)
52 {
53   static Events_Loop* aLoop = Events_Loop::loop();
54   static const Events_ID kChangedEvent = aLoop->eventByName("PreferenceChanged");
55   static const Events_ID kRebuildEvent = aLoop->eventByName("Rebuild");
56   static const Events_ID kCreatedEvent = aLoop->eventByName(EVENT_OBJECT_CREATED);
57   static const Events_ID kUpdatedEvent = aLoop->eventByName(EVENT_OBJECT_UPDATED);
58   static const Events_ID kOpFinishEvent = aLoop->eventByName("FinishOperation");
59   static const Events_ID kOpAbortEvent = aLoop->eventByName("AbortOperation");
60   bool isAutomaticChanged = false;
61   if (theMessage->eventID() == kChangedEvent) { // automatic and manual rebuild flag is changed
62     isAutomatic = 
63       Config_PropManager::findProp("Model update", "automatic_rebuild")->value() == "true";
64   } else if (theMessage->eventID() == kRebuildEvent) { // the rebuild command
65     if (isAutomatic == false) {
66       isAutomaticChanged = true;
67       isAutomatic = true;
68     }
69   } else if (theMessage->eventID() == kCreatedEvent || theMessage->eventID() == kUpdatedEvent) {
70     boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
71         boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
72     const std::set<ObjectPtr>& anObjs = aMsg->objects();
73     std::set<ObjectPtr>::const_iterator anObjIter = anObjs.cbegin();
74     for(; anObjIter != anObjs.cend(); anObjIter++)
75       myJustCreatedOrUpdated.insert(*anObjIter);
76   } else if (theMessage->eventID() == kOpFinishEvent || theMessage->eventID() == kOpAbortEvent) {
77     myJustCreatedOrUpdated.clear();
78     return;
79   }
80
81   if (isExecuted)
82     return;  // nothing to do: it is executed now
83
84   //Events_LongOp::start(this);
85   isExecuted = true;
86   list<boost::shared_ptr<ModelAPI_Document> > aDocs;
87   boost::shared_ptr<ModelAPI_ObjectUpdatedMessage> aMsg =
88       boost::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
89   if (aMsg) myInitial = aMsg->objects();
90   else {
91     myInitial.clear();
92     // on change flag all documents must be updated
93     if (isAutomatic) {
94       aDocs = ModelAPI_Session::get()->allOpenedDocuments();
95     }
96   }
97   // collect all documents involved into the update process
98   set<boost::shared_ptr<ModelAPI_Object> >::iterator aFIter = myInitial.begin();
99   for (; aFIter != myInitial.end(); aFIter++) {
100     aDocs.push_back((*aFIter)->document());
101   }
102   // iterate all features of features-documents to update them (including hidden)
103   list<boost::shared_ptr<ModelAPI_Document> >::iterator aDIter = aDocs.begin();
104   for (; aDIter != aDocs.end(); aDIter++) {
105     int aNbFeatures = (*aDIter)->size(ModelAPI_Feature::group(), true);
106     for (int aFIndex = 0; aFIndex < aNbFeatures; aFIndex++) {
107       FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(
108           (*aDIter)->object(ModelAPI_Feature::group(), aFIndex, true));
109       if (aFeature)
110         updateFeature(aFeature);
111     }
112   }
113   myUpdated.clear();
114   // flush
115   static Events_ID EVENT_DISP = aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY);
116   aLoop->flush(EVENT_DISP);
117   //Events_LongOp::end(this);
118   if (isAutomaticChanged) isAutomatic = false;
119   isExecuted = false;
120 }
121
122 bool Model_Update::updateFeature(FeaturePtr theFeature)
123 {
124   // check it is already processed
125   if (myUpdated.find(theFeature) != myUpdated.end())
126     return myUpdated[theFeature];
127   // check all features this feature depended on (recursive call of updateFeature)
128   bool aMustbeUpdated = myInitial.find(theFeature) != myInitial.end();
129   if (theFeature) {  // only real feature contains references to other objects
130     if (theFeature->data()->mustBeUpdated()) aMustbeUpdated = true;
131
132     // composite feature must be executed after sub-features execution
133     CompositeFeaturePtr aComposite = 
134       boost::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
135     if (aComposite) {
136       int aSubsNum = aComposite->numberOfSubs();
137       for(int a = 0; a < aSubsNum; a++) {
138         if (updateFeature(aComposite->subFeature(a)))
139           aMustbeUpdated = true;
140       }
141     }
142     // check all references: if referenced objects are updated, this object also must be updated
143     std::list<std::pair<std::string, std::list<ObjectPtr> > > aRefs;
144     boost::shared_ptr<Model_Data> aData = 
145       boost::dynamic_pointer_cast<Model_Data>(theFeature->data());
146     aData->referencesToObjects(aRefs);
147     std::list<std::pair<std::string, std::list<ObjectPtr> > >::iterator aRef = aRefs.begin();
148     for(; aRef != aRefs.end(); aRef++) {
149       std::list<ObjectPtr>::iterator aRefObj = aRef->second.begin();
150       for(; aRefObj != aRef->second.end(); aRefObj++) {
151         if (updateObject(*aRefObj)) {
152           aMustbeUpdated = true;
153         }
154       }
155     }
156
157     // execute feature if it must be updated
158     if (aMustbeUpdated) {
159
160       if (boost::dynamic_pointer_cast<Model_Document>(theFeature->document())->executeFeatures() ||
161           !theFeature->isPersistentResult()) {
162         ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
163         if (aFactory->validate(theFeature)) {
164           if (isAutomatic || (myJustCreatedOrUpdated.find(theFeature) != myJustCreatedOrUpdated.end()) ||
165             !theFeature->isPersistentResult() /* execute quick, not persistent results */) 
166           {
167             // before execution update the selection attributes if any
168             list<AttributePtr> aRefs = 
169               theFeature->data()->attributes(ModelAPI_AttributeSelection::type());
170             list<AttributePtr>::iterator aRefsIter = aRefs.begin();
171             for (; aRefsIter != aRefs.end(); aRefsIter++) {
172               boost::shared_ptr<ModelAPI_AttributeSelection> aSel =
173                 boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>(*aRefsIter);
174               aSel->update(); // this must be done on execution since it may be long operation
175             }
176             aRefs = theFeature->data()->attributes(ModelAPI_AttributeSelectionList::type());
177             for (aRefsIter = aRefs.begin(); aRefsIter != aRefs.end(); aRefsIter++) {
178               boost::shared_ptr<ModelAPI_AttributeSelectionList> aSel =
179                 boost::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(*aRefsIter);
180               for(int a = aSel->size() - 1; a >= 0; a--) {
181                 aSel->value(a)->update();
182               }
183             }
184             // execute in try-catch to avoid internal problems of the feature
185             try {
186               theFeature->execute();
187             } catch(...) {
188               Events_Error::send(
189                 "Feature " + theFeature->getKind() + " has failed during the execution");
190               theFeature->eraseResults();
191             }
192             theFeature->data()->mustBeUpdated(false);
193             const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
194             std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
195             for (; aRIter != aResults.cend(); aRIter++) {
196               boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
197               aRes->data()->mustBeUpdated(false);
198             }
199           } else {
200             theFeature->data()->mustBeUpdated(true);
201             const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
202             std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
203             for (; aRIter != aResults.cend(); aRIter++) {
204               boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
205               aRes->data()->mustBeUpdated(true);
206             }
207             aMustbeUpdated = false;
208           }
209         } else {
210           theFeature->eraseResults();
211         }
212       }
213       // redisplay all results
214       static Events_ID EVENT_DISP = Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY);
215       const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
216       std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
217       for (; aRIter != aResults.cend(); aRIter++) {
218         boost::shared_ptr<ModelAPI_Result> aRes = *aRIter;
219         myUpdated[aRes] = true;
220         ModelAPI_EventCreator::get()->sendUpdated(aRes, EVENT_DISP);
221       }
222       // to redisplay "presentable" feature (for ex. distance constraint)
223       ModelAPI_EventCreator::get()->sendUpdated(theFeature, EVENT_DISP);
224     } else {  // returns also true is results were updated: for sketch that refers to sub-features but results of sub-features were changed
225       const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = theFeature->results();
226       std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRIter = aResults.begin();
227       for (; aRIter != aResults.cend(); aRIter++) {
228         if (myInitial.find(*aRIter) != myInitial.end()) {
229           aMustbeUpdated = true;
230           break;
231         }
232       }
233     }
234   }
235   myUpdated[theFeature] = aMustbeUpdated;
236   return aMustbeUpdated;
237 }
238
239 bool Model_Update::updateObject(boost::shared_ptr<ModelAPI_Object> theObject)
240 {
241   if (myUpdated.find(theObject) != myUpdated.end())
242     return myUpdated[theObject];  // already processed
243   return myInitial.find(theObject) != myInitial.end();
244
245   /* remove algorithm for update of all features by dependencies tree
246   if (!theObject)
247     return false;
248   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
249   if (aFeature) {  // for feature just call update Feature
250     return updateFeature(aFeature);
251   }
252   // check general object, possible just a result
253   if (myUpdated.find(theObject) != myUpdated.end())
254     return myUpdated[theObject];  // already processed
255   // check the feature of this object must be executed
256   ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
257   if (aResult) {
258     FeaturePtr aResFeature = aResult->document()->feature(aResult);
259     if (aResFeature) {
260       return updateFeature(aResFeature);
261     }
262   }
263   if (myInitial.find(theObject) != myInitial.end())
264     return true;
265   return false;  // nothing is known
266   */
267 }