]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.proconf.processeditor/src/org/simantics/processeditor/common/PathUtils.java
8a941c5f238872d180e8e71547992d8d36466dd9
[simantics/3d.git] / org.simantics.proconf.processeditor / src / org / simantics / processeditor / common / PathUtils.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.processeditor.common;\r
12 \r
13 import java.util.ArrayList;\r
14 import java.util.Collection;\r
15 \r
16 import org.simantics.db.Graph;\r
17 import org.simantics.db.Resource;\r
18 import org.simantics.layer0.utils.EntityFactory;\r
19 import org.simantics.layer0.utils.IEntity;\r
20 import org.simantics.layer0.utils.viewpoints.TraversalPath;\r
21 import org.simantics.proconf.browsing.GraphExplorer;\r
22 import org.simantics.utils.datastructures.Pair;\r
23 \r
24 \r
25 \r
26 \r
27 \r
28 public class PathUtils {\r
29 \r
30         public static void createPath(java.util.List<Resource> samplePath, TraversalPath path, Resource sampleInstance, Resource sampleSource, GraphExplorer oe) {\r
31 //              while (tNode != null) {\r
32 //                      long[] path = tNode.getPath();\r
33 //                      for (int i = 0; i < path.length; i++) {\r
34 //                              samplePath.add(i, path[i]);\r
35 //                      }\r
36 //                      samplePath.add(path.length,tNode.getCoreId());\r
37 //                      tNode = oe.getTreeParent(tNode);\r
38 //              }\r
39                 while (path != null) {\r
40                         Resource predicate = path.getPredicate();\r
41                         Resource obj = path.getResource();\r
42                         if (predicate != null) {\r
43                                 samplePath.add(0,predicate);\r
44                                 path = path.getTail();\r
45                         } else {\r
46                                 // there is no relation and so this has to be root\r
47                                 path = null; \r
48                         }\r
49                         samplePath.add(1,obj);\r
50                         \r
51                 }\r
52                 \r
53                 if (!sampleInstance.equals(samplePath.get(0)) || !sampleSource.equals(samplePath.get(samplePath.size()-1))) {\r
54                         String s = "";\r
55                         for (int i = 0; i < samplePath.size(); i++) {\r
56                                 s += samplePath.get(i) + " ";\r
57                         }\r
58                         throw new RuntimeException("Path from " + sampleInstance + " to "\r
59                                         + sampleSource + " is broken: " + s);\r
60                 }\r
61 \r
62 //              String s = "";\r
63 //              for (int i = 0; i < samplePath.size(); i++) {\r
64 //                      s += samplePath.get(i) + " ";\r
65 //              }\r
66 //              System.out.println("Path from " + sampleInstance + " to " + sampleSource + " is: " + s);\r
67                 samplePath.remove(0);\r
68         }\r
69         \r
70         \r
71         \r
72         /**\r
73          * Finds similar path in cloned resource\r
74          * TODO : this isn't correct way to do this;\r
75          * Rigth way would be finding mapping between two clones\r
76          * and then find the similar resource \r
77          * (Viewpoint used to create clone is required)\r
78          * \r
79          * @param path\r
80          * @param begin\r
81          * @return\r
82          */\r
83         public static IEntity findSimilar(java.util.List<Resource> path,  IEntity begin) {\r
84                 if (path.size() == 0)\r
85                         return begin;\r
86                 if (path.size() == 1)\r
87                         return null;\r
88                 Graph g = begin.getGraph();\r
89                 java.util.List<Resource> tPath = new ArrayList<Resource>();\r
90                 tPath.addAll(path);\r
91                 \r
92                 Resource p = tPath.get(0); // predicate (relation)\r
93                 Resource o = tPath.get(1); // object \r
94                 tPath.remove(0);\r
95                 tPath.remove(0);\r
96                 \r
97                 IEntity predicate = EntityFactory.create(g, p);\r
98 \r
99                 \r
100                 Collection<IEntity> possibleObjects = begin.getRelatedObjects(predicate);\r
101                 if (possibleObjects.size() == 0)\r
102                         return null;\r
103                 if (possibleObjects.size() == 1) \r
104                         return findSimilar(tPath, possibleObjects.iterator().next());\r
105                 else {\r
106                         IEntity object = EntityFactory.create(g, o);\r
107                         Collection<IEntity> objectTypes = object.getTypes();\r
108                         java.util.List<Pair<IEntity,IEntity>> list = new ArrayList<Pair<IEntity,IEntity>>();\r
109                         for (IEntity possible : possibleObjects) {\r
110                                 boolean matchTypes = true;\r
111                                 for (IEntity type : objectTypes) {\r
112                                         if(!possible.isInstanceOf(type)) {\r
113                                                 matchTypes = false;\r
114                                                 break;\r
115                                         }\r
116                                                 \r
117                                 }\r
118                                 if (matchTypes) {\r
119                                         IEntity r = findSimilar(tPath,possible);\r
120                                         if (r != null)\r
121                                                 list.add(new Pair<IEntity,IEntity>(possible,r));\r
122                                                 //return r;\r
123                                 }\r
124                         }\r
125                         if (list.size() == 0)\r
126                                 return null;\r
127                         if (list.size() == 1)\r
128                                 return list.get(0).second;\r
129                         else {\r
130                                 // uses names of objects to detect similarities\r
131                                 String name = object.getName();\r
132                                 if (name != null) {\r
133                                         for (Pair<IEntity,IEntity> possible : list) {\r
134                                                 String otherName = possible.first.getName();\r
135                                                 //System.out.println(name + " : " + otherName);\r
136                                                 if (otherName != null && name.compareTo(otherName) == 0)\r
137                                                         return possible.second;\r
138                                         }\r
139                                 }\r
140                         }\r
141                 }\r
142                 \r
143                 return null;\r
144         }\r
145 }\r