mttFetchSpecification.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. function model = mttFetchSpecification(filename)
  2. model = [] ;
  3. mttAssert(mttFileExists(filename),...
  4. ['File "',filename,'" not found']) ;
  5. mttNotify([' ...processing ',filename]) ;
  6. mttWriteNewLine ;
  7. model.specification = filename ;
  8. model.source = mttCutText(filename,'_abg.txt') ;
  9. model_name = mttDetachText(model.source,'/') ;
  10. content = mttReadFile(filename) ;
  11. statements = mttExtractStatements(content) ;
  12. number_of_statements = length(statements) ;
  13. abg_declared = 0 ;
  14. next = 0 ;
  15. parsing = 1 ;
  16. while parsing
  17. next = next + 1 ;
  18. statement = statements{next} ;
  19. [keyword,line] = mttSeparateText(statement) ;
  20. switch keyword
  21. case 'abg',
  22. mttAssert(~abg_declared,...
  23. '"abg" declaration must be unique') ;
  24. abg_declared = 1 ;
  25. abg_name = mttCutText(line,'[') ;
  26. mttAssert(strcmp(abg_name,model_name),...
  27. ['Wrong name:[',abg_name,'] Expecting:[',model_name,']']) ;
  28. abg_parameter_list = mttExtractText(line,'[',']') ;
  29. [abg_parameters,abg_defaults] = mttGetParameters(abg_parameter_list) ;
  30. model.name = abg_name ;
  31. model.sympar = abg_parameters ;
  32. model.sympar_default = abg_defaults ;
  33. [abg,next] = fetch_abg(statements,next) ;
  34. model = mttAppendFields(model,abg) ;
  35. case '{',
  36. error('Unexpected "{" found') ;
  37. case '}',
  38. error('Unexpected "}" found') ;
  39. otherwise,
  40. error(['Unrecognised top-level keyword "',keyword,'"']) ;
  41. end
  42. if next==number_of_statements
  43. parsing = 0 ;
  44. end
  45. end
  46. mttCheckBondgraphDeclarations(model) ;
  47. function [abg,next] = fetch_abg(statements,next)
  48. global mtt_environment
  49. abg = [] ;
  50. unit_name = 'abg' ;
  51. user_defined_paths = mttGetFieldNames(mtt_environment,'path') ;
  52. number_of_statements = length(statements) ;
  53. bondgraph = [] ;
  54. use_declared = 0 ;
  55. open = 0 ;
  56. parsing = 1 ;
  57. while parsing
  58. next = next + 1 ;
  59. statement = statements{next} ;
  60. [keyword,line] = mttSeparateText(statement) ;
  61. switch keyword
  62. case 'use',
  63. mttAssert(open,...
  64. ['"use" declaration must be contained inside {...} in "',unit_name,'"']) ;
  65. mttAssert(~use_declared,...
  66. '"use" declaration must be unique') ;
  67. use_declared = 1 ;
  68. bondgraph = line ;
  69. abg.bondgraph = bondgraph ;
  70. case 'input',
  71. mttAssert(open,...
  72. ['"input" declarations must be contained inside {...} in "',unit_name,'"']) ;
  73. input_parameter_list = line ;
  74. [input_parameters,input_defaults] = mttGetParameters(input_parameter_list) ;
  75. abg = mttAppend(abg,'input',input_parameters) ;
  76. abg = mttAppend(abg,'input_default',input_defaults) ;
  77. case 'numpar',
  78. mttAssert(open,...
  79. ['"numpar" declarations must be contained inside {...} in "',unit_name,'"']) ;
  80. numerical_parameter_list = line ;
  81. [numerical_parameters,numerical_defaults] = mttGetParameters(numerical_parameter_list) ;
  82. abg = mttAppend(abg,'numpar',numerical_parameters) ;
  83. abg = mttAppend(abg,'numpar_default',numerical_defaults) ;
  84. case 'object',
  85. mttAssert(open,...
  86. ['"object" declarations must be contained inside {...} in "',unit_name,'"']) ;
  87. object_names = mttGetFieldNames(abg,'obj') ;
  88. [object_name,object_spec] = mttCutText(line,':=') ;
  89. mttAssert(~ismember(object_name,object_names),...
  90. ['Object "',object_name,'" already declared']) ;
  91. implementation = mttCutText(object_spec,'[') ;
  92. mttAssert(~isempty(implementation),...
  93. ['Incomplete specification:[',line,']']) ;
  94. object_parameter_list = mttExtractText(line,'[',']') ;
  95. object_parameters = mttGetInstanceParameters(object_parameter_list) ;
  96. [source,name.item] = mttCutText(implementation,'::') ;
  97. [name.class,name.path] = mttDetachText(source,'/') ;
  98. if isempty(name.class)
  99. name.class = source ;
  100. name.path = [] ;
  101. else
  102. mttAssert(~isempty(name.path),...
  103. ['Empty path name in "',unit_name,'"']) ;
  104. if name.path(1)=='$'
  105. [path_alias,path_branch] = mttCutText(name.path,'/') ;
  106. path_alias(1) = [] ;
  107. mttAssert(ismember(path_alias,user_defined_paths),...
  108. ['Path "',path_alias,'" not recognised']) ;
  109. path_root = getfield(mtt_environment,'path',path_alias) ;
  110. if isempty(path_branch)
  111. name.path = path_root ;
  112. else
  113. name.path = [path_root,'/',path_branch] ;
  114. end
  115. end
  116. end
  117. abg = setfield(abg,'obj',object_name,'name',name) ;
  118. abg = setfield(abg,'obj',object_name,'parameter',object_parameters) ;
  119. case '{',
  120. mttAssert(~open,['Unmatched "{" in "',unit_name,'"']) ;
  121. open = 1 ;
  122. case '}',
  123. mttAssert(open,['Unmatched "}" in "',unit_name,'"']) ;
  124. open = 0 ;
  125. otherwise,
  126. error(['Unrecognised_keyword "',keyword,'" in "',unit_name,'"']) ;
  127. end
  128. mttAssert(~(open & (next==number_of_statements)),...
  129. ['Missing "}" in "',unit_name,'"']) ;
  130. if (~open) | (next==number_of_statements)
  131. parsing = 0 ;
  132. end
  133. end
  134. abg = mttSetFieldDefault(abg,'input',[]) ;
  135. abg = mttSetFieldDefault(abg,'input_default',[]) ;
  136. abg = mttSetFieldDefault(abg,'numpar',[]) ;
  137. abg = mttSetFieldDefault(abg,'numpar_default',[]) ;
  138. abg = mttSetFieldDefault(abg,'bondgraph',[]) ;