Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pycomsdk
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
com
pycomsdk
Commits
ae66f747
Commit
ae66f747
authored
Nov 07, 2019
by
Savva Golubitsky
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
graph factory in work
parent
20635758
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
122 additions
and
71 deletions
+122
-71
__init__.py
comsdk/__init__.py
+0
-0
graph.py
comsdk/graph.py
+1
-1
parser.py
comsdk/parser.py
+121
-70
No files found.
comsdk/__init__.py
0 → 100644
View file @
ae66f747
comsdk/graph.py
View file @
ae66f747
...
@@ -3,7 +3,7 @@ import os
...
@@ -3,7 +3,7 @@ import os
from
enum
import
Enum
,
auto
from
enum
import
Enum
,
auto
from
functools
import
partial
from
functools
import
partial
import
comsdk.
aux
as
aux
import
aux
as
aux
ImplicitParallelizationInfo
=
collections
.
namedtuple
(
'ImplicitParallelizationInfo'
,
[
'array_keys_mapping'
,
'branches_number'
,
'branch_i'
])
ImplicitParallelizationInfo
=
collections
.
namedtuple
(
'ImplicitParallelizationInfo'
,
[
'array_keys_mapping'
,
'branches_number'
,
'branch_i'
])
...
...
comsdk/parser.py
View file @
ae66f747
import
re
import
re
import
graph
as
gr
from
typing
import
NamedTuple
from
typing
import
NamedTuple
from
enum
import
Enum
,
auto
from
enum
import
Enum
,
auto
# Ключевые слова, описывающие граф
# Ключевые слова, описывающие граф
scope
=
[
class
Params
():
"__BEGIN__"
,
"__END__"
]
class
Parms
():
__slots__
=
(
__slots__
=
(
'module'
,
'module'
,
'entry_func'
,
'entry_func'
,
...
@@ -16,7 +12,8 @@ class Parms():
...
@@ -16,7 +12,8 @@ class Parms():
'selector'
,
'selector'
,
'function'
,
'function'
,
'morphism'
,
'morphism'
,
'parallelism'
'parallelism'
,
'comment'
)
)
def
__init__
(
self
):
def
__init__
(
self
):
for
slot
in
self
.
__slots__
:
for
slot
in
self
.
__slots__
:
...
@@ -28,91 +25,145 @@ class Parms():
...
@@ -28,91 +25,145 @@ class Parms():
stri
+=
s
+
": {}, "
.
format
(
getattr
(
self
,
s
))
stri
+=
s
+
": {}, "
.
format
(
getattr
(
self
,
s
))
return
stri
return
stri
class
GraphFactory
():
__slots__
=
(
'states'
,
'graph'
)
def
__init__
(
self
):
self
.
states
=
{}
def
add_state
(
self
,
statename
,
params
=
None
):
newst
=
True
if
statename
in
self
.
states
:
newst
=
False
if
newst
:
newstate
=
gr
.
State
(
statename
)
self
.
states
[
statename
]
=
newstate
def
connect_states
(
self
,
st1
,
st2
,
morphism
=
None
):
pass
# self.states[st1].connect_to(self.states[st2])
fact
=
GraphFactory
()
entities
=
{}
topology
=
{}
def
check_brackets
(
rawfile
):
br
=
0
qu
=
0
for
char
in
rawfile
:
if
char
==
"["
:
br
+=
1
elif
char
==
"{"
:
br
+=
1
elif
char
==
"("
:
br
+=
1
elif
char
==
"]"
:
br
-=
1
elif
char
==
"}"
:
br
-=
1
elif
char
==
")"
:
br
-=
1
elif
char
==
"
\"
"
:
qu
+=
1
if
br
!=
0
or
qu
%
2
!=
0
:
print
(
"Brackets or quotes do not match! Check your file"
)
exit
(
-
1
)
#Props is line "[prop=smth, ...]"
#Props is line "[prop=smth, ...]"
def
parm_from_props
(
props
):
def
param_from_props
(
props
):
parm
=
Parms
()
parm
=
Params
()
rs
=
re
.
split
(
r",\s*"
,
props
[:
-
1
])
#.split(r", ")
props
=
props
.
replace
(
"]"
,
''
)
if
'('
in
props
:
#replaces , in (smth,smth) to ;
props
=
props
[:
props
.
find
(
'('
)]
+
props
[
props
.
find
(
'('
):]
.
replace
(
','
,
';'
)
rs
=
props
.
split
(
r","
)
#.split(r", ")
print
(
rs
)
for
r
in
rs
:
for
r
in
rs
:
r
=
r
.
split
(
r"="
,
1
)
r
=
r
.
split
(
r"="
,
1
)
for
s
in
parm
.
__slots__
:
if
r
[
0
]
in
parm
.
__slots__
:
if
s
==
r
[
0
]:
setattr
(
parm
,
r
[
0
],
r
[
1
])
setattr
(
parm
,
s
,
r
[
1
])
else
:
print
(
"Unknown parameter: "
+
r
[
0
])
exit
(
-
1
)
return
parm
return
parm
def
parm_from_entln
(
raw
):
def
par
a
m_from_entln
(
raw
):
res
=
re
.
split
(
r"\["
,
raw
,
1
)
res
=
re
.
split
(
r"\["
,
raw
,
1
)
return
res
[
0
],
parm_from_props
(
res
[
1
])
return
res
[
0
],
param_from_props
(
res
[
1
])
def
parm_from_topln
(
raw
):
print
(
raw
)
def
param_from_topln
(
raw
):
spl
=
re
.
split
(
r"\s*(=>|->|\[)\s*"
,
raw
)
spl
=
re
.
split
(
r"\s*(=>|->|\[|\])\s*"
,
raw
)
spl
=
list
(
filter
(
lambda
x
:
x
!=
"["
and
x
!=
"]"
and
x
!=
""
,
spl
))
left
=
spl
[
0
]
.
split
(
","
)
right
=
spl
[
2
]
.
split
(
","
)
if
len
(
spl
)
>
3
:
param_from_props
(
spl
[
3
])
if
(
len
(
left
)
!=
1
)
and
(
len
(
right
)
!=
1
):
print
(
"ERROR:Ambigious multiple connection in line:
\n\t
{}"
.
format
(
raw
))
exit
()
elif
len
(
left
)
==
2
:
fact
.
add_state
(
right
[
0
])
for
st
in
left
:
fact
.
add_state
(
st
)
fact
.
connect_states
(
st
,
right
[
0
])
elif
len
(
right
)
==
2
:
fact
.
add_state
(
left
[
0
])
for
st
in
right
:
fact
.
add_state
(
st
)
else
:
fact
.
add_state
(
left
[
0
])
fact
.
add_state
(
right
[
0
])
print
(
spl
)
print
(
spl
)
# ent = [{"name":{"module":"val" , "entr_func":"val"...}}]
file
=
open
(
"./test.adot"
,
"r"
)
file
=
open
(
"./testgr.adot"
,
"r"
)
dot
=
file
.
read
()
dot
=
file
.
read
()
graphcheck
=
re
.
fullmatch
(
r"\A(digraph)\s+\w*\n{(.*\s)*}\Z"
,
dot
)
check_brackets
(
dot
)
if
graphcheck
is
None
:
#commts = re.findall(r"\".*\"", dot)
print
(
"Incorrect graph, check syntax!"
)
#
exit
()
dot
=
re
.
sub
(
r"[ \t\r]"
,
""
,
dot
)
#deleting all spaces
dot
=
re
.
sub
(
r"((digraph\w+\n?)|}|{)"
,
""
,
dot
)
dot
=
re
.
sub
(
r"\/\/.*"
,
""
,
dot
)
dot
=
re
.
sub
(
r"^\n$"
,
""
,
dot
)
#print("Checking graph...")
#graphcheck = re.search(r"\A(digraph)\w+\n?{((\/\/)*.*\n*)+}\Z", dot)
#if graphcheck is None:
# print("Incorrect graph, check syntax!")
# exit()
#
#print("Graph id good, processing!")
#dot = re.sub(r"//*$", "", dot)
dotlines
=
dot
.
splitlines
()
dotlines
=
list
(
filter
(
None
,
dotlines
))
file
.
seek
(
0
)
dotfile
=
file
.
readlines
()
entities
=
{}
topology
=
{}
# ent_re - regular expr for edges, states, functions properties
# ent_re - regular expr for edges, states, functions properties
ent_re
=
re
.
compile
(
r"^\w+\[.*\]$"
)
ent_re
=
re
.
compile
(
r"^\w+\[.*\]$"
)
# top_re - regular expr for topology properties, most time consuming one
# top_re - regular expr for topology properties, most time consuming one
top_re
=
re
.
compile
(
r"^(\w+,?)+(->|=>)(\w+,?)+(\[(\w+=(\(?\w+,?\)?)+,?)+\])?"
)
top_re
=
re
.
compile
(
r"^(\w+,?)+(->|=>)(\w+,?)+(\[(\w+=(\(?\w+,?\)?)+,?)+\])?"
)
# (r"^\w[\w\s,]*(->|=>)\s*\w[\w\s,=\[\]()]*$")
# (r"^\w[\w\s,]*(->|=>)\s*\w[\w\s,=\[\]()]*$")
# com_re - regular expr for comment
com_re
=
re
.
compile
(
r"^\s*//.*$"
)
for
i
,
ln
in
enumerate
(
dotlines
):
if
ent_re
.
match
(
ln
):
for
i
,
ln
in
enumerate
(
dotfile
):
name
,
parm
=
param_from_entln
(
ln
)
ln
=
re
.
sub
(
r"\s"
,
""
,
ln
)
#deleting all spaces
if
com_re
.
match
(
ln
)
or
len
(
ln
)
==
0
:
continue
elif
ent_re
.
match
(
ln
):
name
,
parm
=
parm_from_entln
(
ln
)
entities
[
name
]
=
parm
entities
[
name
]
=
parm
elif
top_re
.
match
(
ln
):
elif
top_re
.
match
(
ln
):
parm_from_topln
(
ln
)
param_from_topln
(
ln
)
#for e in entities:
# print(e, entities[e])
exit
()
print
(
"states:
\n
"
)
for
st
in
fact
.
states
:
print
(
st
)
for
e
in
entities
:
print
(
e
,
entities
[
e
])
# Все что до __BEGIN__ считается объявлением
exit
()
beg
=
re
.
search
((
scope
[
0
]),
dotfile
)
.
start
()
entities
=
{}
#Парсинг свойств переходов и состояний
rawents
=
re
.
findall
(
r"\w+\s*?\[.*\]"
,
dotfile
[:
beg
])
for
raw
in
rawents
:
res
=
re
.
split
(
r"\s+"
,
raw
,
1
)
ent
=
Parms
()
rs
=
re
.
split
(
r",\s*"
,
res
[
1
][
1
:
-
1
])
#.split(r", ")
for
r
in
rs
:
r
=
r
.
split
(
r"="
,
1
)
for
s
in
ent
.
__slots__
:
if
s
==
r
[
0
]:
setattr
(
ent
,
s
,
r
[
1
])
entities
[
res
[
0
]]
=
ent
for
s
in
entities
:
print
(
s
,
entities
[
s
])
#Парсинг топологии графа
rawstmts
=
re
.
findall
(
r"\s*([\w,\s]*(->|=>)[\w,\s]*(\[.*\])?)\n"
,
dotfile
[
beg
:])
print
(
rawstmts
)
# for raw in rawstmts:
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