Newer
Older
AlgebraicDataflowArchitectureModel / AlgebraicDataflowArchitectureModel / src / models / algebra / Symbol.java
  1. package models.algebra;
  2.  
  3. import java.util.List;
  4.  
  5. public class Symbol {
  6. protected String name;
  7. protected String implName;
  8. protected int arity = 0; // -1: variable number
  9. protected Type operatorType = Type.PREFIX;
  10. protected Type implOperatorType = Type.PREFIX;
  11. protected Symbol[] inverses = null;
  12. protected models.algebra.Type[] signature = null;
  13. protected int[] implParamOrder = null;
  14. protected IImplGenerator generator = null;
  15. protected ICalculator calculator = null;
  16. public Symbol(String name) {
  17. this.name = name;
  18. this.implName = name;
  19. this.arity = 0;
  20. }
  21. public Symbol(String name, int arity) {
  22. this.name = name;
  23. this.implName = name;
  24. this.arity = arity;
  25. }
  26. public Symbol(String name, int arity, Type operatorType) {
  27. this(name, arity);
  28. this.operatorType = operatorType;
  29. this.implOperatorType = operatorType;
  30. }
  31. public Symbol(String name, int arity, Type operatorType, String implName, Type implOperatorType) {
  32. this.name = name;
  33. this.implName = implName;
  34. this.arity = arity;
  35. this.operatorType = operatorType;
  36. this.implOperatorType = implOperatorType;
  37. }
  38. public Symbol(String name, int arity, Type operatorType, String implName, Type implOperatorType, int[] implParamOrder) {
  39. this.name = name;
  40. this.implName = implName;
  41. this.arity = arity;
  42. this.operatorType = operatorType;
  43. this.implOperatorType = implOperatorType;
  44. this.implParamOrder = implParamOrder;
  45. }
  46. public Symbol(String name, int arity, Type operatorType, IImplGenerator generator) {
  47. this.name = name;
  48. this.implName = name;
  49. this.arity = arity;
  50. this.operatorType = operatorType;
  51. this.generator = generator;
  52. this.implOperatorType = Type.GENERATIVE;
  53. }
  54. public Symbol(String name, int arity, Type operatorType, IImplGenerator generator, boolean bSideEffect) {
  55. this.name = name;
  56. this.implName = name;
  57. this.arity = arity;
  58. this.operatorType = operatorType;
  59. this.generator = generator;
  60. if (!bSideEffect) {
  61. this.implOperatorType = Type.GENERATIVE;
  62. } else {
  63. this.implOperatorType = Type.GENERATIVE_WITH_SIDE_EFFECT;
  64. }
  65. }
  66. public Symbol(String name, int arity, ICalculator calculator) {
  67. this.name = name;
  68. this.implName = name;
  69. this.arity = arity;
  70. this.calculator = calculator;
  71. }
  72. public Symbol(String name, int arity, Type operatorType, ICalculator calculator) {
  73. this.name = name;
  74. this.implName = name;
  75. this.arity = arity;
  76. this.operatorType = operatorType;
  77. this.implOperatorType = operatorType;
  78. this.calculator = calculator;
  79. }
  80. public Symbol(String name, int arity, Type operatorType, IImplGenerator generator, ICalculator calculator) {
  81. this.name = name;
  82. this.implName = name;
  83. this.arity = arity;
  84. this.operatorType = operatorType;
  85. this.generator = generator;
  86. this.implOperatorType = Type.GENERATIVE;
  87. this.calculator = calculator;
  88. }
  89. public Symbol(String name, int arity, Type operatorType, String implName, Type implOperatorType, ICalculator calculator) {
  90. this.name = name;
  91. this.implName = implName;
  92. this.arity = arity;
  93. this.operatorType = operatorType;
  94. this.implOperatorType = implOperatorType;
  95. this.calculator = calculator;
  96. }
  97. public void setArity(int arity) {
  98. this.arity = arity;
  99. }
  100.  
  101. public int getArity() {
  102. return arity;
  103. }
  104.  
  105. public String getName() {
  106. return name;
  107. }
  108.  
  109. public Type getOperatorType() {
  110. return operatorType;
  111. }
  112. public boolean isInfix() {
  113. return (operatorType == Type.INFIX);
  114. }
  115. public boolean isMethod() {
  116. return (operatorType == Type.METHOD || operatorType == Type.METHOD_WITH_SIDE_EFFECT);
  117. }
  118. public boolean isLambda() {
  119. return (operatorType == Type.LAMBDA);
  120. }
  121.  
  122. public Symbol[] getInverses() {
  123. return inverses;
  124. }
  125.  
  126. public void setInverses(Symbol[] inverses) {
  127. this.inverses = inverses;
  128. }
  129.  
  130. public models.algebra.Type[] getSignature() {
  131. return signature;
  132. }
  133.  
  134. public void setSignature(models.algebra.Type[] signature) {
  135. this.signature = signature;
  136. }
  137.  
  138. public String getImplName() {
  139. return implName;
  140. }
  141.  
  142. public void setImplName(String implName) {
  143. this.implName = implName;
  144. }
  145.  
  146. public Type getImplOperatorType() {
  147. return implOperatorType;
  148. }
  149. public boolean isImplInfix() {
  150. return (implOperatorType == Type.INFIX);
  151. }
  152. public boolean isImplMethod() {
  153. return (implOperatorType == Type.METHOD || implOperatorType == Type.METHOD_WITH_SIDE_EFFECT);
  154. }
  155. public boolean isImplLambda() {
  156. return (implOperatorType == Type.LAMBDA || implOperatorType == Type.LAMBDA_WITH_SIDE_EFFECT);
  157. }
  158. public boolean isImplGenerative() {
  159. return (implOperatorType == Type.GENERATIVE || implOperatorType == Type.GENERATIVE_WITH_SIDE_EFFECT);
  160. }
  161. public boolean isImplWithSideEffect() {
  162. return (implOperatorType == Type.METHOD_WITH_SIDE_EFFECT
  163. || implOperatorType == Type.LAMBDA_WITH_SIDE_EFFECT
  164. || implOperatorType == Type.GENERATIVE_WITH_SIDE_EFFECT);
  165. }
  166.  
  167. public void setImplOperatorType(Type implOperatorType) {
  168. this.implOperatorType = implOperatorType;
  169. }
  170. public int[] getImplParamOrder() {
  171. return implParamOrder;
  172. }
  173. public void setGenerator(IImplGenerator generator) {
  174. this.generator = generator;
  175. }
  176. /**
  177. * Generate the implementation of this symbol
  178. * @param type the type of this symbol
  179. * @param childrenTypes
  180. * @param childrenImpl the implementations of the children
  181. * @param childrenSideEffects (input) an array of the side effects of the children
  182. * @param sideEffect (output) an array of the side effect of this symbol
  183. * @return the implementation
  184. */
  185. public String generate(models.algebra.Type type, models.algebra.Type[] childrenTypes, String[] childrenImpl, String[] childrenSideEffects, String[] sideEffect) {
  186. if (generator != null) {
  187. return generator.generate(type, childrenTypes, childrenImpl, childrenSideEffects, sideEffect);
  188. }
  189. return null;
  190. }
  191. public boolean isCalculatable() {
  192. return (calculator != null);
  193. }
  194. public Expression calculate(List<Expression> args) {
  195. if (calculator != null) {
  196. return calculator.calculate(args);
  197. }
  198. return null;
  199. }
  200.  
  201. public boolean equals(Object another) {
  202. if (!(another instanceof Symbol)) return false;
  203. return name.equals(((Symbol) another).name) && arity == ((Symbol) another).arity;
  204. }
  205. @Override
  206. public int hashCode() {
  207. return name.hashCode();
  208. }
  209. public String toString() {
  210. return name;
  211. }
  212. public String toImplementation() {
  213. return implName;
  214. }
  215. public enum Type {
  216. PREFIX,
  217. INFIX,
  218. METHOD,
  219. METHOD_WITH_SIDE_EFFECT,
  220. LAMBDA,
  221. LAMBDA_WITH_SIDE_EFFECT,
  222. GENERATIVE,
  223. GENERATIVE_WITH_SIDE_EFFECT
  224. }
  225. public Memento createMemento() {
  226. return new Memento(implName, implOperatorType);
  227. }
  228. public void setMemento(Memento memento) {
  229. this.implName = memento.implName;
  230. this.implOperatorType = memento.implOperatorType;
  231. }
  232. public static class Memento {
  233. private String implName;
  234. private Type implOperatorType = Type.PREFIX;
  235. public Memento(String implName, Type implOperatorType) {
  236. this.implName = implName;
  237. this.implOperatorType = implOperatorType;
  238. }
  239. }
  240. public interface IImplGenerator {
  241. /**
  242. * Generate the implementation
  243. * @param type the type of this expression
  244. * @param childrenTypes
  245. * @param children the implementations of the children
  246. * @param childrenSideEffects (input) an array of the side effects of the children
  247. * @param sideEffect (output) an array of the side effect of this generator
  248. * @return the generated implementation
  249. */
  250. public String generate(models.algebra.Type type, models.algebra.Type[] childrenTypes, String children[], String[] childrenSideEffects, String[] sideEffect);
  251. }
  252. public interface ICalculator {
  253. public Expression calculate(List<Expression> args);
  254. }
  255. }