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.

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