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.graph.rules.domain;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.HashMap;
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;
32 * @author Marko Luukkainen
34 public class CompoundValueAccessor implements IDomainAccessor<Resource,Object> {
36 static final Logger LOGGER = LoggerFactory.getLogger(CompoundValueAccessor.class);
42 public CompoundValueAccessor(Resource objRelation, Resource objType, Resource valRelation) {
43 this.objRelation = objRelation;
44 this.objType = objType;
45 this.valRelation = valRelation;
49 public Object get(ReadGraph g, Resource element) throws MappingException {
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));
61 } catch (DatabaseException e) {
62 throw new MappingException(e);
67 public boolean set(WriteGraph g, Resource element, Object v)
68 throws MappingException {
70 Layer0 l0 = Layer0.getInstance(g);
71 LOGGER.info(" CompoundValueAccessor.set");
72 @SuppressWarnings("unchecked")
73 Map<String,Object> values = (Map<String, Object>)v;
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)) {
82 valueMap.put(name, g.getRelatedValue(c.getObject(), valRelation));
85 boolean changed = false;
86 for (String key : values.keySet()) {
87 Object value = values.get(key);
88 if (value.equals(valueMap.get(key)))
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);
100 Statement valueStatement = g.getPossibleStatement(stm.getObject(), valRelation);
101 Resource valueType = CompoundRelatedGetSetValueRuleFactory.dataTypeOfClass(g, value.getClass());
102 if(valueStatement == null) {
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);
111 if (!valueStatement.isAsserted(stm.getObject()))
112 g.claimValue(valueStatement.getObject(), value);
114 Resource valueResource = g.newResource();
115 g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null,
117 g.claim(stm.getObject(), valRelation, valueResource);
118 g.claimValue(valueResource, value);
124 } catch (DatabaseException e) {
125 throw new MappingException(e);
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))
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);