Conversions.java 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package be.uantwerpen.ansymo.semanticadaptation.cg.cpp.generation;
  2. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.SVType;
  3. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.data.SVVariability;
  4. import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.exceptions.InvalidConversionException;
  5. public class Conversions {
  6. public static String fmiTypeToCppType(SVType t) throws InvalidConversionException {
  7. switch (t) {
  8. case Real:
  9. return "double";
  10. case Integer:
  11. return "int";
  12. case Boolean:
  13. return "bool";
  14. case String:
  15. return "string";
  16. default:
  17. throw new InvalidConversionException("The value type: " + t + " is invalid.");
  18. }
  19. }
  20. public static SVVariability fmiTypeToFmiVariability(SVType t) throws InvalidConversionException {
  21. switch (t) {
  22. case Real:
  23. return SVVariability.continuous;
  24. case Integer:
  25. case Boolean:
  26. case String:
  27. return SVVariability.discrete;
  28. default:
  29. throw new InvalidConversionException("The value type: " + t + " is invalid.");
  30. }
  31. }
  32. public static String fmiTypeToCppDefaultValue(SVType t) throws InvalidConversionException {
  33. switch (t) {
  34. case Real:
  35. return "0.0";
  36. case Integer:
  37. return "0";
  38. case Boolean:
  39. return "false";
  40. case String:
  41. return "\"\"";
  42. default:
  43. throw new InvalidConversionException("The value type: " + t + " is invalid.");
  44. }
  45. }
  46. public static SVType typeDecider(SVType t1, SVType t2) throws InvalidConversionException {
  47. switch (t1) {
  48. case Real:
  49. switch (t2) {
  50. case Real:
  51. case Integer:
  52. return SVType.Real;
  53. default:
  54. throw new InvalidConversionException(
  55. "The type: " + t1.name() + " is unmergable with " + t2.name() + ".");
  56. }
  57. case Integer:
  58. switch (t2) {
  59. case Real:
  60. return SVType.Real;
  61. case Integer:
  62. return SVType.Integer;
  63. default:
  64. throw new InvalidConversionException(
  65. "The type: " + t1.name() + " is unmergable with " + t2.name() + ".");
  66. }
  67. case Boolean:
  68. switch (t2) {
  69. case Boolean:
  70. return SVType.Boolean;
  71. default:
  72. throw new InvalidConversionException(
  73. "The type: " + t1.name() + " is unmergable with " + t2.name() + ".");
  74. }
  75. case String:
  76. switch (t2) {
  77. case String:
  78. return SVType.String;
  79. default:
  80. throw new InvalidConversionException(
  81. "The type: " + t1.name() + " is unmergable with " + t2.name() + ".");
  82. }
  83. default:
  84. throw new InvalidConversionException("The type: " + t1 + " is invalid.");
  85. }
  86. }
  87. }