1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2013 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.objmap.graph.schema;
\r
15 import java.util.Stack;
\r
17 import gnu.trove.map.hash.THashMap;
\r
19 import org.simantics.db.ReadGraph;
\r
20 import org.simantics.db.Resource;
\r
21 import org.simantics.db.common.utils.NameUtils;
\r
22 import org.simantics.db.exception.DatabaseException;
\r
23 import org.simantics.objmap.exceptions.MappingException;
\r
27 * An implementation of IMappingSchema that contains
\r
28 * only SimpleLinkTypes. The link type of any domain
\r
29 * element is based solely on its type in database and
\r
30 * the link type of any range element is based on its class.
\r
31 * @author Hannu Niemistö
\r
33 public class SimpleSchema implements IMappingSchema<Resource,Object> {
\r
35 THashMap<Resource, ILinkType<Resource,Object>> domainLinkTypes =
\r
36 new THashMap<Resource, ILinkType<Resource,Object>>();
\r
37 THashMap<Class<?>, ILinkType<Resource,Object>> rangeLinkTypes =
\r
38 new THashMap<Class<?>, ILinkType<Resource,Object>>();
\r
40 public void addLinkType(SimpleLinkType<Object> linkType) {
\r
41 domainLinkTypes.put(linkType.domainType, linkType);
\r
42 rangeLinkTypes.put(linkType.rangeType, linkType);
\r
45 public void addLinkType(AdaptedLinkType<Object> linkType) {
\r
46 domainLinkTypes.put(linkType.domainType, linkType);
\r
47 rangeLinkTypes.put(linkType.rangeType, linkType);
\r
51 public ILinkType<Resource,Object> linkTypeOfDomainElement(ReadGraph g, Resource element) throws MappingException {
\r
54 for(Resource type : g.getTypes(element)) {
\r
56 ILinkType<Resource,Object> linkType = domainLinkTypes.get(type);
\r
57 if(linkType != null) return linkType;
\r
61 throw new MappingException("Didn't find a link type for " + NameUtils.getSafeName(g, element) + ".");
\r
63 } catch (DatabaseException e) {
\r
64 throw new MappingException(e);
\r
69 public ILinkType<Resource,Object> linkTypeOfRangeElement(Object element) throws MappingException {
\r
70 ILinkType<Resource,Object> type = rangeLinkTypes.get(element.getClass());
\r
72 Stack<Class<?>> clazzes = new Stack<Class<?>>();
\r
73 for (Class<?> clazz : element.getClass().getInterfaces()) {
\r
76 clazzes.add(element.getClass().getSuperclass());
\r
79 while (!clazzes.isEmpty()) {
\r
80 Class<?> clazz = clazzes.pop();
\r
82 type = rangeLinkTypes.get(clazz);
\r
85 for (Class<?> c : clazz.getInterfaces())
\r
89 throw new MappingException("Didn't find a link type for " + element.getClass() + ".");
\r