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;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.common.utils.NameUtils;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.objmap.backward.IBackwardMapping;
27 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
28 import org.simantics.objmap.exceptions.MappingException;
29 import org.simantics.objmap.forward.IForwardMapping;
30 import org.simantics.objmap.graph.schema.ILinkType;
31 import org.simantics.objmap.structural.IStructuralObject;
32 import org.simantics.objmap.structural.StructuralResource;
36 public class SimpleLinkType implements ILinkType<StructuralResource,IStructuralObject> {
38 static Logger LOGGER = LoggerFactory.getLogger(SimpleLinkType.class);
40 public Resource domainType;
41 public Class<?> rangeType;
42 ArrayList<IBidirectionalMappingRule<StructuralResource,IStructuralObject>> rules;
44 public SimpleLinkType(Resource domainType, Class<?> rangeType,
45 ArrayList<IBidirectionalMappingRule<StructuralResource,IStructuralObject>> rules) {
46 this.domainType = domainType;
47 this.rangeType = rangeType;
51 public SimpleLinkType(Resource domainType, Class<?> rangeType) {
52 this(domainType, rangeType, new ArrayList<IBidirectionalMappingRule<StructuralResource,IStructuralObject>>());
56 * Adds a new rule to this link type that is enforced
59 public void addRule(IBidirectionalMappingRule<StructuralResource,IStructuralObject> rule) {
64 public StructuralResource createDomainElement(WriteGraph g, IStructuralObject rangeElement)
65 throws MappingException {
67 if(LOGGER.isInfoEnabled())
68 LOGGER.info("SimpleLinkType.createDomainElement " +
69 rangeElement.toString()
71 if (rangeElement.getContext().size() == 0) {
72 // there is no context, this not a structural resource / object.
73 Resource result = newResource(g, domainType);
74 return new StructuralResource(g,result);
76 if (rangeElement.getContext().size() == 1 && rangeElement.getContext().get(0).equals(rangeElement)) {
77 // Structural object's context is itself, we are instantiating a new structural model.
78 Resource type = rangeElement.getType();
79 Resource result = newResource(g, type);
80 return new StructuralResource(g,result,result);
82 // Structural object's context is not itself, which means that the object is inside of a structural model.
83 // At the moment we do not support modifying instantiated structural models.
84 throw new MappingException("Cannot create a new StucturalObject " + rangeElement + " " + rangeElement.getClass());
87 } catch(DatabaseException e) {
88 throw new MappingException(e);
92 protected Resource newResource(WriteGraph g, Resource type) throws DatabaseException {
93 Resource result = g.newResource();
94 g.claim(result, Layer0.getInstance(g).InstanceOf, null, type);
98 public IStructuralObject createRangeElement(ReadGraph g, StructuralResource domainElement)
99 throws MappingException {
101 if(LOGGER.isInfoEnabled())
103 LOGGER.info("SimpleLinkType.createRangeElement " + NameUtils.getSafeName(g, domainElement.getResource()));
104 } catch(DatabaseException e) {
105 throw new MappingException(e);
107 IStructuralObject result = (IStructuralObject)rangeType.newInstance();
108 if (domainElement.getContext().size() == 1) {
109 if (domainElement.getContext().get(0).equals(domainElement.getResource()))
110 result.setContext(Collections.singletonList(result));
112 //result.setContext(result);
116 } catch (InstantiationException e) {
117 throw new MappingException(e);
118 } catch (IllegalAccessException e) {
119 throw new MappingException(e);
123 @SuppressWarnings("unchecked")
124 public void createDomain(WriteGraph graph, IBackwardMapping<StructuralResource,IStructuralObject> mapping, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
125 if (domainElement.isStructuralRoot())
126 // FIXME: this is nasty, but when a structural model is instantiated by creating new IStructuralObject, its related objects must be read from the graph first, or otherwise the objects would be deleted from the graph.
127 // as a side effect, if the new IStructuralObject has any properties set, those properties are set to null (because the graph does not contain those values).
129 updateRange(graph, (IForwardMapping<StructuralResource, IStructuralObject>)mapping, domainElement, rangeElement);
130 updateDomain(graph, mapping, domainElement, rangeElement);
135 public void createRange(ReadGraph graph, IForwardMapping<StructuralResource, IStructuralObject> mapping, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
136 if (rangeElement.getContext().size() == 0 && domainElement.getContext().size() > 0) {
137 List<IStructuralObject> ctx = new ArrayList<IStructuralObject>(domainElement.getContext().size());
139 List<Resource> context = new ArrayList<Resource>();
140 for (int i = 0; i <domainElement.getContext().size(); i++) {
141 context.add(domainElement.getContext().get(i));
142 IStructuralObject ctxObj = mapping.get(new StructuralResource(graph,context.get(context.size()-1),context));
143 if (ctxObj == null) throw new MappingException("Cannot resolve range context for domain element " + domainElement);
146 //ctx = mapping.get(new StructuralResource(graph,context.get(context.size()-1),context));
147 } catch (DatabaseException e) {
148 throw new MappingException(e);
151 throw new MappingException("Cannot find context for structural object, " + domainElement);
152 rangeElement.setContext(ctx);
154 updateRange(graph, mapping, domainElement, rangeElement);
157 public boolean updateDomain(WriteGraph g, IBackwardMapping<StructuralResource,IStructuralObject> map, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
158 if(LOGGER.isInfoEnabled())
160 LOGGER.info("SimpleLinkType.updateDomain " +
161 NameUtils.getSafeName(g, domainElement.getResource()) + " " +
162 rangeElement.toString()
164 } catch(DatabaseException e) {
165 throw new MappingException(e);
168 boolean updated = false;
169 for(IBidirectionalMappingRule<StructuralResource, IStructuralObject> rule : rules)
170 updated |= rule.updateDomain(g, map, domainElement, rangeElement);
174 public boolean updateRange(ReadGraph g, IForwardMapping<StructuralResource, IStructuralObject> map, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
176 if(LOGGER.isInfoEnabled())
178 LOGGER.info("SimpleLinkType.updateRange " +
179 NameUtils.getSafeName(g, domainElement.getResource()) + " " +
180 rangeElement.toString()
182 } catch(DatabaseException e) {
183 throw new MappingException(e);
186 boolean updated = false;
187 for(IBidirectionalMappingRule<StructuralResource, IStructuralObject> rule : rules)
188 updated |= rule.updateRange(g, map, domainElement, rangeElement);