OMLクラスオブジェクトからPythonクラスオブジェクトへのマップ

Composeは、デフォルトのデータ型マッピングでOMLPythonの間の双方向ブリッジをサポートしています。このガイドでは、このマッピングをクラスオブジェクトに拡張する方法を説明します。

OMLクラスオブジェクト

OmlCircleというOMLクラスオブジェクトがあり、そのプロパティに _radius があり、GetRadius、SetRadius、GetAreaなどのメソッドが以下のように定義されているとします:

classdef OmlCircle
    properties
       _radius
    end
	methods
		function self = OmlCircle()
			self._radius = 0;
		end
		function self = OmlCircle(radius)
			self._radius = radius;
		end
		function radius = GetRadius(self)
			radius = self._radius;
		end
		function SetRadius(self,radius)
			self._radius = radius;
		end
		function ar=GetArea(self)
			ar = pi*self._radius^2;
             end
	end
end

Pythonクラスオブジェクト

PythonクラスオブジェクトPyCircleが以下のように定義されているとします:

class PyCircle:
  def __init__(self, radius = 0):
    self._radius = radius
  def GetRadius(self):
    return self._radius
  def SetRadius(self,radius = 0):
    self._radius = radius
  def GetArea(self):
    import math 
    return (math.pi)*(self._radius**2)
pycircleobj=PyCircle(9)

マッピングを定義するOMLクラス

OmlPythonBridgeCircleImpを実装します。OmlPythonBridgeIntクラスを継承し、オブジェクトのマッピングに必要なメソッドを定義します。
  1. OMLオブジェクトをPythonオブジェクトにマップするクラスを作成します。このOMLクラスはOMLクラス"OmlPythonBridgeInt"を継承しなければなりません。
  2. メソッドの定義:
    • Python変数がOMLにインポートできるかどうかをチェックします。
    • OML変数がPythonにエクスポートできるかどうかをチェックします。
    • pythonvarnameからインポートされたOML変数を取得します。
    • OML変数omlvarをpythonvarnameの値としてPythonにエクスポートします。
  3. OMLクラス名を登録するには、OML関数registeromlpythonbridgeextを呼び出します。例えば、registeromlpythonbridgeext('add','ClassName')を使用します。
classdef OmlPythonBridgeCircleImp < OmlPythonBridgeInt
	methods
		
		  function obj = OmlPythonBridgeCircleImp ()
			  obj = struct();
		  end

		% check if python variable can be imported to OML.
		  function status=CanImportPyVar(self, pythonvar)
			status = false;
			analysisusingpythonbegin()
			_is_circle_obj = ['_is_circle_obj' sprintf('%.0f',tic)];
			evalpythonscript([_is_circle_obj '=False']);
			eval_status = evalpythonscript([ 'if isinstance(' pythonvar ', PyCircle):'  _is_circle_obj '=True']);
			if eval_status
				status = getpythonvar(_is_circle_obj);
				evalpythonscript(['del ' _is_circle_obj]);
		    end
			analysisusingpythonend()
		  end
		  
		  %check if OML variable can be exported to python
		  function status=CanExportOmlVar(self, omlvar)
			if strcmp('OmlCircle',class(omlvar))
				status = true;
			else
				status = false;
			end
		  end
		  
		  %to get oml variable imported from pythonvarname
		  function out=GetPythonVar(self, pythonvarname)
			status = true;
			outval = '';
			analysisusingpythonbegin()
			  try
				if ~ isstr(pythonvarname)
					error('pythonvarname must be string.')
				end
				_py_radius_var = ['_radius' sprintf('%.0f',tic)];
				evalpythonscript([_py_radius_var '=' pythonvarname '.GetRadius()']);

				_oml_radius = getpythonvar(_py_radius_var);
				outval = OmlCircle(_oml_radius);
				evalpythonscript(['del ' _py_radius_var]);
			catch
				status = false;
			end
			out = {status, outval};
			analysisusingpythonend()
		  end
		  
	      % to export oml variable omlvar to python with name as value of pythonvarname
		  function status=ExportToPyton(self,omlvar,pythonvarname)
				analysisusingpythonbegin()
			    status = false;
				try
					if ~isstr(pythonvarname)
						error('pythonvarname must be string.')
					end
					_oml_radius = omlvar.GetRadius();
					_py_radius_var = ['_radius' sprintf('%.0f',tic)];
					exporttopython(_oml_radius,_py_radius_var);
					status = evalpythonscript([pythonvarname '=PyCircle(' _py_radius_var ')']);
					evalpythonscript(['del ' _py_radius_var]);
				catch
					%do nothing
				end
				analysisusingpythonend()
			end
	end
end

registeromlpythonbridgeext('add', 'OmlPythonBridgeCircleImp')

PythonBridgeOMLCommandsの呼び出し

実装されている場合、OML Python Bridge コマンドを使用して、クラスオブジェクトをインポートまたはエクスポートできます。

Pythonコマンドウィンドウ:
pycircleobj=PyCircle(9)
OMLコマンドウィンドウ:
omlcircleobj=getpythonvar('pycircleobj'); 
exporttopython(omlcircleobj,'pycircleobjfromoml') 
注: clear all OMLコマンドを実行した後、OMLクラスをインタープリタで使用できるようにするには、OMLファイルを再実行する必要があります。