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;
16 import gnu.trove.map.hash.THashMap;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.common.utils.NameUtils;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.objmap.exceptions.MappingException;
28 public class DefaultSchema implements IMappingSchema<Resource,Object> {
30 THashMap<Resource, ILinkType<Resource,Object>> domainLinkTypes =
31 new THashMap<Resource, ILinkType<Resource,Object>>();
32 THashMap<Class<?>, ILinkType<Resource,Object>> rangeLinkTypes =
33 new THashMap<Class<?>, ILinkType<Resource,Object>>();
35 public void addLinkType(SimpleLinkType<Object> linkType) {
36 domainLinkTypes.put(linkType.domainType, linkType);
37 rangeLinkTypes.put(linkType.rangeType, linkType);
40 public void addLinkType(AdaptedLinkType<Object> linkType) {
41 domainLinkTypes.put(linkType.domainType, linkType);
42 rangeLinkTypes.put(linkType.rangeType, linkType);
46 public ILinkType<Resource,Object> linkTypeOfDomainElement(ReadGraph g, Resource element) throws MappingException {
49 for(Resource type : g.getTypes(element)) {
51 ILinkType<Resource,Object> linkType = domainLinkTypes.get(type);
52 if(linkType != null) return linkType;
56 throw new MappingException("Didn't find a link type for " +
57 NameUtils.getSafeName(g, element) + ".");
59 } catch (DatabaseException e) {
60 throw new MappingException(e);
65 public ILinkType<Resource,Object> linkTypeOfRangeElement(Object element) throws MappingException {
66 ILinkType<Resource,Object> type = rangeLinkTypes.get(element.getClass());
68 for (Class<?> clazz : element.getClass().getInterfaces()) {
69 type = rangeLinkTypes.get(clazz);
74 throw new MappingException("Didn't find a link type for " + element + ".");
80 public ILinkType<Resource,Object> linkTypeOfDomainType(ReadGraph g, Resource type) {
81 return domainLinkTypes.get(type);
84 public ILinkType<Resource,Object> linkTypeOfRangeType(Class<?> clazz) {
85 ILinkType<Resource,Object> type = rangeLinkTypes.get(clazz);
87 // FIXME: c is not referenced at all, should it be?
88 // TODO: should this method take inheritance into account ?
89 for (Class<?> c : clazz.getInterfaces()) {
90 type = rangeLinkTypes.get(clazz);