1 package org.simantics.diagram.export;
3 import java.util.Collection;
4 import java.util.Deque;
5 import java.util.HashMap;
6 import java.util.LinkedList;
9 import org.simantics.NameLabelMode;
10 import org.simantics.NameLabelUtil;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.common.request.UniqueRead;
14 import org.simantics.db.exception.AssumptionException;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
17 import org.simantics.db.exception.ServiceException;
18 import org.simantics.db.exception.ValidationException;
19 import org.simantics.db.layer0.request.PossibleModel;
20 import org.simantics.db.request.Read;
21 import org.simantics.export.core.ExportContext;
22 import org.simantics.export.core.error.ExportException;
23 import org.simantics.export.core.intf.ContentTypeAction;
24 import org.simantics.layer0.Layer0;
25 import org.simantics.simulation.ontology.SimulationResource;
27 public class DiagramContentTypeAction implements ContentTypeAction {
29 public static String getDiagramLabel(ReadGraph graph, Resource r) throws DatabaseException
31 SimulationResource SIM = SimulationResource.getInstance(graph);
33 NameLabelMode mode = NameLabelUtil.getNameLabelMode(graph);
37 Resource model = graph.sync( new PossibleModel(r) );
38 if ( model == null ) break labelFailed;
39 Resource configuration = graph.getPossibleObject(model, SIM.HasConfiguration);
40 if ( configuration == null ) break labelFailed;
41 Deque<Resource> path = getPathTo(graph, r, model);
42 if ( path == null) break labelFailed;
43 path.remove(configuration);
45 StringBuilder sb = new StringBuilder();
46 for ( Resource node : path ) {
47 if ( sb.length()>0 ) sb.append(" / ");
48 String nodeLabel = NameLabelUtil.modalName(graph, node, mode);
49 sb.append( nodeLabel );
53 } catch (AssumptionException e) {
54 } catch (ValidationException e) {
55 } catch (ServiceException e) {
58 String uri = graph.getURI( r );
59 int c = uri.lastIndexOf('/');
61 String label = uri.substring(c+1);
62 if ( !label.isEmpty() ) return label;
69 public Map<String, String> getLabels(ExportContext ctx, final Collection<String> uris) throws ExportException {
70 Read<Map<String, String>> req = new UniqueRead<Map<String, String>>() {
72 public Map<String, String> perform(ReadGraph graph) throws DatabaseException {
73 Map<String, String> result = new HashMap<String, String>();
75 for ( String uri : uris ) {
76 Resource r = graph.getResource( uri );
77 String label = getDiagramLabel(graph, r);
78 result.put(uri, label);
86 return ctx.session.syncRequest( req );
87 } catch (DatabaseException e) {
88 throw new ExportException( e );
93 * Get all resource between start and end by following PartOf relation.
94 * If there is no path, returns null.
95 * Start is included, end is excluded.
100 * @return path from end to start
101 * @throws ServiceException
102 * @throws ManyObjectsForFunctionalRelationException
104 static Deque<Resource> getPathTo(ReadGraph graph, Resource start, Resource end) throws DatabaseException {
105 LinkedList<Resource> result = new LinkedList<Resource>();
107 Layer0 L0 = Layer0.getInstance(graph);
108 Resource pos = start;
109 while ( !pos.equals(end) ) {
111 pos = graph.getPossibleObject(pos, L0.PartOf);
112 if ( pos==null ) return null;