X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fexport%2FDiagramContentTypeAction.java;h=f65493e96255944442e2cef5eb5db935d96aca47;hb=a96e5125d72579c43abd70eb5c23de835324eaad;hp=520841ee23d9d4a21cea39f60626b6f0263abda3;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/export/DiagramContentTypeAction.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/export/DiagramContentTypeAction.java index 520841ee2..f65493e96 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/export/DiagramContentTypeAction.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/export/DiagramContentTypeAction.java @@ -1,117 +1,117 @@ -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 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 getLabels(ExportContext ctx, final Collection uris) throws ExportException { - Read> req = new UniqueRead>() { - @Override - public Map perform(ReadGraph graph) throws DatabaseException { - Map result = new HashMap(); - - 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 getPathTo(ReadGraph graph, Resource start, Resource end) throws DatabaseException { - LinkedList result = new LinkedList(); - - 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; - } - -} +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 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 getLabels(ExportContext ctx, final Collection uris) throws ExportException { + Read> req = new UniqueRead>() { + @Override + public Map perform(ReadGraph graph) throws DatabaseException { + Map result = new HashMap(); + + 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 getPathTo(ReadGraph graph, Resource start, Resource end) throws DatabaseException { + LinkedList result = new LinkedList(); + + 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; + } + +}