Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Pieter Koopman
Personal Prof public repository
Commits
6196c114
Commit
6196c114
authored
Oct 08, 2019
by
Markus Klinik
Browse files
refactor Assignment03Rules
parent
b153f49b
Changes
2
Hide whitespace changes
Inline
Side-by-side
java-feedback-rascal/src/Assignment03Rules.rsc
View file @
6196c114
...
...
@@ -8,80 +8,74 @@ import lang::java::m3::AST;
import Message;
import ImplementsAbstractMethod;
import ClassHierarchy;
import Util;
// Runs all checkers for assignment 3 on the given model
set[Message] allAssignment03Rules(M3 model)
{
errors = a03_geometric_is_comparable(model);
// a03_shapes_implement_comparable can only be called if a03_geometric_is_comparable does not return any errors
return (isEmpty(errors) ? a03_shapes_implement_comparable(model) : errors);
}
= a03_geometric_is_comparable(model)
+ a03_shapes_implement_comparable(model)
;
set[Message] a03_geometric_is_comparable(M3 model)
{
// There should be an interface called "Geometric"
geometrics = { i | i <- interfaces(model), /^Geometric$/ := i.file };
if(size(geometrics) != 1)
set[Message] result = {};
try
{
return { error("There should be an interface called Geometric", model.id) };
geometric = findInterface(model, "Geometric");
if( ! ("Comparable" in { super.file | super <- model.extends[geometric] }) )
{
result += { error("Geometric should extend Comparable", geometric) };
}
}
// Geometric should extend Comparable<Geometric>
geometric = getOneFrom(geometrics);
if( ! ("Comparable" in { super.file | super <- model.extends[geometric] }) )
catch error:
{
return { error
("Geometric should extend Comparable", geometric)
};
return { error };
}
return {};
}
// Assumption: the model passes a03_geometric_is_comparable,
// which means there is exactly one interface called Geometric
loc findGeometric(M3 model)
{
geometrics = { i | i <- interfaces(model), /^Geometric$/ := i.file };
return getOneFrom(geometrics);
return result;
}
set[Message] a03_shapes_implement_comparable(M3 model)
{
geometric = findGeometric(model);
// There should be exactly two subclasses of Geometric
shapes = invert(model.implements)[geometric];
if( {"Rectangle", "Circle"} != { shape.file | shape <- shapes } )
set[Message] result = {};
try
{
return { error("There should be two implementations of Geometric: Rectangle and Circle", geometric) };
}
geometric = findInterface(model, "Geometric");
// There should be exactly two subclasses of Geometric
shapes = invert(model.implements)[geometric];
if( {"Rectangle", "Circle"} != { getName(model, shape) | shape <- shapes } )
{
return { error("There should be two implementations of Geometric: Rectangle and Circle", geometric) };
}
// Rectangle and Circle should only implement Geometric and nothing else
errors = union(
{ model.implements[shape] == { geometric }
? {}
: { error("<shape.file> should implement Geometric and nothing else", shape) }
| shape <- shapes
});
if( ! isEmpty(errors) )
{
return errors;
}
// Rectangle and Circle should only implement Geometric and nothing else
result += union(
{ model.implements[shape] == { geometric }
? {}
: { error("<getName(model,shape)> should implement Geometric and nothing else", shape) }
| shape <- shapes
});
// Rectangle and Circle should implement Comparable.compareTo
loc compareTo = |java+method:///java/lang/Comparable/compareTo(T)|;
// Rectangle and Circle should implement Comparable.compareTo
loc compareTo = |java+method:///java/lang/Comparable/compareTo(T)|;
// for every shape: one of the methods has to override compareTo
for( shape <- shapes )
{
if( !implementsAbstractMethod(model,shape,compareTo) )
// for every shape: one of the methods has to override compareTo
for( shape <- shapes )
{
errors += error("<shape.file> should override Comparable.compareTo", shape);
if( !implementsAbstractMethod(model,shape,compareTo) )
{
result += error("<shape.file> should override Comparable.compareTo", shape);
}
}
}
catch error:
{
return { error };
}
return
errors
;
return
result
;
}
java-feedback-rascal/src/Assignment03RulesSpec.rsc
View file @
6196c114
...
...
@@ -19,7 +19,7 @@ test bool projectWithGeometricInterface()
test bool projectWithoutGeometricInterface()
{
errors = a03_geometric_is_comparable(loadTestProject("assignment01-good"));
return "There should be
an
interface called Geometric" in messages(errors);
return "There should be
one
interface called Geometric" in messages(errors);
}
test bool geometricShouldExtendComparable()
...
...
Write
Preview
Supports
Markdown
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