1 /*******************************************************************************
2 * Copyright (c) 2007, 2014 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
12 *******************************************************************************/
13 package org.simantics.modeling.rules;
15 import java.util.Collection;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.modeling.ModelingResources;
22 import org.simantics.structural2.modelingRules.CPConnection;
23 import org.simantics.structural2.modelingRules.CPConnectionJoin;
24 import org.simantics.structural2.modelingRules.CPTerminal;
25 import org.simantics.structural2.modelingRules.IConnectionPoint;
27 public class Mapping implements IMapping {
29 Resource componentMap;
30 Resource connectionMap;
31 Resource mapsToConnection;
32 Resource invConnectionMap;
33 Resource componentTypeMap;
34 Resource connectionRelationMap;
36 public Mapping(ReadGraph g) throws DatabaseException {
37 initializeRelations(g);
40 protected void initializeRelations(ReadGraph g) throws DatabaseException {
41 componentMap = g.getResource(ModelingResources.URIs.ElementToComponent);
42 connectionMap = g.getResource(ModelingResources.URIs.DiagramConnectionToConnection);
43 mapsToConnection = g.getResource(ModelingResources.URIs.MapsToConnection);
44 invConnectionMap = g.getResource(ModelingResources.URIs.ConnectionToDiagramConnection);
45 componentTypeMap = g.getResource(ModelingResources.URIs.SymbolToComponentType);
46 connectionRelationMap = g.getResource(ModelingResources.URIs.DiagramConnectionRelationToConnectionRelation);
50 public Resource mapComponentType(ReadGraph g, Resource componentType)
51 throws DatabaseException {
52 return g.getPossibleObject(componentType, componentTypeMap);
55 public Collection<Resource> mapToConnections(ReadGraph g, Resource connection)
56 throws DatabaseException {
57 return g.getObjects(connection, mapsToConnection);
61 public Resource mapConnection(ReadGraph g, Resource connection)
62 throws DatabaseException {
63 return g.getPossibleObject(connection, connectionMap);
67 public Resource invMapConnection(WriteGraph g, Resource connection)
68 throws DatabaseException {
69 return g.getPossibleObject(connection, invConnectionMap);
73 public Resource mapConnectionRelation(ReadGraph g, Resource relation)
74 throws DatabaseException {
75 return g.getPossibleObject(relation, connectionRelationMap);
79 public Resource mapComponent(ReadGraph g, Resource component)
80 throws DatabaseException {
81 return g.getPossibleObject(component, componentMap);
85 public IConnectionPoint mapConnectionPoint(ReadGraph g,
86 IConnectionPoint cp) throws DatabaseException {
87 if(cp instanceof CPConnection) {
88 CPConnection ccp = (CPConnection)cp;
89 Resource connection = mapConnection(g, ccp.connection);
90 if (connection == null)
92 return new CPConnection(connection);
94 if(cp instanceof CPTerminal)
95 return mapCPTerminal(g, (CPTerminal)cp);
96 if(cp instanceof CPConnectionJoin)
102 public int mapToConnectionPoints(ReadGraph g, IConnectionPoint cp,
103 Collection<IConnectionPoint> result) throws DatabaseException {
104 if(cp instanceof CPConnection) {
105 CPConnection ccp = (CPConnection)cp;
106 Collection<Resource> connections = mapToConnections(g, ccp.connection);
107 int size = connections.size();
109 for (Resource connection : connections)
110 result.add(new CPConnection(connection));
113 if(cp instanceof CPTerminal) {
114 IConnectionPoint mcp = mapCPTerminal(g, (CPTerminal) cp);
117 return mcp != null ? 1 : 0;
119 if(cp instanceof CPConnectionJoin) {
126 protected IConnectionPoint mapCPTerminal(ReadGraph g, CPTerminal ccp) throws DatabaseException {
127 if(ccp.component == null)
129 Resource component = mapComponent(g, ccp.component);
130 if(component == null)
132 Resource connectionRelation = mapConnectionRelation(g, ccp.relation);
133 if(connectionRelation == null)
135 return new CPTerminal(