-package org.simantics.diagram.export;\r
-\r
-import java.util.Collection;\r
-import java.util.Deque;\r
-import java.util.HashMap;\r
-import java.util.LinkedList;\r
-import java.util.Map;\r
-\r
-import org.simantics.NameLabelMode;\r
-import org.simantics.NameLabelUtil;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.request.UniqueRead;\r
-import org.simantics.db.exception.AssumptionException;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;\r
-import org.simantics.db.exception.ServiceException;\r
-import org.simantics.db.exception.ValidationException;\r
-import org.simantics.db.layer0.request.PossibleModel;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.export.core.ExportContext;\r
-import org.simantics.export.core.error.ExportException;\r
-import org.simantics.export.core.intf.ContentTypeAction;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.simulation.ontology.SimulationResource;\r
-\r
-public class DiagramContentTypeAction implements ContentTypeAction {\r
- \r
- public static String getDiagramLabel(ReadGraph graph, Resource r) throws DatabaseException\r
- {\r
- SimulationResource SIM = SimulationResource.getInstance(graph);\r
-\r
- NameLabelMode mode = NameLabelUtil.getNameLabelMode(graph);\r
-\r
- labelFailed:\r
- try { \r
- Resource model = graph.sync( new PossibleModel(r) );\r
- if ( model == null ) break labelFailed;\r
- Resource configuration = graph.getPossibleObject(model, SIM.HasConfiguration);\r
- if ( configuration == null ) break labelFailed;\r
- Deque<Resource> path = getPathTo(graph, r, model);\r
- if ( path == null) break labelFailed;\r
- path.remove(configuration);\r
-\r
- StringBuilder sb = new StringBuilder();\r
- for ( Resource node : path ) {\r
- if ( sb.length()>0 ) sb.append(" / ");\r
- String nodeLabel = NameLabelUtil.modalName(graph, node, mode);\r
- sb.append( nodeLabel );\r
- }\r
- \r
- return sb.toString();\r
- } catch (AssumptionException e) {\r
- } catch (ValidationException e) {\r
- } catch (ServiceException e) {\r
- }\r
- \r
- String uri = graph.getURI( r );\r
- int c = uri.lastIndexOf('/');\r
- if ( c>=0 ) {\r
- String label = uri.substring(c+1);\r
- if ( !label.isEmpty() ) return label;\r
- }\r
- \r
- return uri;\r
- }\r
-\r
- @Override\r
- public Map<String, String> getLabels(ExportContext ctx, final Collection<String> uris) throws ExportException {\r
- Read<Map<String, String>> req = new UniqueRead<Map<String, String>>() {\r
- @Override\r
- public Map<String, String> perform(ReadGraph graph) throws DatabaseException {\r
- Map<String, String> result = new HashMap<String, String>();\r
- \r
- for ( String uri : uris ) {\r
- Resource r = graph.getResource( uri );\r
- String label = getDiagramLabel(graph, r);\r
- result.put(uri, label);\r
- }\r
- \r
- return result;\r
- }\r
- };\r
- \r
- try {\r
- return ctx.session.syncRequest( req );\r
- } catch (DatabaseException e) {\r
- throw new ExportException( e );\r
- }\r
- }\r
- \r
- /**\r
- * Get all resource between start and end by following PartOf relation.\r
- * If there is no path, returns null.\r
- * Start is included, end is excluded.\r
- * \r
- * @param graph\r
- * @param start\r
- * @param end\r
- * @return path from end to start\r
- * @throws ServiceException \r
- * @throws ManyObjectsForFunctionalRelationException \r
- */\r
- static Deque<Resource> getPathTo(ReadGraph graph, Resource start, Resource end) throws DatabaseException {\r
- LinkedList<Resource> result = new LinkedList<Resource>();\r
- \r
- Layer0 L0 = Layer0.getInstance(graph);\r
- Resource pos = start;\r
- while ( !pos.equals(end) ) {\r
- result.add(0, pos);\r
- pos = graph.getPossibleObject(pos, L0.PartOf);\r
- if ( pos==null ) return null;\r
- }\r
- return result;\r
- }\r
-\r
-}\r
+package org.simantics.diagram.export;
+
+import java.util.Collection;
+import java.util.Deque;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.Map;
+
+import org.simantics.NameLabelMode;
+import org.simantics.NameLabelUtil;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.exception.AssumptionException;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
+import org.simantics.db.exception.ServiceException;
+import org.simantics.db.exception.ValidationException;
+import org.simantics.db.layer0.request.PossibleModel;
+import org.simantics.db.request.Read;
+import org.simantics.export.core.ExportContext;
+import org.simantics.export.core.error.ExportException;
+import org.simantics.export.core.intf.ContentTypeAction;
+import org.simantics.layer0.Layer0;
+import org.simantics.simulation.ontology.SimulationResource;
+
+public class DiagramContentTypeAction implements ContentTypeAction {
+
+ public static String getDiagramLabel(ReadGraph graph, Resource r) throws DatabaseException
+ {
+ SimulationResource SIM = SimulationResource.getInstance(graph);
+
+ NameLabelMode mode = NameLabelUtil.getNameLabelMode(graph);
+
+ labelFailed:
+ try {
+ Resource model = graph.sync( new PossibleModel(r) );
+ if ( model == null ) break labelFailed;
+ Resource configuration = graph.getPossibleObject(model, SIM.HasConfiguration);
+ if ( configuration == null ) break labelFailed;
+ Deque<Resource> path = getPathTo(graph, r, model);
+ if ( path == null) break labelFailed;
+ path.remove(configuration);
+
+ StringBuilder sb = new StringBuilder();
+ for ( Resource node : path ) {
+ if ( sb.length()>0 ) sb.append(" / ");
+ String nodeLabel = NameLabelUtil.modalName(graph, node, mode);
+ sb.append( nodeLabel );
+ }
+
+ return sb.toString();
+ } catch (AssumptionException e) {
+ } catch (ValidationException e) {
+ } catch (ServiceException e) {
+ }
+
+ String uri = graph.getURI( r );
+ int c = uri.lastIndexOf('/');
+ if ( c>=0 ) {
+ String label = uri.substring(c+1);
+ if ( !label.isEmpty() ) return label;
+ }
+
+ return uri;
+ }
+
+ @Override
+ public Map<String, String> getLabels(ExportContext ctx, final Collection<String> uris) throws ExportException {
+ Read<Map<String, String>> req = new UniqueRead<Map<String, String>>() {
+ @Override
+ public Map<String, String> perform(ReadGraph graph) throws DatabaseException {
+ Map<String, String> result = new HashMap<String, String>();
+
+ for ( String uri : uris ) {
+ Resource r = graph.getResource( uri );
+ String label = getDiagramLabel(graph, r);
+ result.put(uri, label);
+ }
+
+ return result;
+ }
+ };
+
+ try {
+ return ctx.session.syncRequest( req );
+ } catch (DatabaseException e) {
+ throw new ExportException( e );
+ }
+ }
+
+ /**
+ * Get all resource between start and end by following PartOf relation.
+ * If there is no path, returns null.
+ * Start is included, end is excluded.
+ *
+ * @param graph
+ * @param start
+ * @param end
+ * @return path from end to start
+ * @throws ServiceException
+ * @throws ManyObjectsForFunctionalRelationException
+ */
+ static Deque<Resource> getPathTo(ReadGraph graph, Resource start, Resource end) throws DatabaseException {
+ LinkedList<Resource> result = new LinkedList<Resource>();
+
+ Layer0 L0 = Layer0.getInstance(graph);
+ Resource pos = start;
+ while ( !pos.equals(end) ) {
+ result.add(0, pos);
+ pos = graph.getPossibleObject(pos, L0.PartOf);
+ if ( pos==null ) return null;
+ }
+ return result;
+ }
+
+}