mttFetchDomain.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. function model = mttFetchDomain(filename)
  2. model.representation = 'domain' ;
  3. mttAssert(mttFileExists(filename),...
  4. ['File "',filename,'" not found']) ;
  5. mttNotify([' ...processing ',filename]) ;
  6. mttWriteNewLine ;
  7. model.source = mttCutText(filename,'_domain.txt') ;
  8. content = mttReadFile(filename) ;
  9. statements = mttExtractStatements(content) ;
  10. number_of_statements = length(statements) ;
  11. next = 0 ;
  12. parsing = 1 ;
  13. while parsing
  14. next = next + 1 ;
  15. statement = statements{next} ;
  16. [keyword,line] = mttSeparateText(statement) ;
  17. switch keyword
  18. case {'domain','public_domain','private_domain'},
  19. domain_name = line ;
  20. mttValidateName(domain_name) ;
  21. [domain,next] = fetch_domain(statements,next,domain_name) ;
  22. model = setfield(model,'item',domain_name,domain) ;
  23. is_private = strcmp(keyword,'private_domain') ;
  24. model = setfield(model,'item',domain_name,'is_private',is_private) ;
  25. case {'multi_domain','public_multi_domain','private_multi_domain'},
  26. domain_name = line ;
  27. mttValidateName(domain_name) ;
  28. [domain,next] = fetch_multi_domain(statements,next,domain_name) ;
  29. model = setfield(model,'item',domain_name,domain) ;
  30. is_private = strcmp(keyword,'private_multi_domain') ;
  31. model = setfield(model,'item',domain_name,'is_private',is_private) ;
  32. case '{',
  33. error('Unexpected "{" found') ;
  34. case '}',
  35. error('Unexpected "}" found') ;
  36. otherwise,
  37. error(['Unrecognised top-level keyword "',keyword,'"']) ;
  38. end
  39. if next==number_of_statements
  40. parsing = 0 ;
  41. end
  42. end
  43. function [domain,next] = fetch_domain(statements,next,domain_name)
  44. domain = [] ;
  45. unit_name = 'domain' ;
  46. number_of_statements = length(statements) ;
  47. effort_declared = 0 ;
  48. flow_declared = 0 ;
  49. open = 0 ;
  50. parsing = 1 ;
  51. while parsing
  52. next = next + 1 ;
  53. statement = statements{next} ;
  54. [keyword,line] = mttSeparateText(statement) ;
  55. switch keyword
  56. case 'effort',
  57. mttAssert(open,...
  58. ['"effort" declarations must be contained inside {...} in "',unit_name,'"']) ;
  59. mttAssert(~effort_declared,...
  60. ['"effort" declarations must be unique in "',unit_name,'"']) ;
  61. mttAssert(~isempty(line),...
  62. ['Undefined "effort" in "',unit_name,'"']) ;
  63. [covar,default] = mttCutText(line,'=') ;
  64. mttValidateName(covar) ;
  65. domain.covar.effort = covar ;
  66. domain.covar.effort_default = default ;
  67. effort_declared = 1 ;
  68. case 'flow',
  69. mttAssert(open,...
  70. ['"flow" declarations must be contained inside {...} in "',unit_name,'"']) ;
  71. mttAssert(~flow_declared,...
  72. ['"flow" declarations must be unique in "',unit_name,'"']) ;
  73. mttAssert(~isempty(line),...
  74. ['Undefined "flow" in "',unit_name,'"']) ;
  75. [covar,default] = mttCutText(line,'=') ;
  76. mttValidateName(covar) ;
  77. domain.covar.flow = covar ;
  78. domain.covar.flow_default = default ;
  79. flow_declared = 1 ;
  80. case '{',
  81. mttAssert(~open,['Unmatched "{" in "',unit_name,'"']) ;
  82. open = 1 ;
  83. case '}',
  84. mttAssert(open,['Unmatched "}" in "',unit_name,'"']) ;
  85. open = 0 ;
  86. otherwise,
  87. error(['Unrecognised_keyword "',keyword,'" in "',unit_name,'"']) ;
  88. end
  89. mttAssert(~(open & (next==number_of_statements)),...
  90. ['Missing "}" in "',unit_name,'"']) ;
  91. if (~open) | (next==number_of_statements)
  92. parsing = 0 ;
  93. end
  94. end
  95. function [domain,next] = fetch_multi_domain(statements,next,domain_name)
  96. domain = [] ;
  97. unit_name = 'domain' ;
  98. number_of_statements = length(statements) ;
  99. counter = 0 ;
  100. open = 0 ;
  101. parsing = 1 ;
  102. while parsing
  103. next = next + 1 ;
  104. statement = statements{next} ;
  105. [keyword,line] = mttSeparateText(statement) ;
  106. switch keyword
  107. case 'bond',
  108. mttAssert(open,...
  109. ['"bond" declarations must be contained inside {...} in "',unit_name,'"']) ;
  110. mttAssert(~isempty(line),...
  111. ['Undefined "bond" in "',unit_name,'"']) ;
  112. [bond_name,bond_domain] = mttCutText(line,'[') ;
  113. [bond_domain,rubbish] = mttCutText(bond_domain,']') ;
  114. mttAssert(isempty(rubbish),...
  115. ['Unexpected text after "]" in "',unit_name,'"']) ;
  116. if ~isempty(bond_name)
  117. mttValidateName(bond_name) ;
  118. end
  119. counter = counter + 1 ;
  120. domain.bond(counter).name = bond_name ;
  121. domain.bond(counter).domain = bond_domain ;
  122. case '{',
  123. mttAssert(~open,['Unmatched "{" in "',unit_name,'"']) ;
  124. open = 1 ;
  125. case '}',
  126. mttAssert(open,['Unmatched "}" in "',unit_name,'"']) ;
  127. open = 0 ;
  128. otherwise,
  129. error(['Unrecognised_keyword "',keyword,'" in "',unit_name,'"']) ;
  130. end
  131. mttAssert(~(open & (next==number_of_statements)),...
  132. ['Missing "}" in "',unit_name,'"']) ;
  133. if (~open) | (next==number_of_statements)
  134. parsing = 0 ;
  135. end
  136. end