forked from gsdlab/chocosolver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstanceModel.java
More file actions
124 lines (108 loc) · 3.47 KB
/
Copy pathInstanceModel.java
File metadata and controls
124 lines (108 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package org.clafer.instance;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.clafer.ast.AstConcreteClafer;
import org.clafer.common.Check;
import org.sysml.ast.SysmlProperty;
import org.sysml.compiler.InstanceSysmlCompiler;
import org.sysml.pprinter.SysmlPrinter;
/**
*
* @author jimmy
*/
public class InstanceModel {
private final InstanceClafer[] topClafers;
public InstanceModel(InstanceClafer... topClafers) {
this.topClafers = Check.noNulls(topClafers);
}
public InstanceClafer[] getTopClafers() {
return topClafers;
}
public InstanceClafer[] getTopClafers(AstConcreteClafer type) {
List<InstanceClafer> typedTopClafers = new ArrayList<>();
for (InstanceClafer topClafer : topClafers) {
if (type.equals(topClafer.getType())) {
typedTopClafers.add(topClafer);
}
}
return typedTopClafers.toArray(new InstanceClafer[typedTopClafers.size()]);
}
public InstanceClafer getTopClafer(AstConcreteClafer type) {
InstanceClafer typedTopClafer = null;
for (InstanceClafer topClafer : topClafers) {
if (type.equals(topClafer.getType())) {
if (typedTopClafer != null) {
throw new IllegalArgumentException("More than one top Clafer with type " + type);
}
typedTopClafer = topClafer;
}
}
if (typedTopClafer == null) {
throw new IllegalArgumentException("No top Clafer with type " + type);
}
return typedTopClafer;
}
/**
* Print solution as SysMLv2
*
* This is the start of exploring an automated product derivation tool
* and the requirements around inferring a system model from clafer are
* still tbd
*
*
*/
public void printSysml(Appendable out, String indent) throws IOException {
for (InstanceClafer top : topClafers) {
SysmlPrinter pprinter = new SysmlPrinter(out);
InstanceSysmlCompiler compiler = new InstanceSysmlCompiler();
// model can be null as the clafer model might not reference sysml concepts
Object _model = compiler.compile(top, top);
if (_model != null)
pprinter.visit((SysmlProperty) _model, indent);
}
}
/**
* Print solution to stdout.
*
* @throws IOException an IO error occurred
*/
public void print() throws IOException {
print(System.out);
}
/**
* Print solution.
*
* @param out the stream to print to
* @throws IOException an IO error occurred
*/
public void print(Appendable out) throws IOException {
for (InstanceClafer top : topClafers) {
top.print(out);
}
}
@Override
public boolean equals(Object obj) {
if (obj instanceof InstanceModel) {
InstanceModel other = (InstanceModel) obj;
return Arrays.equals(topClafers, other.topClafers);
}
return false;
}
@Override
public int hashCode() {
return Arrays.hashCode(topClafers);
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
try {
print(result);
} catch (IOException e) {
// StringBuilder should not throw an IOException.
throw new Error(e);
}
return result.toString();
}
}