Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Christian Müller
NIWO
Commits
eb3e8792
Commit
eb3e8792
authored
Oct 09, 2017
by
Christian Müller
Browse files
testing
parent
5c198d52
Changes
9
Hide whitespace changes
Inline
Side-by-side
src/main/scala/de/tum/workflows/MainInvariants.scala
0 → 100644
View file @
eb3e8792
package
de.tum.workflows
import
foltl.FOLTL._
import
blocks.Workflow._
import
Implicits._
import
com.typesafe.scalalogging.LazyLogging
import
de.tum.workflows.ltl.LTL
import
de.tum.workflows.Utils._
import
de.tum.workflows.foltl.Stubborn
import
de.tum.workflows.foltl.Properties
import
java.io.PrintWriter
import
java.io.File
import
de.tum.workflows.blocks.Workflow
import
de.tum.workflows.blocks.Spec
import
de.tum.workflows.foltl.FormulaFunctions
import
de.tum.workflows.toz3.InvariantChecker
object
Main
extends
App
with
LazyLogging
{
def
generate
(
name
:
String
,
spec
:
Spec
)
{
logger
.
info
(
s
"Encoding Spec:\n$spec"
)
val
t1
=
"pi1"
val
t2
=
"pi2"
def
invariants
=
List
(
InvariantChecker
.
invariantNoninterStubbornBS
_
// InvariantChecker.invariantNoninterStubborn _
// InvariantChecker.invariantAllEqual _
)
def
msgs
=
for
(
inv
<-
invariants
)
yield
{
val
(
t
,
(
safe
,
msg
))
=
time
{
InvariantChecker
.
checkInvariant
(
spec
.
w
,
inv
(
spec
),
true
)
}
s
"Proving Invariant (took $t ms):\n$msg\n"
}
write
(
s
"${name}.inv"
,
msgs
.
mkString
(
"\n"
))
}
def
generateExample
(
name
:
String
)
{
logger
.
info
(
s
"Generating $name"
)
val
spec
=
ExampleWorkflows
.
parseExample
(
name
)
if
(!
spec
.
isDefined
)
{
logger
.
error
(
s
"Not a valid spec: $name"
)
}
spec
.
map
(
generate
(
name
,
_
))
}
def
generateAllExamples
()
{
clear
()
// Fill results alphabetically
for
(
k
<-
ExampleWorkflows
.
examples
.
keys
.
toList
.
sorted
)
{
generateExample
(
k
)
}
}
clear
()
generateExample
(
"nonomitting/conference"
)
// generateExample("tests/declasstest")
// generateAllExamples()
}
\ No newline at end of file
src/main/scala/de/tum/workflows/Main.scala
→
src/main/scala/de/tum/workflows/Main
LTL
.scala
View file @
eb3e8792
...
...
@@ -15,33 +15,9 @@ import de.tum.workflows.blocks.Spec
import
de.tum.workflows.foltl.FormulaFunctions
import
de.tum.workflows.toz3.InvariantChecker
object
Main
extends
App
with
LazyLogging
{
object
Main
LTL
extends
App
with
LazyLogging
{
val
MAXAGENTS
=
8
val
FOLDER
=
"results"
val
checkinvariants
=
true
def
clear
()
{
def
recclear
(
folder
:
File
)
{
for
(
fol
<-
folder
.
listFiles
()
if
fol
.
isDirectory
())
{
recclear
(
fol
)
}
folder
.
listFiles
().
foreach
(
_
.
delete
())
}
val
fol
=
new
File
(
FOLDER
)
fol
.
mkdirs
()
recclear
(
fol
)
}
def
write
(
name
:
String
,
prop
:
String
)
{
val
file
=
new
File
(
name
)
file
.
getParentFile
.
mkdirs
()
val
writer
=
new
PrintWriter
(
file
)
writer
.
print
(
prop
)
writer
.
close
()
}
def
writeExample
(
name
:
String
,
spec
:
Spec
,
prop
:
Formula
)
{
...
...
@@ -95,25 +71,7 @@ object Main extends App with LazyLogging {
if
(!
spec
.
causals
.
isEmpty
)
{
logger
.
info
(
s
"Computing noninterference for target ${spec.target} using causal agents ${spec.causals}"
)
val
cprop
=
Properties
.
noninterCausal
(
spec
)
writeExample
(
s
"$FOLDER/${name}_causal"
,
spec
,
cprop
)
}
if
(
checkinvariants
)
{
def
invariants
=
List
(
InvariantChecker
.
invariantNoninterStubbornBS
_
// InvariantChecker.invariantNoninterStubborn _
// InvariantChecker.invariantAllEqual _
)
def
msgs
=
for
(
inv
<-
invariants
)
yield
{
val
(
t
,
(
safe
,
msg
))
=
time
{
InvariantChecker
.
checkInvariant
(
spec
.
w
,
inv
(
spec
),
true
)
}
s
"Proving Invariant (took $t ms):\n$msg\n"
}
write
(
s
"$FOLDER/${name}.inv"
,
msgs
.
mkString
(
"\n"
))
writeExample
(
s
"${name}_causal"
,
spec
,
cprop
)
}
}
...
...
src/main/scala/de/tum/workflows/Utils.scala
View file @
eb3e8792
...
...
@@ -2,9 +2,13 @@ package de.tum.workflows
import
de.tum.workflows.blocks._
import
de.tum.workflows.foltl.FOLTL._
import
java.io.File
import
java.io.PrintWriter
object
Utils
{
val
RESULTSFOLDER
=
"results"
def
mkString
[
T
](
string
:
Iterable
[
T
],
start
:
String
,
mid
:
String
,
end
:
String
)
=
{
if
(
string
.
isEmpty
)
""
else
string
.
mkString
(
start
,
mid
,
end
)
}
...
...
@@ -25,11 +29,32 @@ object Utils {
def
indentLines
(
s
:
String
)
=
{
s
.
lines
.
map
(
" "
+
_
).
mkString
(
"\n"
)
}
def
time
[
R
](
block
:
=>
R
)
=
{
def
time
[
R
](
block
:
=>
R
)
=
{
val
t0
=
System
.
nanoTime
()
val
result
=
block
// call-by-name
val
result
=
block
// call-by-name
val
t1
=
System
.
nanoTime
()
((
t1
-
t0
)/
1000000
,
result
)
((
t1
-
t0
)
/
1000000
,
result
)
}
def
clear
()
{
def
recclear
(
folder
:
File
)
{
for
(
fol
<-
folder
.
listFiles
()
if
fol
.
isDirectory
())
{
recclear
(
fol
)
}
folder
.
listFiles
().
foreach
(
_
.
delete
())
}
val
fol
=
new
File
(
RESULTSFOLDER
)
fol
.
mkdirs
()
recclear
(
fol
)
}
def
write
(
name
:
String
,
prop
:
String
)
{
val
file
=
new
File
(
s
"$RESULTSFOLDER/$name"
)
file
.
getParentFile
.
mkdirs
()
val
writer
=
new
PrintWriter
(
file
)
writer
.
print
(
prop
)
writer
.
close
()
}
}
\ No newline at end of file
src/main/scala/de/tum/workflows/foltl/FOLTLTermFunctions.scala
View file @
eb3e8792
...
...
@@ -84,9 +84,19 @@ object FormulaFunctions extends LazyLogging {
if
(
t
==
t1
)
t1
else
simplify
(
t1
)
}
def
eliminateImplication
(
f
:
Formula
)
=
{
def
eliminateImplication
(
f
:
Formula
)
:
Formula
=
{
f
everywhere
{
case
Implies
(
f1
,
f2
)
=>
Or
(
Neg
(
f1
),
f2
)
case
Implies
(
f1
,
f2
)
=>
Or
(
Neg
(
eliminateImplication
(
f1
)),
eliminateImplication
(
f2
))
}
}
def
eliminateEq
(
f
:
Formula
)
:
Formula
=
{
f
everywhere
{
case
Eq
(
f1
,
f2
)
=>
{
val
e1
=
eliminateEq
(
f1
)
val
e2
=
eliminateEq
(
f2
)
And
(
Implies
(
e1
,
e2
),
Implies
(
e2
,
e1
))
}
}
}
...
...
@@ -278,7 +288,9 @@ object FormulaFunctions extends LazyLogging {
// logger.info(s"Formula binds variables more than once - renaming bindings: ${f.pretty}")
FormulaFunctions
.
fixBinding
(
f
,
Set
()).
_1
}
else
f
eliminateImplication
(
bound
).
simplify
()
val
noeq
=
eliminateEq
(
bound
)
eliminateImplication
(
noeq
).
simplify
()
}
def
generateName
(
base
:
Var
,
bound
:
Set
[
Var
])
=
{
...
...
src/main/scala/de/tum/workflows/ltl/LTL.scala
View file @
eb3e8792
...
...
@@ -109,7 +109,7 @@ object LTL extends LazyLogging {
/**
* Instantiates universals in EA formulae.
*
Does not change quantifier structure, but returns formula
in NegNF
*
Resulting formula is in E* with all existentials outermost and
in NegNF
*/
def
instantiateUniversals
(
f
:
Formula
)
=
{
// Check fragment
...
...
src/main/scala/de/tum/workflows/toz3/InvariantChecker.scala
View file @
eb3e8792
...
...
@@ -17,6 +17,7 @@ import org.omg.CORBA.TIMEOUT
import
com.microsoft.z3.Status
import
com.typesafe.scalalogging.LazyLogging
import
de.tum.workflows.foltl.Properties
import
de.tum.workflows.foltl.FormulaFunctions
object
InvariantChecker
extends
LazyLogging
{
...
...
@@ -28,13 +29,15 @@ object InvariantChecker extends LazyLogging {
cfg
.
put
(
"timeout"
,
TIMEOUT
.
toString
())
val
ctx
=
new
Context
(
cfg
)
val
qe
=
ctx
.
mkTactic
(
"qe"
)
val
default
=
ctx
.
mkTactic
(
"smt"
)
val
t
=
ctx
.
andThen
(
qe
,
default
)
// val qe = ctx.mkTactic("qe")
// val default = ctx.mkTactic("smt")
// val t = ctx.andThen(qe, default)
// val s = ctx.mkSolver(t)
val
s
=
ctx
.
mkSolver
(
t
)
val
s
=
ctx
.
mkSolver
()
s
.
add
(
Z3
.
translate
(
f
,
ctx
))
// Checking QFree
s
.
add
(
Z3QFree
.
translate
(
f
,
ctx
))
// Send to z3
val
c
=
s
.
check
()
...
...
@@ -79,11 +82,18 @@ object InvariantChecker extends LazyLogging {
}
val
univfree
=
LTL
.
instantiateUniversals
(
tocheck
)
// univfree is now in E*, so can be solved as SAT Problem
val
stripped
=
FormulaFunctions
.
stripQuantifiers
(
univfree
)
// val prefix = FormulaFunctions.collectQuantifiersSub(univfree)
val
satform
=
LTL
.
eliminatePredicates
(
stripped
)
//
val test3 =
univfree
.pretty()
//
println(test3)
//
val test3 =
stripped
.pretty()
//
println(test3)
checkOnZ3
(
univfree
.
simplify
())
checkOnZ3
(
satform
.
simplify
())
}
def
checkInvariant
(
w
:
Workflow
,
inv
:
Formula
,
stubborn
:
Boolean
)
=
{
...
...
src/main/scala/de/tum/workflows/toz3/Z3.scala
View file @
eb3e8792
...
...
@@ -258,15 +258,15 @@ object Z3 extends LazyLogging {
val
sb
=
new
StringBuilder
()
val
consts
=
model
.
getConstDecls
()
val
vals
=
consts
.
map
(
_
.
apply
())
val
typedvals
=
vals
.
groupBy
(
_
.
getSort
)
sb
++=
"Universe:\n"
for
((
k
,
v
)
<-
typedvals
)
{
sb
++=
s
"Type $k: ${v.mkString("
,
")}\n"
}
//
val consts = model.getConstDecls()
//
//
val vals = consts.map(_.apply())
//
val typedvals = vals.groupBy(_.getSort)
//
//
sb ++= "Universe:\n"
//
for ((k, v) <- typedvals) {
//
sb ++= s"Type $k: ${v.mkString(",")}\n"
//
}
sb
++=
"Evaluations:\n"
val
sortedConsts
=
model
.
getConstDecls
().
sortBy
(
_
.
getName
.
toString
())
...
...
src/main/scala/de/tum/workflows/toz3/Z3QFree.scala
View file @
eb3e8792
...
...
@@ -69,6 +69,7 @@ object Z3QFree extends LazyLogging {
}
case
Var
(
x
,
t
)
=>
{
// FIXME: Here we're just assuming all Variables are Boolean
ctx
.
mkBoolConst
(
x
)
}
}
...
...
src/test/scala/Test.scala
View file @
eb3e8792
...
...
@@ -11,36 +11,31 @@ import com.microsoft.z3.Model
object
Test
extends
App
{
println
(
List
(
true
,
false
,
false
).
reduceLeft
(
_
&&
_
))
println
(
List
(
true
).
reduceLeft
(
_
&&
_
))
println
(
List
(
false
).
reduceLeft
(
_
&&
_
))
println
(
List
().
foldLeft
(
true
)((
_:
Boolean
)
&&
(
_:
Boolean
)))
val
TIMEOUT
=
30000
// in milliseconds
val
easyForm
=
Exists
(
List
(
"x"
),
And
(
Fun
(
"p"
,
List
(
"x"
)),
Next
(
Neg
(
Fun
(
"p"
,
List
(
"x"
))))))
val
cfg
=
new
HashMap
[
String
,
String
]()
cfg
.
put
(
"timeout"
,
TIMEOUT
.
toString
())
val
ctx
=
new
Context
(
cfg
)
val
qe
=
ctx
.
mkTactic
(
"qe"
)
val
default
=
ctx
.
mkTactic
(
"smt"
)
val
t
=
ctx
.
andThen
(
qe
,
default
)
val
s
=
ctx
.
mkSolver
(
t
)
s
.
add
(
Z3
.
translate
(
easyForm
,
ctx
))
val
c
=
s
.
check
()
val
model
=
s
.
getModel
()
println
(
s
"Z3 model:\n${model}"
)
val
consts
=
model
.
getConstDecls
()
println
(
consts
(
0
).
getName
)
// val TIMEOUT = 30000 // in milliseconds
//
// val easyForm = Exists(List("x"), And(Fun("p", List("x")), Next(Neg(Fun("p", List("x"))))))
//
// val cfg = new HashMap[String, String]()
// cfg.put("timeout", TIMEOUT.toString())
//
// val ctx = new Context(cfg)
//
// val qe = ctx.mkTactic("qe")
// val default = ctx.mkTactic("smt")
// val t = ctx.andThen(qe, default)
//
// val s = ctx.mkSolver(t)
//
// s.add(toZ3.translate(easyForm, ctx))
// val c = s.check()
// val model = s.getModel()
//
// println(s"Z3 model:\n${model}")
//
// val consts = model.getConstDecls()
// println(consts(0).getName)
//
// println("MODEL:")
// toZ3.printModel(model)
println
(
"MODEL:"
)
Z3
.
printModel
(
model
)
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment