1 /*******************************************************************************
\r
2 * Copyright (c) 2007- VTT Technical Research Centre of Finland.
\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
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.processeditor.common;
\r
13 import java.util.ArrayList;
\r
14 import java.util.Collection;
\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
28 public class PathUtils {
\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
36 // samplePath.add(path.length,tNode.getCoreId());
\r
37 // tNode = oe.getTreeParent(tNode);
\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
46 // there is no relation and so this has to be root
\r
49 samplePath.add(1,obj);
\r
53 if (!sampleInstance.equals(samplePath.get(0)) || !sampleSource.equals(samplePath.get(samplePath.size()-1))) {
\r
55 for (int i = 0; i < samplePath.size(); i++) {
\r
56 s += samplePath.get(i) + " ";
\r
58 throw new RuntimeException("Path from " + sampleInstance + " to "
\r
59 + sampleSource + " is broken: " + s);
\r
63 // for (int i = 0; i < samplePath.size(); i++) {
\r
64 // s += samplePath.get(i) + " ";
\r
66 // System.out.println("Path from " + sampleInstance + " to " + sampleSource + " is: " + s);
\r
67 samplePath.remove(0);
\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
83 public static IEntity findSimilar(java.util.List<Resource> path, IEntity begin) {
\r
84 if (path.size() == 0)
\r
86 if (path.size() == 1)
\r
88 Graph g = begin.getGraph();
\r
89 java.util.List<Resource> tPath = new ArrayList<Resource>();
\r
92 Resource p = tPath.get(0); // predicate (relation)
\r
93 Resource o = tPath.get(1); // object
\r
97 IEntity predicate = EntityFactory.create(g, p);
\r
100 Collection<IEntity> possibleObjects = begin.getRelatedObjects(predicate);
\r
101 if (possibleObjects.size() == 0)
\r
103 if (possibleObjects.size() == 1)
\r
104 return findSimilar(tPath, possibleObjects.iterator().next());
\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
119 IEntity r = findSimilar(tPath,possible);
\r
121 list.add(new Pair<IEntity,IEntity>(possible,r));
\r
125 if (list.size() == 0)
\r
127 if (list.size() == 1)
\r
128 return list.get(0).second;
\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