]> gerrit.simantics Code Review - simantics/platform.git/blob
78b48430310356fd0c4f0561aaefb0736f8f907d
[simantics/platform.git] /
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.rules.domain;
13
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import org.apache.log4j.Logger;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.Statement;
23 import org.simantics.db.WriteGraph;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.objmap.exceptions.MappingException;
27 import org.simantics.objmap.graph.annotations.factories.CompoundRelatedGetSetValueRuleFactory;
28
29 /**
30  * 
31  * @author Marko Luukkainen
32  */
33 public class CompoundValueAccessor implements IDomainAccessor<Resource,Object> {
34
35     static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
36     
37         Resource objRelation;
38         Resource objType;
39         Resource valRelation;
40
41         public CompoundValueAccessor(Resource objRelation, Resource objType, Resource valRelation) {
42                 this.objRelation = objRelation;
43                 this.objType = objType;
44                 this.valRelation = valRelation;
45         }
46
47         @Override
48         public Object get(ReadGraph g, Resource element) throws MappingException {
49                 try {
50                         Layer0 l0 = Layer0.getInstance(g);
51                     LOGGER.info("        CompoundValueAccessor.get");
52                     Collection<Statement> coll = g.getStatements(element, objRelation);
53                     Map<String,Object> map = new HashMap<String, Object>();
54                     for (Statement c : coll) {
55                         String name = g.getRelatedValue(c.getObject(), l0.HasName);
56                         if (!map.containsKey(name) || !c.isAsserted(element))
57                                 map.put(name, g.getRelatedValue(c.getObject(), valRelation));
58                     }
59                         return map;
60                 } catch (DatabaseException e) {
61                         throw new MappingException(e);
62                 }
63         }
64         
65         @Override
66         public boolean set(WriteGraph g, Resource element, Object v)
67                         throws MappingException {
68                 try {
69                         Layer0 l0 = Layer0.getInstance(g);
70                     LOGGER.info("        CompoundValueAccessor.set");
71                     @SuppressWarnings("unchecked")
72                     Map<String,Object> values = (Map<String, Object>)v;
73                     
74                     Collection<Statement> coll = g.getStatements(element, objRelation);
75                     Map<String,Statement> stmMap = new HashMap<String, Statement>();
76                     Map<String,Object> valueMap = new HashMap<String, Object>();
77                     for (Statement c : coll) {
78                         String name = g.getRelatedValue(c.getObject(), l0.HasName);
79                         if (!stmMap.containsKey(name) || !c.isAsserted(element)) {
80                                 stmMap.put(name, c);
81                                 valueMap.put(name, g.getRelatedValue(c.getObject(), valRelation));
82                         }
83                     }
84                     boolean changed = false;
85                     for (String key : values.keySet()) {
86                         Object value = values.get(key);
87                         if (value.equals(valueMap.get(key)))
88                                 continue;
89                         changed = true;
90                         Statement stm = stmMap.get(key);
91                         if (stm == null || stm.isAsserted(element)) {
92                                 Resource obj = g.newResource();
93                                 g.claim(obj, l0.InstanceOf, objType);
94                                 g.claimLiteral(obj, l0.HasName, key);
95                                 g.claim(element, objRelation, obj);
96                                 stm = getStatement(g, element, objRelation, obj);
97                         }
98                         
99                         Statement valueStatement = g.getPossibleStatement(stm.getObject(), valRelation);
100                         Resource valueType = CompoundRelatedGetSetValueRuleFactory.dataTypeOfClass(g, value.getClass());
101                                 if(valueStatement == null) {
102                                         
103                                         Resource valueResource = g.newResource();
104                                         g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null,  valueType);
105                                         g.claim(stm.getObject(), valRelation, valueResource);
106                                         g.claimValue(valueResource, value);                             
107                                 } else {
108                                                                         
109                                         
110                                         if (!valueStatement.isAsserted(stm.getObject()))
111                                                 g.claimValue(valueStatement.getObject(), value);
112                                         else {
113                                                 Resource valueResource = g.newResource();
114                                                 g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null,
115                                                                 valueType);
116                                                 g.claim(stm.getObject(), valRelation, valueResource);
117                                                 g.claimValue(valueResource, value);
118                                         }
119                                 }
120                     }
121                     return changed;
122                     
123                 } catch (DatabaseException e) {
124                         throw new MappingException(e);
125                 }
126                 
127         }
128         
129         private Statement getStatement(ReadGraph g, Resource s, Resource p, Resource o) throws DatabaseException{
130                 for (Statement stm : g.getStatements(s, p)) {
131                         if (stm.getObject().equals(o))
132                                 return stm;
133                 }
134                 return null;
135         }
136         
137         private boolean equals(Object o1, Object o2) {
138                 if (o1 instanceof boolean[])
139                         Arrays.equals((boolean[])o1,(boolean[])o2);
140                 if (o1 instanceof int[])
141                         Arrays.equals((int[])o1,(int[])o2);
142                 if (o1 instanceof float[])
143                         Arrays.equals((float[])o1,(float[])o2);
144                 if (o1 instanceof double[])
145                         Arrays.equals((double[])o1,(double[])o2);
146                 if (o1 instanceof byte[])
147                         Arrays.equals((byte[])o1,(byte[])o2);
148                 return o1.equals(o2);
149                 
150         }
151
152 }