Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
trs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
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
Taddeüs Kroes
trs
Commits
ad201451
Commit
ad201451
authored
13 years ago
by
Sander Mathijs van Veen
Browse files
Options
Downloads
Patches
Plain Diff
Added basic graph rewrite prototype.
parent
ae546156
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
src/node.py
+127
-0
127 additions, 0 deletions
src/node.py
with
127 additions
and
0 deletions
src/node.py
0 → 100644
+
127
−
0
View file @
ad201451
import
os.path
import
sys
sys
.
path
.
insert
(
0
,
os
.
path
.
realpath
(
'
external
'
))
from
graph_drawing.graph
import
generate_graph
class
ExpressionNode
(
object
):
def
__init__
(
self
,
operator
,
*
args
):
super
(
ExpressionNode
,
self
).
__init__
()
self
.
operator
,
self
.
args
=
operator
,
list
(
args
)
for
a
in
self
.
args
:
a
.
parent
=
self
def
title
(
self
):
return
self
.
operator
def
replace
(
self
,
node
):
pos
=
self
.
parent
.
args
.
index
(
self
)
self
.
parent
.
args
[
pos
]
=
node
node
.
parent
=
self
.
parent
self
.
parent
=
None
def
__iter__
(
self
):
return
iter
(
self
.
args
)
def
__len__
(
self
):
return
len
(
self
.
args
)
def
__getitem__
(
self
,
n
):
return
self
.
args
[
n
]
def
__setitem__
(
self
,
n
,
arg
):
self
.
args
[
n
]
=
arg
def
__str__
(
self
):
return
generate_graph
(
self
,
ExpressionNode
)
class
ExpressionLeaf
(
object
):
def
__init__
(
self
,
value
):
super
(
ExpressionLeaf
,
self
).
__init__
()
self
.
value
=
value
def
replace
(
self
,
node
):
if
not
hasattr
(
self
,
'
parent
'
):
return
pos
=
self
.
parent
.
args
.
index
(
self
)
self
.
parent
.
args
[
pos
]
=
node
node
.
parent
=
self
.
parent
self
.
parent
=
None
def
title
(
self
):
return
str
(
self
.
value
)
def
__add__
(
self
,
b
):
return
self
.
value
+
b
.
value
def
__repr__
(
self
):
return
repr
(
self
.
value
)
def
__str__
(
self
):
return
str
(
self
.
value
)
l0
=
ExpressionLeaf
(
3
)
l1
=
ExpressionLeaf
(
4
)
l2
=
ExpressionLeaf
(
5
)
l3
=
ExpressionLeaf
(
7
)
n0
=
ExpressionNode
(
'
+
'
,
l0
,
l1
)
n1
=
ExpressionNode
(
'
+
'
,
l2
,
l3
)
n2
=
ExpressionNode
(
'
*
'
,
n0
,
n1
)
print
n2
N
=
ExpressionNode
def
rewrite_multiply
(
node
):
a
,
b
=
node
[
0
]
c
,
d
=
node
[
1
]
ac
=
N
(
'
*
'
,
a
,
c
)
ad
=
N
(
'
*
'
,
a
,
d
)
bc
=
N
(
'
*
'
,
b
,
c
)
bd
=
N
(
'
*
'
,
b
,
d
)
res
=
N
(
'
+
'
,
N
(
'
+
'
,
N
(
'
+
'
,
ac
,
ad
),
bc
),
bd
)
return
res
possibilities
=
[
(
n0
,
lambda
(
x
,
y
):
ExpressionLeaf
(
x
+
y
)),
(
n1
,
lambda
(
x
,
y
):
ExpressionLeaf
(
x
+
y
)),
(
n2
,
rewrite_multiply
),
]
print
'
\n
--- after rule 2 ---
\n
'
n_
,
method
=
possibilities
[
2
]
new
=
method
(
n_
)
print
new
print
'
\n
--- original graph ---
\n
'
print
n2
print
'
\n
--- apply rule 0 ---
\n
'
n_
,
method
=
possibilities
[
0
]
new
=
method
(
n_
)
n_
.
replace
(
new
)
print
n2
# Revert rule 0
new
.
replace
(
n_
)
print
'
\n
--- apply rule 1 ---
\n
'
n_
,
method
=
possibilities
[
1
]
new
=
method
(
n_
)
n_
.
replace
(
new
)
print
n2
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