1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
|
/*=========================================================================
Program: ParaView
Module: vtkSMStateLoader.cxx
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkSMStateLoader.h"
#include "vtkObjectFactory.h"
#include "vtkPVInstantiator.h"
#include "vtkPVXMLElement.h"
#include "vtkSMProperty.h"
#include "vtkSMPropertyLink.h"
#include "vtkSMProxyIterator.h"
#include "vtkSMProxyLink.h"
#include "vtkSMProxyLocator.h"
#include "vtkSMProxyManager.h"
#include "vtkSMSession.h"
#include "vtkSMSessionProxyManager.h"
#include "vtkSMSourceProxy.h"
#include "vtkSMStateVersionController.h"
#include "vtkSmartPointer.h"
#include <cassert>
#include <cstdlib>
#include <vector>
vtkObjectFactoryNewMacro(vtkSMStateLoader);
vtkCxxSetObjectMacro(vtkSMStateLoader, ProxyLocator, vtkSMProxyLocator);
//---------------------------------------------------------------------------
struct vtkSMStateLoaderRegistrationInfo
{
std::string GroupName;
std::string ProxyName;
};
struct vtkSMStateLoaderInternals
{
bool KeepOriginalId;
typedef std::vector<vtkSMStateLoaderRegistrationInfo> VectorOfRegInfo;
typedef std::map<int, VectorOfRegInfo> RegInfoMapType;
RegInfoMapType RegistrationInformation;
std::vector<vtkTypeUInt32> AlignedMappingIdTable;
/// Vector filled up in CreatedNewProxy() to keep order in which the proxies
/// are created.
typedef std::pair<vtkTypeUInt32, vtkWeakPointer<vtkSMProxy> > ProxyCreationOrderItem;
typedef std::vector<ProxyCreationOrderItem> ProxyCreationOrderType;
ProxyCreationOrderType ProxyCreationOrder;
bool DeferProxyRegistration;
vtkSMStateLoaderInternals()
: KeepOriginalId(false)
, DeferProxyRegistration(false)
{
}
};
//---------------------------------------------------------------------------
vtkSMStateLoader::vtkSMStateLoader()
{
this->Internal = new vtkSMStateLoaderInternals;
this->ServerManagerStateElement = 0;
this->KeepIdMapping = 0;
this->ProxyLocator = vtkSMProxyLocator::New();
}
//---------------------------------------------------------------------------
vtkSMStateLoader::~vtkSMStateLoader()
{
this->SetProxyLocator(0);
this->ServerManagerStateElement = 0;
this->ProxyLocator = 0;
delete this->Internal;
}
//-----------------------------------------------------------------------------
vtkSMProxy* vtkSMStateLoader::LocateExistingProxyUsingRegistrationName(vtkTypeUInt32 id)
{
vtkSMStateLoaderInternals::RegInfoMapType::iterator iter =
this->Internal->RegistrationInformation.find(id);
if (iter == this->Internal->RegistrationInformation.end())
{
return NULL;
}
vtkSMSessionProxyManager* pxm = this->GetSessionProxyManager();
assert(pxm != NULL);
vtkSMStateLoaderInternals::VectorOfRegInfo::iterator iter2;
for (iter2 = iter->second.begin(); iter2 != iter->second.end(); iter2++)
{
vtkSMProxy* proxy = pxm->GetProxy(iter2->GroupName.c_str(), iter2->ProxyName.c_str());
if (proxy)
{
return proxy;
}
}
return NULL;
}
//-----------------------------------------------------------------------------
vtkSMProxy* vtkSMStateLoader::CreateProxy(
const char* xml_group, const char* xml_name, const char* subProxyName)
{
vtkSMSessionProxyManager* pxm = this->GetSessionProxyManager();
assert(pxm != NULL);
//**************************************************************************
// This is temporary code until we clean up time-keeper and animation scene
// interactions. There needs to be some rework with the management of
// time-keeper, making it a SM-behavior perhaps. Until that happens, I am
// letting this piece of code be which ensures that there's only open
// time-keeper and animation scene in the application.
if (xml_group && xml_name && strcmp(xml_group, "animation") == 0 &&
strcmp(xml_name, "AnimationScene") == 0)
{
// If an animation scene already exists, we use that.
vtkSMProxy* scene = pxm->FindProxy("animation", "animation", "AnimationScene");
if (scene)
{
scene->Register(this);
return scene;
}
}
else if (xml_group && xml_name && strcmp(xml_group, "animation") == 0 &&
strcmp(xml_name, "TimeAnimationCue") == 0)
{
// If an animation cue already exists, we use that.
vtkSMProxy* cue = pxm->FindProxy("animation", "animation", "TimeAnimationCue");
if (cue)
{
cue->Register(this);
return cue;
}
}
else if (xml_group && xml_name && strcmp(xml_group, "misc") == 0 &&
strcmp(xml_name, "TimeKeeper") == 0)
{
// There is only one time keeper per connection, simply
// load the state on the timekeeper.
vtkSMProxy* timekeeper = pxm->FindProxy("timekeeper", xml_group, xml_name);
if (timekeeper)
{
timekeeper->Register(this);
return timekeeper;
}
}
//**************************************************************************
// If all else fails, let the superclass handle it:
return this->Superclass::CreateProxy(xml_group, xml_name, subProxyName);
}
//---------------------------------------------------------------------------
void vtkSMStateLoader::CreatedNewProxy(vtkTypeUInt32 id, vtkSMProxy* proxy)
{
// Ensure that the proxy is created before it is registered, unless we are
// reviving the server-side server manager, which needs special handling.
if (this->Internal->KeepOriginalId)
{
proxy->SetGlobalID(id);
}
// Calling UpdateVTKObjects() will assign the proxy a GlobalId, if needed.
proxy->UpdateVTKObjects();
if (proxy->IsA("vtkSMSourceProxy"))
{
vtkSMSourceProxy::SafeDownCast(proxy)->UpdatePipelineInformation();
}
if (this->Internal->DeferProxyRegistration)
{
this->Internal->ProxyCreationOrder.push_back(
vtkSMStateLoaderInternals::ProxyCreationOrderItem(id, proxy));
}
else
{
this->RegisterProxy(id, proxy);
}
}
//---------------------------------------------------------------------------
void vtkSMStateLoader::RegisterProxy(vtkTypeUInt32 id, vtkSMProxy* proxy)
{
vtkSMStateLoaderInternals::RegInfoMapType::iterator iter =
this->Internal->RegistrationInformation.find(id);
if (iter == this->Internal->RegistrationInformation.end())
{
return;
}
vtkSMStateLoaderInternals::VectorOfRegInfo::iterator iter2;
for (iter2 = iter->second.begin(); iter2 != iter->second.end(); iter2++)
{
this->RegisterProxyInternal(iter2->GroupName.c_str(), iter2->ProxyName.c_str(), proxy);
}
}
//---------------------------------------------------------------------------
void vtkSMStateLoader::RegisterProxyInternal(
const char* cgroup, const char* cname, vtkSMProxy* proxy)
{
assert(cgroup != NULL && cname != NULL);
std::string group(cgroup);
std::string name(cname);
if (this->UpdateRegistrationInfo(group, name, proxy))
{
vtkSMSessionProxyManager* pxm = this->GetSessionProxyManager();
assert(pxm != NULL);
if (pxm->GetProxyName(group.c_str(), proxy))
{
// Don't re-register a proxy in the same group.
return;
}
pxm->RegisterProxy(group.c_str(), name.c_str(), proxy);
}
}
//---------------------------------------------------------------------------
bool vtkSMStateLoader::UpdateRegistrationInfo(
std::string& group, std::string& vtkNotUsed(name), vtkSMProxy* vtkNotUsed(proxy))
{
static const char* helper_proxies_prefix = "pq_helper_proxies.";
static size_t len = strlen(helper_proxies_prefix);
if (group.compare(0, len, helper_proxies_prefix) == 0)
{
// a helper proxy groupname, must update it.
std::string pid = group.substr(len);
vtkTypeUInt32 gid = static_cast<vtkTypeUInt32>(std::atoi(pid.c_str()));
if (vtkSMProxy* helpedProxy = this->ProxyLocator->LocateProxy(gid))
{
group = helper_proxies_prefix;
group += helpedProxy->GetGlobalIDAsString();
}
}
return true;
}
//---------------------------------------------------------------------------
vtkPVXMLElement* vtkSMStateLoader::LocateProxyElement(vtkTypeUInt32 id)
{
return this->LocateProxyElementInternal(this->ServerManagerStateElement, id);
}
//---------------------------------------------------------------------------
vtkPVXMLElement* vtkSMStateLoader::LocateProxyElementInternal(
vtkPVXMLElement* root, vtkTypeUInt32 id_)
{
if (!root)
{
vtkErrorMacro("No root is defined. Cannot locate proxy element with id " << id_);
return 0;
}
vtkIdType id = static_cast<vtkIdType>(id_);
unsigned int numElems = root->GetNumberOfNestedElements();
unsigned int i = 0;
for (i = 0; i < numElems; i++)
{
vtkPVXMLElement* currentElement = root->GetNestedElement(i);
if (currentElement->GetName() && strcmp(currentElement->GetName(), "Proxy") == 0)
{
vtkIdType currentId;
if (!currentElement->GetScalarAttribute("id", ¤tId))
{
continue;
}
if (id != currentId)
{
continue;
}
return currentElement;
}
}
// If proxy was not found on root level, go into nested elements
for (i = 0; i < numElems; i++)
{
vtkPVXMLElement* currentElement = root->GetNestedElement(i);
vtkPVXMLElement* res = this->LocateProxyElementInternal(currentElement, id);
if (res)
{
return res;
}
}
return 0;
}
//---------------------------------------------------------------------------
int vtkSMStateLoader::BuildProxyCollectionInformation(vtkPVXMLElement* collectionElement)
{
const char* groupName = collectionElement->GetAttribute("name");
if (!groupName)
{
vtkErrorMacro("Required attribute name is missing.");
return 0;
}
unsigned int numElems = collectionElement->GetNumberOfNestedElements();
for (unsigned int i = 0; i < numElems; i++)
{
vtkPVXMLElement* currentElement = collectionElement->GetNestedElement(i);
if (currentElement->GetName() && strcmp(currentElement->GetName(), "Item") == 0)
{
int id;
if (!currentElement->GetScalarAttribute("id", &id))
{
vtkErrorMacro("Could not read id for Item. Skipping.");
continue;
}
const char* name = currentElement->GetAttribute("name");
if (!name)
{
vtkErrorMacro("Attribute: name is missing. Cannot register proxy "
"with the proxy manager.");
continue;
}
vtkSMStateLoaderRegistrationInfo info;
info.GroupName = groupName;
info.ProxyName = name;
this->Internal->RegistrationInformation[id].push_back(info);
}
}
return 1;
}
//---------------------------------------------------------------------------
int vtkSMStateLoader::HandleProxyCollection(vtkPVXMLElement* collectionElement)
{
const char* groupName = collectionElement->GetAttribute("name");
if (!groupName)
{
vtkErrorMacro("Required attribute name is missing.");
return 0;
}
unsigned int numElems = collectionElement->GetNumberOfNestedElements();
for (unsigned int i = 0; i < numElems; i++)
{
vtkPVXMLElement* currentElement = collectionElement->GetNestedElement(i);
if (currentElement->GetName() && strcmp(currentElement->GetName(), "Item") == 0)
{
int id;
if (!currentElement->GetScalarAttribute("id", &id))
{
vtkErrorMacro("Could not read id for Item. Skipping.");
continue;
}
vtkSMProxy* proxy = this->ProxyLocator->LocateProxy(id);
if (!proxy)
{
continue;
}
const char* name = currentElement->GetAttribute("name");
if (!name)
{
vtkErrorMacro("Attribute: name is missing. Cannot register proxy "
"with the proxy manager.");
proxy->Delete();
continue;
}
}
}
return 1;
}
//---------------------------------------------------------------------------
void vtkSMStateLoader::HandleCustomProxyDefinitions(vtkPVXMLElement* element)
{
vtkSMSessionProxyManager* pm = this->GetSessionProxyManager();
assert(pm != NULL);
pm->LoadCustomProxyDefinitions(element);
}
//---------------------------------------------------------------------------
int vtkSMStateLoader::HandleLinks(vtkPVXMLElement* element)
{
vtkSMSessionProxyManager* pxm = this->GetSessionProxyManager();
assert(pxm != NULL);
unsigned int numElems = element->GetNumberOfNestedElements();
for (unsigned int cc = 0; cc < numElems; cc++)
{
vtkPVXMLElement* currentElement = element->GetNestedElement(cc);
const char* name = currentElement->GetName();
const char* linkname = currentElement->GetAttribute("name");
if (name && linkname)
{
vtkSMLink* link = pxm->GetRegisteredLink(linkname);
if (link == NULL)
{
std::string classname = "vtkSM";
classname += name;
vtkSmartPointer<vtkObject> obj;
obj.TakeReference(vtkPVInstantiator::CreateInstance(classname.c_str()));
link = vtkSMLink::SafeDownCast(obj);
if (link == NULL)
{
vtkWarningMacro("Failed to create object for link (name="
<< name << "). Expected type was " << classname.c_str() << ". Skipping.");
continue;
}
pxm->RegisterLink(linkname, link);
}
assert(link != NULL);
if (!link->LoadXMLState(currentElement, this->ProxyLocator))
{
return 0;
}
}
}
// Load the global_properties
if (vtkSMProxy* globalPropertiesProxy = pxm->GetProxy("global_properties", "ColorPalette"))
{
globalPropertiesProxy->LoadXMLState(element, this->ProxyLocator);
}
return 1;
}
//---------------------------------------------------------------------------
bool vtkSMStateLoader::VerifyXMLVersion(vtkPVXMLElement* rootElement)
{
const char* version = rootElement->GetAttribute("version");
if (!version)
{
vtkWarningMacro("ServerManagerState missing \"version\" information.");
return true;
}
// Nothing to check here really.
return true;
}
//---------------------------------------------------------------------------
int vtkSMStateLoader::LoadState(vtkPVXMLElement* elem, bool keepOriginalId)
{
this->Internal->KeepOriginalId = keepOriginalId;
if (!elem)
{
vtkErrorMacro("Cannot load state from (null) root element.");
return 0;
}
vtkSMSessionProxyManager* pxm = this->GetSessionProxyManager();
if (pxm == NULL)
{
vtkErrorMacro("Cannot load state without a ProxyManager");
return 0;
}
if (!this->ProxyLocator)
{
vtkErrorMacro("Please set the locator correctly.");
return 0;
}
this->ProxyLocator->SetDeserializer(this);
int ret = this->LoadStateInternal(elem);
this->ProxyLocator->SetDeserializer(0);
// BUG #10650. When animation scene time ranges are read from the state, they
// often override those that the timekeeper painstakingly computed. Here we
// explicitly trigger the timekeeper so that the scene re-determines the
// ranges, unless they are locked of course.
vtkSMProxy* timekeeper = pxm->GetProxy("timekeeper", "TimeKeeper");
if (timekeeper)
{
timekeeper->GetProperty("TimeRange")->Modified();
timekeeper->GetProperty("TimestepValues")->Modified();
}
return ret;
}
//---------------------------------------------------------------------------
int vtkSMStateLoader::LoadStateInternal(vtkPVXMLElement* parent)
{
vtkPVXMLElement* rootElement = parent;
if (rootElement->GetName() && strcmp(rootElement->GetName(), "ServerManagerState") != 0)
{
rootElement = rootElement->FindNestedElementByName("ServerManagerState");
if (!rootElement)
{
vtkErrorMacro("Failed to locate <ServerManagerState /> element."
<< "Cannot load server manager state.");
return 0;
}
}
vtkSMStateVersionController* convertor = vtkSMStateVersionController::New();
if (!convertor->Process(parent))
{
vtkWarningMacro("State convertor was not able to convert the state to current "
"version successfully");
}
convertor->Delete();
if (!this->VerifyXMLVersion(rootElement))
{
return 0;
}
this->ServerManagerStateElement = rootElement;
unsigned int numElems = rootElement->GetNumberOfNestedElements();
unsigned int i;
for (i = 0; i < numElems; i++)
{
vtkPVXMLElement* currentElement = rootElement->GetNestedElement(i);
const char* name = currentElement->GetName();
if (name)
{
if (strcmp(name, "ProxyCollection") == 0)
{
if (!this->BuildProxyCollectionInformation(currentElement))
{
return 0;
}
}
}
}
// Load all compound proxy definitions.
for (i = 0; i < numElems; i++)
{
vtkPVXMLElement* currentElement = rootElement->GetNestedElement(i);
const char* name = currentElement->GetName();
if (name)
{
if (strcmp(name, "CustomProxyDefinitions") == 0)
{
this->HandleCustomProxyDefinitions(currentElement);
}
}
}
// Iterate over all proxy collections to create all proxies. None are
// registered at this point, just created. Since we don't register proxies
// here, we have to take special care for loading state of proxies that are
// already registered. These include "TimeKeeper", "AnimationScene", and
// "TimeAnimationCue". We simply defer creation of proxies in "animation"
// and "timekeeper" group until all other proxies have been created and
// registered. That way, when properties on TimeKeeper or AnimationScene
// start getting modified, the proxies they may refer to are already
// present and registered.
std::vector<vtkSmartPointer<vtkPVXMLElement> > deferredCollections;
this->Internal->DeferProxyRegistration = true;
for (i = 0; i < numElems; i++)
{
vtkPVXMLElement* currentElement = rootElement->GetNestedElement(i);
const char* name = currentElement->GetName();
if (name != NULL && strcmp(name, "ProxyCollection") == 0)
{
const char* group_name = currentElement->GetAttributeOrEmpty("name");
if (strcmp(group_name, "animation") == 0 || strcmp(group_name, "timekeeper") == 0)
{
deferredCollections.push_back(currentElement);
}
else if (!this->HandleProxyCollection(currentElement))
{
return 0;
}
}
}
// Register proxies in order they were created (as that's a good dependency
// order).
for (vtkSMStateLoaderInternals::ProxyCreationOrderType::const_iterator iter =
this->Internal->ProxyCreationOrder.begin();
iter != this->Internal->ProxyCreationOrder.end(); ++iter)
{
this->RegisterProxy(iter->first, iter->second);
}
this->Internal->ProxyCreationOrder.clear();
// Now handle animation and timekeeper collections. This time, we let the
// proxies be registered as needed.
this->Internal->DeferProxyRegistration = false;
for (size_t cc = 0; cc < deferredCollections.size(); ++cc)
{
if (!this->HandleProxyCollection(deferredCollections[cc]))
{
return 0;
}
}
assert(this->Internal->ProxyCreationOrder.size() == 0);
// Process link elements.
for (i = 0; i < numElems; i++)
{
vtkPVXMLElement* currentElement = rootElement->GetNestedElement(i);
const char* name = currentElement->GetName();
if (name && strcmp(name, "Links") == 0)
{
this->HandleLinks(currentElement);
}
}
// If KeepIdMapping
this->Internal->AlignedMappingIdTable.clear();
if (this->KeepIdMapping != 0)
{
vtkSMStateLoaderInternals::RegInfoMapType::iterator iter =
this->Internal->RegistrationInformation.begin();
while (iter != this->Internal->RegistrationInformation.end())
{
vtkSMProxy* proxy = this->LocateExistingProxyUsingRegistrationName(iter->first);
if (proxy)
{
this->Internal->AlignedMappingIdTable.push_back(iter->first);
this->Internal->AlignedMappingIdTable.push_back(proxy->GetGlobalID());
}
// Move forward
iter++;
}
}
// Clear internal data structures.
this->Internal->ProxyCreationOrder.clear();
this->Internal->RegistrationInformation.clear();
this->ServerManagerStateElement = 0;
return 1;
}
//---------------------------------------------------------------------------
void vtkSMStateLoader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//---------------------------------------------------------------------------
vtkTypeUInt32* vtkSMStateLoader::GetMappingArray(int& size)
{
size = static_cast<int>(this->Internal->AlignedMappingIdTable.size());
return &this->Internal->AlignedMappingIdTable[0];
}
|