]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/rules/domain/RelatedValueAccessor.java
Use trace level debug messages with ObjMap
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / structural / rules / domain / RelatedValueAccessor.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.structural.rules.domain;
13
14 import java.util.Arrays;
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.Statement;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.objmap.exceptions.MappingException;
25 import org.simantics.objmap.graph.rules.domain.IDomainAccessor;
26 import org.simantics.objmap.structural.StructuralResource;
27
28
29 public class RelatedValueAccessor implements IDomainAccessor<StructuralResource,Object> {
30
31     static Logger LOGGER = LoggerFactory.getLogger(RelatedValueAccessor.class);
32     
33         Resource relation;
34         Resource valueType;
35         boolean useTypeResource;
36         
37         private boolean preventStructuralChanges = true;
38         private boolean preventStructuralRootChanges = true;
39         
40         public RelatedValueAccessor(Resource relation, Resource valueType, boolean useTypeResource) {
41                 this.relation = relation;
42                 this.valueType = valueType;
43                 this.useTypeResource = useTypeResource;
44         }
45         
46         public RelatedValueAccessor(Resource relation, Resource valueType, boolean useTypeResource, boolean preventStructuralChanges, boolean preventStructuralRootChanges) {
47                 this.relation = relation;
48                 this.valueType = valueType;
49                 this.useTypeResource = useTypeResource;
50                 this.preventStructuralChanges = preventStructuralChanges;
51                 this.preventStructuralRootChanges = preventStructuralRootChanges;
52         }
53         
54         private boolean preventChange(StructuralResource element) {
55         return preventStructuralChanges && element.isStructural() && (!element.isStructuralRoot()||preventStructuralRootChanges);       
56     }
57
58         @Override
59         public Object get(ReadGraph g, StructuralResource element) throws MappingException {
60                 try {
61                     LOGGER.trace("        RelatedValueAccessor.get");
62                     Resource res = getServiceResource(g, element);
63                     if (res == null)
64                         return null;
65                         Resource valueResource = g.getPossibleObject(res, relation);
66                         if(valueResource == null)
67                                 return null;
68                         return g.getValue(valueResource);
69                 } catch (DatabaseException e) {
70                         throw new MappingException(e);
71                 }
72         }
73         
74         @Override
75         public boolean set(WriteGraph g, StructuralResource relement, Object value)
76                         throws MappingException {
77                 try {
78                     LOGGER.trace("        RelatedValueAccessor.set");
79                     
80                     Resource element = getServiceResource(g, relement);
81                     if (element == null)
82                         return false;
83                     Statement valueStatement = g.getPossibleStatement(element, relation);
84                         if(valueStatement == null) {
85                                 if(value == null)
86                                         return false;
87                                 if (preventChange(relement))
88                                         return false;
89                                 Resource valueResource = g.newResource();
90                                 g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null,
91                                                 valueType);
92                                 g.claim(element, relation, valueResource);
93                                 g.claimValue(valueResource, value);                             
94                                 return true;
95                         }
96                         else {
97                                 if(value == null) {
98                                         if (preventChange(relement))
99                                                 return false;
100                                         if (!valueStatement.isAsserted(element)) {
101                                                 g.deny(valueStatement.getObject());
102                                                 return true;
103                                         } else {
104                                                 return false;
105                                         }
106                                         
107                                 }                               
108                                 Object currentValue = g.getValue(valueStatement.getObject());
109                                 if(equals(currentValue,value))
110                                         return false;
111                                 if (preventChange(relement))
112                                         return false;
113                                 if (!valueStatement.isAsserted(element))
114                                         g.claimValue(valueStatement.getObject(), value);
115                                 else {
116                                         Resource valueResource = g.newResource();
117                                         g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null,
118                                                         valueType);
119                                         g.claim(element, relation, valueResource);
120                                         g.claimValue(valueResource, value);
121                                 }
122                                 return true;
123                         }
124                 } catch (DatabaseException e) {
125                         throw new MappingException(e);
126                 }
127                 
128         }
129         
130         private Resource getServiceResource(ReadGraph g, StructuralResource element) {
131                 if (!useTypeResource)
132                         return element.getResource();
133                 return element.getTypeResource();
134         }
135         
136         private boolean equals(Object o1, Object o2) {
137                 if (o1 instanceof boolean[])
138                         Arrays.equals((boolean[])o1,(boolean[])o2);
139                 if (o1 instanceof int[])
140                         Arrays.equals((int[])o1,(int[])o2);
141                 if (o1 instanceof float[])
142                         Arrays.equals((float[])o1,(float[])o2);
143                 if (o1 instanceof double[])
144                         Arrays.equals((double[])o1,(double[])o2);
145                 if (o1 instanceof byte[])
146                         Arrays.equals((byte[])o1,(byte[])o2);
147                 return o1.equals(o2);
148                 
149         }
150
151 }