X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.objmap2%2Fsrc%2Forg%2Fsimantics%2Fobjmap%2Fgraph%2Frules%2Fdomain%2FCompoundValueAccessor.java;h=39df524b270943022478f51669f2a6627cbbe03e;hb=993f8fda339c833e182a540bd4c10d3a91624966;hp=4d29ac0a61bf234fff66895aeb8d3a466eeb8bf5;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/domain/CompoundValueAccessor.java b/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/domain/CompoundValueAccessor.java index 4d29ac0a6..39df524b2 100644 --- a/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/domain/CompoundValueAccessor.java +++ b/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/rules/domain/CompoundValueAccessor.java @@ -1,152 +1,154 @@ -/******************************************************************************* - * Copyright (c) 2007, 2013 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.objmap.graph.rules.domain; - -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import org.apache.log4j.Logger; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.Statement; -import org.simantics.db.WriteGraph; -import org.simantics.db.exception.DatabaseException; -import org.simantics.layer0.Layer0; -import org.simantics.objmap.exceptions.MappingException; -import org.simantics.objmap.graph.annotations.factories.CompoundRelatedGetSetValueRuleFactory; - -/** - * - * @author Marko Luukkainen - */ -public class CompoundValueAccessor implements IDomainAccessor { - - static Logger LOGGER = Logger.getLogger("org.simantics.objmap"); - - Resource objRelation; - Resource objType; - Resource valRelation; - - public CompoundValueAccessor(Resource objRelation, Resource objType, Resource valRelation) { - this.objRelation = objRelation; - this.objType = objType; - this.valRelation = valRelation; - } - - @Override - public Object get(ReadGraph g, Resource element) throws MappingException { - try { - Layer0 l0 = Layer0.getInstance(g); - LOGGER.info(" CompoundValueAccessor.get"); - Collection coll = g.getStatements(element, objRelation); - Map map = new HashMap(); - for (Statement c : coll) { - String name = g.getRelatedValue(c.getObject(), l0.HasName); - if (!map.containsKey(name) || !c.isAsserted(element)) - map.put(name, g.getRelatedValue(c.getObject(), valRelation)); - } - return map; - } catch (DatabaseException e) { - throw new MappingException(e); - } - } - - @Override - public boolean set(WriteGraph g, Resource element, Object v) - throws MappingException { - try { - Layer0 l0 = Layer0.getInstance(g); - LOGGER.info(" CompoundValueAccessor.set"); - @SuppressWarnings("unchecked") - Map values = (Map)v; - - Collection coll = g.getStatements(element, objRelation); - Map stmMap = new HashMap(); - Map valueMap = new HashMap(); - for (Statement c : coll) { - String name = g.getRelatedValue(c.getObject(), l0.HasName); - if (!stmMap.containsKey(name) || !c.isAsserted(element)) { - stmMap.put(name, c); - valueMap.put(name, g.getRelatedValue(c.getObject(), valRelation)); - } - } - boolean changed = false; - for (String key : values.keySet()) { - Object value = values.get(key); - if (value.equals(valueMap.get(key))) - continue; - changed = true; - Statement stm = stmMap.get(key); - if (stm == null || stm.isAsserted(element)) { - Resource obj = g.newResource(); - g.claim(obj, l0.InstanceOf, objType); - g.claimLiteral(obj, l0.HasName, key); - g.claim(element, objRelation, obj); - stm = getStatement(g, element, objRelation, obj); - } - - Statement valueStatement = g.getPossibleStatement(stm.getObject(), valRelation); - Resource valueType = CompoundRelatedGetSetValueRuleFactory.dataTypeOfClass(g, value.getClass()); - if(valueStatement == null) { - - Resource valueResource = g.newResource(); - g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null, valueType); - g.claim(stm.getObject(), valRelation, valueResource); - g.claimValue(valueResource, value); - } else { - - - if (!valueStatement.isAsserted(stm.getObject())) - g.claimValue(valueStatement.getObject(), value); - else { - Resource valueResource = g.newResource(); - g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null, - valueType); - g.claim(stm.getObject(), valRelation, valueResource); - g.claimValue(valueResource, value); - } - } - } - return changed; - - } catch (DatabaseException e) { - throw new MappingException(e); - } - - } - - private Statement getStatement(ReadGraph g, Resource s, Resource p, Resource o) throws DatabaseException{ - for (Statement stm : g.getStatements(s, p)) { - if (stm.getObject().equals(o)) - return stm; - } - return null; - } - - private boolean equals(Object o1, Object o2) { - if (o1 instanceof boolean[]) - Arrays.equals((boolean[])o1,(boolean[])o2); - if (o1 instanceof int[]) - Arrays.equals((int[])o1,(int[])o2); - if (o1 instanceof float[]) - Arrays.equals((float[])o1,(float[])o2); - if (o1 instanceof double[]) - Arrays.equals((double[])o1,(double[])o2); - if (o1 instanceof byte[]) - Arrays.equals((byte[])o1,(byte[])o2); - return o1.equals(o2); - - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2013 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.objmap.graph.rules.domain; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.simantics.databoard.binding.Binding; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.Statement; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.ServiceException; +import org.simantics.layer0.Layer0; +import org.simantics.objmap.exceptions.MappingException; +import org.simantics.objmap.graph.annotations.factories.DataTypeUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author Marko Luukkainen + */ +public class CompoundValueAccessor implements IDomainAccessor { + + static final Logger LOGGER = LoggerFactory.getLogger(CompoundValueAccessor.class); + + Resource objRelation; + Resource objType; + Resource valRelation; + Binding valueBinding; + + public CompoundValueAccessor(Resource objRelation, Resource objType, Resource valRelation, Binding valueBinding) { + this.objRelation = objRelation; + this.objType = objType; + this.valRelation = valRelation; + this.valueBinding = valueBinding; + } + + @Override + public Object get(ReadGraph g, Resource element) throws MappingException { + try { + Layer0 l0 = Layer0.getInstance(g); + LOGGER.trace(" CompoundValueAccessor.get"); + Collection coll = g.getStatements(element, objRelation); + Map map = new HashMap(); + for (Statement c : coll) { + String name = g.getRelatedValue(c.getObject(), l0.HasName); + if (!map.containsKey(name) || !c.isAsserted(element)) { + final Object value = getValue(g, c.getObject()); + map.put(name, value); + } + } + return map; + } catch (DatabaseException e) { + throw new MappingException(e); + } + } + + @Override + public boolean set(WriteGraph g, Resource element, Object v) + throws MappingException { + try { + Layer0 l0 = Layer0.getInstance(g); + LOGGER.trace(" CompoundValueAccessor.set"); + @SuppressWarnings("unchecked") + Map values = (Map)v; + + Collection coll = g.getStatements(element, objRelation); + Map stmMap = new HashMap(); + Map valueMap = new HashMap(); + for (Statement c : coll) { + String name = g.getRelatedValue(c.getObject(), l0.HasName); + if (!stmMap.containsKey(name) || !c.isAsserted(element)) { + stmMap.put(name, c); + valueMap.put(name, g.getRelatedValue(c.getObject(), valRelation)); + } + } + + boolean changed = false; + for (String key : values.keySet()) { + Object value = values.get(key); + if (value.equals(valueMap.get(key))) + continue; + changed = true; + Statement stm = stmMap.get(key); + if (stm == null || stm.isAsserted(element)) { + Resource obj = g.newResource(); + g.claim(obj, l0.InstanceOf, objType); + g.claimLiteral(obj, l0.HasName, key); + g.claim(element, objRelation, obj); + stm = getStatement(g, element, objRelation, obj); + } + + Statement valueStatement = g.getPossibleStatement(stm.getObject(), valRelation); + Resource valueType = valueBinding != null ? + DataTypeUtils.dataTypeOfDatatype(g, valueBinding.type()) : + DataTypeUtils.dataTypeOfClass(g, value.getClass()); + + if(valueStatement == null) { + Resource valueResource = g.newResource(); + g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null, valueType); + g.claim(stm.getObject(), valRelation, valueResource); + claimValue(g, valueResource, value); + } else { + if (!valueStatement.isAsserted(stm.getObject())) + g.claimValue(valueStatement.getObject(), value); + else { + Resource valueResource = g.newResource(); + g.claim(valueResource, Layer0.getInstance(g).InstanceOf, null, valueType); + g.claim(stm.getObject(), valRelation, valueResource); + claimValue(g, valueResource, value); + } + } + } + + return changed; + } catch (DatabaseException e) { + throw new MappingException(e); + } + + } + + private void claimValue(WriteGraph g, Resource valueResource, Object value) throws ServiceException { + if (valueBinding != null) + g.claimValue(valueResource, value, valueBinding); + else + g.claimValue(valueResource, value); + } + + private Statement getStatement(ReadGraph g, Resource s, Resource p, Resource o) throws DatabaseException{ + for (Statement stm : g.getStatements(s, p)) { + if (stm.getObject().equals(o)) + return stm; + } + return null; + } + + private Object getValue(ReadGraph g, final Resource object) throws DatabaseException { + return valueBinding != null ? g.getRelatedValue(object, valRelation, valueBinding) : g.getRelatedValue(object, valRelation); + } + +}