]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/TransferableGraphConfiguration2.java
Work in progress
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / TransferableGraphConfiguration2.java
1 package org.simantics.db.layer0.util;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.Map;
7 import java.util.TreeMap;
8
9 import org.simantics.databoard.binding.mutable.Variant;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.RequestProcessor;
12 import org.simantics.db.Resource;
13 import org.simantics.db.Statement;
14 import org.simantics.db.common.NamedResource;
15 import org.simantics.db.common.request.UniqueRead;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus;
18 import org.simantics.db.layer0.util.DomainProcessor3.ExclusionDecision;
19 import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec.SeedSpecType;
20 import org.simantics.scl.runtime.function.Function1;
21
22 public class TransferableGraphConfiguration2 {
23
24         public static class SeedSpec {
25
26                 public static enum SeedSpecType {
27                         // These seeds and all their child resources (ConsistsOfProcess) are internal
28                         INTERNAL,
29                         // These seeds are reported as root entries in TG, 
30                         ROOT,
31                         SPECIAL_ROOT
32                 }
33
34                 public final Resource resource;
35                 public final String name;
36                 // Special roots are not normal seeds - e.g. %model
37                 public final SeedSpecType specType;
38                 /**
39                  * Optional, may be null.
40                  */
41                 public final String type;
42
43                 public SeedSpec(Resource resource, String name, SeedSpecType specType) {
44                         this(resource, name, specType, null);
45                 }
46
47                 public SeedSpec(Resource resource, String name, SeedSpecType specType, String type) {
48                         if (resource == null)
49                                 throw new NullPointerException("null resource");
50                         if (name == null)
51                                 throw new NullPointerException("null name");
52                         this.resource = resource;
53                         this.name = name;
54                         this.specType = specType;
55                         this.type = type;
56                 }
57
58                 public static SeedSpec internal(Resource resource) {
59                         return new SeedSpec(resource, "", SeedSpecType.ROOT);
60                 }
61
62                 @Override
63                 public String toString() {
64                         return "SeedSpec[" + name + ", " + resource + ", " + specType + ", " + type + "]";
65                 }
66
67                 @Override
68                 public int hashCode() {
69                         final int prime = 31;
70                         int result = 1;
71                         result = prime * result + specType.hashCode();
72                         result = prime * result + resource.hashCode();
73                         result = prime * result + name.hashCode();
74                         result = prime * result + (type != null ? type.hashCode() : 0);
75                         return result;
76                 }
77
78                 @Override
79                 public boolean equals(Object obj) {
80                         if (this == obj)
81                                 return true;
82                         if (obj == null)
83                                 return false;
84                         if (getClass() != obj.getClass())
85                                 return false;
86                         SeedSpec other = (SeedSpec) obj;
87                         return specType.equals(other.specType) && resource.equals(other.resource) && name.equals(other.name)
88                                         && objectEquals(type, other.type);
89                 }
90
91                 private boolean objectEquals(Object o1, Object o2) {
92                         if (o1 == o2) return true;
93                         if (o1 == null && o2 == null) return true;
94                         if (o1 == null || o2 == null) return false;
95                         return o1.equals(o2);
96                 }
97         }
98
99         final public TreeMap<String, Variant> baseExtensions = new TreeMap<>();
100         final public Resource indexRoot;
101         final public Collection<SeedSpec> seeds;
102         final public Map<Resource, ExtentStatus> preStatus;
103         final public boolean ignoreVirtualResources;
104         final public boolean validate;
105
106         /**
107          * <code>true</code> to export values, <code>false</code> to skip the actual
108          * values and only write the resource id.
109          */
110         public boolean values = true;
111
112         public Collection<TGValueModifier> valueModifiers;
113         public Function1<Statement,ExclusionDecision> exclusionFunction;
114
115         public TransferableGraphConfiguration2(Resource indexRoot, Collection<SeedSpec> seeds, Map<Resource, ExtentStatus> preStatus, boolean ignoreVirtualResources, boolean validate) {
116                 this.indexRoot = indexRoot;
117                 this.seeds = seeds;
118                 this.preStatus = preStatus;
119                 this.ignoreVirtualResources = ignoreVirtualResources;
120                 this.validate = validate;
121                 this.valueModifiers = null;
122         }
123
124         public TransferableGraphConfiguration2(Resource indexRoot, Collection<SeedSpec> roots, Map<Resource, ExtentStatus> preStatus, boolean ignoreVirtualResources) {
125                 this(indexRoot, roots, preStatus, ignoreVirtualResources, true);
126         }
127
128         public TransferableGraphConfiguration2(Resource indexRoot, Collection<SeedSpec> roots, Map<Resource, ExtentStatus> preStatus) {
129                 this(indexRoot, roots, preStatus, true);
130         }
131
132         public TransferableGraphConfiguration2(TransferableGraphConfiguration2 conf) throws DatabaseException {
133                 this(conf.indexRoot, conf.seeds, conf.preStatus, conf.ignoreVirtualResources, conf.validate);
134         }
135
136         public TransferableGraphConfiguration2(ReadGraph graph, Resource model, boolean ignoreVirtualResources, boolean validate) throws DatabaseException {
137                 this(new TGConfigurer(graph, ignoreVirtualResources, validate).roots2(Collections.singletonList(model)).create());
138         }
139
140         public TransferableGraphConfiguration2(ReadGraph graph, Collection<Resource> roots, boolean ignoreVirtualResources, boolean validate) throws DatabaseException {
141             this(new TGConfigurer(graph, ignoreVirtualResources, validate).roots2(roots).create());
142         }
143
144         public TransferableGraphConfiguration2(ReadGraph graph, Resource model, boolean ignoreVirtualResources) throws DatabaseException {
145                 this(graph, model, ignoreVirtualResources, true);
146         }
147
148         public TransferableGraphConfiguration2(ReadGraph graph, Resource model) throws DatabaseException {
149                 this(graph, model, true);
150         }
151
152         public TransferableGraphConfiguration2(ReadGraph graph, Collection<SeedSpec> roots, Collection<Resource> resourceRoots, Collection<Resource> exclusions) throws DatabaseException {
153                 this(new TGConfigurer(graph, true).roots(roots).roots2(resourceRoots).exclusions(exclusions).create());
154         }
155
156         public TransferableGraphConfiguration2(ReadGraph graph, Collection<SeedSpec> roots, Collection<Resource> resourceRoots, Collection<Resource> exclusions, boolean ignoreVirtualResource, boolean validate) throws DatabaseException {
157                 this(new TGConfigurer(graph, ignoreVirtualResource, validate).roots(roots).roots2(resourceRoots).exclusions(exclusions).create());
158         }
159
160         public static TransferableGraphConfiguration2 createWithNames(RequestProcessor processor, final Collection<NamedResource> roots, final Collection<Resource> exclusions, final boolean ignoreVirtualResource, final boolean validate) throws DatabaseException {
161                 return processor.sync(new UniqueRead<TransferableGraphConfiguration2>() {
162
163                         @Override
164                         public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
165                                 return new TransferableGraphConfiguration2(graph, translate(roots), Collections.<Resource>emptyList(), exclusions, ignoreVirtualResource, validate);
166                         }
167
168                 });
169         }
170
171         public static TransferableGraphConfiguration2 createWithNames2(RequestProcessor processor, final Collection<SeedSpec> roots, final Collection<Resource> exclusions, final boolean ignoreVirtualResource, final boolean validate) throws DatabaseException {
172                 return processor.sync(new UniqueRead<TransferableGraphConfiguration2>() {
173
174                         @Override
175                         public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
176                                 return new TransferableGraphConfiguration2(graph, roots, Collections.<Resource>emptyList(), exclusions, ignoreVirtualResource, validate);
177                         }
178
179                 });
180         }
181
182         public static TransferableGraphConfiguration2 createWithNames(RequestProcessor processor, final Collection<NamedResource> roots, final Collection<Resource> exclusions) throws DatabaseException {
183                 return createWithNames(processor, roots, exclusions, true, true);
184         }
185
186         public static TransferableGraphConfiguration2 createWithResources(RequestProcessor processor, final Collection<Resource> roots, final Collection<Resource> exclusions) throws DatabaseException {
187                 return processor.sync(new UniqueRead<TransferableGraphConfiguration2>() {
188
189                         @Override
190                         public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
191                                 return new TransferableGraphConfiguration2(graph, Collections.<SeedSpec>emptyList(), roots, exclusions);
192                         }
193
194                 });
195         }
196
197         public static TransferableGraphConfiguration2 createForModel(RequestProcessor processor, final Resource model) throws DatabaseException {
198                 return createWithResources(processor, Collections.singletonList(model), Collections.<Resource>emptyList());
199         }
200
201         private static Collection<SeedSpec> translate(Collection<NamedResource> roots) {
202                 ArrayList<SeedSpec> result = new ArrayList<>();
203                 for(NamedResource nr : roots) result.add(new SeedSpec(nr.getResource(), nr.getName(), SeedSpecType.ROOT));
204                 return result;
205         }
206
207 }