Nektar++
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
CellMLToNektar.pycml.MathsError Class Reference
Inheritance diagram for CellMLToNektar.pycml.MathsError:
[legend]

Public Member Functions

def __init__ (self, context_obj, message, warn=False, level=None)
 
def show_xml_context_only (self)
 
def __str__ (self)
 
def ordinal (self, i)
 
def __unicode__ (self)
 

Public Attributes

 message
 
 warn
 
 level
 
 show_xml_context
 
 cname
 
 ename
 
 context
 
 expr_index
 
 math_index
 
 reaction_spec
 

Private Member Functions

def _generate_message (self, where)
 

Private Attributes

 _show_xml_context_only
 

Detailed Description

Exception class for validation errors raised while checking mathematics.

Definition at line 3343 of file pycml.py.

Constructor & Destructor Documentation

◆ __init__()

def CellMLToNektar.pycml.MathsError.__init__ (   self,
  context_obj,
  message,
  warn = False,
  level = None 
)
Create a mathematics validation error.

context_class should be the object that is reporting the error.
message gives more details on what went wrong.
If warn is set to true then produce a warning message, not an error.
level gives the level of the message logged.

Reimplemented in CellMLToNektar.pycml.UnitsError.

Definition at line 3347 of file pycml.py.

3347 def __init__(self, context_obj, message, warn=False, level=None):
3348 """Create a mathematics validation error.
3349
3350 context_class should be the object that is reporting the error.
3351 message gives more details on what went wrong.
3352 If warn is set to true then produce a warning message, not an error.
3353 level gives the level of the message logged.
3354 """
3355 self.message = message
3356 self.warn = warn
3357 self.level = level or (logging.ERROR,logging.WARNING)[warn]
3358 self.show_xml_context = False
3359 self._show_xml_context_only = False
3360
3361 self.cname = context_obj.component.name
3362 self.ename = context_obj.localName
3363 self.context = context_obj
3364
3365 # Nicer context explanation
3366 if isinstance(context_obj, cellml_variable):
3367 self.expr_index = self.math_index = 0
3368 self.reaction_spec = ''
3369 return
3370 expr_root = context_obj.xml_xpath(u'ancestor-or-self::*[local-name(..)="math"]')[0]
3371 self.expr_index = self.math_index = 1
3372 math = expr_root.xml_parent
3373 for elt in math.xml_element_children():
3374 if elt is expr_root: break
3375 if elt.localName in [u'apply', u'semantics']:
3376 self.expr_index += 1
3377 for elt in math.xml_element_children(math.xml_parent):
3378 if elt is math: break
3379 if elt.localName == u'math':
3380 self.math_index += 1
3381 # Is this in a reaction?
3382 vref = context_obj.xml_xpath(u'ancestor::cml:variable_ref')
3383 if vref:
3384 self.reaction_spec = ' in mathematics for variable "%s" in a reaction' % vref[0].variable
3385 else:
3386 self.reaction_spec = ''
3387 return
3388

Member Function Documentation

◆ __str__()

def CellMLToNektar.pycml.MathsError.__str__ (   self)

Definition at line 3394 of file pycml.py.

3394 def __str__(self):
3395 msg = unicode(self)
3396 return msg.encode('UTF-8')
3397

◆ __unicode__()

def CellMLToNektar.pycml.MathsError.__unicode__ (   self)

Reimplemented in CellMLToNektar.pycml.UnitsError.

Definition at line 3425 of file pycml.py.

3425 def __unicode__(self):
3426 return self._generate_message('checking mathematics')
3427

References CellMLToNektar.pycml.MathsError._generate_message().

◆ _generate_message()

def CellMLToNektar.pycml.MathsError._generate_message (   self,
  where 
)
private

Definition at line 3407 of file pycml.py.

