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.apache.log4j.Logger;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.utils.NameUtils;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.layer0.Layer0;
25 import org.simantics.objmap.backward.IBackwardMapping;
26 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
27 import org.simantics.objmap.exceptions.MappingException;
28 import org.simantics.objmap.forward.IForwardMapping;
29 import org.simantics.objmap.graph.schema.ILinkType;
30 import org.simantics.objmap.structural.IStructuralObject;
31 import org.simantics.objmap.structural.StructuralResource;
35 public class SimpleLinkType implements ILinkType<StructuralResource,IStructuralObject> {
37 static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
39 public Resource domainType;
40 public Class<?> rangeType;
41 ArrayList<IBidirectionalMappingRule<StructuralResource,IStructuralObject>> rules;
43 public SimpleLinkType(Resource domainType, Class<?> rangeType,
44 ArrayList<IBidirectionalMappingRule<StructuralResource,IStructuralObject>> rules) {
45 this.domainType = domainType;
46 this.rangeType = rangeType;
50 public SimpleLinkType(Resource domainType, Class<?> rangeType) {
51 this(domainType, rangeType, new ArrayList<IBidirectionalMappingRule<StructuralResource,IStructuralObject>>());
55 * Adds a new rule to this link type that is enforced
58 public void addRule(IBidirectionalMappingRule<StructuralResource,IStructuralObject> rule) {
63 public StructuralResource createDomainElement(WriteGraph g, IStructuralObject rangeElement)
64 throws MappingException {
66 if(LOGGER.isInfoEnabled())
67 LOGGER.info("SimpleLinkType.createDomainElement " +
68 rangeElement.toString()
70 if (rangeElement.getContext().size() == 0) {
71 // there is no context, this not a structural resource / object.
72 Resource result = newResource(g, domainType);
73 return new StructuralResource(g,result);
75 if (rangeElement.getContext().size() == 1 && rangeElement.getContext().get(0).equals(rangeElement)) {
76 // Structural object's context is itself, we are instantiating a new structural model.
77 Resource type = rangeElement.getType();
78 Resource result = newResource(g, type);
79 return new StructuralResource(g,result,result);
81 // Structural object's context is not itself, which means that the object is inside of a structural model.
82 // At the moment we do not support modifying instantiated structural models.
83 throw new MappingException("Cannot create a new StucturalObject " + rangeElement + " " + rangeElement.getClass());
86 } catch(DatabaseException e) {
87 throw new MappingException(e);
91 protected Resource newResource(WriteGraph g, Resource type) throws DatabaseException {
92 Resource result = g.newResource();
93 g.claim(result, Layer0.getInstance(g).InstanceOf, null, type);
97 public IStructuralObject createRangeElement(ReadGraph g, StructuralResource domainElement)
98 throws MappingException {
100 if(LOGGER.isInfoEnabled())
102 LOGGER.info("SimpleLinkType.createRangeElement " + NameUtils.getSafeName(g, domainElement.getResource()));
103 } catch(DatabaseException e) {
104 throw new MappingException(e);
106 IStructuralObject result = (IStructuralObject)rangeType.newInstance();
107 if (domainElement.getContext().size() == 1) {
108 if (domainElement.getContext().get(0).equals(domainElement.getResource()))
109 result.setContext(Collections.singletonList(result));
111 //result.setContext(result);
115 } catch (InstantiationException e) {
116 throw new MappingException(e);
117 } catch (IllegalAccessException e) {
118 throw new MappingException(e);
122 @SuppressWarnings("unchecked")
123 public void createDomain(WriteGraph graph, IBackwardMapping<StructuralResource,IStructuralObject> mapping, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
124 if (domainElement.isStructuralRoot())
125 // 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.
126 // 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).
128 updateRange(graph, (IForwardMapping<StructuralResource, IStructuralObject>)mapping, domainElement, rangeElement);
129 updateDomain(graph, mapping, domainElement, rangeElement);
134 public void createRange(ReadGraph graph, IForwardMapping<StructuralResource, IStructuralObject> mapping, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
135 if (rangeElement.getContext().size() == 0 && domainElement.getContext().size() > 0) {
136 List<IStructuralObject> ctx = new ArrayList<IStructuralObject>(domainElement.getContext().size());
138 List<Resource> context = new ArrayList<Resource>();
139 for (int i = 0; i <domainElement.getContext().size(); i++) {
140 context.add(domainElement.getContext().get(i));
141 IStructuralObject ctxObj = mapping.get(new StructuralResource(graph,context.get(context.size()-1),context));
142 if (ctxObj == null) throw new MappingException("Cannot resolve range context for domain element " + domainElement);
145 //ctx = mapping.get(new StructuralResource(graph,context.get(context.size()-1),context));
146 } catch (DatabaseException e) {
147 throw new MappingException(e);
150 throw new MappingException("Cannot find context for structural object, " + domainElement);
151 rangeElement.setContext(ctx);
153 updateRange(graph, mapping, domainElement, rangeElement);
156 public boolean updateDomain(WriteGraph g, IBackwardMapping<StructuralResource,IStructuralObject> map, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
157 if(LOGGER.isInfoEnabled())
159 LOGGER.info("SimpleLinkType.updateDomain " +
160 NameUtils.getSafeName(g, domainElement.getResource()) + " " +
161 rangeElement.toString()
163 } catch(DatabaseException e) {
164 throw new MappingException(e);
167 boolean updated = false;
168 for(IBidirectionalMappingRule<StructuralResource, IStructuralObject> rule : rules)
169 updated |= rule.updateDomain(g, map, domainElement, rangeElement);
173 public boolean updateRange(ReadGraph g, IForwardMapping<StructuralResource, IStructuralObject> map, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
175 if(LOGGER.isInfoEnabled())
177 LOGGER.info("SimpleLinkType.updateRange " +
178 NameUtils.getSafeName(g, domainElement.getResource()) + " " +
179 rangeElement.toString()
181 } catch(DatabaseException e) {
182 throw new MappingException(e);
185 boolean updated = false;
186 for(IBidirectionalMappingRule<StructuralResource, IStructuralObject> rule : rules)
187 updated |= rule.updateRange(g, map, domainElement, rangeElement);