You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
5.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. from . import core
  2. from .core import *
  3. from . import storage
  4. from .storage import *
  5. from .version import __version__
  6. from ._config import *
  7. from .logic import *
  8. from pycarl import Variable # needed for building parametric models
  9. core._set_up("")
  10. def build_model(program, properties=None):
  11. """
  12. Build a model from a symbolic description.
  13. :param PrismProgram program: Prism program to translate into a model.
  14. :param List[Property] properties: List of properties that should be preserved during the translation. If None, then all properties are preserved.
  15. :return: Model in sparse representation.
  16. :rtype: SparseDtmc or SparseMdp
  17. """
  18. if properties:
  19. formulae = [prop.raw_formula for prop in properties]
  20. intermediate = core._build_sparse_model_from_prism_program(program, formulae)
  21. else:
  22. intermediate = core._build_sparse_model_from_prism_program(program)
  23. assert not intermediate.supports_parameters
  24. if intermediate.model_type == ModelType.DTMC:
  25. return intermediate._as_dtmc()
  26. elif intermediate.model_type == ModelType.MDP:
  27. return intermediate._as_mdp()
  28. else:
  29. raise RuntimeError("Not supported non-parametric model constructed")
  30. def build_parametric_model(program, properties=None):
  31. """
  32. Build a parametric model from a symbolic description.
  33. :param PrismProgram program: Prism program with open constants to translate into a parametric model.
  34. :param List[Property] properties: List of properties that should be preserved during the translation. If None, then all properties are preserved.
  35. :return: Parametric model in sparse representation.
  36. :rtype: SparseParametricDtmc or SparseParametricMdp
  37. """
  38. if properties:
  39. formulae = [prop.raw_formula for prop in properties]
  40. else:
  41. formulae = []
  42. intermediate = core._build_sparse_parametric_model_from_prism_program(program, formulae)
  43. assert intermediate.supports_parameters
  44. if intermediate.model_type == ModelType.DTMC:
  45. return intermediate._as_pdtmc()
  46. elif intermediate.model_type == ModelType.MDP:
  47. return intermediate._as_pmdp()
  48. else:
  49. raise RuntimeError("Not supported parametric model constructed")
  50. def build_model_from_drn(file):
  51. """
  52. Build a model from the explicit DRN representation.
  53. :param String file: DRN file containing the model.
  54. :return: Model in sparse representation.
  55. :rtype: SparseDtmc or SparseMdp or SparseCTMC or SparseMA
  56. """
  57. intermediate = core._build_sparse_model_from_drn(file)
  58. assert not intermediate.supports_parameters
  59. if intermediate.model_type == ModelType.DTMC:
  60. return intermediate._as_dtmc()
  61. elif intermediate.model_type == ModelType.MDP:
  62. return intermediate._as_mdp()
  63. elif intermediate.model_type == ModelType.CTMC:
  64. return intermediate._as_ctmc()
  65. elif intermediate.model_type == ModelType.MA:
  66. return intermediate._as_ma()
  67. else:
  68. raise RuntimeError("Not supported non-parametric model constructed")
  69. def build_parametric_model_from_drn(file):
  70. """
  71. Build a parametric model from the explicit DRN representation.
  72. :param String file: DRN file containing the model.
  73. :return: Parametric model in sparse representation.
  74. :rtype: SparseParametricDtmc or SparseParametricMdp or SparseParametricCTMC or SparseParametricMA
  75. """
  76. intermediate = core._build_sparse_parametric_model_from_drn(file)
  77. assert intermediate.supports_parameters
  78. if intermediate.model_type == ModelType.DTMC:
  79. return intermediate._as_pdtmc()
  80. elif intermediate.model_type == ModelType.MDP:
  81. return intermediate._as_pmdp()
  82. elif intermediate.model_type == ModelType.CTMC:
  83. return intermediate._as_pctmc()
  84. elif intermediate.model_type == ModelType.MA:
  85. return intermediate._as_pma()
  86. else:
  87. raise RuntimeError("Not supported parametric model constructed")
  88. def perform_bisimulation(model, properties, bisimulation_type):
  89. formulae = [prop.raw_formula for prop in properties]
  90. if model.supports_parameters:
  91. return core._perform_parametric_bisimulation(model, formulae, bisimulation_type)
  92. else:
  93. return core._perform_bisimulation(model, formulae, bisimulation_type)
  94. def model_checking(model, property):
  95. if model.supports_parameters:
  96. task = core.ParametricCheckTask(property.raw_formula, False)
  97. return core._parametric_model_checking_sparse_engine(model, task)
  98. else:
  99. task = core.CheckTask(property.raw_formula, False)
  100. return core._model_checking_sparse_engine(model, task)
  101. def compute_prob01_states(model, phi_states, psi_states):
  102. """
  103. Compute prob01 states for properties of the form phi_states until psi_states
  104. :param SparseDTMC model:
  105. :param BitVector phi_states:
  106. :param BitVector psi_states:
  107. """
  108. if model.model_type != ModelType.DTMC:
  109. raise ValueError("Prob 01 is only defined for DTMCs -- model must be a DTMC")
  110. if model.supports_parameters:
  111. return core._compute_prob01states_rationalfunc(model, phi_states, psi_states)
  112. else:
  113. return core._compute_prob01states_double(model, phi_states, psi_states)
  114. def compute_prob01min_states(model, phi_states, psi_states):
  115. if model.model_type == ModelType.DTMC:
  116. return compute_prob01_states(model, phi_states, psi_states)
  117. if model.supports_parameters:
  118. return core._compute_prob01states_min_rationalfunc(model, phi_states, psi_states)
  119. else:
  120. return core._compute_prob01states_min_double(model, phi_states, psi_states)
  121. def compute_prob01max_states(model, phi_states, psi_states):
  122. if model.model_type == ModelType.DTMC:
  123. return compute_prob01_states(model, phi_states, psi_states)
  124. if model.supports_parameters:
  125. return core._compute_prob01states_max_rationalfunc(model, phi_states, psi_states)
  126. else:
  127. return core._compute_prob01states_max_double(model, phi_states, psi_states)