]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/rules/Mapping.java
Remove version identifier from the proposed name
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / rules / Mapping.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2014 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *     Semantum Oy - #5290
12  *******************************************************************************/
13 package org.simantics.modeling.rules;
14
15 import java.util.Collection;
16
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;
26
27 public class Mapping implements IMapping {
28
29         Resource componentMap;
30         Resource connectionMap;
31         Resource mapsToConnection;
32         Resource invConnectionMap;
33         Resource componentTypeMap;
34         Resource connectionRelationMap;
35                 
36         public Mapping(ReadGraph g) throws DatabaseException {
37                 initializeRelations(g);
38         }
39
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);
47         }
48
49         @Override
50         public Resource mapComponentType(ReadGraph g, Resource componentType)
51                         throws DatabaseException {
52                 return g.getPossibleObject(componentType, componentTypeMap);
53         }
54
55         public Collection<Resource> mapToConnections(ReadGraph g, Resource connection)
56                         throws DatabaseException {
57                 return g.getObjects(connection, mapsToConnection);
58         }
59
60         @Override
61         public Resource mapConnection(ReadGraph g, Resource connection)
62                         throws DatabaseException {
63                 return g.getPossibleObject(connection, connectionMap);
64         }
65         
66         @Override
67         public Resource invMapConnection(WriteGraph g, Resource connection) 
68                         throws DatabaseException {
69                 return g.getPossibleObject(connection, invConnectionMap);
70         }
71         
72         @Override
73         public Resource mapConnectionRelation(ReadGraph g, Resource relation)
74                         throws DatabaseException {
75                 return g.getPossibleObject(relation, connectionRelationMap);
76         }
77
78         @Override
79         public Resource mapComponent(ReadGraph g, Resource component)
80                 throws DatabaseException {
81                 return g.getPossibleObject(component, componentMap);
82         }
83         
84         @Override
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)
91                                 return null;
92                         return new CPConnection(connection);
93                 }
94                 if(cp instanceof CPTerminal)
95                         return mapCPTerminal(g, (CPTerminal)cp);
96                 if(cp instanceof CPConnectionJoin)
97                         return cp;
98                 return null;
99         }       
100
101         @Override
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();
108                         if (size > 0)
109                                 for (Resource connection : connections)
110                                         result.add(new CPConnection(connection));
111                         return size;
112                 }
113                 if(cp instanceof CPTerminal) {
114                         IConnectionPoint mcp = mapCPTerminal(g, (CPTerminal) cp);
115                         if (mcp != null)
116                                 result.add(mcp);
117                         return mcp != null ? 1 : 0;
118                 }
119                 if(cp instanceof CPConnectionJoin) {
120                         result.add(cp);
121                         return 1;
122                 }
123                 return 0;
124         }
125
126         protected IConnectionPoint mapCPTerminal(ReadGraph g, CPTerminal ccp) throws DatabaseException {
127             if(ccp.component == null)
128                 return null;
129                 Resource component = mapComponent(g, ccp.component);
130                 if(component == null)
131                         return null;
132                 Resource connectionRelation = mapConnectionRelation(g, ccp.relation);
133                 if(connectionRelation == null)
134                         return null;
135                 return new CPTerminal(
136                                 component,
137                                 connectionRelation
138                         );
139         }
140
141 }