1 package org.simantics.scenegraph.loader;
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;
23 import gnu.trove.set.hash.THashSet;
25 public class ScenegraphLoaderProcess implements Disposable, ListenerSupport {
27 final private String name;
28 final Class<?> loaderClass;
32 private boolean disposed = false;
34 final protected Set<String> registeredURIs = new THashSet<String>();
36 public ScenegraphLoaderProcess(String name) {
37 this(null, ScenegraphLoader.class, name);
40 public ScenegraphLoaderProcess(INode root, String name) {
41 this(root, ScenegraphLoader.class, name);
44 public ScenegraphLoaderProcess(INode root, Class<?> loaderClass, String name) {
46 this.loaderClass = loaderClass;
48 //System.out.println("NEW ScenegraphLoaderProcess(" + name + "): " + this);
49 //new Exception("NEW ScenegraphLoaderProcess(" + name + "): " + this).printStackTrace();
52 public INode getRoot() {
57 public String toString() {
61 protected void initialize(RequestProcessor processor, Variable configuration) throws DatabaseException {
65 final public <N extends ParentNode<?>> N load(final String curi, final Resource runtime) throws DatabaseException {
66 return load(Simantics.getSession(), curi, runtime);
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);
75 final public <N extends ParentNode<?>> N load(Resource configuration, Resource runtime) throws DatabaseException {
76 return load(Simantics.getSession(), configuration, runtime);
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));
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);
90 final static public Variable getVariable(RequestProcessor processor, final ScenegraphLoaderProcess process, final Resource configuration, final Resource runtime, final INode root) throws DatabaseException {
94 Pair<Variable,String> result = processor.sync(new TernaryRead<INode, Resource, Resource, Pair<Variable,String>>(root, runtime, configuration) {
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));
105 VariableRepository.register(result.second, result.first);
106 if(process != null) process.registeredURIs.add(result.second);
111 final public <N extends ParentNode<?>> N load(RequestProcessor processor, final Variable configuration) throws DatabaseException {
113 initialize(processor, configuration);
114 N root = (N)getRoot();
115 load(processor, configuration, root);
120 <T extends ScenegraphLoader> Class<T> getLoaderClass() {
121 return (Class<T>)ScenegraphLoader.class;
124 protected ScenegraphLoader getLoader(RequestProcessor processor, Variable configuration) throws DatabaseException {
126 return processor.sync(new UnaryRead<Variable, ScenegraphLoader>(configuration) {
129 public ScenegraphLoader perform(ReadGraph graph) throws DatabaseException {
130 return parameter.adapt(graph, getLoaderClass());
136 @SuppressWarnings("unchecked")
137 final private INode load(RequestProcessor processor, Variable configuration, ParentNode<?> current) throws DatabaseException {
139 ScenegraphLoader loader = getLoader(processor, configuration);
141 INode node = loader.create(processor, this, current, configuration);
142 if(node instanceof ParentNode) {
144 for(Variable child : ScenegraphLoaderUtils.getChildren(processor, configuration)) {
145 load(processor, child, (ParentNode<INode>)node);
155 public void exception(Throwable t) {
156 Logger.defaultLogError(t);
160 public boolean isDisposed() {
165 public void dispose() {
166 //System.out.println("DISPOSE ScenegraphLoaderProcess(" + name + "): " + this);
167 //new Exception("DISPOSED ScenegraphLoaderProcess(" + name + "): " + this).printStackTrace();