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 INTERNAL, ROOT, SPECIAL_ROOT
30 public final Resource resource;
31 public final String name;
32 // Special roots are not normal seeds - e.g. %model
33 public final SeedSpecType specType;
35 * Optional, may be null.
37 public final String type;
39 public SeedSpec(Resource resource, String name, SeedSpecType specType) {
40 this(resource, name, specType, null);
43 public SeedSpec(Resource resource, String name, SeedSpecType specType, String type) {
45 throw new NullPointerException("null resource");
47 throw new NullPointerException("null name");
48 this.resource = resource;
50 this.specType = specType;
54 public static SeedSpec internal(Resource resource) {
55 return new SeedSpec(resource, "", SeedSpecType.ROOT);
59 public String toString() {
60 return "SeedSpec[" + name + ", " + resource + ", " + specType + ", " + type + "]";
64 public int hashCode() {
67 result = prime * result + specType.hashCode();
68 result = prime * result + resource.hashCode();
69 result = prime * result + name.hashCode();
70 result = prime * result + (type != null ? type.hashCode() : 0);
75 public boolean equals(Object obj) {
80 if (getClass() != obj.getClass())
82 SeedSpec other = (SeedSpec) obj;
83 return specType.equals(other.specType) && resource.equals(other.resource) && name.equals(other.name)
84 && objectEquals(type, other.type);
87 private boolean objectEquals(Object o1, Object o2) {
88 if (o1 == o2) return true;
89 if (o1 == null && o2 == null) return true;
90 if (o1 == null || o2 == null) return false;
95 final public TreeMap<String, Variant> baseExtensions = new TreeMap<>();
96 final public Resource indexRoot;
97 final public Collection<SeedSpec> seeds;
98 final public Map<Resource, ExtentStatus> preStatus;
99 final public boolean ignoreVirtualResources;
100 final public boolean validate;
103 * <code>true</code> to export values, <code>false</code> to skip the actual
104 * values and only write the resource id.
106 public boolean values = true;
108 public Collection<TGValueModifier> valueModifiers;
109 public Function1<Statement,ExclusionDecision> exclusionFunction;
111 public TransferableGraphConfiguration2(Resource indexRoot, Collection<SeedSpec> seeds, Map<Resource, ExtentStatus> preStatus, boolean ignoreVirtualResources, boolean validate) {
112 this.indexRoot = indexRoot;
114 this.preStatus = preStatus;
115 this.ignoreVirtualResources = ignoreVirtualResources;
116 this.validate = validate;
117 this.valueModifiers = null;
120 public TransferableGraphConfiguration2(Resource indexRoot, Collection<SeedSpec> roots, Map<Resource, ExtentStatus> preStatus, boolean ignoreVirtualResources) {
121 this(indexRoot, roots, preStatus, ignoreVirtualResources, true);
124 public TransferableGraphConfiguration2(Resource indexRoot, Collection<SeedSpec> roots, Map<Resource, ExtentStatus> preStatus) {
125 this(indexRoot, roots, preStatus, true);
128 public TransferableGraphConfiguration2(TransferableGraphConfiguration2 conf) throws DatabaseException {
129 this(conf.indexRoot, conf.seeds, conf.preStatus, conf.ignoreVirtualResources, conf.validate);
132 public TransferableGraphConfiguration2(ReadGraph graph, Resource model, boolean ignoreVirtualResources, boolean validate) throws DatabaseException {
133 this(new TGConfigurer(graph, ignoreVirtualResources, validate).roots2(Collections.singletonList(model)).create());
136 public TransferableGraphConfiguration2(ReadGraph graph, Collection<Resource> roots, boolean ignoreVirtualResources, boolean validate) throws DatabaseException {
137 this(new TGConfigurer(graph, ignoreVirtualResources, validate).roots2(roots).create());
140 public TransferableGraphConfiguration2(ReadGraph graph, Resource model, boolean ignoreVirtualResources) throws DatabaseException {
141 this(graph, model, ignoreVirtualResources, true);
144 public TransferableGraphConfiguration2(ReadGraph graph, Resource model) throws DatabaseException {
145 this(graph, model, true);
148 public TransferableGraphConfiguration2(ReadGraph graph, Collection<SeedSpec> roots, Collection<Resource> resourceRoots, Collection<Resource> exclusions) throws DatabaseException {
149 this(new TGConfigurer(graph, true).roots(roots).roots2(resourceRoots).exclusions(exclusions).create());
152 public TransferableGraphConfiguration2(ReadGraph graph, Collection<SeedSpec> roots, Collection<Resource> resourceRoots, Collection<Resource> exclusions, boolean ignoreVirtualResource, boolean validate) throws DatabaseException {
153 this(new TGConfigurer(graph, ignoreVirtualResource, validate).roots(roots).roots2(resourceRoots).exclusions(exclusions).create());
156 public static TransferableGraphConfiguration2 createWithNames(RequestProcessor processor, final Collection<NamedResource> roots, final Collection<Resource> exclusions, final boolean ignoreVirtualResource, final boolean validate) throws DatabaseException {
157 return processor.sync(new UniqueRead<TransferableGraphConfiguration2>() {
160 public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
161 return new TransferableGraphConfiguration2(graph, translate(roots), Collections.<Resource>emptyList(), exclusions, ignoreVirtualResource, validate);
167 public static TransferableGraphConfiguration2 createWithNames2(RequestProcessor processor, final Collection<SeedSpec> roots, final Collection<Resource> exclusions, final boolean ignoreVirtualResource, final boolean validate) throws DatabaseException {
168 return processor.sync(new UniqueRead<TransferableGraphConfiguration2>() {
171 public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
172 return new TransferableGraphConfiguration2(graph, roots, Collections.<Resource>emptyList(), exclusions, ignoreVirtualResource, validate);
178 public static TransferableGraphConfiguration2 createWithNames(RequestProcessor processor, final Collection<NamedResource> roots, final Collection<Resource> exclusions) throws DatabaseException {
179 return createWithNames(processor, roots, exclusions, true, true);
182 public static TransferableGraphConfiguration2 createWithResources(RequestProcessor processor, final Collection<Resource> roots, final Collection<Resource> exclusions) throws DatabaseException {
183 return processor.sync(new UniqueRead<TransferableGraphConfiguration2>() {
186 public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {
187 return new TransferableGraphConfiguration2(graph, Collections.<SeedSpec>emptyList(), roots, exclusions);
193 public static TransferableGraphConfiguration2 createForModel(RequestProcessor processor, final Resource model) throws DatabaseException {
194 return createWithResources(processor, Collections.singletonList(model), Collections.<Resource>emptyList());
197 private static Collection<SeedSpec> translate(Collection<NamedResource> roots) {
198 ArrayList<SeedSpec> result = new ArrayList<>();
199 for(NamedResource nr : roots) result.add(new SeedSpec(nr.getResource(), nr.getName(), SeedSpecType.ROOT));