Map OML Class Object to Python Class Objects

Compose supports bi-directional bridge between OML and Python with default datatype mappings. This guide shows how to expand this mapping to class objects.

OML Class Object

Say you have an OML Class object called OmlCircle with _radius as its property and methods like GetRadius, SetRadius, and GetArea are defined as below:

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 Class Object

Say you have a Python Class Object PyCircle defined as below:

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 Class to Define the Mapping

Implement OmlPythonBridgeCircleImp, which inherits the OmlPythonBridgeInt class and defines necessary methods for the successful mapping of objects.
  1. Create a class to map OML objects to Python objects. This OML class has to inherit from OML class "OmlPythonBridgeInt."
  2. Define Methods to:
    • Check if Python variable can be imported to OML.
    • Check if OML variable can be exported to Python.
    • Get OML variable imported from pythonvarname.
    • Export OML variable omlvar to Python with the name as the value of pythonvarname.
  3. To register the OML class name, call the OML function registeromlpythonbridgeext. For example, 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')

Call PythonBridgeOMLCommands

When Implemented, you can use OML Python Bridge commands to import or export the class objects.

In the Python Command window:
pycircleobj=PyCircle(9)
In the OML Command window:
omlcircleobj=getpythonvar('pycircleobj'); 
exporttopython(omlcircleobj,'pycircleobjfromoml') 
Note: After you execute the clear all OML command, you must rerun the OML files to make OML classes available in the interpreter.