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.

173 lines
6.5 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 ._config import *
  6. from .logic import *
  7. from pycarl import Variable # needed for building parametric models
  8. __version__ = "unknown"
  9. try:
  10. from _version import __version__
  11. except ImportError:
  12. # We're running in a tree that doesn't have a _version.py, so we don't know what our version is.
  13. pass
  14. core._set_up("")
  15. def build_model(program, properties=None):
  16. """
  17. Build a model from a symbolic description.
  18. :param PrismProgram program: Prism program to translate into a model.
  19. :param List[Property] properties: List of properties that should be preserved during the translation. If None, then all properties are preserved.
  20. :return: Model in sparse representation.
  21. :rtype: SparseDtmc or SparseMdp
  22. """
  23. if properties:
  24. formulae = [prop.raw_formula for prop in properties]
  25. intermediate = core._build_sparse_model_from_prism_program(program, formulae)
  26. else:
  27. intermediate = core._build_sparse_model_from_prism_program(program)
  28. assert not intermediate.supports_parameters
  29. if intermediate.model_type == ModelType.DTMC:
  30. return intermediate._as_dtmc()
  31. elif intermediate.model_type == ModelType.MDP:
  32. return intermediate._as_mdp()
  33. else:
  34. raise RuntimeError("Not supported non-parametric model constructed")
  35. def build_parametric_model(program, properties=None):
  36. """
  37. Build a parametric model from a symbolic description.
  38. :param PrismProgram program: Prism program with open constants to translate into a parametric model.
  39. :param List[Property] properties: List of properties that should be preserved during the translation. If None, then all properties are preserved.
  40. :return: Parametric model in sparse representation.
  41. :rtype: SparseParametricDtmc or SparseParametricMdp
  42. """
  43. if properties:
  44. formulae = [prop.raw_formula for prop in properties]
  45. else:
  46. formulae = []
  47. intermediate = core._build_sparse_parametric_model_from_prism_program(program, formulae)
  48. assert intermediate.supports_parameters
  49. if intermediate.model_type == ModelType.DTMC:
  50. return intermediate._as_pdtmc()
  51. elif intermediate.model_type == ModelType.MDP:
  52. return intermediate._as_pmdp()
  53. else:
  54. raise RuntimeError("Not supported parametric model constructed")
  55. def build_model_from_drn(file):
  56. """
  57. Build a model from the explicit DRN representation.
  58. :param String file: DRN file containing the model.
  59. :return: Model in sparse representation.
  60. :rtype: SparseDtmc or SparseMdp or SparseCTMC or SparseMA
  61. """
  62. intermediate = core._build_sparse_model_from_drn(file)
  63. assert not intermediate.supports_parameters
  64. if intermediate.model_type == ModelType.DTMC:
  65. return intermediate._as_dtmc()
  66. elif intermediate.model_type == ModelType.MDP:
  67. return intermediate._as_mdp()
  68. elif intermediate.model_type == ModelType.CTMC:
  69. return intermediate._as_ctmc()
  70. elif intermediate.model_type == ModelType.MA:
  71. return intermediate._as_ma()
  72. else:
  73. raise RuntimeError("Not supported non-parametric model constructed")
  74. def build_parametric_model_from_drn(file):
  75. """
  76. Build a parametric model from the explicit DRN representation.
  77. :param String file: DRN file containing the model.
  78. :return: Parametric model in sparse representation.
  79. :rtype: SparseParametricDtmc or SparseParametricMdp or SparseParametricCTMC or SparseParametricMA
  80. """
  81. intermediate = core._build_sparse_parametric_model_from_drn(file)
  82. assert intermediate.supports_parameters
  83. if intermediate.model_type == ModelType.DTMC:
  84. return intermediate._as_pdtmc()
  85. elif intermediate.model_type == ModelType.MDP:
  86. return intermediate._as_pmdp()
  87. elif intermediate.model_type == ModelType.CTMC:
  88. return intermediate._as_pctmc()
  89. elif intermediate.model_type == ModelType.MA:
  90. return intermediate._as_pma()
  91. else:
  92. raise RuntimeError("Not supported parametric model constructed")
  93. def perform_bisimulation(model, properties, bisimulation_type):
  94. """
  95. Perform bisimulation on model.
  96. :param model: Model.
  97. :param properties: Properties to preserve during bisimulation.
  98. :param bisimulation_type: Type of bisimulation (weak or strong).
  99. :return: Model after bisimulation.
  100. """
  101. formulae = [prop.raw_formula for prop in properties]
  102. if model.supports_parameters:
  103. return core._perform_parametric_bisimulation(model, formulae, bisimulation_type)
  104. else:
  105. return core._perform_bisimulation(model, formulae, bisimulation_type)
  106. def model_checking(model, property):
  107. """
  108. Perform model checking on model for property.
  109. :param model: Model.
  110. :param property: Property to check for.
  111. :return: Model checking result.
  112. :rtype: CheckResult
  113. """
  114. if model.supports_parameters:
  115. task = core.ParametricCheckTask(property.raw_formula, False)
  116. return core._parametric_model_checking_sparse_engine(model, task)
  117. else:
  118. task = core.CheckTask(property.raw_formula, False)
  119. return core._model_checking_sparse_engine(model, task)
  120. def compute_prob01_states(model, phi_states, psi_states):
  121. """
  122. Compute prob01 states for properties of the form phi_states until psi_states
  123. :param SparseDTMC model:
  124. :param BitVector phi_states:
  125. :param BitVector psi_states:
  126. """
  127. if model.model_type != ModelType.DTMC:
  128. raise ValueError("Prob 01 is only defined for DTMCs -- model must be a DTMC")
  129. if model.supports_parameters:
  130. return core._compute_prob01states_rationalfunc(model, phi_states, psi_states)
  131. else:
  132. return core._compute_prob01states_double(model, phi_states, psi_states)
  133. def compute_prob01min_states(model, phi_states, psi_states):
  134. if model.model_type == ModelType.DTMC:
  135. return compute_prob01_states(model, phi_states, psi_states)
  136. if model.supports_parameters:
  137. return core._compute_prob01states_min_rationalfunc(model, phi_states, psi_states)
  138. else:
  139. return core._compute_prob01states_min_double(model, phi_states, psi_states)
  140. def compute_prob01max_states(model, phi_states, psi_states):
  141. if model.model_type == ModelType.DTMC:
  142. return compute_prob01_states(model, phi_states, psi_states)
  143. if model.supports_parameters:
  144. return core._compute_prob01states_max_rationalfunc(model, phi_states, psi_states)
  145. else:
  146. return core._compute_prob01states_max_double(model, phi_states, psi_states)