]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
de793edbdb88d9ddc19f8fd2a18902772dfcacf2
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.modelImport.model.support;\r
2 \r
3 import java.util.Arrays;\r
4 import java.util.HashSet;\r
5 import java.util.Set;\r
6 \r
7 import org.simantics.db.Resource;\r
8 import org.simantics.db.WriteGraph;\r
9 import org.simantics.db.common.utils.ListUtils;\r
10 import org.simantics.db.exception.DatabaseException;\r
11 import org.simantics.layer0.Layer0;\r
12 import org.simantics.layer0.utils.direct.GraphUtils;\r
13 import org.simantics.sysdyn.SysdynResource;\r
14 import org.simantics.sysdyn.modelImport.model.IWriteableObject;\r
15 import org.simantics.sysdyn.modelImport.model.WriteContext;\r
16 \r
17 public class Enumeration implements IWriteableObject {\r
18         \r
19         private String name;\r
20         private Set<String> values;\r
21         private boolean copy;\r
22         \r
23         private Resource enumeration;\r
24         \r
25         public Enumeration(String name, String...values) {\r
26                 this.name = name;\r
27                 this.values = new HashSet<String>(Arrays.asList(values));\r
28                 \r
29                 copy = false;\r
30         }\r
31         \r
32         public Enumeration(String name, Enumeration other) {\r
33                 this.name = name;\r
34                 this.values = other.getValues();\r
35                 \r
36                 copy = true;\r
37         }\r
38         \r
39         public String getName() {\r
40                 return name;\r
41         }\r
42         \r
43         public Set<String> getValues() {\r
44                 return values;\r
45         }\r
46 \r
47         @Override\r
48         public Resource write(WriteGraph graph, Resource parent, WriteContext context)\r
49                         throws DatabaseException {\r
50                 Layer0 l0 = Layer0.getInstance(graph);\r
51                 SysdynResource sr = SysdynResource.getInstance(graph);\r
52                 \r
53                 enumeration = GraphUtils.create2(graph, sr.Enumeration,\r
54                                 l0.HasName, name,\r
55                                 l0.PartOf, parent);\r
56                 \r
57                 Set<Resource> indices = new HashSet<Resource>();\r
58                 for (String index : values) {\r
59                         indices.add(GraphUtils.create2(graph, sr.EnumerationIndex,\r
60                                         l0.HasName, index));\r
61                 }\r
62                 \r
63                 graph.claim(enumeration, sr.Enumeration_enumerationIndexList, ListUtils.create(graph, indices));\r
64                 \r
65                 // register this enumeration with the context\r
66                 context.registerEnumeration(this, copy);\r
67                 \r
68                 return enumeration;\r
69         }\r
70         \r
71         @Override\r
72         public Resource getResource() {\r
73                 return enumeration;\r
74         }\r
75 \r
76 }\r