1 package org.simantics.plant3d.scl;
3 import org.simantics.Simantics;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.exception.DatabaseException;
7 import org.simantics.db.request.Read;
8 import org.simantics.g3d.scenegraph.base.INode;
9 import org.simantics.objmap.graph.IMapping;
10 import org.simantics.objmap.graph.Mappings;
11 import org.simantics.objmap.graph.schema.IMappingSchema;
12 import org.simantics.plant3d.ontology.Plant3D;
13 import org.simantics.plant3d.scenegraph.P3DRootNode;
14 import org.simantics.plant3d.scenegraph.SchemaBuilder;
15 import org.simantics.plant3d.scenegraph.controlpoint.ControlPointFactory;
16 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
17 import org.simantics.plant3d.utils.ComponentUtils;
18 import org.simantics.plant3d.utils.P3DUtil;
20 public class SCLUtil {
22 public static interface SchemaProvider {
23 IMappingSchema<Resource,INode> get(ReadGraph graph) throws DatabaseException;
26 public static interface NodeMapProvider {
27 P3DScriptNodeMap get(IMapping<Resource, INode> mapping, P3DRootNode rootNode);
30 public static P3DScriptNodeMap load(final Resource root) throws DatabaseException {
31 preload(Plant3D.URIs.Builtin);
33 SchemaProvider schemaProvider = g -> SchemaBuilder.getSchema(g);
34 NodeMapProvider mapProvider = (mapping, rootNode) -> new P3DScriptNodeMap(Simantics.getSession(), mapping, rootNode);
35 return load(root, schemaProvider, mapProvider);
38 public static P3DRootNode loadReadOnly(final Resource root) throws DatabaseException {
39 preload(Plant3D.URIs.Builtin);
41 SchemaProvider schemaProvider = g -> SchemaBuilder.getSchema(g);
42 return loadReadOnly(root, schemaProvider);
45 public static void preload(String symbolSet) throws DatabaseException {
47 ControlPointFactory.preloadCache(symbolSet);
48 ComponentUtils.preloadCache();
49 } catch (Exception e) {
50 throw new DatabaseException(e);
54 public static <NodeMap extends P3DScriptNodeMap> NodeMap load(final Resource root, SchemaProvider schemaProvider, NodeMapProvider mapProvider) throws DatabaseException {
55 return Simantics.getSession().syncRequest(new Read<NodeMap>() {
57 public NodeMap perform(ReadGraph graph) throws DatabaseException {
58 PipingRules.setEnabled(false);
59 IMappingSchema<Resource, INode> schema = schemaProvider.get(graph);
60 IMapping<Resource, INode> mapping = Mappings.createWithListening(schema);
61 P3DRootNode rootNode = (P3DRootNode) mapping.map(graph, root);
62 rootNode.setMapping(mapping);
64 P3DUtil.finalizeDBLoad(rootNode);
65 // FIXME: Something needs to be done here...
66 P3DUtil.finalizeDBLoad2(rootNode);
67 } catch (Exception e) {
68 throw new DatabaseException(e);
71 @SuppressWarnings("unchecked")
72 NodeMap nodeMap = (NodeMap) mapProvider.get(mapping, rootNode);
78 public static <Root extends P3DRootNode> Root loadReadOnly(final Resource root, SchemaProvider schemaProvider)
79 throws DatabaseException {
80 return Simantics.getSession().syncRequest(new Read<Root>() {
82 public Root perform(ReadGraph graph) throws DatabaseException {
83 PipingRules.setEnabled(false);
84 IMappingSchema<Resource, INode> schema = schemaProvider.get(graph);
85 IMapping<Resource, INode> mapping = Mappings.createWithoutListening(schema);
86 @SuppressWarnings("unchecked")
87 Root rootNode = (Root) mapping.map(graph, root);
88 rootNode.setMapping(mapping);
90 P3DUtil.finalizeDBLoad(rootNode);
91 P3DUtil.finalizeDBLoad2(rootNode);
92 } catch (Exception e) {
93 throw new DatabaseException(e);