Ejemplo n.º 1
0
void udmCopy(ObjectImpl *p_srcRoot, ObjectImpl *p_dstRoot, DataNetwork *p_dstBackend, UdmUtil::copy_assoc_map &cam, const string &m_backend_ext, bool inside_lib = false)
{
	vector<ObjectImpl*> children = p_srcRoot->getChildren(NULL, p_srcRoot->type());
	for (vector<ObjectImpl*>::const_iterator i = children.begin(); i != children.end(); i++) 
	{

		ObjectImpl *p_srcChild = *i;

		string lib_name;
		if (!p_srcChild->getLibraryName(lib_name)) {
			p_srcChild->release();
			continue;
		}

		if (lib_name.length() == 0) 
		{ // a library with an empty lib name
			Udm::Object srcChild = p_srcChild->clone();
			lib_name = UdmUtil::ExtractName(srcChild);
			if (lib_name.length() == 0 || lib_name == "<empty string>" || lib_name == "<no name specified>")
				lib_name = "lib";
		}

		// remove file extension if it looks like a
		// backend designation
		if (lib_name.length() > 4 && lib_name[ lib_name.length() - 4 ] == '.')
			lib_name = lib_name.substr(0, lib_name.length() - 4);

		string new_lib_name = lib_name + "." + m_backend_ext;

		//new_lib_name could contain the path which is not exist

		// create datanetwork for standalone library and build map
		// from source object to standalone library object
		Udm::SmartDataNetwork libDN(CyPhyML::diagram);
		const ::Uml::Class & safe_type = ::Uml::SafeTypeContainer::GetSafeType(p_srcChild->type());
		libDN.CreateNew(new_lib_name, "CyPhyML", safe_type, Udm::CHANGES_LOST_DEFAULT);
		Object p_root = libDN.GetRootObject();
		ObjectImpl *p_libRoot = p_root.__impl();

		// copy, including nested libraries
		UdmUtil::copy_assoc_map lib_cam;
		udmCopy(p_srcChild, p_libRoot, &libDN, lib_cam, m_backend_ext, true);
		lib_cam.insert( make_pair(p_srcChild->clone(), p_libRoot->clone()) );
		libDN.SaveAs(new_lib_name);


		// attach library to destination host and build map
		// from standalone library object to private copy object
		// (the map already has a mapping from src root to dst root)
		Udm::t_lib_to_copy_impl_map lib_copy_impl_cam;
		ObjectImpl *p_newLibRoot = p_dstRoot->AttachLibrary(p_libRoot, new_lib_name, &lib_copy_impl_cam);

		// we need a new map with Object elements instead of ObjectImpl* elements
		UdmUtil::copy_assoc_map lib_copy_cam;
		for (Udm::t_lib_to_copy_impl_map::const_iterator i = lib_copy_impl_cam.begin(); i != lib_copy_impl_cam.end(); i++) {
			lib_copy_cam.insert( make_pair(i->first, i->second) );
		}


		// map from source to library private copy
		for (UdmUtil::copy_assoc_map::const_iterator i = lib_cam.begin(); i != lib_cam.end(); i++) {
			UdmUtil::copy_assoc_map::const_iterator fi = lib_copy_cam.find(i->second);
			if (fi == lib_copy_cam.end())
				throw udm_exception("object not found in map");
			cam.insert( make_pair(i->first, fi->second) );
		}

		libDN.CloseNoUpdate();

		cam.insert( make_pair(p_srcChild, p_newLibRoot) );
	}

	UdmUtil::CopyOpts opts = { true, inside_lib };
	UdmUtil::CopyObjectHierarchy(p_srcRoot, p_dstRoot, p_dstBackend, cam, opts);
}
Ejemplo n.º 2
0
// This is the main component method for interpereters and plugins.
// May als be used in case of invokeable addons
STDMETHODIMP RawComponent::InvokeEx( IMgaProject *project,  IMgaFCO *currentobj,
                  IMgaFCOs *selectedobjs,  long param) {
  // Calling the user's initialization function
  if(CUdmApp::Initialize())
  {
    return S_FALSE;
  }

  CComPtr<IMgaProject>ccpProject(project);
  try
  {
    if(interactive)
    {
    CComBSTR projname;
    CComBSTR focusname = "<nothing>";
    CComPtr<IMgaTerritory> terr;
    COMTHROW(ccpProject->CreateTerritory(NULL, &terr));

    // Setting up Udm
#ifdef _DYNAMIC_META
  #ifdef _DYNAMIC_META_DOM
      // Loading the meta for the project
      UdmDom::DomDataNetwork  ddnMeta(Uml::diagram);
      Uml::Diagram theUmlDiagram;

      // Opening the XML meta of the project
      ddnMeta.OpenExisting(config.metaPath,"uml.xsd", Udm::CHANGES_LOST_DEFAULT);

      // Casting the DataNetwork to diagram
      theUmlDiagram = Uml::Diagram::Cast(ddnMeta.GetRootObject());

      // Creating the UDM diagram
      Udm::UdmDiagram udmDataDiagram;
      udmDataDiagram.dgr = &theUmlDiagram;
      udmDataDiagram.init = dummy;

  #elif defined _DYNAMIC_META_STATIC
      // Loading the meta for the project
      UdmStatic::StaticDataNetwork  dnsMeta(Uml::diagram);
      Uml::Diagram theUmlDiagram;

      // Opening the static meta of the project
      dnsMeta.OpenExisting(config.metaPath, "", Udm::CHANGES_LOST_DEFAULT);

      // Casting the DataNetwork to diagram
      theUmlDiagram = Uml::Diagram::Cast(dnsMeta.GetRootObject());

      // Creating the UDM diagram
      Udm::UdmDiagram udmDataDiagram;
      udmDataDiagram.dgr = &theUmlDiagram;
      udmDataDiagram.init = dummy;

  #else
      ASSERT((0,"Nor _DYNAMIC_META_DOM either _DYNAMIC_META_STATIC defined for dynamic loading"));
  #endif
      // Loading the project
      UdmGme::GmeDataNetwork dngBackend(udmDataDiagram);

#else
    using namespace META_NAMESPACE;

    // Loading the project
    UdmGme::GmeDataNetwork dngBackend(META_NAMESPACE::diagram);

#endif
    try
    {
      // Opening backend
      dngBackend.OpenExisting(ccpProject);


      CComPtr<IMgaFCO> ccpFocus(currentobj);
      Udm::Object currentObject;
      if(ccpFocus)
      {
        currentObject=dngBackend.Gme2Udm(ccpFocus);
      }

      set<Udm::Object> selectedObjects;

      CComPtr<IMgaFCOs> ccpSelObject(selectedobjs);

      MGACOLL_ITERATE(IMgaFCO,ccpSelObject){
        Udm::Object currObj;
        if(MGACOLL_ITER)
        {
          currObj=dngBackend.Gme2Udm(MGACOLL_ITER);
        }
       selectedObjects.insert(currObj);
      }MGACOLL_ITERATE_END;

#ifdef _ACCESS_MEMORY
      // Creating Cache
  #ifdef _DYNAMIC_META
      UdmStatic::StaticDataNetwork dnsCacheBackend(udmDataDiagram);
  #else
      UdmStatic::StaticDataNetwork dnsCacheBackend(META_NAMESPACE::diagram);
  #endif

      const Uml::Class & safeType = Uml::SafeTypeContainer::GetSafeType(dngBackend.GetRootObject().type());

      dnsCacheBackend.CreateNew("","",safeType, Udm::CHANGES_LOST_DEFAULT);

      Udm::Object nullObject(&Udm::__null);
      UdmUtil::copy_assoc_map copyAssocMap;
      copyAssocMap[currentObject]=nullObject; // currentObject may be null object

      for(set<Udm::Object>::iterator p_CurrSelObject=selectedObjects.begin();
        p_CurrSelObject!=selectedObjects.end();p_CurrSelObject++)
      {
          pair<Udm::Object const, Udm::Object> item(*p_CurrSelObject, nullObject);

          pair<UdmUtil::copy_assoc_map::iterator, bool> insRes = copyAssocMap.insert(item);

          if (!insRes.second)
          {
            ASSERT(NULL);
          }

      }

      // Copying from GME to memory
      UdmUtil::CopyObjectHierarchy(
        dngBackend.GetRootObject().__impl(),
        dnsCacheBackend.GetRootObject().__impl(),
        &dnsCacheBackend,
        copyAssocMap);

      // Searching for focus object
      Udm::Object currentObjectCache;
      UdmUtil::copy_assoc_map::iterator currObject = copyAssocMap.find(currentObject);
      if (currObject != copyAssocMap.end()) // It is in the map
      {
        currentObjectCache=currObject->second;
      }


      // Searching for selected objects
      set<Udm::Object> selectedObjectsCache;

      for( p_CurrSelObject=selectedObjects.begin();
        p_CurrSelObject!=selectedObjects.end();p_CurrSelObject++)
      {
        Udm::Object object;
        UdmUtil::copy_assoc_map::iterator currSelObjectIt = copyAssocMap.find(*p_CurrSelObject);
        if (currSelObjectIt != copyAssocMap.end()) // It is in the map
        {
          object=currSelObjectIt->second;
          selectedObjectsCache.insert(object);
        }
      }


      // Closing GME backend
      dngBackend.CloseNoUpdate();

      // Calling the main entry point
      CUdmApp::UdmMain(&dnsCacheBackend,currentObjectCache,selectedObjectsCache,param);
      // Close cache backend
      dnsCacheBackend.CloseNoUpdate();

#else
      // Calling the main entry point
      CUdmApp::UdmMain(&dngBackend,currentObject,selectedObjects,param);
      // Closing backend
      dngBackend.CloseWithUpdate();
#endif

    }
    catch(udm_exception &exc)
    {
#ifdef _META_ACCESS_MEMORY
      dnCacheBackend.CloseNoUpdate();
#endif
      // Close GME Backend (we may close it twice, but GmeDataNetwork handles it)
      dngBackend.CloseNoUpdate();

      AfxMessageBox(exc.what());
      return S_FALSE;
    }
    }
// This is the main component method for interpereters and plugins. 
// May als be used in case of invokeable addons
STDMETHODIMP RawComponent::InvokeEx( IMgaProject *project,  IMgaFCO *currentobj,  
									IMgaFCOs *selectedobjs,  long param) 
{
	CUdmApp udmApp;
	// Calling the user's initialization function
	if(udmApp.Initialize())
	{
		return S_FALSE;
	}

	CComPtr<IMgaProject>ccpProject(project);

	try
	{
		// Setting up the console
		GMEConsole::Console::setupConsole(ccpProject);
		GMEConsole::Console::clear();

		char tmpbuf[128];
		_strdate_s(tmpbuf,128);
		std::string date(tmpbuf);
		_strtime_s(tmpbuf,128);
		std::string time(tmpbuf);

		GMEConsole::Console::writeLine(date + " " + time + " Starting CyPhyElaborate Tester Interpreter", MSG_INFO);


	  if(interactive)
	  {
		CComBSTR projname;
		CComBSTR focusname = "<nothing>";
		CComPtr<IMgaTerritory> terr;
		//COMTHROW(ccpProject->CreateTerritory(NULL, &terr));
		//COMTHROW(ccpProject->BeginTransaction(terr));

		// Setting up Udm
#ifdef _DYNAMIC_META
	#ifdef _DYNAMIC_META_DOM
			// Loading the meta for the project
			UdmDom::DomDataNetwork  ddnMeta(Uml::diagram);
			Uml::Diagram theUmlDiagram;

			// Opening the XML meta of the project
			ddnMeta.OpenExisting(META_PATH,"uml.dtd", Udm::CHANGES_LOST_DEFAULT);

			// Casting the DataNetwork to diagram
			theUmlDiagram = Uml::Diagram::Cast(ddnMeta.GetRootObject());

			// Creating the UDM diagram
			Udm::UdmDiagram udmDataDiagram;
			udmDataDiagram.dgr = &theUmlDiagram;
			udmDataDiagram.init = dummy;

	#elif defined _DYNAMIC_META_STATIC
			// Loading the meta for the project
			Udm::SmartDataNetwork  dnsMeta(Uml::diagram);
			Uml::Diagram theUmlDiagram;

			// Opening the static meta of the project
			dnsMeta.OpenExisting(META_PATH, "", Udm::CHANGES_LOST_DEFAULT);

			// Casting the DataNetwork to diagram
			theUmlDiagram = Uml::Diagram::Cast(dnsMeta.GetRootObject());

			// Creating the UDM diagram
			Udm::UdmDiagram udmDataDiagram;
			udmDataDiagram.dgr = &theUmlDiagram;
			udmDataDiagram.init = dummy;

	#else
			#error "Neither _DYNAMIC_META_DOM or _DYNAMIC_META_STATIC defined for dynamic loading"
	#endif
			// Loading the project
			UdmGme::GmeDataNetwork dngBackend(udmDataDiagram);

#else
		using namespace META_NAMESPACE;

		// Loading the project
		UdmGme::GmeDataNetwork dngBackend(META_NAMESPACE::diagram);

#endif
		try
		{
			long status;
			COMTHROW(ccpProject->get_ProjectStatus(&status));
			if (!(status & 8))
				COMTHROW(ccpProject->BeginTransactionInNewTerr(TRANSACTION_NON_NESTED, &terr));

            try {
            // Opening backend if it is not already opened
            dngBackend.OpenExisting(ccpProject, Udm::CHANGES_LOST_DEFAULT, true);


			CComPtr<IMgaFCO> ccpFocus(currentobj);
			Udm::Object currentObject;
			if(ccpFocus)
			{
				currentObject=dngBackend.Gme2Udm(ccpFocus);
			}

			std::set<Udm::Object> selectedObjects;

			CComPtr<IMgaFCOs> ccpSelObject(selectedobjs);

			MGACOLL_ITERATE(IMgaFCO,ccpSelObject){
				Udm::Object currObj;
				if(MGACOLL_ITER)
				{
					currObj=dngBackend.Gme2Udm(MGACOLL_ITER);
				}
			 selectedObjects.insert(currObj);
			}MGACOLL_ITERATE_END;

#ifdef _ACCESS_MEMORY
			// Creating Cache
	#ifdef _DYNAMIC_META
			Udm::SmartDataNetwork dnsCacheBackend(udmDataDiagram);
	#else
			Udm::SmartDataNetwork dnsCacheBackend(META_NAMESPACE::diagram);
	#endif

			const Uml::Class & safeType = Uml::SafeTypeContainer::GetSafeType(dngBackend.GetRootObject().type());

			dnsCacheBackend.CreateNew("tmp.mem","",safeType, Udm::CHANGES_LOST_DEFAULT);

			Udm::Object nullObject(&Udm::_null);
			UdmUtil::copy_assoc_map copyAssocMap;
			copyAssocMap[currentObject]=nullObject; // currentObject may be null object

			std::set<Udm::Object>::iterator p_CurrSelObject;
			for(p_CurrSelObject=selectedObjects.begin();
				p_CurrSelObject!=selectedObjects.end();p_CurrSelObject++)
			{
					std::pair<Udm::Object const, Udm::Object> item(*p_CurrSelObject, nullObject);

					std::pair<UdmUtil::copy_assoc_map::iterator, bool> insRes = copyAssocMap.insert(item);

					if (!insRes.second)
					{
						assert(false);
					}

			}

			// Copying from GME to memory
			UdmUtil::CopyObjectHierarchy(
				dngBackend.GetRootObject().__impl(),
				dnsCacheBackend.GetRootObject().__impl(),
				&dnsCacheBackend,
				copyAssocMap);

			// Searching for focus object
			Udm::Object currentObjectCache;
			UdmUtil::copy_assoc_map::iterator currObject = copyAssocMap.find(currentObject);
			if (currObject != copyAssocMap.end()) // It is in the map
			{
				currentObjectCache=currObject->second;
			}


			// Searching for selected objects
			std::set<Udm::Object> selectedObjectsCache;

			for( p_CurrSelObject=selectedObjects.begin();
				p_CurrSelObject!=selectedObjects.end();p_CurrSelObject++)
			{
				Udm::Object object;
				UdmUtil::copy_assoc_map::iterator currSelObjectIt = copyAssocMap.find(*p_CurrSelObject);
				if (currSelObjectIt != copyAssocMap.end()) // It is in the map
				{
					object=currSelObjectIt->second;
					selectedObjectsCache.insert(object);
				}
			}


			// Closing GME backend
			dngBackend.CloseNoUpdate();

			// Calling the main entry point
			CUdmApp::UdmMain(&dnsCacheBackend,currentObjectCache,selectedObjectsCache,param);
			// Close cache backend
			dnsCacheBackend.CloseNoUpdate();

#else
			// Calling the main entry point
			udmApp.UdmMain(&dngBackend,currentObject,selectedObjects,param);
			traceability = std::move(udmApp.traceability);
			// Closing backend
			dngBackend.CloseWithUpdate();
			if (!(status & 8))
				ccpProject->CommitTransaction();
			} catch (...) {
				if (!(status & 8))
					ccpProject->AbortTransaction();
				throw;
			}
#endif

		}
		catch(udm_exception &exc)
		{
#ifdef _META_ACCESS_MEMORY
			dnCacheBackend.CloseNoUpdate();
#endif
			// Close GME Backend (we may close it twice, but GmeDataNetwork handles it)
			dngBackend.CloseNoUpdate();
			//COMTHROW(ccpProject->AbortTransaction());

			GMEConsole::Console::Error::writeLine(exc.what());
			return S_FALSE;
		}
	  }