The name of the initial branch for new projects is now "main" instead of "master". Existing projects remain unchanged. More information: https://doku.lrz.de/display/PUBLIC/GitLab

SARA.hs 2.38 KB
Newer Older
1 2
{-# LANGUAGE OverloadedStrings #-}

3
module Printer.SARA
4
    (printProperties)
5 6
where

7 8 9
import qualified Data.ByteString.Lazy as L
import Data.ByteString.Builder
import Data.Monoid
10 11 12 13 14

import Printer
import PetriNet
import Property

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
renderSimpleTerm :: Integer -> Term -> Builder
renderSimpleTerm fac (Var x) = if fac == 1 then stringUtf8 x
                               else integerDec fac <> stringUtf8 x
renderSimpleTerm fac (Const c) = integerDec (fac*c)
renderSimpleTerm fac (Const c :*: t) = renderSimpleTerm (fac*c) t
renderSimpleTerm fac (t :*: Const c) = renderSimpleTerm (fac*c) t
renderSimpleTerm fac (Minus t) = renderSimpleTerm (-fac) t
renderSimpleTerm _ t = error $ "term not supported for sara: " <> show t

renderTerm :: Term -> Builder
renderTerm (t :+: u) = renderTerm t <> "+" <> renderSimpleTerm 1 u
renderTerm (t :-: u) = renderTerm t <> "+" <> renderSimpleTerm (-1) u
renderTerm t = renderSimpleTerm 1 t

renderOp :: Op -> Builder
renderOp Ge = ">"
renderOp Eq = ":"
renderOp Le = "<"
renderOp op = error $ "operand not supported for sara: " <> show op

renderLinIneq :: LinearInequation -> Builder
renderLinIneq (LinIneq lhs op (Const c)) =
        renderTerm lhs <> renderOp op <> integerDec c
renderLinIneq l = error $ "linear inequation not supported for sara: " <> show l

renderFormula :: Formula -> Builder
renderFormula (Atom a) = renderLinIneq a
renderFormula (Neg _) = error "negation not supported for sara"
renderFormula (p :&: q) = renderFormula p <> "," <> renderFormula q
renderFormula f = error $ "formula not supported for sara: " <> show f

renderProperty :: String -> PetriNet -> Property -> Builder
renderProperty filename net (Property propname Safety f) =
        "PROBLEM " <> stringUtf8 (validateId propname) <> ":\n" <>
        "GOAL REACHABILITY;\n" <>
        "FILE " <> stringUtf8 (reverse (takeWhile (/='/') (reverse filename)))
            <> " TYPE LOLA;\n" <>
        "INITIAL " <> intercalate ","
            (map (\(p,i) -> stringUtf8 p <> ":" <> integerDec i) (initials net))
            <> ";\n" <>
        "FINAL COVER;\n" <>
        "CONSTRAINTS " <> renderFormula f <> ";"
renderProperty _ _ (Property _ Liveness _) =
58
        error "liveness property not supported for sara"
59 60 61 62 63

printProperties :: String -> PetriNet -> [Property] -> L.ByteString
printProperties filename net props =
        toLazyByteString $ intercalate "\n" $
            map (renderProperty filename net) props