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;
16 import org.apache.log4j.Logger;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Statement;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.objmap.exceptions.MappingException;
26 * Accesses a value attached to the element by given functional relation.
27 * @author Hannu Niemist�
29 public class RelatedValueAccessor implements IDomainAccessor<Resource,Object> {
31 static Logger LOGGER = Logger.getLogger("org.simantics.objmap");
36 public RelatedValueAccessor(Resource relation, Resource valueType) {
37 this.relation = relation;
38 this.valueType = valueType;
42 public Object get(ReadGraph g, Resource element) throws MappingException {
44 LOGGER.info(" RelatedValueAccessor.get");
45 Resource valueResource = g.getPossibleObject(element, relation);
46 if(valueResource == null)
48 return g.getValue(valueResource);
49 } catch (DatabaseException e) {
50 throw new MappingException(e);
55 public boolean set(WriteGraph g, Resource element, Object value)
56 throws MappingException {
58 LOGGER.info(" RelatedValueAccessor.set");
59 Statement valueStatement = g.getPossibleStatement(element, relation);
60 if(valueStatement == null) {
63 Resource valueResource = g.newResource();
64 g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null,
66 g.claim(element, relation, valueResource);
67 g.claimValue(valueResource, value);
72 if (!valueStatement.isAsserted(element)) {
73 g.deny(valueStatement.getObject());
79 Object currentValue = g.getValue(valueStatement.getObject());
80 if(equals(currentValue,value))
82 if (!valueStatement.isAsserted(element))
83 g.claimValue(valueStatement.getObject(), value);
85 Resource valueResource = g.newResource();
86 g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null,
88 g.claim(element, relation, valueResource);
89 g.claimValue(valueResource, value);
93 } catch (DatabaseException e) {
94 throw new MappingException(e);
99 private boolean equals(Object o1, Object o2) {
100 if (o1 instanceof boolean[])
101 Arrays.equals((boolean[])o1,(boolean[])o2);
102 if (o1 instanceof int[])
103 Arrays.equals((int[])o1,(int[])o2);
104 if (o1 instanceof float[])
105 Arrays.equals((float[])o1,(float[])o2);
106 if (o1 instanceof double[])
107 Arrays.equals((double[])o1,(double[])o2);
108 if (o1 instanceof byte[])
109 Arrays.equals((byte[])o1,(byte[])o2);
110 return o1.equals(o2);