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
a156a2b5
Commit
a156a2b5
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Added fucntion to check if a node contains another node.
parent
dd613cc3
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/node.py
+17
-2
17 additions, 2 deletions
src/node.py
tests/test_node.py
+9
-0
9 additions, 0 deletions
tests/test_node.py
with
26 additions
and
2 deletions
src/node.py
+
17
−
2
View file @
a156a2b5
...
@@ -50,8 +50,9 @@ OP_HINT = 20
...
@@ -50,8 +50,9 @@ OP_HINT = 20
OP_REWRITE_ALL
=
21
OP_REWRITE_ALL
=
21
OP_REWRITE
=
22
OP_REWRITE
=
22
# Special identifier
d
# Special identifier
s
PI
=
'
pi
'
PI
=
'
pi
'
E
=
'
e
'
TYPE_MAP
=
{
TYPE_MAP
=
{
...
@@ -178,7 +179,7 @@ class ExpressionBase(object):
...
@@ -178,7 +179,7 @@ class ExpressionBase(object):
and
(
identifier
==
None
or
self
.
value
==
identifier
)
and
(
identifier
==
None
or
self
.
value
==
identifier
)
def
is_variable
(
self
):
def
is_variable
(
self
):
return
self
.
type
==
TYPE_IDENTIFIER
and
self
.
value
!=
PI
return
self
.
type
==
TYPE_IDENTIFIER
and
self
.
value
not
in
(
PI
,
E
)
def
is_int
(
self
):
def
is_int
(
self
):
return
self
.
type
==
TYPE_INTEGER
return
self
.
type
==
TYPE_INTEGER
...
@@ -217,6 +218,20 @@ class ExpressionBase(object):
...
@@ -217,6 +218,20 @@ class ExpressionBase(object):
"""
Negate the node n times.
"""
"""
Negate the node n times.
"""
return
negate
(
self
,
self
.
negated
+
n
)
return
negate
(
self
,
self
.
negated
+
n
)
def
contains
(
self
,
node
,
include_self
=
True
):
"""
Check if a node equal to the specified one exists within this node.
"""
if
include_self
and
self
==
node
:
return
True
if
not
self
.
is_leaf
:
for
child
in
self
:
if
child
.
contains
(
node
,
include_self
=
True
):
return
True
return
False
class
ExpressionNode
(
Node
,
ExpressionBase
):
class
ExpressionNode
(
Node
,
ExpressionBase
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
def
__init__
(
self
,
*
args
,
**
kwargs
):
...
...
This diff is collapsed.
Click to expand it.
tests/test_node.py
+
9
−
0
View file @
a156a2b5
...
@@ -209,3 +209,12 @@ class TestNode(RulesTestCase):
...
@@ -209,3 +209,12 @@ class TestNode(RulesTestCase):
n
=
tree
(
'
-(a + b)
'
)
n
=
tree
(
'
-(a + b)
'
)
self
.
assertEqualNodes
(
Scope
(
n
).
as_nary_node
(),
n
)
self
.
assertEqualNodes
(
Scope
(
n
).
as_nary_node
(),
n
)
self
.
assertEqualNodes
(
Scope
(
-
n
).
as_nary_node
(),
-
n
)
self
.
assertEqualNodes
(
Scope
(
-
n
).
as_nary_node
(),
-
n
)
def
test_contains
(
self
):
a
,
ab
,
bc
,
ln0
,
ln1
=
tree
(
'
a, ab, bc, ln(a) + 1, ln(b) + 1
'
)
self
.
assertTrue
(
a
.
contains
(
a
))
self
.
assertTrue
(
ab
.
contains
(
a
))
self
.
assertFalse
(
bc
.
contains
(
a
))
self
.
assertTrue
(
ln0
.
contains
(
a
))
self
.
assertFalse
(
ln1
.
contains
(
a
))
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