1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.query;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Statement;
20 import org.simantics.db.common.ResourceArray;
21 import org.simantics.db.common.request.ResourceRead;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.procedure.Listener;
24 import org.simantics.db.request.AsyncRead;
25 import org.simantics.db.request.Read;
26 import org.simantics.diagram.adapter.IDiagramLoader;
27 import org.simantics.diagram.adapter.NodeRequest;
28 import org.simantics.diagram.connection.ConnectionVisuals;
29 import org.simantics.diagram.content.TerminalMap;
30 import org.simantics.diagram.flag.FlagUtil;
31 import org.simantics.diagram.stubs.DiagramResource;
32 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
33 import org.simantics.g2d.canvas.ICanvasContext;
34 import org.simantics.g2d.diagram.IDiagram;
35 import org.simantics.g2d.element.ElementClass;
36 import org.simantics.g2d.element.IElement;
37 import org.simantics.g2d.page.DiagramDesc;
38 import org.simantics.structural.stubs.StructuralResource2;
39 import org.simantics.structural2.modelingRules.CPConnection;
40 import org.simantics.structural2.modelingRules.CPTerminal;
41 import org.simantics.structural2.modelingRules.IConnectionPoint;
42 import org.simantics.structural2.modelingRules.IModelingRules;
43 import org.simantics.utils.datastructures.hints.IHintObservable;
44 import org.simantics.utils.page.PageDesc;
47 * @author Tuukka Lehtonen
49 public class DiagramRequests {
55 public static Read<PageDesc> getPageDesc(final Resource diagram, final PageDesc defaultValue) {
56 return new Read<PageDesc>() {
58 public PageDesc perform(ReadGraph graph) throws DatabaseException {
59 return DiagramGraphUtil.getPageDesc(graph, diagram, defaultValue);
64 public static Read<DiagramDesc> getDiagramDesc(final Resource diagram) {
65 return new DiagramDescRead(diagram);
69 * Adapt a diagram Element type resource into an {@link ElementClass}.
75 public static AsyncRead<ElementClass> getElementClass(Resource elementType, IHintObservable hints) {
76 return new ElementClassQuery(elementType, hints);
86 public static AsyncRead<IElement> getElement(ICanvasContext canvas, IDiagram diagram, Resource element, Listener<IElement> loadListener) {
87 return new NodeRequest(canvas, diagram, element, loadListener);
93 * @param structuralPath
98 public static Read<IDiagram> loadDiagram(IProgressMonitor monitor, Resource model, Resource resource, Resource runtime, ResourceArray structuralPath,
99 IDiagramLoader loader, IHintObservable initialHints) {
100 return new DiagramLoadQuery(monitor, model, resource, runtime, structuralPath, loader, initialHints);
107 public static Read<IModelingRules> getModelingRules(final Resource diagram, final IModelingRules defaultValue) {
108 return new Read<IModelingRules>() {
110 public IModelingRules perform(ReadGraph graph) throws DatabaseException {
111 StructuralResource2 sr = StructuralResource2.getInstance(graph);
112 Resource rules = graph.getPossibleObject(diagram, sr.HasModelingRules);
115 return graph.adapt(rules, IModelingRules.class);
121 * @param connectionType
124 public static Read<ConnectionVisuals> getConnectionVisuals(Resource connectionType) {
125 return new ConnectionVisualsRequest(connectionType);
132 public static Read<String[]> getFlagText(Resource flag) {
133 return new FlagTextQuery(flag);
136 public static Read<Resource> getFlagPair(Resource flag) {
137 return new FlagPairQuery(flag);
144 public static Read<TerminalMap> elementTypeTerminals(Resource elementType) {
145 return new ResourceRead<TerminalMap>(elementType) {
147 public TerminalMap perform(ReadGraph graph) throws DatabaseException {
148 return DiagramGraphUtil.getElementTypeTerminals(graph, resource);
157 public static Read<TerminalMap> elementTerminals(Resource element) {
158 return new ResourceRead<TerminalMap>(element) {
160 public TerminalMap perform(ReadGraph graph) throws DatabaseException {
161 return DiagramGraphUtil.getElementTerminals(graph, resource);
167 * Browses all diagram connections that are related to {@code connection} by flags.
168 * Adds the connections to set {@code conns} and all related connection points to set {@code cps}.
170 public static void expandConnections(ReadGraph graph, Resource connection, Set<Resource> conns, Set<IConnectionPoint> cps) throws DatabaseException {
172 if(!conns.add(connection)) return;
174 DiagramResource DIA = DiagramResource.getInstance(graph);
175 StructuralResource2 STR = StructuralResource2.getInstance(graph);
177 cps.add(new CPConnection(connection));
178 for(Resource connector : graph.getObjects(connection, DIA.HasConnector)) {
179 for(Statement stm : graph.getStatements(connector, STR.Connects)) {
180 Resource element = stm.getObject();
181 if(element.equals(connection)) continue;
182 if(graph.isInstanceOf(element, DIA.Flag)) {
183 FlagUtil.forCounterparts(graph, element, (otherFlag) -> {
184 Resource flagConnector = graph.getPossibleObject(otherFlag, DIA.Flag_ConnectionPoint);
185 if(flagConnector != null) {
186 Resource otherConnection = graph.getPossibleObject(flagConnector, DIA.IsConnectorOf);
187 if(otherConnection != null) {
188 expandConnections(graph, otherConnection, conns, cps);
193 Resource connectionPoint = graph.getInverse(stm.getPredicate());
194 //System.err.println("term: " + NameUtils.getSafeName(graph, element) + " : " + NameUtils.getSafeName(graph, connectionPoint));
195 cps.add(new CPTerminal(element, connectionPoint));