3407 def _generate_message(self, where):
3408 if self.warn: type = 'Warning'
3409 else: type = 'Error'
3410 msg = [type, ' ', where, ': ', self.message]
3411 if not self._show_xml_context_only:
3412 msg.extend(['\n Context: ', self.ordinal(self.expr_index),
3413 ' expression in the ', self.ordinal(self.math_index),
3414 ' math element', self.reaction_spec,
3415 ' in component ', self.cname, '\n XPath: ',
3416 element_xpath(self.context)])
3417 msg = u''.join(msg)
3418 if self.show_xml_context:
3419 # Return the context XML tree as well.
3420 xml = self.context.xml(indent = u'yes',
3421 omitXmlDeclaration = u'yes')
3422 msg = msg + u'\n' + unicode(xml, encoding='UTF-8')
3423 return msg
3424

References CellMLToNektar.pycml.MathsError._show_xml_context_only, CellMLToNektar.pycml.MathsError.cname, CellMLToNektar.pycml.MathsError.context, CellMLToNektar.utilities.element_xpath(), CellMLToNektar.pycml.MathsError.expr_index, CellMLToNektar.pycml.MathsError.math_index, CellMLToNektar.pycml.MathsError.message, CellMLToNektar.pycml.MathsError.ordinal(), CellMLToNektar.pycml.MathsError.reaction_spec, CellMLToNektar.pycml.MathsError.show_xml_context, and CellMLToNektar.pycml.MathsError.warn.

Referenced by CellMLToNektar.pycml.MathsError.__unicode__(), and CellMLToNektar.pycml.UnitsError.__unicode__().

◆ ordinal()

def CellMLToNektar.pycml.MathsError.ordinal (   self,
  i 
)

Definition at line 3398 of file pycml.py.

3398 def ordinal(self, i):
3399 "Convert integer i to an ordinal string."
3400 if i // 10 == 1: suf = 'th'
3401 elif i % 10 == 1: suf = 'st'
3402 elif i % 10 == 2: suf = 'nd'
3403 elif i % 10 == 3: suf = 'rd'
3404 else: suf = 'th'
3405 return "%d%s" % (i, suf)
3406

Referenced by CellMLToNektar.pycml.MathsError._generate_message().

◆ show_xml_context_only()

def CellMLToNektar.pycml.MathsError.show_xml_context_only (   self)
Only show the XML where the error occurred.

Definition at line 3389 of file pycml.py.

3389 def show_xml_context_only(self):
3390 """Only show the XML where the error occurred."""
3391 self.show_xml_context = True
3392 self._show_xml_context_only = True
3393

References CellMLToNektar.pycml.MathsError._show_xml_context_only, and CellMLToNektar.pycml.MathsError.show_xml_context.

Referenced by CellMLToNektar.processors.UnitsConverter.try_convert().

Member Data Documentation

◆ _show_xml_context_only

CellMLToNektar.pycml.MathsError._show_xml_context_only
private

◆ cname

CellMLToNektar.pycml.MathsError.cname

Definition at line 3361 of file pycml.py.

Referenced by CellMLToNektar.pycml.MathsError._generate_message().

◆ context

CellMLToNektar.pycml.MathsError.context

Definition at line 3363 of file pycml.py.

Referenced by CellMLToNektar.pycml.MathsError._generate_message().

◆ ename

CellMLToNektar.pycml.MathsError.ename

Definition at line 3362 of file pycml.py.

◆ expr_index

CellMLToNektar.pycml.MathsError.expr_index

Definition at line 3367 of file pycml.py.

Referenced by CellMLToNektar.pycml.MathsError._generate_message().

◆ level

CellMLToNektar.pycml.MathsError.level

Definition at line 3357 of file pycml.py.

◆ math_index

CellMLToNektar.pycml.MathsError.math_index

Definition at line 3367 of file pycml.py.

Referenced by CellMLToNektar.pycml.MathsError._generate_message().

◆ message

CellMLToNektar.pycml.MathsError.message

Definition at line 3355 of file pycml.py.

Referenced by CellMLToNektar.pycml.MathsError._generate_message().

◆ reaction_spec

CellMLToNektar.pycml.MathsError.reaction_spec

Definition at line 3368 of file pycml.py.

Referenced by CellMLToNektar.pycml.MathsError._generate_message().

◆ show_xml_context

CellMLToNektar.pycml.MathsError.show_xml_context

◆ warn

CellMLToNektar.pycml.MathsError.warn

Definition at line 3356 of file pycml.py.

Referenced by CellMLToNektar.pycml.MathsError._generate_message().