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.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.Statement;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.objmap.exceptions.MappingException;
27 * Accesses a value attached to the element by given functional relation.
28 * @author Hannu Niemist�
30 public class RelatedValueAccessor implements IDomainAccessor<Resource,Object> {
32 static Logger LOGGER = LoggerFactory.getLogger(RelatedValueAccessor.class);
37 public RelatedValueAccessor(Resource relation, Resource valueType) {
38 this.relation = relation;
39 this.valueType = valueType;
43 public Object get(ReadGraph g, Resource element) throws MappingException {
45 LOGGER.info(" RelatedValueAccessor.get");
46 Resource valueResource = g.getPossibleObject(element, relation);
47 if(valueResource == null)
49 return g.getValue(valueResource);
50 } catch (DatabaseException e) {
51 throw new MappingException(e);
56 public boolean set(WriteGraph g, Resource element, Object value)
57 throws MappingException {
59 LOGGER.info(" RelatedValueAccessor.set");
60 Statement valueStatement = g.getPossibleStatement(element, relation);
61 if(valueStatement == null) {
64 Resource valueResource = g.newResource();
65 g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null,
67 g.claim(element, relation, valueResource);
68 g.claimValue(valueResource, value);
73 if (!valueStatement.isAsserted(element)) {
74 g.deny(valueStatement.getObject());
80 Object currentValue = g.getValue(valueStatement.getObject());
81 if(equals(currentValue,value))
83 if (!valueStatement.isAsserted(element))
84 g.claimValue(valueStatement.getObject(), value);
86 Resource valueResource = g.newResource();
87 g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null,
89 g.claim(element, relation, valueResource);
90 g.claimValue(valueResource, value);
94 } catch (DatabaseException e) {
95 throw new MappingException(e);
100 private boolean equals(Object o1, Object o2) {
101 if (o1 instanceof boolean[])
102 Arrays.equals((boolean[])o1,(boolean[])o2);
103 if (o1 instanceof int[])
104 Arrays.equals((int[])o1,(int[])o2);
105 if (o1 instanceof float[])
106 Arrays.equals((float[])o1,(float[])o2);
107 if (o1 instanceof double[])
108 Arrays.equals((double[])o1,(double[])o2);
109 if (o1 instanceof byte[])
110 Arrays.equals((byte[])o1,(byte[])o2);
111 return o1.equals(o2);