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.isInfoEnabled())
68 LOGGER.info("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.isInfoEnabled())
85 LOGGER.info("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);
107 public boolean updateDomain(WriteGraph g, IBackwardMapping<Resource,Range> map, Resource domainElement, Range rangeElement) throws MappingException {
108 if(LOGGER.isInfoEnabled())
110 LOGGER.info("SimpleLinkType.updateDomain " +
111 NameUtils.getSafeName(g, domainElement) + " " +
112 rangeElement.toString()
114 } catch(DatabaseException e) {
115 throw new MappingException(e);
118 boolean updated = false;
119 for(IBidirectionalMappingRule<Resource, Range> rule : rules)
120 updated |= rule.updateDomain(g, map, domainElement, rangeElement);
124 public boolean updateRange(ReadGraph g, IForwardMapping<Resource,Range> map, Resource domainElement, Range rangeElement) throws MappingException {
126 if(LOGGER.isInfoEnabled())
128 LOGGER.info("SimpleLinkType.updateRange " +
129 NameUtils.getSafeName(g, domainElement) + " " +
130 rangeElement.toString()
132 } catch(DatabaseException e) {
133 throw new MappingException(e);
136 boolean updated = false;
137 for(IBidirectionalMappingRule<Resource, Range> rule : rules)
138 updated |= rule.updateRange(g, map, domainElement, rangeElement);