Skip to content

Commit dae72c0

Browse files
committed
Project local protocols to a module per role - this is different from the current 0.3 version of the spec, but may provide a simpler approach for dealing with the source files
1 parent 62d9cdb commit dae72c0

26 files changed

Lines changed: 229 additions & 150 deletions

modules/model/src/main/java/org/scribble/model/Argument.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,15 @@ public MessageSignature getMessageSignature() {
7575
* @param sig The message signature
7676
*/
7777
public void setMessageSignature(MessageSignature sig) {
78+
if (_messageSignature != null) {
79+
_messageSignature.setParent(null);
80+
}
81+
7882
_messageSignature = sig;
83+
84+
if (_messageSignature != null) {
85+
_messageSignature.setParent(this);
86+
}
7987
}
8088

8189
/**

modules/model/src/main/java/org/scribble/model/global/GDo.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.scribble.model.global;
1717

1818
import org.scribble.model.Argument;
19+
import org.scribble.model.ContainmentList;
1920
import org.scribble.model.FullyQualifiedName;
2021
import org.scribble.model.Role;
2122
import org.scribble.model.RoleDecl;
@@ -29,8 +30,9 @@ public class GDo extends GActivity {
2930

3031
private FullyQualifiedName _protocol=null;
3132
private String _scopeName=null;
32-
private java.util.List<Argument> _arguments=new java.util.ArrayList<Argument>();
33-
private java.util.List<RoleInstantiation> _roleInstantiations=new java.util.ArrayList<RoleInstantiation>();
33+
private java.util.List<Argument> _arguments=new ContainmentList<Argument>(this, Argument.class);
34+
private java.util.List<RoleInstantiation> _roleInstantiations=new ContainmentList<RoleInstantiation>(this,
35+
RoleInstantiation.class);
3436

3537
/**
3638
* This is the default constructor.

modules/model/src/main/java/org/scribble/model/global/GProtocolInstance.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.scribble.model.global;
1717

1818
import org.scribble.model.Argument;
19+
import org.scribble.model.ContainmentList;
1920
import org.scribble.model.ParameterDecl;
2021
import org.scribble.model.ProtocolDecl;
2122
import org.scribble.model.RoleDecl;
@@ -28,8 +29,9 @@
2829
public class GProtocolInstance extends ProtocolDecl {
2930

3031
private String _memberName=null;
31-
private java.util.List<RoleInstantiation> _roleInstantiations=new java.util.ArrayList<RoleInstantiation>();
32-
private java.util.List<Argument> _arguments=new java.util.ArrayList<Argument>();
32+
private java.util.List<Argument> _arguments=new ContainmentList<Argument>(this, Argument.class);
33+
private java.util.List<RoleInstantiation> _roleInstantiations=new ContainmentList<RoleInstantiation>(this,
34+
RoleInstantiation.class);
3335

3436
/**
3537
* The default constructor.

modules/model/src/main/java/org/scribble/model/local/LDo.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.scribble.model.local;
1717

1818
import org.scribble.model.Argument;
19+
import org.scribble.model.ContainmentList;
1920
import org.scribble.model.FullyQualifiedName;
2021
import org.scribble.model.RoleInstantiation;
2122

@@ -26,8 +27,9 @@
2627
public class LDo extends LActivity {
2728
private FullyQualifiedName _protocol=null;
2829
private String _scope=null;
29-
private java.util.List<Argument> _arguments=new java.util.ArrayList<Argument>();
30-
private java.util.List<RoleInstantiation> _roleInstantiations=new java.util.ArrayList<RoleInstantiation>();
30+
private java.util.List<Argument> _arguments=new ContainmentList<Argument>(this, Argument.class);
31+
private java.util.List<RoleInstantiation> _roleInstantiations=new ContainmentList<RoleInstantiation>(this,
32+
RoleInstantiation.class);
3133

3234
/**
3335
* This is the default constructor.

modules/model/src/main/java/org/scribble/model/local/LProtocolInstance.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.scribble.model.local;
1717

1818
import org.scribble.model.Argument;
19+
import org.scribble.model.ContainmentList;
1920
import org.scribble.model.ParameterDecl;
2021
import org.scribble.model.RoleDecl;
2122
import org.scribble.model.RoleInstantiation;
@@ -27,8 +28,9 @@
2728
public class LProtocolInstance extends LProtocolDecl {
2829

2930
private String _memberName=null;
30-
private java.util.List<RoleInstantiation> _roleInstantiations=new java.util.ArrayList<RoleInstantiation>();
31-
private java.util.List<Argument> _arguments=new java.util.ArrayList<Argument>();
31+
private java.util.List<Argument> _arguments=new ContainmentList<Argument>(this, Argument.class);
32+
private java.util.List<RoleInstantiation> _roleInstantiations=new ContainmentList<RoleInstantiation>(this,
33+
RoleInstantiation.class);
3234

3335
/**
3436
* The default constructor.

modules/projection/src/main/java/org/scribble/projection/ProtocolProjector.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,19 @@ public class ProtocolProjector {
3939
* @param module The module
4040
* @param loader The module loader
4141
* @param logger The logger
42+
* @return The set of modules representing the local projections
4243
*/
43-
public Module project(Resource resource, Module module,
44+
@SuppressWarnings("unchecked")
45+
public java.util.Set<Module> project(Resource resource, Module module,
4446
ModuleLoader loader, ScribbleLogger logger) {
45-
Module ret=null;
47+
java.util.Set<Module> ret=null;
4648
ProjectionRule rule=ProjectionRuleFactory.getProjectionRule(module);
4749

4850
if (rule != null) {
4951
DefaultModuleContext context=new DefaultModuleContext(resource,
5052
module, loader, new ModuleCache());
5153

52-
ret = (Module)rule.project(context, module, null, logger);
54+
ret = (java.util.Set<Module>)rule.project(context, module, null, logger);
5355
}
5456

5557
return (ret);

modules/projection/src/main/java/org/scribble/projection/rules/ModuleProjectionRule.java

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,57 +39,67 @@ public class ModuleProjectionRule implements ProjectionRule {
3939
*/
4040
public Object project(ModuleContext context, ModelObject mobj,
4141
RoleDecl role, ScribbleLogger logger) {
42-
Module projected=null;
42+
java.util.Set<Module> ret=new java.util.HashSet<Module>();
4343
Module source=(Module)mobj;
44+
45+
java.util.Set<RoleDecl> roles=new java.util.HashSet<RoleDecl>();
4446

45-
// Create new module
46-
projected = new Module();
47-
48-
projected.derivedFrom(source);
49-
50-
if (source.getFullyQualifiedName() != null) {
51-
projected.setFullyQualifiedName(new FullyQualifiedName(source.getFullyQualifiedName()));
52-
}
53-
54-
// Copy imports
55-
for (ImportDecl imp : source.getImports()) {
56-
projected.getImports().add(new ImportDecl(imp));
57-
}
58-
59-
// Copy payload type declarations
60-
for (PayloadTypeDecl ptd : source.getPayloadTypeDeclarations()) {
61-
projected.getPayloadTypeDeclarations().add(new PayloadTypeDecl(ptd));
47+
// Build list of roles
48+
for (ProtocolDecl pd : source.getProtocols()) {
49+
roles.addAll(pd.getRoleDeclarations());
6250
}
6351

64-
// Project global protocols, to all of their roles (for now)
65-
// TODO: Allow config to specify specific list of roles (and possibly global
66-
// protocols if more than one)
67-
for (ProtocolDecl pd : source.getProtocols()) {
52+
for (RoleDecl rd : roles) {
53+
54+
// Create new module
55+
Module projected = new Module();
56+
57+
projected.derivedFrom(source);
6858

69-
if (pd instanceof GProtocolDefinition || pd instanceof GProtocolInstance) {
70-
ProtocolDecl gpd=(ProtocolDecl)pd;
59+
if (source.getFullyQualifiedName() != null) {
60+
projected.setFullyQualifiedName(new FullyQualifiedName(source.getFullyQualifiedName()));
7161

72-
// Identify roles
73-
for (RoleDecl rd : gpd.getRoleDeclarations()) {
74-
75-
// TODO: Check if role projection already exists
76-
77-
ProjectionRule rule=ProjectionRuleFactory.getProjectionRule(gpd);
62+
// TODO: Need to investigate options for best pro
63+
projected.getFullyQualifiedName().setName(projected.getFullyQualifiedName().getName()
64+
+"@"+rd.getName());
65+
}
66+
67+
// Copy imports
68+
for (ImportDecl imp : source.getImports()) {
69+
projected.getImports().add(new ImportDecl(imp));
70+
71+
// Modify the import statements
72+
}
73+
74+
// Copy payload type declarations
75+
for (PayloadTypeDecl ptd : source.getPayloadTypeDeclarations()) {
76+
projected.getPayloadTypeDeclarations().add(new PayloadTypeDecl(ptd));
77+
}
78+
79+
for (ProtocolDecl pd : source.getProtocols()) {
80+
81+
if (pd instanceof GProtocolDefinition || pd instanceof GProtocolInstance) {
82+
ProtocolDecl gpd=(ProtocolDecl)pd;
7883

79-
if (rule != null) {
80-
ProtocolDecl lpd=(ProtocolDecl)rule.project(context, gpd, rd, logger);
84+
if (pd.getRoleDeclarations().contains(rd)) {
8185

82-
if (lpd != null) {
83-
projected.getProtocols().add(lpd);
86+
ProjectionRule rule=ProjectionRuleFactory.getProjectionRule(gpd);
87+
88+
if (rule != null) {
89+
ProtocolDecl lpd=(ProtocolDecl)rule.project(context, gpd, rd, logger);
90+
91+
if (lpd != null) {
92+
projected.getProtocols().add(lpd);
93+
}
8494
}
8595
}
86-
8796
}
8897
}
98+
99+
ret.add(projected);
89100
}
90-
91-
92-
return (projected);
101+
102+
return (ret);
93103
}
94104

95105
}

modules/projection/src/test/java/org/scribble/projection/ProtocolProjectionTest.java

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -97,39 +97,47 @@ protected void testProjection(String name) {
9797

9898
ProtocolProjector projector=new ProtocolProjector();
9999

100-
Module projected=projector.project(isr, module, loader, logger);
100+
java.util.Set<Module> projected=projector.project(isr, module, loader, logger);
101101

102-
is = ClassLoader.getSystemResourceAsStream("scribble/results/"+name+".scr");
103-
104-
byte[] b=new byte[is.available()];
105-
is.read(b);
106-
107-
is.close();
108-
109-
String projd=projected.toString().trim();
110-
String expecting=new String(b).trim();
111-
112-
if (!projd.equals(expecting)) {
113-
int len=projd.length();
114-
if (len > expecting.length()) {
115-
len = expecting.length();
116-
}
117-
for (int i=0; i < len; i++) {
118-
if (projd.charAt(i) != expecting.charAt(i)) {
119-
System.out.println("DIFF AT POSITION: "+i);
120-
int showto=i+30;
121-
if (i+10 >= len) {
122-
showto = len;
123-
}
124-
System.out.println("PROJECTED: "+projd.substring(i, showto));
125-
System.out.println("EXPECT: "+expecting.substring(i, showto));
126-
break;
127-
}
128-
}
102+
for (Module lm : projected) {
103+
String filename="scribble/results/"+lm.getFullyQualifiedName().getLastPart()+".scr";
129104

130-
System.err.println("Projected protocol '"+name+
131-
"' mismatch\nExpecting:\n"+expecting+"\nProjected:\n"+projd);
132-
fail("Projected protocol '"+name+"' mismatch");
105+
is = ClassLoader.getSystemResourceAsStream(filename);
106+
107+
if (is == null) {
108+
fail("Project result for '"+filename+"' not found");
109+
}
110+
111+
byte[] b=new byte[is.available()];
112+
is.read(b);
113+
114+
is.close();
115+
116+
String projd=lm.toString().trim();
117+
String expecting=new String(b).trim();
118+
119+
if (!projd.equals(expecting)) {
120+
int len=projd.length();
121+
if (len > expecting.length()) {
122+
len = expecting.length();
123+
}
124+
for (int i=0; i < len; i++) {
125+
if (projd.charAt(i) != expecting.charAt(i)) {
126+
System.out.println("DIFF AT POSITION: "+i);
127+
int showto=i+30;
128+
if (i+10 >= len) {
129+
showto = len;
130+
}
131+
System.out.println("PROJECTED: "+projd.substring(i, showto));
132+
System.out.println("EXPECT: "+expecting.substring(i, showto));
133+
break;
134+
}
135+
}
136+
137+
System.err.println("Projected protocol '"+name+
138+
"' mismatch\nExpecting:\n"+expecting+"\nProjected:\n"+projd);
139+
fail("Projected protocol '"+name+"' mismatch");
140+
}
133141
}
134142

135143
} catch (Exception e) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module scribble.examples.GChoice@Buyer;
2+
3+
type <xsd> "{http://www.acme.com/financial}Order" from "http://www.acme.com/schemas/Order.xsd" as Order;
4+
type <xsd> "{http://www.acme.com/financial}QuoteRequest" from "http://www.acme.com/schemas/QuoteRequest.xsd" as QuoteRequest;
5+
6+
local protocol GChoiceTest at Buyer(role Buyer,role Seller) {
7+
choice at Buyer {
8+
buy(Order) to Seller;
9+
} or {
10+
buy(QuoteRequest) to Seller;
11+
}
12+
}

modules/projection/src/test/resources/scribble/results/GChoice.scr renamed to modules/projection/src/test/resources/scribble/results/GChoice@Seller.scr

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
module scribble.examples.GChoice;
1+
module scribble.examples.GChoice@Seller;
22

33
type <xsd> "{http://www.acme.com/financial}Order" from "http://www.acme.com/schemas/Order.xsd" as Order;
44
type <xsd> "{http://www.acme.com/financial}QuoteRequest" from "http://www.acme.com/schemas/QuoteRequest.xsd" as QuoteRequest;
55

6-
local protocol GChoiceTest at Buyer(role Buyer,role Seller) {
7-
choice at Buyer {
8-
buy(Order) to Seller;
9-
} or {
10-
buy(QuoteRequest) to Seller;
11-
}
12-
}
13-
146
local protocol GChoiceTest at Seller(role Buyer,role Seller) {
157
choice at Buyer {
168
buy(Order) from Buyer;

0 commit comments

Comments
 (0)