Commit db3908ff authored by René Schöne's avatar René Schöne
Browse files

Add LeftDirection ('<-') for relations.

- Also add some tests for left direction
parent 3b305708
Pipeline #4421 passed with stage
in 2 minutes and 29 seconds
......@@ -29,11 +29,15 @@ aspect TypeAnalysis {
aspect ComponentAnalysis {
syn boolean Component.isTargetOfDirectedRelation() = false;
eq RelationComponent.isTargetOfDirectedRelation() = isTargetOfRightDirection();
eq RelationComponent.isTargetOfDirectedRelation() = isTargetOfRightDirection() | isTargetOfLeftDirection();
inh boolean RelationComponent.isTargetOfRightDirection();
eq Relation.getRight().isTargetOfRightDirection()
= getDirection() instanceof RightDirection;
eq Program.getChild().isTargetOfRightDirection() = false;
inh boolean RelationComponent.isTargetOfLeftDirection();
eq Relation.getLeft().isTargetOfLeftDirection()
= getDirection() instanceof LeftDirection;
eq Program.getChild().isTargetOfLeftDirection() = false;
syn String Component.name() = getID();
......
......@@ -216,6 +216,9 @@ aspect BackendDirectedAPI {
public void RightDirection.generateAPI(StringBuilder sb) {
relation().getLeft().generateDirectedAPI(sb);
}
public void LeftDirection.generateAPI(StringBuilder sb) {
relation().getRight().generateDirectedAPI(sb);
}
public abstract void RelationComponent.generateDirectedAPI(StringBuilder sb);
public void OneRelationComponent.generateDirectedAPI(StringBuilder sb) {
......@@ -904,6 +907,9 @@ aspect NameResolutionHelper {
public void RightDirection.generateContextDependentNameResolution(StringBuilder sb) {
relation().getLeft().generateContextDependentNameResolution(sb);
}
public void LeftDirection.generateContextDependentNameResolution(StringBuilder sb) {
relation().getRight().generateContextDependentNameResolution(sb);
}
public void Bidirectional.generateContextDependentNameResolution(StringBuilder sb) {
relation().getLeft().generateContextDependentNameResolution(sb);
relation().getRight().generateContextDependentNameResolution(sb);
......@@ -1451,6 +1457,9 @@ aspect PrettyPrint {
public String RightDirection.prettyPrint() {
return "->";
}
public String LeftDirection.prettyPrint() {
return "<-";
}
public String Bidirectional.prettyPrint() {
return "<->";
}
......
......@@ -21,4 +21,5 @@ OptionalRelationComponent : RelationComponent;
ManyRelationComponent : RelationComponent;
abstract Direction;
RightDirection : Direction;
LeftDirection : Direction;
Bidirectional : Direction;
......@@ -62,6 +62,7 @@ ID = [a-zA-Z$_][a-zA-Z0-9$_]*
"/" { return sym(Terminals.SLASH); }
"?" { return sym(Terminals.QUESTION_MARK); }
"->" { return sym(Terminals.RIGHT); }
"<-" { return sym(Terminals.LEFT); }
"<->" { return sym(Terminals.BIDIRECTIONAL); }
// ID
......
......@@ -101,5 +101,6 @@ RelationComponent relation_comp =
Direction direction =
RIGHT {: return new RightDirection(); :}
| LEFT {: return new LeftDirection(); :}
| BIDIRECTIONAL {: return new Bidirectional(); :}
;
......@@ -14,7 +14,7 @@ import java.util.List;
public class Compiler {
private static final String VERSION = "0.2.2";
private static final String VERSION = "0.2.3";
private ArrayList<Option<?>> options;
private FlagOption optionWriteToFile;
......
Errors:
$FILENAME Line 5, column 10: Role name missing for type 'A'
$FILENAME Line 6, column 15: Role name missing for type 'B'
$FILENAME Line 7, column 5: The target of a directed relation cannot have a role name
$FILENAME Line 8, column 5: The target of a directed relation may only have multiplicity 1
Program ::= A* B*;
A;
B;
rel B <- A;
rel A.bs* <-> B*;
rel B.b <- A.b;
rel B* <- A.b2;
Errors:
$FILENAME Line 2, column 12: Component 'X' is already declared for type 'B1'
$FILENAME Line 6, column 10: Component 'X' is already declared for type 'B2'
A ::= X;
B1 : A ::= X;
B2 : A;
X;
rel X <- B2.X;
Errors:
$FILENAME1 Line 5, column 10: Role name missing for type 'A'
$FILENAME1 Line 6, column 15: Role name missing for type 'B'
$FILENAME2 Line 1, column 5: The target of a directed relation cannot have a role name
$FILENAME2 Line 2, column 5: The target of a directed relation may only have multiplicity 1
Program ::= A* B*;
A;
B;
rel B <- A;
rel A.bs* <-> B*;
......@@ -17,3 +17,8 @@ rel A.Bi6? <-> B.Bi6*;
rel A.Bi7* <-> B.Bi7;
rel A.Bi8* <-> B.Bi8?;
rel A.Bi9* <-> B.Bi9*;
rel A <- Root.AaLeft?;
rel B <- A.Di1Left ;
rel B <- A.Di2Left?;
rel B <- A.Di3Left*;
......@@ -27,16 +27,31 @@ class Errors {
test("Errors");
}
@Test
void test1Left() throws IOException {
test("ErrorsLeft");
}
@Test
void test2() throws IOException {
test("Inheritance");
}
@Test
void test2Left() throws IOException {
test("InheritanceLeft");
}
@Test
void test3() throws IOException {
test("Multiple", "Multiple_1", "Multiple_2");
}
@Test
void test3Left() throws IOException {
test("MultipleLeft", "MultipleLeft_1", "MultipleLeft_2");
}
private void test(String name, String... inFilenames) throws IOException {
List<String> inFiles = Arrays.stream(inFilenames.length > 0 ? inFilenames : new String[]{name})
.map(f -> "./src/test/jastadd/errors/" + f + ".relast")
......
......@@ -132,6 +132,100 @@ class Relations {
assertEquals(a3.getDi3List(), Arrays.asList());
}
/**
* rel A.Di1 -> B;
*/
@Test
void testDi1Left() {
setup();
a1.setDi1Left(b2);
a2.setDi1Left(b1);
assertSame(a1.getDi1Left(), b2);
assertSame(a2.getDi1Left(), b1);
a2.setDi1Left(b2);
assertSame(a1.getDi1Left(), b2);
assertSame(a2.getDi1Left(), b2);
try {
a3.setDi1Left(null);
fail("should throw an exception");
} catch (Exception e) {
// OK
}
}
/**
* rel A.Di2? -> B;
*/
@Test
void testDi2Left() {
setup();
a1.setDi2Left(b2);
a2.setDi2Left(b1);
assertSame(a1.getDi2Left(), b2);
assertSame(a2.getDi2Left(), b1);
a2.setDi2Left(b2);
assertSame(a1.getDi2Left(), b2);
assertSame(a2.getDi2Left(), b2);
a2.clearDi2Left();
assertSame(a1.getDi2Left(), b2);
assertNull(a2.getDi2Left());
assertTrue(a1.hasDi2Left());
assertFalse(a2.hasDi2Left());
assertFalse(a3.hasDi2Left());
}
/**
* rel A.Di3* -> B;
*/
@Test
void testDi3Left() {
setup();
a1.addDi3Left(b1);
a1.addDi3Left(b2);
a1.addDi3Left(b3);
a2.addDi3Left(b2);
assertEquals(a1.getDi3Lefts(), Arrays.asList(b1, b2, b3));
assertEquals(a1.getDi3LeftList(), Arrays.asList(b1, b2, b3));
assertEquals(a2.getDi3Lefts(), Arrays.asList(b2));
assertEquals(a2.getDi3LeftList(), Arrays.asList(b2));
assertEquals(a3.getDi3Lefts(), Arrays.asList());
assertEquals(a3.getDi3LeftList(), Arrays.asList());
a1.addDi3Left(b1);
a2.addDi3Left(b1);
a2.addDi3Left(b2);
assertEquals(a1.getDi3Lefts(), Arrays.asList(b1, b2, b3, b1));
assertEquals(a1.getDi3LeftList(), Arrays.asList(b1, b2, b3, b1));
assertEquals(a2.getDi3Lefts(), Arrays.asList(b2, b1, b2));
assertEquals(a2.getDi3LeftList(), Arrays.asList(b2, b1, b2));
assertEquals(a3.getDi3Lefts(), Arrays.asList());
assertEquals(a3.getDi3LeftList(), Arrays.asList());
a1.removeDi3Left(b1);
a2.removeDi3Left(b2);
assertEquals(a1.getDi3Lefts(), Arrays.asList(b2, b3, b1));
assertEquals(a1.getDi3LeftList(), Arrays.asList(b2, b3, b1));
assertEquals(a2.getDi3Lefts(), Arrays.asList(b1, b2));
assertEquals(a2.getDi3LeftList(), Arrays.asList(b1, b2));
assertEquals(a3.getDi3Lefts(), Arrays.asList());
assertEquals(a3.getDi3LeftList(), Arrays.asList());
}
/**
* rel A.Bi1 <-> B.Bi1;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment