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.structural.schema;
16 import java.util.Stack;
18 import gnu.trove.map.hash.THashMap;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.common.utils.NameUtils;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.objmap.exceptions.MappingException;
25 import org.simantics.objmap.graph.schema.ILinkType;
26 import org.simantics.objmap.graph.schema.IMappingSchema;
27 import org.simantics.objmap.structural.IStructuralObject;
28 import org.simantics.objmap.structural.StructuralResource;
34 public class DefaultSchema implements IMappingSchema<StructuralResource, IStructuralObject> {
36 THashMap<Resource, ILinkType<StructuralResource, IStructuralObject>> domainLinkTypes =
37 new THashMap<Resource, ILinkType<StructuralResource, IStructuralObject>>();
38 THashMap<Class<?>, ILinkType<StructuralResource, IStructuralObject>> rangeLinkTypes =
39 new THashMap<Class<?>, ILinkType<StructuralResource, IStructuralObject>>();
41 public void addLinkType(SimpleLinkType linkType) {
42 domainLinkTypes.put(linkType.domainType, linkType);
43 rangeLinkTypes.put(linkType.rangeType, linkType);
46 public void addLinkType(AdaptedLinkType linkType) {
47 domainLinkTypes.put(linkType.domainType, linkType);
48 rangeLinkTypes.put(linkType.rangeType, linkType);
52 public ILinkType<StructuralResource, IStructuralObject> linkTypeOfDomainElement(ReadGraph g, StructuralResource element) throws MappingException {
55 for(Resource type : g.getTypes(element.getResource())) {
57 ILinkType<StructuralResource, IStructuralObject> linkType = domainLinkTypes.get(type);
58 if(linkType != null) return linkType;
62 throw new MappingException("Didn't find a link type for " +
63 NameUtils.getSafeName(g, element.getResource()) + ".");
65 } catch (DatabaseException e) {
66 throw new MappingException(e);
71 public ILinkType<StructuralResource, IStructuralObject> linkTypeOfRangeElement(IStructuralObject element) throws MappingException {
72 ILinkType<StructuralResource, IStructuralObject> type = rangeLinkTypes.get(element.getClass());
74 Stack<Class<?>> clazzes = new Stack<Class<?>>();
75 for (Class<?> clazz : element.getClass().getInterfaces()) {
78 clazzes.add(element.getClass().getSuperclass());
80 while (!clazzes.isEmpty()) {
81 Class<?> clazz = clazzes.pop();
83 type = rangeLinkTypes.get(clazz);
86 for (Class<?> c : clazz.getInterfaces())
90 throw new MappingException("Didn't find a link type for " + element + ".");
96 public ILinkType<StructuralResource, IStructuralObject> linkTypeOfDomainType(ReadGraph g, Resource type) {
97 return domainLinkTypes.get(type);
100 public ILinkType<StructuralResource, IStructuralObject> linkTypeOfRangeType(Class<?> clazz) {
101 ILinkType<StructuralResource, IStructuralObject> type = rangeLinkTypes.get(clazz);
103 Stack<Class<?>> clazzes = new Stack<Class<?>>();
104 for (Class<?> c : clazz.getInterfaces()) {
107 clazzes.add(clazz.getSuperclass());
109 while (!clazzes.isEmpty()) {
110 Class<?> c = clazzes.pop();
112 type = rangeLinkTypes.get(c);
115 for (Class<?> c2 : c.getInterfaces())