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;
14 import java.util.ArrayList;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.common.utils.NameUtils;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.objmap.backward.IBackwardMapping;
25 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
26 import org.simantics.objmap.exceptions.MappingException;
27 import org.simantics.objmap.forward.IForwardMapping;
31 * A link type that is associated with single domain and range type (class).
32 * SimpleLinkType is composed of simpler rules whose combination determines
34 * @author Hannu Niemist�
36 public class SimpleLinkType<Range> implements ILinkType<Resource,Range> {
38 static Logger LOGGER = LoggerFactory.getLogger(SimpleLinkType.class);
40 public Resource domainType;
41 public Class<?> rangeType;
42 ArrayList<IBidirectionalMappingRule<Resource, Range>> rules;
44 public SimpleLinkType(Resource domainType, Class<?> rangeType,
45 ArrayList<IBidirectionalMappingRule<Resource, Range>> rules) {
46 this.domainType = domainType;
47 this.rangeType = rangeType;
51 public SimpleLinkType(Resource domainType, Class<?> rangeType) {
52 this(domainType, rangeType, new ArrayList<IBidirectionalMappingRule<Resource, Range>>());
56 * Adds a new rule to this link type that is enforced
59 public void addRule(IBidirectionalMappingRule<Resource, Range> rule) {
64 public Resource createDomainElement(WriteGraph g, Range rangeElement)
65 throws MappingException {
67 if(LOGGER.isTraceEnabled())
68 LOGGER.trace("SimpleLinkType.createDomainElement " +
69 rangeElement.toString()
71 Resource result = g.newResource();
72 g.claim(result, Layer0.getInstance(g).InstanceOf, null, domainType);
74 } catch(DatabaseException e) {
75 throw new MappingException(e);
78 @SuppressWarnings("unchecked")
80 public Range createRangeElement(ReadGraph g, Resource domainElement)
81 throws MappingException {
83 if(LOGGER.isTraceEnabled())
85 LOGGER.trace("SimpleLinkType.createRangeElement " +
86 NameUtils.getSafeName(g, domainElement)
88 } catch(DatabaseException e) {
89 throw new MappingException(e);
91 return (Range)rangeType.newInstance();
92 } catch (InstantiationException e) {
93 throw new MappingException(e);
94 } catch (IllegalAccessException e) {
95 throw new MappingException(e);
99 public void createDomain(WriteGraph graph, IBackwardMapping<Resource,Range> mapping, Resource domainElement, Range rangeElement) throws MappingException {
100 updateDomain(graph, mapping, domainElement, rangeElement);
103 public void createRange(ReadGraph graph, org.simantics.objmap.forward.IForwardMapping<Resource,Range> mapping, Resource domainElement, Range rangeElement) throws MappingException {
104 updateRange(graph, mapping, domainElement, rangeElement);
108 public boolean checkChanges(ReadGraph g, IForwardMapping<Resource, Range> map, Resource domainElement,
109 Range rangeElement) throws MappingException {
110 boolean updated = false;
111 for(IBidirectionalMappingRule<Resource, Range> rule : rules)
112 updated |= rule.checkChanges(g, map, domainElement, rangeElement);
116 public boolean updateDomain(WriteGraph g, IBackwardMapping<Resource,Range> map, Resource domainElement, Range rangeElement) throws MappingException {
117 if(LOGGER.isTraceEnabled())
119 LOGGER.trace("SimpleLinkType.updateDomain " +
120 NameUtils.getSafeName(g, domainElement) + " " +
121 rangeElement.toString()
123 } catch(DatabaseException e) {
124 throw new MappingException(e);
127 boolean updated = false;
128 for(IBidirectionalMappingRule<Resource, Range> rule : rules)
129 updated |= rule.updateDomain(g, map, domainElement, rangeElement);
133 public boolean updateRange(ReadGraph g, IForwardMapping<Resource,Range> map, Resource domainElement, Range rangeElement) throws MappingException {
135 if(LOGGER.isTraceEnabled())
137 LOGGER.trace("SimpleLinkType.updateRange " +
138 NameUtils.getSafeName(g, domainElement) + " " +
139 (rangeElement.getClass().getName() + "@" + Integer.toHexString(rangeElement.hashCode()))
141 } catch(DatabaseException e) {
142 throw new MappingException(e);
145 boolean updated = false;
146 for(IBidirectionalMappingRule<Resource, Range> rule : rules)
147 updated |= rule.updateRange(g, map, domainElement, rangeElement);