Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
OUTDATED Verse-library
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AutonomyCourse_ECEIllinois
OUTDATED Verse-library
Commits
ebaaa03b
Commit
ebaaa03b
authored
2 years ago
by
braught2
Browse files
Options
Downloads
Patches
Plain Diff
WIP on graph generation, fix guard issues
parent
bba24c94
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
pythonparser.py
+141
-22
141 additions, 22 deletions
pythonparser.py
with
141 additions
and
22 deletions
pythonparser.py
+
141
−
22
View file @
ebaaa03b
...
...
@@ -14,6 +14,13 @@ import ast
#from cfg import CFG
#import clang.cfg
class
Edge
:
def
__init__
(
self
,
source
,
dest
,
guards
,
resets
):
self
.
source
=
source
self
.
dest
=
dest
self
.
guards
=
guards
self
.
resets
=
resets
class
Statement
:
def
__init__
(
self
,
code
,
mode
,
modeType
):
self
.
code
=
code
...
...
@@ -37,11 +44,11 @@ class Guard(Statement):
def
parseGuard
(
node
,
code
):
#assume guard is a strict comparision (modeType == mode)
if
isinstance
(
node
.
test
,
ast
.
Compare
):
if
(
"
mode
"
in
st
r
(
node
.
test
.
left
.
id
)
):
modeType
=
str
(
node
.
test
.
left
.
id
)
#TODO: get this to work
mode
=
str
(
node
.
test
.
comparators
[
0
].
attr
)
return
Guard
(
ast
.
get_source_segment
(
code
,
node
.
test
),
mode
,
modeType
)
if
is
inst
ance
(
node
.
test
.
comparators
[
0
],
ast
.
Attribute
):
if
(
"
Mode
"
in
str
(
node
.
test
.
comparators
[
0
].
value
.
id
)
):
modeType
=
str
(
node
.
test
.
comparators
[
0
].
value
.
id
)
mode
=
str
(
node
.
test
.
comparators
[
0
].
attr
)
return
Guard
(
ast
.
get_source_segment
(
code
,
node
.
test
),
mode
,
modeType
)
else
:
return
Guard
(
ast
.
get_source_segment
(
code
,
node
.
test
),
None
,
None
)
...
...
@@ -123,6 +130,96 @@ def parsenodelist(code, nodes, addResets, pathsToMe):
return
pathsafterme
def
resetString
(
resets
):
outstr
=
""
for
reset
in
resets
:
outstr
+=
reset
.
code
+
"
;
"
outstr
=
outstr
.
strip
(
"
;
"
)
return
outstr
def
guardString
(
guards
):
return
guards
#modes are the list of all modes in the current vertex
#vertices are all the vertexs
def
getIndex
(
modes
,
vertices
):
#TODO: will this work if ordering is lost, will ordering be lost?
return
vertices
.
index
(
tuple
(
modes
))
#for index in range(0, len(vertices)):
# allMatch = True
# for mode in modes:
# if not (mode in vertices[index]):
# allMatch = False
# if allMatch:
# return index
return
-
1
def
createTransition
(
path
,
vertices
,
modes
):
guards
=
[]
resets
=
[]
modeChecks
=
[]
modeUpdates
=
[]
for
item
in
path
:
if
isinstance
(
item
,
Guard
):
if
not
item
.
isModeCheck
():
guards
.
append
(
item
)
else
:
modeChecks
.
append
(
item
)
if
isinstance
(
item
,
Reset
):
if
not
item
.
isModeUpdate
():
resets
.
append
(
item
)
else
:
modeUpdates
.
append
(
item
)
unfoundSourceModeTypes
=
[]
sourceModes
=
[]
unfoundDestModeTypes
=
[]
destModes
=
[]
for
modeType
in
modes
.
keys
():
foundMode
=
False
for
condition
in
modeChecks
:
#print(condition.modeType)
#print(modeType)
if
condition
.
modeType
==
modeType
:
sourceModes
.
append
(
condition
.
mode
)
foundMode
=
True
if
foundMode
==
False
:
unfoundSourceModeTypes
.
append
(
modeType
)
foundMode
=
False
for
condition
in
modeUpdates
:
if
condition
.
modeType
==
modeType
:
destModes
.
append
(
condition
.
mode
)
foundMode
=
True
if
foundMode
==
False
:
unfoundDestModeTypes
.
append
(
modeType
)
unfoundModes
=
[]
for
modeType
in
unfoundSourceModeTypes
:
unfoundModes
.
append
(
modes
[
modeType
])
unfoundModeCombinations
=
itertools
.
product
(
*
unfoundModes
)
sourceVertices
=
[]
for
unfoundModeCombo
in
unfoundModeCombinations
:
sourceVertex
=
sourceModes
.
copy
()
sourceVertex
.
extend
(
unfoundModeCombo
)
sourceVertices
.
append
(
sourceVertex
)
unfoundModes
=
[]
for
modeType
in
unfoundDestModeTypes
:
unfoundModes
.
append
(
modes
[
modeType
])
unfoundModeCombinations
=
itertools
.
product
(
*
unfoundModes
)
destVertices
=
[]
for
unfoundModeCombo
in
unfoundModeCombinations
:
destVertex
=
destModes
.
copy
()
destVertex
.
extend
(
unfoundModeCombo
)
destVertices
.
append
(
destVertex
)
edges
=
[]
for
source
in
sourceVertices
:
sourceindex
=
getIndex
(
source
,
vertices
)
for
dest
in
destVertices
:
destindex
=
getIndex
(
dest
,
vertices
)
edges
.
append
(
Edge
(
sourceindex
,
destindex
,
guards
,
resets
))
return
edges
##main code###
#print(sys.argv)
...
...
@@ -147,15 +244,15 @@ if __name__ == "__main__":
code
=
f
.
read
()
tree
=
ast
.
parse
(
code
)
#tree = ast.parse()
result
s
,
vars
,
modes
=
walktree
(
code
,
tree
)
path
s
,
vars
,
modes
=
walktree
(
code
,
tree
)
print
(
"
Paths found:
"
)
for
result
in
result
s
:
for
item
in
result
:
item
.
print
()
#
print("Paths found:")
#
for result in
path
s:
#
for item in result:
#
item.print()
#print(item.mode)
#print(item.modeType)
print
()
#
print()
print
(
"
Modes found:
"
)
print
(
modes
)
...
...
@@ -163,19 +260,41 @@ if __name__ == "__main__":
output_dict
.
update
(
input_json
)
#TODO: create graph!
vertices
=
[]
vertexStrings
=
[]
for
vertex
in
itertools
.
product
(
*
modes
.
values
()):
vertices
.
append
(
vertex
)
vertexstring
=
vertex
[
0
]
for
index
in
range
(
1
,
len
(
vertex
)):
vertexstring
+=
"
;
"
+
vertex
[
index
]
vertexStrings
.
append
(
vertexstring
)
edges
=
[]
guards
=
[]
resets
=
[]
for
path
in
paths
:
transitions
=
createTransition
(
path
,
vertices
,
modes
)
for
edge
in
transitions
:
edges
.
append
([
edge
.
source
,
edge
.
dest
])
guards
.
append
(
guardString
(
edge
.
guards
))
resets
.
append
(
resetString
(
edge
.
resets
))
output_dict
[
'
vertex
'
]
=
vertices
#print(vertices)
output_dict
[
'
variables
'
]
=
vars
# #add edge, transition(guards) and resets
#output_dict['edge'] = edges
#output_dict['guards'] = guards
#output_dict['resets'] = resets
#output_dict['vertex'] = mode_list
output_json
=
json
.
dumps
(
output_dict
,
indent
=
4
)
outfile
=
open
(
output_file_name
,
"
w
"
)
outfile
.
write
(
output_json
)
outfile
.
close
()
output_dict
[
'
edge
'
]
=
edges
#print(edges)
output_dict
[
'
guards
'
]
=
guards
#print(guards)
output_dict
[
'
resets
'
]
=
resets
print
(
resets
)
#output_json = json.dumps(output_dict, indent=4)
#outfile = open(output_file_name, "w")
#outfile.write(output_json)
#outfile.close()
print
(
"
wrote json to
"
+
output_file_name
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment