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.objmap.graph.IMapping;
9 import org.simantics.objmap.graph.Mappings;
10 import org.simantics.objmap.graph.schema.IMappingSchema;
11 import org.simantics.plant3d.ontology.Plant3D;
12 import org.simantics.plant3d.scenegraph.P3DRootNode;
13 import org.simantics.plant3d.scenegraph.SchemaBuilder;
14 import org.simantics.plant3d.scenegraph.controlpoint.ControlPointFactory;
15 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
16 import org.simantics.plant3d.utils.ComponentUtils;
17 import org.simantics.plant3d.utils.P3DUtil;
19 public class SCLUtil {
21 public static interface SchemaProvider {
22 IMappingSchema<Resource,Object> get(ReadGraph graph) throws DatabaseException;
25 public static interface NodeMapProvider {
26 P3DScriptNodeMap get(IMapping<Resource, Object> mapping, P3DRootNode rootNode);
29 public static P3DScriptNodeMap load(final Resource root) throws DatabaseException {
30 preload(Plant3D.URIs.Builtin);
32 SchemaProvider schemaProvider = g -> SchemaBuilder.getSchema(g);
33 NodeMapProvider mapProvider = (mapping, rootNode) -> new P3DScriptNodeMap(Simantics.getSession(), mapping, rootNode);
34 return load(root, schemaProvider, mapProvider);
37 public static P3DRootNode loadReadOnly(final Resource root) throws DatabaseException {
38 preload(Plant3D.URIs.Builtin);
40 SchemaProvider schemaProvider = g -> SchemaBuilder.getSchema(g);
41 return loadReadOnly(root, schemaProvider);
44 public static void preload(String symbolSet) throws DatabaseException {
46 ControlPointFactory.preloadCache(symbolSet);
47 ComponentUtils.preloadCache();
48 } catch (Exception e) {
49 throw new DatabaseException(e);
53 public static <NodeMap extends P3DScriptNodeMap> NodeMap load(final Resource root, SchemaProvider schemaProvider, NodeMapProvider mapProvider) throws DatabaseException {
54 return Simantics.getSession().syncRequest(new Read<NodeMap>() {
56 public NodeMap perform(ReadGraph graph) throws DatabaseException {
57 PipingRules.setEnabled(false);
58 IMappingSchema<Resource, Object> schema = schemaProvider.get(graph);
59 IMapping<Resource, Object> mapping = Mappings.createWithListening(schema);
60 P3DRootNode rootNode = (P3DRootNode) mapping.map(graph, root);
61 rootNode.setMapping((IMapping)mapping);
63 P3DUtil.finalizeDBLoad(rootNode);
64 // FIXME: Something needs to be done here...
65 P3DUtil.finalizeDBLoad2(rootNode);
66 } catch (Exception e) {
67 throw new DatabaseException(e);
70 @SuppressWarnings("unchecked")
71 NodeMap nodeMap = (NodeMap) mapProvider.get(mapping, rootNode);
77 public static <Root extends P3DRootNode> Root loadReadOnly(final Resource root, SchemaProvider schemaProvider)
78 throws DatabaseException {
79 return Simantics.getSession().syncRequest(new Read<Root>() {
81 public Root perform(ReadGraph graph) throws DatabaseException {
82 PipingRules.setEnabled(false);
83 IMappingSchema<Resource, Object> schema = schemaProvider.get(graph);
84 IMapping<Resource, Object> mapping = Mappings.createWithoutListening(schema);
85 @SuppressWarnings("unchecked")
86 Root rootNode = (Root) mapping.map(graph, root);
87 rootNode.setMapping((IMapping)mapping);
89 P3DUtil.finalizeDBLoad(rootNode);
90 P3DUtil.finalizeDBLoad2(rootNode);
91 } catch (Exception e) {
92 throw new DatabaseException(e);