package org.simantics.modeling.ui.componentTypeEditor;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.simantics.Simantics;
+import org.simantics.databoard.Bindings;
import org.simantics.databoard.type.NumberType;
import org.simantics.databoard.units.internal.library.UnitLibrary;
import org.simantics.databoard.util.Limit;
import org.simantics.databoard.util.RangeException;
import org.simantics.db.RequestProcessor;
import org.simantics.db.Resource;
+import org.simantics.db.Statement;
import org.simantics.db.WriteGraph;
import org.simantics.db.common.NamedResource;
+import org.simantics.db.common.request.IndexRoot;
import org.simantics.db.common.request.WriteRequest;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.function.DbConsumer;
+import org.simantics.db.layer0.QueryIndexUtils;
+import org.simantics.db.layer0.util.Layer0Utils;
import org.simantics.layer0.Layer0;
import org.simantics.modeling.userComponent.ComponentTypeCommands;
import org.simantics.scl.runtime.function.Function2;
import org.simantics.scl.runtime.function.Function4;
+import org.simantics.utils.datastructures.Pair;
import org.simantics.utils.threads.ThreadUtils;
import org.simantics.utils.ui.ErrorLogger;
if (propertyInfo.immutable)
return;
+
Simantics.getSession().async(new WriteRequest() {
@Override
public void perform(WriteGraph graph)
throws DatabaseException {
graph.markUndoPoint();
- ComponentTypeCommands.editType(graph, componentType, propertyInfo.resource, convertDefaultValue, newValue);
+
+ String newValue2 = newValue;
+
+ Resource possibleGraphType = null;
+ Resource root = graph.syncRequest(new IndexRoot(componentType));
+
+ Resource L0Res = graph.getResource("http://www.simantics.org/Layer0-1.1");
+ Layer0 L0 = Layer0.getInstance(graph);
+
+ Collection<Resource> graphTypes1 = QueryIndexUtils.searchByTypeAndName(graph, L0Res, L0.ValueType, newValue);
+ Collection<Resource> graphTypes2 = QueryIndexUtils.searchByTypeAndName(graph, root, L0.ValueType, newValue);
+
+ Collection<Resource> graphTypes = new HashSet<>(graphTypes1);
+ graphTypes.addAll(graphTypes2);
+
+ Set<Pair<Resource, String>> candidates = new HashSet<>();
+ for (Resource graphType : graphTypes) {
+ Collection<Statement> stms = graph.getAssertedStatements(graphType, L0.HasValueType);
+ if(stms.size() == 1) {
+ // Only accept valueType if it asserts HasValueType with the same name
+ String hasValueType = graph.getValue(stms.iterator().next().getObject(), Bindings.STRING);
+ if (hasValueType.equals(newValue)) {
+ candidates.add(new Pair<>(graphType, hasValueType));
+ }
+ }
+ }
+
+ // We support only graph types with unique name at this point. Later we could implement UI to let the user to select from multiple graph types.
+ if (candidates.size() == 1) {
+ Pair<Resource, String> result = candidates.iterator().next();
+ possibleGraphType = result.first;
+ newValue2 = result.second;
+ }
+
+ ComponentTypeCommands.editType(graph, componentType, propertyInfo.resource, convertDefaultValue, newValue2, possibleGraphType);
if (range != null) ComponentTypeCommands.setRange(graph, componentType, propertyInfo.resource, range);
}
});
*******************************************************************************/
package org.simantics.modeling.userComponent;
+import java.util.Collections;
import java.util.Map;
import org.simantics.databoard.Bindings;
import org.simantics.db.Statement;
import org.simantics.db.WriteGraph;
import org.simantics.db.common.CommentMetadata;
+import org.simantics.db.common.request.EnumerationMap;
+import org.simantics.db.common.request.IsEnumeratedValue;
import org.simantics.db.common.request.UnaryRead;
import org.simantics.db.common.utils.NameUtils;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.exception.ServiceException;
-import org.simantics.db.layer0.request.ModelInstances;
+import org.simantics.db.layer0.QueryIndexUtils;
import org.simantics.db.layer0.util.Layer0Utils;
import org.simantics.layer0.Layer0;
import org.simantics.modeling.ModelingResources;
import org.simantics.selectionview.SelectionViewResources;
import org.simantics.structural.stubs.StructuralResource2;
import org.simantics.structural2.utils.StructuralUtils;
+import org.simantics.utils.strings.AlphanumComparator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public static void setRequiredType(WriteGraph g, Resource componentType, Resource property,
String requiredType) throws DatabaseException {
+ setRequiredType(g, componentType, property, requiredType, null);
+ }
+
+ public static void setRequiredType(WriteGraph g, Resource componentType, Resource property,
+ String requiredType, Resource possibleType) throws DatabaseException {
Layer0 L0 = Layer0.getInstance(g);
g.claimLiteral(property, L0.RequiresValueType, requiredType);
}
}
+ // We assert the range of the property only if we are given a dedicated graph value type
+ if(g.hasStatement(property, L0.HasRange))
+ g.deny(property, L0.HasRange);
+
+ if(possibleType != null) {
+ // We have a dedicated graph type for this SCL value type
+ if(g.hasStatement(possibleType, L0.Enumeration)) {
+ // This value type is an enumeration - let's constrain the range of this predicate to match the enumeration type only
+ g.claim(property, L0.HasRange, possibleType);
+ }
+ }
+
CommentMetadata cm = g.getMetadata(CommentMetadata.class);
g.addMetadata(cm.add("Set required type "+ requiredType + " for component/annotation " + property));
}
public static void editType(WriteGraph graph, Resource componentType, Resource property, boolean convertDefaultValue, String newValue) throws DatabaseException {
- ComponentTypeCommands.setRequiredType(graph, componentType, property, newValue);
+ editType(graph, componentType, property, convertDefaultValue, newValue, null);
+ }
+
+ public static void editType(WriteGraph graph, Resource componentType, Resource property, boolean convertDefaultValue, String newValue, Resource possibleType) throws DatabaseException {
+ ComponentTypeCommands.setRequiredType(graph, componentType, property, newValue, possibleType);
if (convertDefaultValue) {
- ComponentTypeCommands.convertDefaultValue(graph, componentType, property, newValue);
- Map<String, Resource> instances = graph.sync(new ModelInstances(componentType, componentType));
- for(Resource instance : instances.values()) {
- ComponentTypeCommands.convertInstantiatedValue(graph, instance, property, newValue);
+ ComponentTypeCommands.convertDefaultValue(graph, componentType, property, newValue, possibleType);
+ for (Resource indexRoot : Layer0Utils.listIndexRoots(graph)) {
+ for(Resource instance : QueryIndexUtils.searchByTypeShallow(graph, indexRoot, componentType)) {
+ ComponentTypeCommands.convertInstantiatedValue(graph, instance, property, newValue, componentType);
+ }
}
}
}
" in " + NameUtils.getSafeName(g, type) + ".");
return;
}
+
+ Layer0 L0 = Layer0.getInstance(g);
+ Resource range = g.getPossibleObject(relation, L0.HasRange);
+ if (range != null) {
+ if(g.hasStatement(range, L0.Enumeration)) {
+ Map<String,Resource> values = g.syncRequest(new EnumerationMap(range));
+ Resource value = values.get(valueText);
+ if (value != null) {
+ for(Resource assertion : g.getObjects(type, L0.Asserts)) {
+ Resource p = g.getSingleObject(assertion, L0.HasPredicate);
+ if (p.equals(relation)) {
+ g.deny(assertion, L0.HasObject, object);
+ g.claim(assertion, L0.HasObject, value);
+ }
+ }
+ }
+ return;
+ }
+ }
if(valueText.length() > 0 && valueText.charAt(0) == '=') {
String expression = valueText.substring(1);
- Layer0 L0 = Layer0.getInstance(g);
ModelingResources MOD = ModelingResources.getInstance(g);
if(!g.isInstanceOf(object, MOD.SCLValue)) {
Resource assertion = g.getSingleObject(object, L0.HasObjectInverse);
} else {
- Layer0 L0 = Layer0.getInstance(g);
ModelingResources MOD = ModelingResources.getInstance(g);
if(g.isInstanceOf(object, MOD.SCLValue)) {
Resource assertion = g.getSingleObject(object, L0.HasObjectInverse);
public static void convertDefaultValue(WriteGraph g,
Resource type, Resource relation, String newSCLType) throws DatabaseException {
+ convertDefaultValue(g, type, relation, newSCLType, null);
+ }
+
+ private static Resource findAssertionWithPO(ReadGraph graph, Resource possibleType, Resource predicate, Resource object) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ for(Resource assertion : graph.getObjects(possibleType, L0.Asserts)) {
+ Resource p = graph.getSingleObject(assertion, L0.HasPredicate);
+ Resource o = graph.getSingleObject(assertion, L0.HasObject);
+ if(predicate.equals(p) && object.equals(o))
+ return assertion;
+ }
+ return null;
+ }
+
+ public static void convertDefaultValue(WriteGraph g,
+ Resource type, Resource relation, String newSCLType, Resource possibleType) throws DatabaseException {
Resource object = getAssertedObject(g, type, relation);
if(object == null) {
LOGGER.warn("Didn't find assertion for " + NameUtils.getSafeName(g, relation) +
return;
}
+ Layer0 L0 = Layer0.getInstance(g);
+ if(possibleType != null) {
+ if(g.hasStatement(possibleType, L0.Enumeration)) {
+ if(!g.isInstanceOf(object, possibleType)) {
+ Map<String, Resource> enumMap = g.syncRequest(new EnumerationMap(possibleType));
+ String firstKey = Collections.min(enumMap.keySet(), AlphanumComparator.COMPARATOR);
+ Resource defaultValue = enumMap.get(firstKey);
+
+ if (defaultValue != null) {
+ Resource assertion = findAssertionWithPO(g, type, relation, object);
+ if(assertion != null) {
+ g.deny(assertion, L0.HasObject);
+ g.claim(assertion, L0.HasObject, defaultValue);
+ return;
+ } else {
+ Layer0Utils.assert_(g, type, relation, defaultValue);
+ return;
+ }
+ }
+ } else {
+ return;
+ }
+ }
+ }
+
Tuple tuple = getDatatypeValueAndBinding(g, object, newSCLType);
if (tuple == null)
return;
- Layer0 L0 = Layer0.getInstance(g);
+ if(g.sync(new IsEnumeratedValue(object))) {
+ Resource assertion = findAssertionWithPO(g, type, relation, object);
+ object = g.newResource();
+ g.claim(object, L0.InstanceOf, L0.Literal);
+ if(assertion != null) {
+ g.deny(assertion, L0.HasObject);
+ g.claim(assertion, L0.HasObject, object);
+ }
+ }
+
g.claimLiteral(object, L0.HasDataType, L0.DataType, tuple.get(0), Bindings.getBindingUnchecked(Datatype.class));
g.claimLiteral(object, L0.HasValueType, g.<String>getRelatedValue(relation, L0.RequiresValueType, Bindings.STRING), Bindings.STRING);
g.claimValue(object, tuple.get(1), (Binding)tuple.get(2));
}
- public static void convertInstantiatedValue(WriteGraph g, Resource instance, Resource relation, String newSCLType)
+ public static void convertInstantiatedValue(WriteGraph g, Resource instance, Resource relation, String newSCLType) throws DatabaseException {
+ convertInstantiatedValue(g, instance, relation, newSCLType, null);
+ }
+
+ public static void convertInstantiatedValue(WriteGraph g, Resource instance, Resource relation, String newSCLType, Resource possibleType)
throws DatabaseException {
Statement stm = g.getPossibleStatement(instance, relation);
if(stm != null && !stm.isAsserted(instance)) {
+ Layer0 L0 = Layer0.getInstance(g);
Resource object = stm.getObject();
+ if(g.sync(new IsEnumeratedValue(object))) {
+ if(!g.isInstanceOf(object, possibleType)) {
+ g.deny(instance, relation);
+ }
+ return;
+ }
+
// We can only convert literals
- Layer0 L0 = Layer0.getInstance(g);
if(!g.isInstanceOf(object, L0.Literal)) return;
Tuple tuple = getDatatypeValueAndBinding(g, object, newSCLType);
import java.io.IOException;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.Map;
import java.util.function.Consumer;
import org.simantics.Simantics;
import org.simantics.browsing.ui.NodeContext;
import org.simantics.browsing.ui.content.Labeler.DialogModifier;
+import org.simantics.browsing.ui.graph.impl.GetEnumerationValue;
import org.simantics.common.format.Formatter;
import org.simantics.databoard.Bindings;
import org.simantics.databoard.Datatypes;
import org.simantics.ui.selection.WorkbenchSelectionElement;
import org.simantics.ui.selection.WorkbenchSelectionUtils;
import org.simantics.utils.datastructures.collections.CollectionUtils;
+import org.simantics.utils.strings.AlphanumComparator;
import org.simantics.utils.ui.AdaptionUtils;
import org.simantics.utils.ui.ErrorLogger;
import org.simantics.utils.ui.ISelectionUtils;
Resource parameterResource = parameter.getRepresents(graph);
if(graph.sync(new IsEnumeratedValue(parameterResource))) {
Map<String, Resource> map = graph.sync(new InstanceEnumerationMap(parameterResource));
- return new ArrayList<String>(map.keySet());
+ ArrayList<String> values = new ArrayList<>(map.keySet());
+ Collections.sort(values, AlphanumComparator.COMPARATOR);
+ return values;
} else if(graph.isInstanceOf(parameterResource, L0.Boolean)) {
return CollectionUtils.toList("true", "false");
}
value = formatterFunction.apply(property.getValue(graph));
}
}
+
+ Resource possibleValue = context.getParent(graph).getPossibleRepresents(graph);
+ if(possibleValue != null) {
+ if(graph.syncRequest(new IsEnumeratedValue(possibleValue))) {
+ return GetEnumerationValue.getEnumerationValueName(graph, possibleValue);
+ }
+ }
+
if(value == null) {
Variant variant = property.getVariantValue(graph);
String parsedLabel = (String)_value;
Object value = parsedLabel;
+ boolean isEnumeration = false;
+ Resource possibleValue = context.getParent(graph).getPossibleRepresents(graph);
+ if(possibleValue != null) {
+ isEnumeration = graph.syncRequest(new IsEnumeratedValue(possibleValue));
+ }
+
Datatype type = context.getParent(graph).getPossibleDatatype(graph);
- if (type != null) {
+ if (type != null && !isEnumeration) {
Binding binding = Bindings.getBinding(type);