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