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