mttCreateEnvironment.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. function model = mttCreateEnvironment(context)
  2. global mtt_env
  3. mttNotify('...setting "env" definition') ;
  4. mttWriteNewLine ;
  5. directory_name = identify_directory_name(context) ;
  6. source_name = identify_source_name(context,directory_name) ;
  7. environment_filename = [source_name,'_env.txt'] ;
  8. model = mttFetchEnvironment(environment_filename) ;
  9. model = establish_path_names(model) ;
  10. mttWriteNewLine ;
  11. mttNotify('...acquiring "domain" source definitions') ;
  12. mttWriteNewLine ;
  13. model = establish_user_domains(model) ;
  14. number_of_domains = mttGetFieldLength(model,'dom') ;
  15. if number_of_domains>0
  16. for i = 1:number_of_domains
  17. model.public_domain(i).representation = model.dom(i).representation ;
  18. model.public_domain(i).source = model.dom(i).source ;
  19. domain = model.dom(i) ;
  20. item_names = mttGetFieldNames(domain,'item') ;
  21. for j = 1:length(item_names)
  22. item_name = item_names{j} ;
  23. item = getfield(domain,'item',item_name) ;
  24. compound_item = mttIdentifyDomainCovariables(item,domain,model) ;
  25. model = setfield(model,'public_domain',{i},'item',item_name,compound_item) ;
  26. end
  27. end
  28. model = hide_private_domains(model) ;
  29. end
  30. model = mttDeleteField(model,'dom') ;
  31. mttWriteNewLine ;
  32. mttNotify('...acquiring "struct" source definitions') ;
  33. mttWriteNewLine ;
  34. model = establish_user_structs(model) ;
  35. number_of_structs = mttGetFieldLength(model,'str') ;
  36. if number_of_structs>0
  37. for i = 1:number_of_structs
  38. model.public_struct(i).representation = model.str(i).representation ;
  39. model.public_struct(i).source = model.str(i).source ;
  40. struct = model.str(i) ;
  41. item_names = mttGetFieldNames(struct,'item') ;
  42. for j = 1:length(item_names)
  43. item_name = item_names{j} ;
  44. item = getfield(struct,'item',item_name) ;
  45. compound_item = mttIdentifyStructVariables(item,struct,model) ;
  46. model = setfield(model,'public_struct',{i},'item',item_name,compound_item) ;
  47. end
  48. end
  49. model = hide_private_structs(model) ;
  50. end
  51. model = mttDeleteField(model,'str') ;
  52. function directory = identify_directory_name(context)
  53. mttAssert(ischar(context),'Context must be specified by name') ;
  54. working_directory = pwd ;
  55. working_directory = strrep(working_directory,'\','/') ;
  56. [system_name,local_directory] = mttDetachText(context,'/') ;
  57. if isempty(system_name)
  58. local_directory = [] ;
  59. end
  60. if isempty(local_directory)
  61. directory = working_directory ;
  62. else
  63. directory = mttLocateDirectory(working_directory,local_directory) ;
  64. end
  65. function source = identify_source_name(context,directory)
  66. [context_name,local_directory] = mttDetachText(context,'/') ;
  67. if isempty(context_name)
  68. context_name = context ;
  69. local_directory = [] ;
  70. end
  71. source = [directory,'/',context_name] ;
  72. function model = establish_path_names(model)
  73. path_names = mttGetFieldNames(model,'path') ;
  74. for n = 1:length(path_names)
  75. path_name = path_names{n} ;
  76. path_spec = getfield(model,'path',path_name) ;
  77. [rubbish,working_directory] = mttDetachText(model.source,'/') ;
  78. directory = identify_directory(working_directory,path_spec,model) ;
  79. mttAssert(~isempty(directory),...
  80. ['Undefined path "',path_name,'"']) ;
  81. model = setfield(model,'path',path_name,directory) ;
  82. end
  83. function directory = identify_directory(working_directory,path_spec,model)
  84. path_names = mttGetFieldNames(model,'path') ;
  85. if isempty(path_spec)
  86. directory = [] ;
  87. else
  88. if path_spec(1)=='$'
  89. [path_alias,path_branch] = mttCutText(path_spec,'/') ;
  90. path_alias(1) = [] ;
  91. mttAssert(ismember(path_alias,path_names),...
  92. ['Path "',path_alias,'" not recognised']) ;
  93. path_root = getfield(model,'path',path_alias) ;
  94. if isempty(path_branch)
  95. directory = path_root ;
  96. else
  97. directory = [path_root,'/',path_branch] ;
  98. end
  99. else
  100. [local_directory,name] = mttCutText(path_spec,'/') ;
  101. directory_located = 0 ;
  102. if strcmp(local_directory,'.')
  103. if isempty(name)
  104. directory = working_directory ;
  105. directory_located = 1 ;
  106. else
  107. local_directory = name ;
  108. end
  109. else
  110. local_directory = path_spec ;
  111. end
  112. if ~directory_located
  113. directory = mttLocateDirectory(working_directory,local_directory) ;
  114. end
  115. end
  116. end
  117. function model = establish_user_domains(model)
  118. path_names = mttGetFieldNames(model,'path') ;
  119. domain_names = mttGetFieldNames(model,'domain') ;
  120. counter = 0 ;
  121. for n = 1:length(domain_names)
  122. domain_name = domain_names{n} ;
  123. domain_spec = getfield(model,'domain',domain_name) ;
  124. [rubbish,working_directory] = mttDetachText(model.source,'/') ;
  125. [domain_source,domain_item] = mttCutText(domain_spec,'::') ;
  126. [name,path_spec] = mttDetachText(domain_source,'/') ;
  127. if isempty(name)
  128. source_name = [working_directory,'/',domain_source] ;
  129. else
  130. directory = identify_directory(working_directory,path_spec,model) ;
  131. source_name = [directory,'/',name] ;
  132. end
  133. domain_filename = [source_name,'_domain.txt'] ;
  134. domain_index = [] ;
  135. if isfield(model,'dom')
  136. existing_doms = {model.dom.source} ;
  137. domain_index = strmatch(source_name,existing_doms,'exact') ;
  138. end
  139. if isempty(domain_index)
  140. counter = counter + 1 ;
  141. model.dom(counter) = mttFetchDomain(domain_filename) ;
  142. domain_index = counter ;
  143. end
  144. domain_spec.dom = domain_index ;
  145. domain_spec.item = domain_item ;
  146. model = setfield(model,'domain',domain_name,domain_spec) ;
  147. end
  148. function model = hide_private_domains(model)
  149. for n = 1:mttGetFieldLength(model,'public_domain')
  150. dom = model.dom(n) ;
  151. dom_items = dom.item ;
  152. domain = model.public_domain(n) ;
  153. domain_items = domain.item ;
  154. item_names = mttGetFieldNames(domain,'item') ;
  155. for j = 1:length(item_names)
  156. item_name = item_names{j} ;
  157. is_private(j) = getfield(dom_items,item_name,'is_private') ;
  158. if is_private(j)
  159. domain_items = mttDeleteField(domain_items,item_name) ;
  160. mttAssert(~isempty(domain_items),...
  161. ['No public domains in ',dom.source]) ;
  162. end
  163. end
  164. model = setfield(model,'public_domain',{n},'item',domain_items) ;
  165. end
  166. user_domain_names = mttGetFieldNames(model,'domain') ;
  167. for i = 1:length(user_domain_names)
  168. user_domain_name = user_domain_names{i} ;
  169. user_domain = getfield(model,'domain',user_domain_name) ;
  170. dom = model.dom(user_domain.dom) ;
  171. if ~isempty(user_domain.item)
  172. is_private = getfield(dom,'item',user_domain.item,'is_private') ;
  173. mttAssert(~is_private,...
  174. ['User-defined domain "',user_domain_name,'" is declared as private']) ;
  175. end
  176. end
  177. function model = establish_user_structs(model)
  178. path_names = mttGetFieldNames(model,'path') ;
  179. struct_names = mttGetFieldNames(model,'struct') ;
  180. counter = 0 ;
  181. for n = 1:length(struct_names)
  182. struct_name = struct_names{n} ;
  183. struct_spec = getfield(model,'struct',struct_name) ;
  184. [rubbish,working_directory] = mttDetachText(model.source,'/') ;
  185. [struct_source,struct_item] = mttCutText(struct_spec,'::') ;
  186. [name,path_spec] = mttDetachText(struct_source,'/') ;
  187. if isempty(name)
  188. source_name = [working_directory,'/',struct_source] ;
  189. else
  190. directory = identify_directory(working_directory,path_spec,model) ;
  191. source_name = [directory,'/',name] ;
  192. end
  193. struct_filename = [source_name,'_struct.txt'] ;
  194. struct_index = [] ;
  195. if isfield(model,'str')
  196. existing_strs = {model.str.source} ;
  197. struct_index = strmatch(source_name,existing_strs,'exact') ;
  198. end
  199. if isempty(struct_index)
  200. counter = counter + 1 ;
  201. model.str(counter) = mttFetchStruct(struct_filename) ;
  202. struct_index = counter ;
  203. end
  204. struct_spec.str = struct_index ;
  205. struct_spec.item = struct_item ;
  206. model = setfield(model,'struct',struct_name,struct_spec) ;
  207. end
  208. function model = hide_private_structs(model)
  209. for n = 1:mttGetFieldLength(model,'public_struct')
  210. str = model.str(n) ;
  211. str_items = str.item ;
  212. struct = model.public_struct(n) ;
  213. struct_items = struct.item ;
  214. item_names = mttGetFieldNames(struct,'item') ;
  215. for j = 1:length(item_names)
  216. item_name = item_names{j} ;
  217. is_private(j) = getfield(str_items,item_name,'is_private') ;
  218. if is_private(j)
  219. struct_items = mttDeleteField(struct_items,item_name) ;
  220. mttAssert(~isempty(struct_items),...
  221. ['No public structs in ',str.source]) ;
  222. end
  223. end
  224. model = setfield(model,'public_struct',{n},'item',struct_items) ;
  225. end
  226. user_struct_names = mttGetFieldNames(model,'struct') ;
  227. for i = 1:length(user_struct_names)
  228. user_struct_name = user_struct_names{i} ;
  229. user_struct = getfield(model,'struct',user_struct_name) ;
  230. str = model.str(user_struct.str) ;
  231. if ~isempty(user_struct.item)
  232. is_private = getfield(str,'item',user_struct.item,'is_private') ;
  233. mttAssert(~is_private,...
  234. ['User-defined struct "',user_struct_name,'" is declared as private']) ;
  235. end
  236. end