+++ /dev/null
-/*******************************************************************************\r
- * Copyright (c) 2007- VTT Technical Research Centre of Finland.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.processeditor.common;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-\r
-import org.simantics.db.Graph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.layer0.utils.EntityFactory;\r
-import org.simantics.layer0.utils.IEntity;\r
-import org.simantics.layer0.utils.viewpoints.TraversalPath;\r
-import org.simantics.proconf.browsing.GraphExplorer;\r
-import org.simantics.utils.datastructures.Pair;\r
-\r
-\r
-\r
-\r
-\r
-public class PathUtils {\r
-\r
- public static void createPath(java.util.List<Resource> samplePath, TraversalPath path, Resource sampleInstance, Resource sampleSource, GraphExplorer oe) {\r
-// while (tNode != null) {\r
-// long[] path = tNode.getPath();\r
-// for (int i = 0; i < path.length; i++) {\r
-// samplePath.add(i, path[i]);\r
-// }\r
-// samplePath.add(path.length,tNode.getCoreId());\r
-// tNode = oe.getTreeParent(tNode);\r
-// }\r
- while (path != null) {\r
- Resource predicate = path.getPredicate();\r
- Resource obj = path.getResource();\r
- if (predicate != null) {\r
- samplePath.add(0,predicate);\r
- path = path.getTail();\r
- } else {\r
- // there is no relation and so this has to be root\r
- path = null; \r
- }\r
- samplePath.add(1,obj);\r
- \r
- }\r
- \r
- if (!sampleInstance.equals(samplePath.get(0)) || !sampleSource.equals(samplePath.get(samplePath.size()-1))) {\r
- String s = "";\r
- for (int i = 0; i < samplePath.size(); i++) {\r
- s += samplePath.get(i) + " ";\r
- }\r
- throw new RuntimeException("Path from " + sampleInstance + " to "\r
- + sampleSource + " is broken: " + s);\r
- }\r
-\r
-// String s = "";\r
-// for (int i = 0; i < samplePath.size(); i++) {\r
-// s += samplePath.get(i) + " ";\r
-// }\r
-// System.out.println("Path from " + sampleInstance + " to " + sampleSource + " is: " + s);\r
- samplePath.remove(0);\r
- }\r
- \r
- \r
- \r
- /**\r
- * Finds similar path in cloned resource\r
- * TODO : this isn't correct way to do this;\r
- * Rigth way would be finding mapping between two clones\r
- * and then find the similar resource \r
- * (Viewpoint used to create clone is required)\r
- * \r
- * @param path\r
- * @param begin\r
- * @return\r
- */\r
- public static IEntity findSimilar(java.util.List<Resource> path, IEntity begin) {\r
- if (path.size() == 0)\r
- return begin;\r
- if (path.size() == 1)\r
- return null;\r
- Graph g = begin.getGraph();\r
- java.util.List<Resource> tPath = new ArrayList<Resource>();\r
- tPath.addAll(path);\r
- \r
- Resource p = tPath.get(0); // predicate (relation)\r
- Resource o = tPath.get(1); // object \r
- tPath.remove(0);\r
- tPath.remove(0);\r
- \r
- IEntity predicate = EntityFactory.create(g, p);\r
-\r
- \r
- Collection<IEntity> possibleObjects = begin.getRelatedObjects(predicate);\r
- if (possibleObjects.size() == 0)\r
- return null;\r
- if (possibleObjects.size() == 1) \r
- return findSimilar(tPath, possibleObjects.iterator().next());\r
- else {\r
- IEntity object = EntityFactory.create(g, o);\r
- Collection<IEntity> objectTypes = object.getTypes();\r
- java.util.List<Pair<IEntity,IEntity>> list = new ArrayList<Pair<IEntity,IEntity>>();\r
- for (IEntity possible : possibleObjects) {\r
- boolean matchTypes = true;\r
- for (IEntity type : objectTypes) {\r
- if(!possible.isInstanceOf(type)) {\r
- matchTypes = false;\r
- break;\r
- }\r
- \r
- }\r
- if (matchTypes) {\r
- IEntity r = findSimilar(tPath,possible);\r
- if (r != null)\r
- list.add(new Pair<IEntity,IEntity>(possible,r));\r
- //return r;\r
- }\r
- }\r
- if (list.size() == 0)\r
- return null;\r
- if (list.size() == 1)\r
- return list.get(0).second;\r
- else {\r
- // uses names of objects to detect similarities\r
- String name = object.getName();\r
- if (name != null) {\r
- for (Pair<IEntity,IEntity> possible : list) {\r
- String otherName = possible.first.getName();\r
- //System.out.println(name + " : " + otherName);\r
- if (otherName != null && name.compareTo(otherName) == 0)\r
- return possible.second;\r
- }\r
- }\r
- }\r
- }\r
- \r
- return null;\r
- }\r
-}\r