import org.simantics.objmap.exceptions.MappingException;
public interface IForwardMappingRule<Domain, Range> {
+ /**
+ * Check whether a range element needs to be updated due to a change in the range element, without modifying the range element.
+ */
+ boolean checkChanges(ReadGraph graph, IForwardMapping<Domain, Range> mapping, Domain domainElement, Range rangeElement) throws MappingException;
+
/**
* Modifies the range element so that it corresponds to the domain element.
* @param g read transaction
* @return true if the rule made some modifications
* @throws MappingException
*/
- boolean updateRange(ReadGraph graph, IForwardMapping<Domain, Range> mapping, Domain domainElement, Range rangeElement) throws MappingException;
+ boolean updateRange(ReadGraph graph, IForwardMapping<Domain, Range> mapping, Domain domainElement, Range rangeElement) throws MappingException;
void createRange(ReadGraph graph, IForwardMapping<Domain, Range> mapping, Domain domainElement, Range rangeElement) throws MappingException;
}
return false;
}
+ @Override
+ public boolean checkChanges(ReadGraph g, IForwardMapping<Domain, Range> map, Domain domainElement,
+ Range rangeElement) throws MappingException {
+ return false;
+ }
+
public void createDomain(WriteGraph g, IBackwardMapping<Domain,Range> map, Domain domainElement, Range rangeElement) throws MappingException {
updateDomain(g, map, domainElement, rangeElement);
};
package org.simantics.objmap.graph.impl;
-import gnu.trove.map.hash.THashMap;
-
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.simantics.db.ReadGraph;
import org.simantics.db.WriteGraph;
import org.simantics.db.exception.DatabaseException;
-
import org.simantics.objmap.backward.IBackwardMapping;
import org.simantics.objmap.exceptions.MappingException;
import org.simantics.objmap.forward.IForwardMapping;
import org.simantics.objmap.graph.IMappingListener;
import org.simantics.objmap.graph.schema.ILinkType;
import org.simantics.objmap.graph.schema.IMappingSchema;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import gnu.trove.map.hash.THashMap;
/**
* An implementation of IMapping. The class should not be created
if(listensDomain) {
RangeUpdateRequest<Domain,Range> request = new RangeUpdateRequest<Domain,Range>(link, map, this);
+ boolean changes;
try {
- g.syncRequest(request, request);
+ changes = g.syncRequest(request, request) > 0;
} catch (DatabaseException e) {
throw new MappingException(e);
}
- // TODO check if really modified
- updated.add(link.rangeElement);
+
+ if (changes)
+ updated.add(link.rangeElement);
}
else
if(link.type.updateRange(g, map, link.domainElement, link.rangeElement))
import org.simantics.objmap.graph.impl.Link;
-public class RangeUpdateRequest<Domain,Range> implements Read<Boolean>, SyncListener<Boolean> {
+public class RangeUpdateRequest<Domain,Range> implements Read<Integer>, SyncListener<Integer> {
Link<Domain,Range> link;
/*
*/
IForwardMapping<Domain, Range> map; // map==null is used to flag that request is performed once
Mapping<Domain, Range> mapping; // mapping==null is used as a flag the request disposed
+ int counter;
public RangeUpdateRequest(Link<Domain,Range> link, IForwardMapping<Domain,Range> map, Mapping<Domain,Range> mapping) {
this.link = link;
this.map = map;
this.mapping = mapping;
+ this.counter = 0;
}
@Override
- public Boolean perform(ReadGraph g) throws DatabaseException {
- if(map != null) {
- link.type.updateRange(g, map, link.domainElement, link.rangeElement);
- map = null;
- return Boolean.TRUE;
- }
- else if(mapping != null) {
- mapping.domainModified(link);
- mapping = null;
- return Boolean.FALSE;
- }
- else
- return null;
+ public Integer perform(ReadGraph g) throws DatabaseException {
+ boolean changed = false;
+ if (map != null)
+ changed = link.type.checkChanges(g, map, link.domainElement, link.rangeElement);
+ else if (mapping != null)
+ changed = link.type.checkChanges(g, mapping, link.domainElement, link.rangeElement);
+
+ return changed ? counter + 1 : counter;
}
@Override
}
@Override
- public void execute(ReadGraph graph, Boolean result)
- throws DatabaseException {
+ public void execute(ReadGraph graph, Integer result)
+ throws DatabaseException {
+ boolean changed = result != counter;
+ counter = result;
+
+ if (map != null) {
+ if (changed)
+ link.type.updateRange(graph, map, link.domainElement, link.rangeElement);
+ map = null;
+ }
+ else if (mapping != null && changed) {
+ mapping.domainModified(link);
+ mapping = null;
+ }
}
@Override
public boolean isDisposed() {
return mapping == null || link.removed || mapping.isDisposed();
}
-
-
-
}
*******************************************************************************/
package org.simantics.objmap.graph.rules;
+import java.util.Objects;
+
import org.simantics.db.ReadGraph;
import org.simantics.db.WriteGraph;
import org.simantics.objmap.backward.IBackwardMapping;
return rangeAccessor.set(rangeElement, mappedValue);
}
+ @Override
+ public boolean checkChanges(ReadGraph g, IForwardMapping<Domain, Range> map, Domain domainElement, Range rangeElement) throws MappingException {
+ Domain value = domainAccessor.get(g, domainElement);
+ Range mappedValue = value == null ? null : map.map(g, value);
+ return mappedValue == rangeAccessor.get(rangeElement);
+ }
+
+ @Override
public void createDomain(WriteGraph g, IBackwardMapping<Domain,Range> map, Domain domainElement, Range rangeElement) throws MappingException {
updateDomain(g, map, domainElement, rangeElement);
};
+ @Override
public void createRange(ReadGraph g, IForwardMapping<Domain,Range> map, Domain domainElement, Range rangeElement) throws MappingException {
updateRange(g, map, domainElement, rangeElement);
};
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Objects;
import org.simantics.db.ReadGraph;
import org.simantics.db.WriteGraph;
Domain domainElement, Range rangeElement)
throws MappingException {
LOGGER.trace(" MappedElementsRule.updateRange");
- Collection<Domain> value = domainAccessor.get(g, domainElement);
+ ArrayList<Range> mappedValue = getMappedValue(g, map, domainElement);
+ return rangeAccessor.set(rangeElement, mappedValue);
+ }
+
+ @Override
+ public boolean checkChanges(ReadGraph g, IForwardMapping<Domain, Range> map, Domain domainElement, Range rangeElement) throws MappingException {
+ LOGGER.trace(" MappedElementsRule.updateRange");
+ ArrayList<Range> mappedValue = getMappedValue(g, map, domainElement);
+ return Objects.equals(mappedValue, rangeAccessor.get(rangeElement));
+ }
+
+ private ArrayList<Range> getMappedValue(ReadGraph g, IForwardMapping<Domain, Range> map, Domain domainElement)
+ throws MappingException {
+ Collection<Domain> value = domainAccessor.get(g, domainElement);
ArrayList<Range> mappedValue = new ArrayList<Range>(value.size());
for(Domain r : value)
mappedValue.add(map.map(g, r));//map.get(r));
- return rangeAccessor.set(rangeElement, mappedValue);
- }
+ return mappedValue;
+ }
public void createDomain(WriteGraph g, IBackwardMapping<Domain,Range> map, Domain domainElement, Range rangeElement) throws MappingException {
updateDomain(g, map, domainElement, rangeElement);
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+
+import java.util.Objects;
+
import org.simantics.db.ReadGraph;
import org.simantics.db.WriteGraph;
import org.simantics.objmap.backward.IBackwardMapping;
LOGGER.trace(" ValueRule.updateRange");
Object value = domainAccessor.get(g, domainElement);
return rangeAccessor.set(rangeElement, value);
- }
+ }
+
+ @Override
+ public boolean checkChanges(ReadGraph g, IForwardMapping<Domain, Range> map, Domain domainElement,
+ Range rangeElement) throws MappingException {
+ Object value = rangeAccessor.get(rangeElement);
+ return Objects.equals(value, domainAccessor.get(g, domainElement));
+ }
public void createDomain(WriteGraph g, IBackwardMapping<Domain,Range> map, Domain domainElement, Range rangeElement) throws MappingException {
updateDomain(g, map, domainElement, rangeElement);
}
}
+ @Override
public void createDomain(WriteGraph graph, IBackwardMapping<Resource,Range> mapping, Resource domainElement, Range rangeElement) throws MappingException {
};
+ @Override
public void createRange(ReadGraph graph, org.simantics.objmap.forward.IForwardMapping<Resource,Range> mapping, Resource domainElement, Range rangeElement) throws MappingException {
};
+ @Override
+ public boolean checkChanges(ReadGraph g, IForwardMapping<Resource, Range> map, Resource domainElement, Range rangeElement) throws MappingException {
+ return false;
+ }
+
+ @Override
public boolean updateDomain(WriteGraph g, IBackwardMapping<Resource,Range> map, Resource domainElement, Range rangeElement) throws MappingException {
return false;
}
+ @Override
public boolean updateRange(ReadGraph g, IForwardMapping<Resource,Range> map, Resource domainElement, Range rangeElement) throws MappingException {
return false;
}
updateRange(graph, mapping, domainElement, rangeElement);
};
+ @Override
+ public boolean checkChanges(ReadGraph g, IForwardMapping<Resource, Range> map, Resource domainElement,
+ Range rangeElement) throws MappingException {
+ boolean updated = false;
+ for(IBidirectionalMappingRule<Resource, Range> rule : rules)
+ updated |= rule.checkChanges(g, map, domainElement, rangeElement);
+ return updated;
+ }
+
public boolean updateDomain(WriteGraph g, IBackwardMapping<Resource,Range> map, Resource domainElement, Range rangeElement) throws MappingException {
if(LOGGER.isTraceEnabled())
try {
return false;
}
+ @Override
+ public boolean checkChanges(ReadGraph g, IForwardMapping<Domain, Range> map, Domain domainElement,
+ Range rangeElement) throws MappingException {
+ return false;
+ }
+
public void createDomain(WriteGraph g, IBackwardMapping<Domain,Range> map, Domain domainElement, Range rangeElement) throws MappingException {
updateDomain(g, map, domainElement, rangeElement);
};
}
}
+ @Override
public void createDomain(WriteGraph graph, IBackwardMapping<StructuralResource, IStructuralObject> mapping, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
};
+ @Override
public void createRange(ReadGraph graph, org.simantics.objmap.forward.IForwardMapping<StructuralResource, IStructuralObject> mapping, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
};
+ @Override
+ public boolean checkChanges(ReadGraph graph, IForwardMapping<StructuralResource, IStructuralObject> mapping, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
+ return false;
+ }
+
+ @Override
public boolean updateDomain(WriteGraph g, IBackwardMapping<StructuralResource, IStructuralObject> map, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
return false;
}
+ @Override
public boolean updateRange(ReadGraph g, IForwardMapping<StructuralResource, IStructuralObject> map, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
return false;
}
updateRange(graph, mapping, domainElement, rangeElement);
}
+ @Override
+ public boolean checkChanges(ReadGraph g, IForwardMapping<StructuralResource, IStructuralObject> map,
+ StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
+ boolean updated = false;
+ for(IBidirectionalMappingRule<StructuralResource, IStructuralObject> rule : rules)
+ updated |= rule.checkChanges(g, map, domainElement, rangeElement);
+ return updated;
+ }
+
public boolean updateDomain(WriteGraph g, IBackwardMapping<StructuralResource,IStructuralObject> map, StructuralResource domainElement, IStructuralObject rangeElement) throws MappingException {
if(LOGGER.isTraceEnabled())
try {