]> gerrit.simantics Code Review - simantics/platform.git/blob
e901b0cdba63e54c397613d49c021f34beda838f
[simantics/platform.git] /
1 package org.simantics.scenegraph.loader;
2
3 import java.util.Set;
4
5 import org.simantics.Simantics;
6 import org.simantics.db.Disposable;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.RequestProcessor;
9 import org.simantics.db.Resource;
10 import org.simantics.db.common.primitiverequest.PossibleResource;
11 import org.simantics.db.common.procedure.adapter.ListenerSupport;
12 import org.simantics.db.common.request.TernaryRead;
13 import org.simantics.db.common.request.UnaryRead;
14 import org.simantics.db.common.utils.Logger;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.request.ResourceVariable;
17 import org.simantics.db.layer0.variable.Variable;
18 import org.simantics.db.layer0.variable.VariableRepository;
19 import org.simantics.scenegraph.INode;
20 import org.simantics.scenegraph.ParentNode;
21 import org.simantics.utils.datastructures.Pair;
22
23 import gnu.trove.set.hash.THashSet;
24
25 public class ScenegraphLoaderProcess implements Disposable, ListenerSupport {
26
27         final private String name;
28         final Class<?> loaderClass;
29
30         protected INode root;
31
32         private boolean disposed = false;
33         
34         final protected Set<String> registeredURIs = new THashSet<String>();
35         
36         public ScenegraphLoaderProcess(String name) {
37                 this(null, ScenegraphLoader.class, name);
38         }
39
40         public ScenegraphLoaderProcess(INode root, String name) {
41                 this(root, ScenegraphLoader.class, name);
42         }
43         
44         public ScenegraphLoaderProcess(INode root, Class<?> loaderClass, String name) {
45                 this.root = root;
46                 this.loaderClass = loaderClass;
47                 this.name = name;
48                 //System.out.println("NEW ScenegraphLoaderProcess(" + name + "): " + this);
49                 //new Exception("NEW ScenegraphLoaderProcess(" + name + "): " + this).printStackTrace();
50         }
51
52         public INode getRoot() {
53                 return root;
54         }
55         
56         @Override
57         public String toString() {
58                 return name;
59         }
60
61         protected void initialize(RequestProcessor processor, Variable configuration) throws DatabaseException {
62                 
63         }
64         
65         final public <N extends ParentNode<?>> N load(final String curi, final Resource runtime) throws DatabaseException {
66                 return load(Simantics.getSession(), curi, runtime);
67         }
68
69         final public <N extends ParentNode<?>> N load(RequestProcessor processor, final String curi, final Resource runtime) throws DatabaseException {
70                 Resource resource = processor.sync(new PossibleResource(curi));
71                 if(resource == null) throw new IllegalArgumentException("The given URI is invalid: " + curi);
72                 return load(processor, resource, runtime);
73         }
74
75         final public <N extends ParentNode<?>> N load(Resource configuration, Resource runtime) throws DatabaseException {
76                 return load(Simantics.getSession(), configuration, runtime);
77         }
78
79         final public <N extends ParentNode<?>> N load(RequestProcessor processor, Resource configuration, Resource runtime) throws DatabaseException {
80                 N root = (N)getRoot();
81                 return load(processor, getVariable(processor, this, configuration, runtime, root));
82         }
83         
84         final public Variable getVariable(RequestProcessor processor, String curi, Resource runtime) throws DatabaseException {
85                 Resource resource = processor.sync(new PossibleResource(curi));
86                 if(resource == null) throw new IllegalArgumentException("The given URI is invalid: " + curi);
87                 return getVariable(processor, this, resource, runtime, root);
88         }
89         
90         final static public Variable getVariable(RequestProcessor processor, final ScenegraphLoaderProcess process, final Resource configuration, final Resource runtime, final INode root) throws DatabaseException {
91                 
92                 assert(root != null);
93
94                 Pair<Variable,String> result = processor.sync(new TernaryRead<INode, Resource, Resource, Pair<Variable,String>>(root, runtime, configuration) {
95
96                         @Override
97                         public Pair<Variable,String> perform(ReadGraph graph) throws DatabaseException {
98                                 Variable conf = graph.sync(new ResourceVariable(parameter3));
99                                 Variable result = new ScenegraphVariable(conf, parameter3, parameter2, parameter);
100                                 return Pair.make(result, result.getURI(graph));
101                         }
102                         
103                 });
104
105                 VariableRepository.register(result.second, result.first);
106                 if(process != null) process.registeredURIs.add(result.second);
107                 return result.first;
108                 
109         }
110         
111         final public <N extends ParentNode<?>> N load(RequestProcessor processor, final Variable configuration) throws DatabaseException {
112                 
113                 initialize(processor, configuration);
114                 N root = (N)getRoot();
115                 load(processor, configuration, root);
116                 return root;
117                 
118         }
119
120         <T extends ScenegraphLoader> Class<T> getLoaderClass() {
121                 return (Class<T>)ScenegraphLoader.class;
122         }
123         
124         protected ScenegraphLoader getLoader(RequestProcessor processor, Variable configuration) throws DatabaseException {
125                 
126                 return processor.sync(new UnaryRead<Variable, ScenegraphLoader>(configuration) {
127
128                         @Override
129                         public ScenegraphLoader perform(ReadGraph graph) throws DatabaseException {
130                                 return parameter.adapt(graph, getLoaderClass());
131                         }
132
133                 });
134         }
135         
136         @SuppressWarnings("unchecked")
137         final private INode load(RequestProcessor processor, Variable configuration, ParentNode<?> current) throws DatabaseException {
138                 
139                 ScenegraphLoader loader = getLoader(processor, configuration);
140                 
141                 INode node = loader.create(processor, this, current, configuration);
142                 if(node instanceof ParentNode) {
143                 
144                         for(Variable child : ScenegraphLoaderUtils.getChildren(processor, configuration)) {
145                                 load(processor, child, (ParentNode<INode>)node);
146                         }
147                         
148                 }
149                 
150                 return node;
151                 
152         }
153
154         @Override
155         public void exception(Throwable t) {
156                 Logger.defaultLogError(t);
157         }
158
159         @Override
160         public boolean isDisposed() {
161                 return disposed;
162         }
163
164         @Override
165         public void dispose() {
166                 //System.out.println("DISPOSE ScenegraphLoaderProcess(" + name + "): " + this);
167                 //new Exception("DISPOSED ScenegraphLoaderProcess(" + name + "): " + this).printStackTrace();
168                 disposed = true;
169         }
170         
171 }