]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/DefinedUCInterfaceMap.java
Replaceable Defined Component Types
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / DefinedUCInterfaceMap.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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  *******************************************************************************/
12 package org.simantics.structural2;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16
17 import org.simantics.databoard.Bindings;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.Statement;
21 import org.simantics.db.common.request.ObjectsWithType;
22 import org.simantics.db.common.request.ResourceRead;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.layer0.Layer0;
25 import org.simantics.structural.stubs.StructuralResource2;
26 import org.simantics.structural2.Functions.InterfaceResolution;
27
28 public class DefinedUCInterfaceMap extends ResourceRead<Collection<InterfaceResolution>> {
29
30     public DefinedUCInterfaceMap(Resource resource) {
31         super(resource);
32     }
33
34     @Override
35     public Collection<InterfaceResolution> perform(ReadGraph graph)
36             throws DatabaseException {
37
38         StructuralResource2 STR = StructuralResource2.getInstance(graph);
39         Resource definition = graph.getPossibleObject(resource, STR.IsDefinedBy);
40         if(definition != null) {
41             Collection<InterfaceResolution> result = new ArrayList<>();
42             Layer0 L0 = Layer0.getInstance(graph);
43             for(Resource cp : graph.syncRequest(new ObjectsWithType(resource, L0.ConsistsOf, STR.ConnectionRelation))) {
44                 String cpName = graph.getRelatedValue(cp, L0.HasName, Bindings.STRING);
45                 for(Resource conn : graph.getObjects(cp, STR.IsBoundBy)) {
46                     Statement stm = graph.getPossibleStatement(conn, STR.Connects);
47                     if(stm == null) continue;
48                     Resource component = stm.getObject();
49                     String componentName = graph.getRelatedValue(component, L0.HasName, Bindings.STRING);
50                     result.add(new InterfaceResolution(cp, cpName, componentName, graph.getInverse(stm.getPredicate())));
51                 }
52             }
53             return result;
54         }
55         return null;
56     }
57
58 }