1 package org.simantics.db.layer0.util;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
7 import java.util.TreeMap;
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;
22 public class TransferableGraphConfiguration2 {
24 public static class SeedSpec {
26 public static enum SeedSpecType {
27 // These seeds and all their child resources (ConsistsOfProcess) are internal
29 // These seeds are reported as root entries in TG,
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;
39 * Optional, may be null.
41 public final String type;
43 public SeedSpec(Resource resource, String name, SeedSpecType specType) {
44 this(resource, name, specType, null);
47 public SeedSpec(Resource resource, String name, SeedSpecType specType, String type) {
49 throw new NullPointerException("null resource");
51 throw new NullPointerException("null name");
52 this.resource = resource;
54 this.specType = specType;
58 public static SeedSpec internal(Resource resource) {
59 return new SeedSpec(resource, "", SeedSpecType.ROOT);
63 public String toString() {
64 return "SeedSpec[" + name + ", " + resource + ", " + specType + ", " + type + "]";
68 public int hashCode() {
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);
79 public boolean equals(Object obj) {
84 if (getClass() != obj.getClass())
86 SeedSpec other = (SeedSpec) obj;
87 return specType.equals(other.specType) && resource.equals(other.resource) && name.equals(other.name)
88 && objectEquals(type, other.type);
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;
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;
107 * <code>true</code> to export values, <code>false</code> to skip the actual
108 * values and only write the resource id.
110 public boolean values = true;
112 public Collection<TGValueModifier> valueModifiers;
113 public Function1<Statement,ExclusionDecision> exclusionFunction;
115 public TransferableGraphConfiguration2(Resource indexRoot, Collection<SeedSpec> seeds, Map<Resource, ExtentStatus> preStatus, boolean ignoreVirtualResources, boolean validate) {
116 this.indexRoot = indexRoot;
118 this.preStatus = preStatus;
119 this.ignoreVirtualResources = ignoreVirtualResources;
120 this.validate = validate;
121 this.valueModifiers = null;
124 public TransferableGraphConfiguration2(Resource indexRoot, Collection<SeedSpec> roots, Map<Resource, ExtentStatus> preStatus, boolean ignoreVirtualResources) {
125 this(indexRoot, roots, preStatus, ignoreVirtualResources, true);
128 public TransferableGraphConfiguration2(Resource indexRoot, Collection<SeedSpec> roots, Map<Resource, ExtentStatus> preStatus) {
129 this(indexRoot, roots, preStatus, true);
132 public TransferableGraphConfiguration2(TransferableGraphConfiguration2 conf) throws DatabaseException {
133 this(conf.indexRoot, conf.seeds, conf.preStatus, conf.ignoreVirtualResources, conf.validate);
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());
140 public TransferableGraphConfiguration2(ReadGraph graph, Collection<Resource> roots, boolean ignoreVirtualResources, boolean validate) throws DatabaseException {
141 this(new TGConfigurer(graph, ignoreVirtualResources, validate).roots2(roots).create());
144 public TransferableGraphConfiguration2(ReadGraph graph, Resource model, boolean ignoreVirtualResources) throws DatabaseException {
145 this(graph, model, ignoreVirtualResources, true);
148 public TransferableGraphConfiguration2(ReadGraph graph, Resource model) throws DatabaseException {
149 this(graph, model, true);
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());
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());
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>() {
164 public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
165 return new TransferableGraphConfiguration2(graph, translate(roots), Collections.<Resource>emptyList(), exclusions, ignoreVirtualResource, validate);
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>() {
175 public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
176 return new TransferableGraphConfiguration2(graph, roots, Collections.<Resource>emptyList(), exclusions, ignoreVirtualResource, validate);
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);
186 public static TransferableGraphConfiguration2 createWithResources(RequestProcessor processor, final Collection<Resource> roots, final Collection<Resource> exclusions) throws DatabaseException {
187 return processor.sync(new UniqueRead<TransferableGraphConfiguration2>() {
190 public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
191 return new TransferableGraphConfiguration2(graph, Collections.<SeedSpec>emptyList(), roots, exclusions);
197 public static TransferableGraphConfiguration2 createForModel(RequestProcessor processor, final Resource model) throws DatabaseException {
198 return createWithResources(processor, Collections.singletonList(model), Collections.<Resource>emptyList());
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));