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