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
0eadc533
Commit
0eadc533
authored
12 years ago
by
Taddeüs Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Implemented a new, lightweight way of validation as described in the TODO list.
parent
0efcd75c
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/parser.py
+5
-0
5 additions, 0 deletions
src/parser.py
src/validation.py
+28
-30
28 additions, 30 deletions
src/validation.py
tests/test_validation.py
+22
-18
22 additions, 18 deletions
tests/test_validation.py
with
55 additions
and
48 deletions
src/parser.py
+
5
−
0
View file @
0eadc533
...
@@ -382,6 +382,11 @@ class Parser(BisonParser):
...
@@ -382,6 +382,11 @@ class Parser(BisonParser):
return
self
.
root_node
return
self
.
root_node
def
rewrite_and_count_all
(
self
,
check_implicit
=
True
,
verbose
=
False
):
steps
=
self
.
rewrite_all
(
include_steps
=
True
,
check_implicit
=
check_implicit
,
verbose
=
verbose
)
return
self
.
root_node
,
len
(
steps
)
#def hook_run(self, filename, retval):
#def hook_run(self, filename, retval):
# return retval
# return retval
...
...
This diff is collapsed.
Click to expand it.
src/validation.py
+
28
−
30
View file @
0eadc533
...
@@ -18,45 +18,43 @@ from strategy import find_possibilities
...
@@ -18,45 +18,43 @@ from strategy import find_possibilities
from
tests.parser
import
ParserWrapper
from
tests.parser
import
ParserWrapper
def
validate
(
exp
,
result
):
VALIDATE_FAILURE
=
0
VALIDATE_NOPROGRESS
=
1
VALIDATE_SUCCESS
=
2
VALIDATE_ERROR
=
3
def
validate
(
a
,
b
):
"""
"""
Validate that
exp
=>*
result
.
Validate that
a
=>*
b
.
"""
"""
parser
=
ParserWrapper
(
Parser
)
parser
=
ParserWrapper
(
Parser
)
exp
=
parser
.
run
([
exp
])
# Parse both expressions
result
=
parser
.
run
([
result
])
a
=
parser
.
run
([
a
])
b
=
parser
.
run
([
b
])
# Compare the simplified expressions first, in order to avoid the
# Evaluate a and b, counting the number of steps
# computational intensive traversal of the possibilities tree.
parser
.
set_root_node
(
a
)
parser
.
set_root_node
(
exp
)
A
,
a_steps
=
parser
.
rewrite_and_count_all
()
a
=
parser
.
rewrite_all
()
if
not
a
:
if
not
a
:
return
False
return
VALIDATE_ERROR
parser
.
set_root_node
(
result
)
b
=
parser
.
rewrite_all
()
if
not
a
.
equals
(
b
):
return
False
# TODO: make sure cycles are avoided / eliminated using cycle detection.
parser
.
set_root_node
(
b
)
def
traverse_preorder
(
node
,
result
):
B
,
b_steps
=
parser
.
rewrite_and_count_all
()
#print 'node:', node, 'result:', result
if
node
.
equals
(
result
):
return
True
for
p
in
find_possibilities
(
node
):
if
not
B
:
# Clone the root node because it will be used in multiple
return
VALIDATE_ERROR
# substitutions
temp
=
node
.
clone
()
child
=
apply_suggestion
(
node
,
p
)
node
=
temp
if
traverse_preorder
(
child
,
result
):
# Evaluations must be equal
return
True
if
not
A
.
equals
(
B
):
return
VALIDATE_FAILURE
return
False
# If evaluation of b took more staps than evaluation of a, the step from a
# to b was probably useless or even bad
if
b_steps
>=
a_steps
:
return
VALIDATE_NOPROGRESS
return
traverse_preorder
(
exp
,
result
)
# Evaluations match and b is evaluated quicker than a => success
return
VALIDATE_SUCCESS
This diff is collapsed.
Click to expand it.
tests/test_validation.py
+
22
−
18
View file @
0eadc533
...
@@ -13,38 +13,42 @@
...
@@ -13,38 +13,42 @@
# You should have received a copy of the GNU Affero General Public License
# You should have received a copy of the GNU Affero General Public License
# along with TRS. If not, see <http://www.gnu.org/licenses/>.
# along with TRS. If not, see <http://www.gnu.org/licenses/>.
from
unittest
import
TestCase
from
unittest
import
TestCase
from
src.validation
import
validate
from
src.validation
import
validate
,
VALIDATE_SUCCESS
as
OK
,
\
VALIDATE_FAILURE
as
FAIL
,
VALIDATE_NOPROGRESS
as
NP
class
TestValidation
(
TestCase
):
class
TestValidation
(
TestCase
):
def
test_simple_success
(
self
):
def
test_simple_success
(
self
):
self
.
assert
True
(
validate
(
'
3a + a
'
,
'
4a
'
))
self
.
assert
Equal
(
validate
(
'
3a + a
'
,
'
4a
'
)
,
OK
)
def
test_simple_failure
(
self
):
def
test_simple_failure
(
self
):
self
.
assert
False
(
validate
(
'
3a + a
'
,
'
4a + 1
'
))
self
.
assert
Equal
(
validate
(
'
3a + a
'
,
'
4a + 1
'
)
,
FAIL
)
def
test_intermediate_success
(
self
):
def
test_intermediate_success
(
self
):
self
.
assert
True
(
validate
(
'
3a + a + b + 2b
'
,
'
4a + 3b
'
))
self
.
assert
Equal
(
validate
(
'
3a + a + b + 2b
'
,
'
4a + 3b
'
)
,
OK
)
self
.
assert
True
(
validate
(
'
a / b / (c / d)
'
,
'
(ad) / (bc)
'
))
self
.
assert
Equal
(
validate
(
'
a / b / (c / d)
'
,
'
(ad) / (bc)
'
)
,
OK
)
def
test_intermediate_failure
(
self
):
def
test_intermediate_failure
(
self
):
self
.
assert
False
(
validate
(
'
3a + a + b + 2b
'
,
'
4a + 4b
'
))
self
.
assert
Equal
(
validate
(
'
3a + a + b + 2b
'
,
'
4a + 4b
'
)
,
FAIL
)
#
def test_success(self):
def
test_success
(
self
):
#
self.assert
True
(validate('x^2 + x - 2x^2 + 3x + 1',
self
.
assert
Equal
(
validate
(
'
x^2 + x - 2x^2 + 3x + 1
'
,
#
'x^2 + 4x - 2x^2 + 1'))
'
x^2 + 4x - 2x^2 + 1
'
)
,
OK
)
#
def test_indefinite_integral(self):
def
test_indefinite_integral
(
self
):
#
self.assert
True
(validate('int_2^4 x^2', '4^3/3 - 2^3/3'))
self
.
assert
Equal
(
validate
(
'
int_2^4 x^2
'
,
'
4^3/3 - 2^3/3
'
)
,
OK
)
#
def test_advanced_failure(self):
def
test_advanced_failure
(
self
):
#
self.assert
False
(validate('(x-1)^3+(x-1)^3', '4a+4b'))
self
.
assert
Equal
(
validate
(
'
(x-1)^3+(x-1)^3
'
,
'
4a+4b
'
)
,
FAIL
)
def
test_sphere_volume
(
self
):
def
test_sphere_volume
(
self
):
self
.
assert
True
(
validate
(
'
int_(-r)^(r) pi * (r^2 - x^2) dx
'
,
self
.
assert
Equal
(
validate
(
'
int_(-r)^(r) pi * (r^2 - x^2) dx
'
,
'
4 / 3 * pi * r ^ 3
'
))
'
4 / 3 * pi * r ^ 3
'
)
,
OK
)
#def test_sphere_volume_alternative(self):
def
test_sphere_volume_alternative_notation
(
self
):
# self.assertTrue(validate('int_(-r)^(r) pi * (r^2 - x^2) dx',
self
.
assertEqual
(
validate
(
'
int_(-r)^(r) pi * (r^2 - x^2) dx
'
,
# '4 * pi * r ^ 3 / 3'))
'
4 * pi * r ^ 3 / 3
'
),
OK
)
def
test_noprogress_simple
(
self
):
self
.
assertEqual
(
validate
(
'
2 + 2
'
,
'
3 + 1
'
),
NP
)
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