]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/SimpleLinkType.java
096d07cb83d609b0d33ccc49090ba49d649f32fb
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / schema / SimpleLinkType.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2013 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.objmap.graph.schema;
13
14 import java.util.ArrayList;
15
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;
28
29
30 /**
31  * A link type that is associated with single domain and range type (class).
32  * SimpleLinkType is composed of simpler rules whose combination determines
33  * its update policy.
34  * @author Hannu Niemist�
35  */
36 public class SimpleLinkType<Range> implements ILinkType<Resource,Range> {
37     
38     static Logger LOGGER = LoggerFactory.getLogger(SimpleLinkType.class);
39     
40     public Resource domainType;
41     public Class<?> rangeType;
42     ArrayList<IBidirectionalMappingRule<Resource, Range>> rules;
43     
44     public SimpleLinkType(Resource domainType, Class<?> rangeType,
45             ArrayList<IBidirectionalMappingRule<Resource, Range>> rules) {
46         this.domainType = domainType;
47         this.rangeType = rangeType;
48         this.rules = rules;
49     }
50
51     public SimpleLinkType(Resource domainType, Class<?> rangeType) {
52         this(domainType, rangeType, new ArrayList<IBidirectionalMappingRule<Resource, Range>>());
53     }
54
55     /**
56      * Adds a new rule to this link type that is enforced
57      * during updates.
58      */
59     public void addRule(IBidirectionalMappingRule<Resource, Range> rule) {
60         rules.add(rule);
61     }
62     
63     @Override
64     public Resource createDomainElement(WriteGraph g, Range rangeElement)
65             throws MappingException {
66         try {
67             if(LOGGER.isInfoEnabled())
68                 LOGGER.info("SimpleLinkType.createDomainElement " +
69                         rangeElement.toString()
70                 );
71             Resource result = g.newResource();
72             g.claim(result, Layer0.getInstance(g).InstanceOf, null, domainType);
73             return result;
74         } catch(DatabaseException e) {
75             throw new MappingException(e);
76         }
77     }
78     @SuppressWarnings("unchecked")
79         @Override
80     public Range createRangeElement(ReadGraph g, Resource domainElement)
81             throws MappingException {
82         try {
83             if(LOGGER.isInfoEnabled())
84                 try { 
85                     LOGGER.info("SimpleLinkType.createRangeElement " +
86                                 NameUtils.getSafeName(g, domainElement)
87                             );
88                 } catch(DatabaseException e) {
89                     throw new MappingException(e);
90                 }
91             return (Range)rangeType.newInstance();
92         } catch (InstantiationException e) {
93             throw new MappingException(e);
94         } catch (IllegalAccessException e) {
95             throw new MappingException(e);
96         }
97     }
98     
99     public void createDomain(WriteGraph graph, IBackwardMapping<Resource,Range> mapping, Resource domainElement, Range rangeElement) throws MappingException {
100         updateDomain(graph, mapping, domainElement, rangeElement);
101     };
102     
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);
105     };
106     
107     public boolean updateDomain(WriteGraph g, IBackwardMapping<Resource,Range> map, Resource domainElement, Range rangeElement) throws MappingException {
108         if(LOGGER.isInfoEnabled())
109             try { 
110                 LOGGER.info("SimpleLinkType.updateDomain " +
111                         NameUtils.getSafeName(g, domainElement) + " " +
112                         rangeElement.toString()
113                         );
114             } catch(DatabaseException e) {
115                 throw new MappingException(e);
116             }
117         
118         boolean updated = false;
119         for(IBidirectionalMappingRule<Resource, Range> rule : rules)
120                 updated |= rule.updateDomain(g, map, domainElement, rangeElement);
121         return updated;
122     }
123     
124     public boolean updateRange(ReadGraph g, IForwardMapping<Resource,Range> map, Resource domainElement, Range rangeElement) throws MappingException {
125     
126         if(LOGGER.isInfoEnabled())
127             try { 
128                 LOGGER.info("SimpleLinkType.updateRange " +
129                                 NameUtils.getSafeName(g, domainElement) + " " +
130                         (rangeElement.getClass().getName() + "@" + Integer.toHexString(rangeElement.hashCode()))
131                         );
132             } catch(DatabaseException e) {
133                 throw new MappingException(e);
134             }
135         
136         boolean updated = false;
137         for(IBidirectionalMappingRule<Resource, Range> rule : rules)
138             updated |= rule.updateRange(g, map, domainElement, rangeElement);
139         return updated;
140     }
141 }