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.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;
31 * @author Marko Luukkainen
33 public class CompoundValueAccessor implements IDomainAccessor<Resource,Object> {
35 static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
41 public CompoundValueAccessor(Resource objRelation, Resource objType, Resource valRelation) {
42 this.objRelation = objRelation;
43 this.objType = objType;
44 this.valRelation = valRelation;
48 public Object get(ReadGraph g, Resource element) throws MappingException {
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));
60 } catch (DatabaseException e) {
61 throw new MappingException(e);
66 public boolean set(WriteGraph g, Resource element, Object v)
67 throws MappingException {
69 Layer0 l0 = Layer0.getInstance(g);
70 LOGGER.info(" CompoundValueAccessor.set");
71 @SuppressWarnings("unchecked")
72 Map<String,Object> values = (Map<String, Object>)v;
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)) {
81 valueMap.put(name, g.getRelatedValue(c.getObject(), valRelation));
84 boolean changed = false;
85 for (String key : values.keySet()) {
86 Object value = values.get(key);
87 if (value.equals(valueMap.get(key)))
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);
99 Statement valueStatement = g.getPossibleStatement(stm.getObject(), valRelation);
100 Resource valueType = CompoundRelatedGetSetValueRuleFactory.dataTypeOfClass(g, value.getClass());
101 if(valueStatement == null) {
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);
110 if (!valueStatement.isAsserted(stm.getObject()))
111 g.claimValue(valueStatement.getObject(), value);
113 Resource valueResource = g.newResource();
114 g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null,
116 g.claim(stm.getObject(), valRelation, valueResource);
117 g.claimValue(valueResource, value);
123 } catch (DatabaseException e) {
124 throw new MappingException(e);
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))
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);