LTLTests.scala 3.72 KB
Newer Older
Christian Müller's avatar
Christian Müller committed
1
2
3
4
5
6
7
8
9
package de.tum.workflows.ltl.tests

import org.scalatest._
import org.scalatest.Matchers._
import org.scalatest.Inspectors._

import de.tum.workflows.foltl.FOLTL._
import de.tum.workflows.ltl.LTL
import de.tum.workflows.Implicits._
10
import de.tum.workflows.WorkflowParser
Christian Müller's avatar
Christian Müller committed
11
import de.tum.workflows.foltl.FormulaFunctions
Christian Müller's avatar
Christian Müller committed
12
13
14

class LTLSpec extends FlatSpec {

15
16
  "FOLTL simplification" should "simplify double negation" in {
    Neg(Neg(True)).simplify() should be(True)
Christian Müller's avatar
Christian Müller committed
17
  }
18
19

  "universal elimination" should "work" in {
Christian Müller's avatar
Christian Müller committed
20
    val f = Globally(Forall(List("x", "y"), Fun("f", List("x", "y"))))
21
22
23

    LTL.eliminateUniversals(f, List("a")) should be(Globally(Fun("f", List("a", "a"))))

Christian Müller's avatar
Christian Müller committed
24
    val f2 = Exists("x", Forall(List("y"), Fun("f", List("x", "y"))))
Christian Müller's avatar
Christian Müller committed
25
    LTL.eliminateUniversals(f2, List()) should be(Exists("x", Fun("f", List("x", "x"))))
26
27
28
    
    val f3 = Forall(List("x","y"), Fun("f", List("x", "y")))
    val r = """
Christian Müller's avatar
Christian Müller committed
29
        (f(x,x) ∧ f(x,y) ∧ f(y,x) ∧ f(y,y))   
30
31
        """
    LTL.eliminateUniversals(f3, List("x", "y")) should be(WorkflowParser.parseTerm(r).get)
32
  }
Christian Müller's avatar
Christian Müller committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  
  it should "generate the correct types" in {
    
    val x = Var("x", "agent")
    val p = Var("p","paper")
    val f = Globally(Forall(List(x, p), Fun("f", List(x, p))))
    
    LTL.eliminateUniversals(f, List(x, p)) should be (
      Globally(Fun("f", List(x,p)))    
    )
    
    val x2 = Var("x2", "agent")
    val f2 = Forall(List(x, p), Fun("f", List(x, p)))
    val r2 = And(Fun("f", List(x,p)), Fun("f", List(x2, p)))
    LTL.eliminateUniversals(f2, List(x, p, x2)) should be(r2)    
    
    val f3 = Forall(List(x, x2, p), Fun("f", List(x, x2, p)))
    val r3 = And.make(Fun("f", List(x,x,p)), Fun("f", List(x, x2, p)), Fun("f", List(x2,x,p)), Fun("f", List(x2,x2,p))) 
    LTL.eliminateUniversals(f3, List(x, x2, p)) should be(r3)
  }
53
54
55
56
57

  "Existential elimination" should "work" in {
    val test = Exists("a", And(Globally(Exists("b", True)), Exists("c", Forall("d", Exists("e", False)))))
    LTL.eliminateExistentials(test) should be(
      (List(Var("a"), Var("c")), And(Globally(Exists("b", True)), Forall("d", Exists("e", False)))))
Christian Müller's avatar
Christian Müller committed
58
  }
59
60
61
62
63
64
65
66
67
68
  
  "String generation" should "work" in {
    
    val f = And(And(True, False), And(And(True, False), Globally(False)))
    
    f.toString should be (
      "True ∧ False ∧ True ∧ False ∧ G False"
    )
  }
  
Christian Müller's avatar
Christian Müller committed
69
70
71
  "Existential Elimination" should "work in AE formulae" in {
    val f = Forall(List("x","y"), Exists("z", Fun("f", List("x","z"))))
    
Christian Müller's avatar
Christian Müller committed
72
    LTL.instantiateExistentials(f) should be (
Christian Müller's avatar
Christian Müller committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
      Forall(List("x","y"), Or(Fun("f", List("x","x")), Fun("f", List("x","y")))) 
    )
   }
  
  "hasSubFormula" should "find subformulas" in {
    val f = Forall(List("x","y"), Exists("z", Fun("f", List("x","z"))))
    val f2 = And("b", Or("c", f))
    
    f2.hasSubFormula({
      case inner if f == inner => true
    }) should be (true)
    
        
    f2.hasSubFormula({
      case inner if f == inner => false
    }) should be (false)
    
    f2.hasSubFormula({
      case something => false
    }) should be (false)
    
    f2.hasSubFormula({
      case And(f1, f2) => true
      case Forall(vars, f) => false
    }) should be (true)
    
    f2.hasSubFormula({
      case Forall(vars, f) => true
    }) should be (true)
  }
  
  "removePredicate" should "remove correct things" in {
    val f = And(Or(Fun("f", "x"), Neg(Fun("f","x"))), "y")
    
    FormulaFunctions.eliminatePredicate(f, "f") should be (
      Var("y")    
    )
    
    val f2 = And(Or(Fun("f", "x"), Neg(Fun("f","y"))), "z")
    
    FormulaFunctions.eliminatePredicate(f2, "f") should be (
      False
    )   
    
    val f3 = Forall("x", And(Or(Fun("f", "x"), Neg(Fun("f","y"))), "z"))
    
    FormulaFunctions.eliminatePredicate(f3, "f") should be (
      False
    )  
  }
123
  
Christian Müller's avatar
Christian Müller committed
124
}