/DkalTranslator/DkalObjects.cs

# · C# · 459 lines · 313 code · 91 blank · 55 comment · 1 complexity · 8989455f82afa6db43c13b445e9d861c MD5 · raw file

  1. // *********************************************************
  2. //
  3. // Copyright (c) Microsoft. All rights reserved.
  4. // This code is licensed under the Apache License, Version 2.0.
  5. // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
  6. // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
  7. // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
  8. // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
  9. //
  10. // *********************************************************
  11. using System;
  12. using System.Collections;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Text;
  16. using TUVienna.CS_CUP.Runtime;
  17. /* Class definitions for the data structure needed when parsing a dkal spec. They represent
  18. * each type of dkal constructions.
  19. * These objects are then taken by a some translator (Z3 or Formula) to produce the final translation.
  20. */
  21. namespace DkalTranslator
  22. {
  23. /* Base class for facts, queries and communications
  24. */
  25. public abstract class Spec {
  26. }
  27. /* Principal knows knowledge
  28. */
  29. public class Fact : Spec
  30. {
  31. Principal principal;
  32. Infon knowledge;
  33. public Fact(Principal principal, Infon knowledge)
  34. {
  35. this.principal = principal;
  36. this.knowledge = knowledge;
  37. }
  38. public Principal getPrincipal() {
  39. return this.principal;
  40. }
  41. public Infon getKnowledge() {
  42. return this.knowledge;
  43. }
  44. }
  45. /*?Principal knows knowledge
  46. */
  47. public class Query : Spec {
  48. Fact fact;
  49. public Query(Fact fact) {
  50. this.fact = fact;
  51. }
  52. public Fact getFact() {
  53. return this.fact;
  54. }
  55. }
  56. /*Base class for TO and FROM communication assertions
  57. */
  58. public class Communication : Spec {
  59. Principal principalSource;
  60. Principal principalDest;
  61. Proviso body;
  62. Infon conditional;
  63. public Communication() { }
  64. public Communication(Principal principalSource, Principal principalDest, Proviso body, Infon conditional) {
  65. this.principalSource = principalSource;
  66. this.principalDest = principalDest;
  67. this.body = body;
  68. this.conditional = conditional;
  69. }
  70. public Communication(Principal principalSource, Principal principalDest, Proviso body)
  71. {
  72. this.principalSource = principalSource;
  73. this.principalDest = principalDest;
  74. this.body = body;
  75. this.conditional = new TrueInfon();
  76. }
  77. public Principal getSource() {
  78. return principalSource;
  79. }
  80. public Principal getDest()
  81. {
  82. return principalDest;
  83. }
  84. public Infon getConditional() {
  85. return conditional;
  86. }
  87. public Infon getMessage() {
  88. return body.getMessage();
  89. }
  90. public bool hasProviso() {
  91. return body.hasProviso();
  92. }
  93. public Infon getProviso() {
  94. return this.body.getProviso();
  95. }
  96. }
  97. /*Principal TO Principal:[message] <= condition
  98. */
  99. public class To : Communication {
  100. public To(Principal principalSource, Principal principalDest, Proviso body, Infon conditional)
  101. : base(principalSource, principalDest, body, conditional){
  102. }
  103. public To(Principal principalSource, Principal principalDest, Proviso body)
  104. : base(principalSource, principalDest, body)
  105. {
  106. }
  107. }
  108. /*Principal FROM Principal:[message] <= condition
  109. */
  110. public class From : Communication
  111. {
  112. public From(Principal principalSource, Principal principalDest, Proviso body, Infon conditional)
  113. : base(principalSource, principalDest, body, conditional)
  114. {
  115. }
  116. public From(Principal principalSource, Principal principalDest, Proviso body)
  117. : base(principalSource, principalDest, body)
  118. {
  119. }
  120. }
  121. /* A message, with an optional proviso
  122. */
  123. public class Proviso {
  124. Infon message;
  125. Infon proviso;
  126. public Proviso(Infon main, Infon proviso) {
  127. this.message = main;
  128. this.proviso = proviso;
  129. }
  130. public Proviso(Infon main)
  131. {
  132. this.message = main;
  133. }
  134. public Infon getMessage() {
  135. return message;
  136. }
  137. public Infon getProviso(){
  138. return proviso;
  139. }
  140. public bool hasProviso(){
  141. return proviso != null;
  142. }
  143. }
  144. /*A principal, with a flag that indicates whether it is a free variable (for communication assertions)
  145. *
  146. */
  147. public class Principal
  148. {
  149. String name;
  150. bool isVariable;
  151. public Principal(String name)
  152. {
  153. this.name = name;
  154. this.isVariable = false;
  155. }
  156. public Principal(String name, bool isVariable)
  157. {
  158. this.name = name;
  159. this.isVariable = isVariable;
  160. }
  161. public string getName() {
  162. return name;
  163. }
  164. public bool isVar() {
  165. return isVariable;
  166. }
  167. }
  168. /* Base class for Infons. An infon has a list of free variables that are used in the translation
  169. */
  170. public abstract class Infon
  171. {
  172. //Free variables of an Infon
  173. protected System.Collections.Generic.List<Variable> varList = new System.Collections.Generic.List<Variable>();
  174. public Infon() {
  175. }
  176. public System.Collections.Generic.List<Variable> getVariableList()
  177. {
  178. return varList;
  179. }
  180. }
  181. public class TrueInfon : Infon
  182. {
  183. }
  184. /* The type of a variable is filled during parsing.
  185. */
  186. public class Variable : Infon
  187. {
  188. string name;
  189. string type;
  190. public Variable(string name) {
  191. this.name = name;
  192. this.getVariableList().Add(this);
  193. }
  194. public string getName() {
  195. return this.name;
  196. }
  197. public void setType(string type) {
  198. this.type = type;
  199. }
  200. public string getType() {
  201. return this.type;
  202. }
  203. }
  204. public class Plus : Infon
  205. {
  206. Infon left;
  207. Infon right;
  208. public Plus(Infon left, Infon right) {
  209. this.left = left;
  210. this.right = right;
  211. this.getVariableList().AddRange(left.getVariableList());
  212. this.getVariableList().AddRange(right.getVariableList());
  213. }
  214. public Infon getLeft(){
  215. return this.left;
  216. }
  217. public Infon getRight(){
  218. return this.right;
  219. }
  220. }
  221. public class Implies : Infon
  222. {
  223. Infon left;
  224. Infon right;
  225. public Implies(Infon left, Infon right) {
  226. this.left = left;
  227. this.right = right;
  228. this.getVariableList().AddRange(left.getVariableList());
  229. this.getVariableList().AddRange(right.getVariableList());
  230. }
  231. public Infon getLeft()
  232. {
  233. return this.left;
  234. }
  235. public Infon getRight()
  236. {
  237. return this.right;
  238. }
  239. }
  240. /*Base class for Said and Put
  241. */
  242. public class SaidPut : Infon {
  243. Principal principal;
  244. Infon knowledge;
  245. public SaidPut() { }
  246. public SaidPut(Principal principal, Infon knowledge)
  247. {
  248. this.principal = principal;
  249. this.knowledge = knowledge;
  250. this.getVariableList().AddRange(knowledge.getVariableList());
  251. }
  252. public SaidPut(Infon knowledge)
  253. {
  254. this.knowledge = knowledge;
  255. }
  256. public void setPrincipal(Principal principal) {
  257. this.principal = principal;
  258. }
  259. public Principal getPrincipal() {
  260. return this.principal;
  261. }
  262. public Infon getKnowledge() {
  263. return this.knowledge;
  264. }
  265. }
  266. public class Said : SaidPut
  267. {
  268. public Said(){}
  269. public Said(Principal principal, Infon knowledge)
  270. : base(principal, knowledge)
  271. {
  272. }
  273. public Said(Infon knowledge)
  274. : base(knowledge)
  275. {
  276. }
  277. }
  278. public class Put : SaidPut
  279. {
  280. public Put(){}
  281. public Put(Principal principal, Infon knowledge)
  282. : base(principal, knowledge)
  283. {
  284. }
  285. public Put(Infon knowledge)
  286. : base(knowledge)
  287. {
  288. }
  289. }
  290. /*A function has a name and a list of arguments
  291. */
  292. public class Function : Infon {
  293. string name;
  294. System.Collections.ArrayList arguments;
  295. public Function(string constant) {
  296. this.name = constant;
  297. this.arguments = new System.Collections.ArrayList();
  298. }
  299. public Function(string name, System.Collections.ArrayList arguments) {
  300. this.name = name;
  301. this.arguments = arguments;
  302. foreach (Infon arg in arguments) {
  303. this.getVariableList().AddRange(arg.getVariableList());
  304. }
  305. }
  306. public string getName() {
  307. return this.name;
  308. }
  309. public ArrayList getArguments() {
  310. return this.arguments;
  311. }
  312. }
  313. /* This class is here because C# CUP does not allow ">" and "<" in the specification file.
  314. * Acts only as a synonym
  315. */
  316. public class SpecList : System.Collections.Generic.List<Spec> {
  317. }
  318. /* Idem
  319. */
  320. public class StringList : System.Collections.Generic.List<string> {
  321. }
  322. /* Idem
  323. */
  324. public class FunctionDictionary : System.Collections.Generic.Dictionary<string, ArrayList> {
  325. }
  326. /* Idem
  327. */
  328. public class FixedFunctionList : System.Collections.Generic.List<FunctionValDefinition> {
  329. }
  330. /* This is to correct a bug in C# CUP. After generating the CUP files, mStack must be replaced
  331. * with dstack in parser.cs
  332. */
  333. public class dstack : System.Collections.Stack
  334. {
  335. public dstack(Stack origin) : base(origin) { }
  336. public object elementAt(int index)
  337. {
  338. /* string tmp = (string)((Symbol)(base.ToArray()[index])).value;*/
  339. return base.ToArray()[index];
  340. }
  341. }
  342. /* Class that represents the definition of the value of a function for a fixed set of arguments.
  343. */
  344. public class FunctionValDefinition
  345. {
  346. string functionName;
  347. List<string> arguments;
  348. string value;
  349. public FunctionValDefinition(string functionName, List<string> arguments, string value)
  350. {
  351. this.functionName = functionName;
  352. this.arguments = arguments;
  353. this.value = value;
  354. }
  355. public string getFunctionName()
  356. {
  357. return functionName;
  358. }
  359. public List<string> getArguments()
  360. {
  361. return this.arguments;
  362. }
  363. public string getValue()
  364. {
  365. return this.value;
  366. }
  367. }
  368. }