From: Tuukka Lehtonen Date: Tue, 4 Oct 2016 20:07:47 +0000 (+0300) Subject: Sync git svn branch with SVN repository r33303. X-Git-Tag: v1.25.0~76^2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=55ca14c4198659b908aeffc8d16e240b35a75e66 Sync git svn branch with SVN repository r33303. refs #6740 refs #6726 refs #6739 refs #6735 refs #6731 refs #6730 refs #6729 refs #6728 refs #6724 refs #6701 refs #6669 --- diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/EntityInstances.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/EntityInstances.java index b0d71503b..428b96a5d 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/EntityInstances.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/EntityInstances.java @@ -11,8 +11,6 @@ *******************************************************************************/ package org.simantics.db.layer0.adapter.impl; -import gnu.trove.set.hash.THashSet; - import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -30,11 +28,15 @@ import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.adapter.Instances; import org.simantics.db.layer0.genericrelation.IndexQueries; import org.simantics.db.layer0.util.Layer0Utils; +import org.simantics.db.request.ReadExt; +import org.simantics.db.request.RequestFlags; import org.simantics.db.service.CollectionSupport; import org.simantics.layer0.Layer0; import org.simantics.operation.Layer0X; import org.simantics.scl.runtime.function.Function; +import gnu.trove.set.hash.THashSet; + /** * @author Antti Villberg * @author Tuukka Lehtonen @@ -58,7 +60,7 @@ public class EntityInstances implements Instances { * A (cacheable) query to optimize single index queries for immutable * indexes such as ontologies. */ - public static class QueryIndex extends TernaryRead> { + public static class QueryIndex extends TernaryRead> implements ReadExt { public QueryIndex(Resource index, Resource type, String filter) { super(index, type, filter); @@ -125,6 +127,17 @@ public class EntityInstances implements Instances { return "QueryIndex " + parameter + " " + parameter2 + " " + parameter3; } + @Override + public boolean isImmutable(ReadGraph graph) throws DatabaseException { + // TODO Auto-generated method stub + return false; + } + + @Override + public int getType() { + return RequestFlags.IMMEDIATE_UPDATE; + } + } private List findRec(ReadGraph graph, Resource index, String filter, Set visited) throws DatabaseException { diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/OntologiesFromLibrary.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/OntologiesFromLibrary.java index a742b1e48..1b88cbb65 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/OntologiesFromLibrary.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/OntologiesFromLibrary.java @@ -17,23 +17,21 @@ import java.util.List; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.common.request.ResourceRead; import org.simantics.db.exception.DatabaseException; -import org.simantics.db.request.Read; import org.simantics.layer0.Layer0; -public class OntologiesFromLibrary implements Read> { - - Resource library; +public class OntologiesFromLibrary extends ResourceRead> { public OntologiesFromLibrary(Resource library) { - this.library = library; + super(library); } @Override public List perform(ReadGraph graph) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); ArrayList result = new ArrayList(); - for(Resource r : graph.syncRequest(new ObjectsWithType(library, L0.ConsistsOf, L0.Library))) { + for(Resource r : graph.syncRequest(new ObjectsWithType(resource, L0.ConsistsOf, L0.Library))) { if(graph.isInstanceOf(r, L0.Ontology)) result.add(r); else diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/Configuration.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/Configuration.java index 1ec3e5150..584916366 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/Configuration.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/Configuration.java @@ -13,6 +13,7 @@ package org.simantics.db.layer0.request; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.common.request.IndexRoot; import org.simantics.db.common.request.ResourceRead; import org.simantics.db.exception.DatabaseException; import org.simantics.simulation.ontology.SimulationResource; @@ -26,7 +27,7 @@ public class Configuration extends ResourceRead { @Override public Resource perform(ReadGraph graph) throws DatabaseException { - Resource model = graph.syncRequest(new Model(resource)); + Resource model = graph.syncRequest(new IndexRoot(resource)); return graph.getSingleObject(model, SimulationResource.getInstance(graph).HasConfiguration); } diff --git a/bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/Content.java b/bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/Content.java new file mode 100644 index 000000000..ba933d71c --- /dev/null +++ b/bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/Content.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2014, 2016 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 THTH Simantics + * Division Member Component License which accompanies this + * distribution, and is available at + * http://www.simantics.org/legal/sdmcl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.document.server.io; + +public class Content { + private byte[] data; + private String mimeType; + private long lastModified; + + public Content(byte[] data, String mimeType, long lastModified) { + this.data = data; + this.mimeType = mimeType; + this.lastModified = lastModified; + } + + public byte[] getData() { + return data; + } + + public void setData(byte[] data) { + this.data = data; + } + + public String getMimeType() { + return mimeType; + } + + public void setMimeType(String mimeType) { + this.mimeType = mimeType; + } + + public long getLastModified() { + return lastModified; + } + + public void setLastModified(long lastModified) { + this.lastModified = lastModified; + } +} diff --git a/bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/IAbstractRequestInterface.java b/bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/IAbstractRequestInterface.java index c7bd29392..dce45ac05 100644 --- a/bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/IAbstractRequestInterface.java +++ b/bundles/org.simantics.document.server.io/src/org/simantics/document/server/io/IAbstractRequestInterface.java @@ -12,6 +12,7 @@ public interface IAbstractRequestInterface { String requestCSS(String documentURI); int[] requestLoadingIndicatorDelay(String documentURI); Double requestPollingInterval(String documentURI); + Content requestContent(String resourceURI); public CommandResult execute(String id, CommandContext context); public void subscribeDocument(String document, String sessionGUID, IDocumentListener documentListener); public IJSONResult readChanges(String uri, String sessionGUID, int sequenceNumber); diff --git a/bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/NewDocumentFolder.java b/bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/NewDocumentFolder.java new file mode 100644 index 000000000..5a9960482 --- /dev/null +++ b/bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/NewDocumentFolder.java @@ -0,0 +1,52 @@ +package org.simantics.document.ui.actions; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.ActionFactory; +import org.simantics.layer0.Layer0; +import org.simantics.ui.SimanticsUI; + +public class NewDocumentFolder implements ActionFactory { + Resource folderType; + Resource relation; + + public NewDocumentFolder(ReadGraph graph, String folderTypeUri, String relationUri) throws DatabaseException { + folderType = graph.getResource(folderTypeUri); + relation = graph.getResource(relationUri); + } + + @Override + public Runnable create(Object target) { + + if(!(target instanceof Resource)) + return null; + + final Resource resource = (Resource)target; + + return new Runnable() { + @Override + public void run() { + SimanticsUI.getSession().asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + graph.markUndoPoint(); + + Layer0 l0 = Layer0.getInstance(graph); + + String name = NameUtils.findFreshName(graph, "Folder", resource, relation); + Resource folder = graph.newResource(); + graph.claim(folder, l0.InstanceOf, folderType); + graph.claimLiteral(folder, l0.HasName, name); + graph.claim(resource, relation, folder); + } + + }); + } + }; + } +} diff --git a/bundles/org.simantics.fileimport/src/org/simantics/fileimport/SimanticsResourceFileImport.java b/bundles/org.simantics.fileimport/src/org/simantics/fileimport/SimanticsResourceFileImport.java index 70439ef1d..eea85d47d 100644 --- a/bundles/org.simantics.fileimport/src/org/simantics/fileimport/SimanticsResourceFileImport.java +++ b/bundles/org.simantics.fileimport/src/org/simantics/fileimport/SimanticsResourceFileImport.java @@ -32,7 +32,14 @@ public abstract class SimanticsResourceFileImport implements IGenericFileImport final public Optional perform(Path file) throws Exception { Path dropins = Activator.getDropinsFolder(); - Path parts = dropins.relativize(file); + + Path parts; + if (file.toAbsolutePath().toString().startsWith(dropins.toAbsolutePath().toString())) { + parts = dropins.relativize(file); + } else { + parts = file.getFileName(); + } + Resource parent = resolveParent(null, parts); if (parent == null) return Optional.empty(); diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramViewerSelectionProvider.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramViewerSelectionProvider.java index 4317f5979..42c489e24 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramViewerSelectionProvider.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DiagramViewerSelectionProvider.java @@ -18,6 +18,7 @@ import org.simantics.db.layer0.request.PossibleModel; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.Variables; import org.simantics.diagram.Logger; +import org.simantics.diagram.flag.FlagUtil; import org.simantics.diagram.stubs.DiagramResource; import org.simantics.diagram.ui.DiagramModelHints; import org.simantics.diagram.ui.WorkbenchSelectionProvider; @@ -25,6 +26,7 @@ import org.simantics.g2d.diagram.IDiagram; import org.simantics.g2d.element.ElementHints; import org.simantics.g2d.element.ElementUtils; import org.simantics.g2d.element.IElement; +import org.simantics.layer0.Layer0; import org.simantics.modeling.ModelingResources; import org.simantics.ui.SimanticsUI; import org.simantics.ui.selection.AnyResource; @@ -83,8 +85,16 @@ public class DiagramViewerSelectionProvider extends WorkbenchSelectionProvider { //return Variables.getPossibleVariable(graph, resource2); return var; } + + if(graph.isInstanceOf(resource2, DIA.Flag)) { + Layer0 L0 = Layer0.getInstance(graph); + Variable signal = FlagUtil.getPossibleFlagSignal(graph, var, resource2, L0.Entity); + if(signal != null) + return signal; + } return null; + } return var.browsePossible(graph, config); diff --git a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/SingleElementNode.java b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/SingleElementNode.java index c6a8b950e..9220e18fd 100644 --- a/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/SingleElementNode.java +++ b/bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/SingleElementNode.java @@ -144,7 +144,7 @@ public class SingleElementNode extends TransformNode implements InitValueSupport } protected boolean hitTest(MouseEvent event) { - Rectangle2D bounds = super.getBoundsInLocal(false); + Rectangle2D bounds = super.getBoundsInLocal(true); if (bounds == null) return false; Point2D localPos = NodeUtil.worldToLocal(this, event.controlPosition, new Point2D.Double()); diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/elaboration/constraints/ConstraintEnvironment.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/elaboration/constraints/ConstraintEnvironment.java index 82df90007..ba982b1c6 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/elaboration/constraints/ConstraintEnvironment.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/elaboration/constraints/ConstraintEnvironment.java @@ -1,11 +1,7 @@ package org.simantics.scl.compiler.internal.elaboration.constraints; -import org.cojen.classfile.TypeDesc; import org.simantics.scl.compiler.common.exceptions.InternalCompilerError; import org.simantics.scl.compiler.constants.ClassConstant; -import org.simantics.scl.compiler.constants.JavaConstructor; -import org.simantics.scl.compiler.constants.JavaStaticField; -import org.simantics.scl.compiler.constants.JavaStaticMethod; import org.simantics.scl.compiler.constants.StringConstant; import org.simantics.scl.compiler.elaboration.expressions.EApply; import org.simantics.scl.compiler.elaboration.expressions.EConstant; @@ -49,35 +45,7 @@ public class ConstraintEnvironment { } return result; } - - private static final Type SERIALIZABLE_BOOLEAN = Types.pred(Types.SERIALIZABLE, Types.BOOLEAN); - private static final Type SERIALIZABLE_BYTE = Types.pred(Types.SERIALIZABLE, Types.BYTE); - private static final Type SERIALIZABLE_SHORT = Types.pred(Types.SERIALIZABLE, Types.SHORT); - private static final Type SERIALIZABLE_INTEGER = Types.pred(Types.SERIALIZABLE, Types.INTEGER); - private static final Type SERIALIZABLE_LONG = Types.pred(Types.SERIALIZABLE, Types.LONG); - private static final Type SERIALIZABLE_FLOAT = Types.pred(Types.SERIALIZABLE, Types.FLOAT); - private static final Type SERIALIZABLE_DOUBLE = Types.pred(Types.SERIALIZABLE, Types.DOUBLE); - private static final Type SERIALIZABLE_STRING = Types.pred(Types.SERIALIZABLE, Types.STRING); - - private static final Type SERIALIZABLE_BOOLEAN_ARRAY = Types.pred(Types.SERIALIZABLE, Types.BOOLEAN_ARRAY); - private static final Type SERIALIZABLE_BYTE_ARRAY = Types.pred(Types.SERIALIZABLE, Types.BYTE_ARRAY); - private static final Type SERIALIZABLE_SHORT_ARRAY = Types.pred(Types.SERIALIZABLE, Types.SHORT_ARRAY); - private static final Type SERIALIZABLE_INTEGER_ARRAY = Types.pred(Types.SERIALIZABLE, Types.INTEGER_ARRAY); - private static final Type SERIALIZABLE_LONG_ARRAY = Types.pred(Types.SERIALIZABLE, Types.LONG_ARRAY); - private static final Type SERIALIZABLE_FLOAT_ARRAY = Types.pred(Types.SERIALIZABLE, Types.FLOAT_ARRAY); - private static final Type SERIALIZABLE_DOUBLE_ARRAY = Types.pred(Types.SERIALIZABLE, Types.DOUBLE_ARRAY); - - private static final Type SERIALIZABLE_BOOLEAN_VECTOR = Types.pred(Types.SERIALIZABLE, Types.pred(Types.VECTOR, Types.BOOLEAN)); - private static final Type SERIALIZABLE_BYTE_VECTOR = Types.pred(Types.SERIALIZABLE, Types.pred(Types.VECTOR, Types.BYTE)); - private static final Type SERIALIZABLE_SHORT_VECTOR = Types.pred(Types.SERIALIZABLE, Types.pred(Types.VECTOR, Types.SHORT)); - private static final Type SERIALIZABLE_INTEGER_VECTOR = Types.pred(Types.SERIALIZABLE, Types.pred(Types.VECTOR, Types.INTEGER)); - private static final Type SERIALIZABLE_LONG_VECTOR = Types.pred(Types.SERIALIZABLE, Types.pred(Types.VECTOR, Types.LONG)); - private static final Type SERIALIZABLE_FLOAT_VECTOR = Types.pred(Types.SERIALIZABLE, Types.pred(Types.VECTOR, Types.FLOAT)); - private static final Type SERIALIZABLE_DOUBLE_VECTOR = Types.pred(Types.SERIALIZABLE, Types.pred(Types.VECTOR, Types.DOUBLE)); - - private static final Type SERIALIZABLE_TUPLE0 = Types.pred(Types.SERIALIZABLE, Types.tuple()); - private static final Type SERIALIZABLE_VARIANT = Types.pred(Types.SERIALIZABLE, Types.VARIANT); - + public Reduction reduce(TPred constraint) { // VecComp if(constraint.typeClass == Types.VEC_COMP) { @@ -87,145 +55,8 @@ public class ConstraintEnvironment { Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); } // Serializable - if(constraint.typeClass == Types.SERIALIZABLE) { - Type parameter = Types.canonical(constraint.parameters[0]); - if(parameter instanceof TCon) { - TCon con = (TCon)parameter; - if(con == Types.DOUBLE) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "DOUBLE", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/DoubleBinding"), SERIALIZABLE_DOUBLE, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - else if(con == Types.STRING) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "STRING", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/StringBinding"), SERIALIZABLE_STRING, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - else if(con == Types.INTEGER) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "INTEGER", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/IntegerBinding"), SERIALIZABLE_INTEGER, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - else if(con == Types.BOOLEAN) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "BOOLEAN", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/BooleanBinding"), SERIALIZABLE_BOOLEAN, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - else if(con == Types.BYTE) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "BYTE", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ByteBinding"), SERIALIZABLE_BYTE, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - else if(con == Types.FLOAT) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "FLOAT", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/FloatBinding"), SERIALIZABLE_FLOAT, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - else if(con == Types.LONG) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "LONG", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/LongBinding"), SERIALIZABLE_LONG, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - else if(con == Types.tupleConstructor(0)) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "VOID", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/Binding"), SERIALIZABLE_TUPLE0, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - else if(con == Types.VARIANT) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "VARIANT", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/VariantBinding"), SERIALIZABLE_VARIANT, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - else if(con == Types.DOUBLE_ARRAY) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "DOUBLE_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_DOUBLE_ARRAY, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - else if(con == Types.INTEGER_ARRAY) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "INTEGER_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_INTEGER_ARRAY, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - else if(con == Types.BOOLEAN_ARRAY) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "BOOLEAN_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_BOOLEAN_ARRAY, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - else if(con == Types.BYTE_ARRAY) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "BYTE_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_BYTE_ARRAY, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - else if(con == Types.FLOAT_ARRAY) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "FLOAT_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_FLOAT_ARRAY, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - else if(con == Types.LONG_ARRAY) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "LONG_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_LONG_ARRAY, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - } - else if(parameter instanceof TApply) { - TApply apply1 = (TApply)parameter; - Type f1 = Types.canonical(apply1.function); - Type p1 = Types.canonical(apply1.parameter); - if(f1 instanceof TCon) { - TCon con = (TCon)f1; - if(con == Types.LIST) - return new Reduction( - new ELiteral(new JavaConstructor("org/simantics/databoard/binding/impl/ArrayListBinding", Types.NO_EFFECTS, - Types.pred(Types.SERIALIZABLE, Types.list(p1)), Types.pred(Types.SERIALIZABLE, p1))), - Type.EMPTY_ARRAY, new TPred[] { Types.pred(Types.SERIALIZABLE, p1) }); - else if(con == Types.MAYBE) - return new Reduction( - new ELiteral(new JavaConstructor("org/simantics/databoard/binding/impl/OptionalBindingDefault", Types.NO_EFFECTS, - Types.pred(Types.SERIALIZABLE, Types.apply(Types.MAYBE, p1)), Types.pred(Types.SERIALIZABLE, p1))), - Type.EMPTY_ARRAY, new TPred[] { Types.pred(Types.SERIALIZABLE, p1) }); - else if(con == Types.VECTOR) { - if (p1 == Types.DOUBLE) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "DOUBLE_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_DOUBLE_VECTOR, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - else if(p1 == Types.INTEGER) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "INTEGER_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_INTEGER_VECTOR, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - else if(p1 == Types.BOOLEAN) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "BOOLEAN_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_BOOLEAN_VECTOR, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - else if(p1 == Types.BYTE) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "BYTE_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_BYTE_VECTOR, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - else if(p1 == Types.FLOAT) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "FLOAT_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_FLOAT_VECTOR, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - else if(p1 == Types.LONG) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "LONG_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_LONG_VECTOR, -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - else - return new Reduction( - new ELiteral(new JavaStaticMethod("org/simantics/databoard/Bindings", "getArrayBinding", Types.NO_EFFECTS, - Types.pred(Types.SERIALIZABLE, Types.pred(Types.VECTOR, p1)), Types.pred(Types.SERIALIZABLE, p1))), - Type.EMPTY_ARRAY, new TPred[] { Types.pred(Types.SERIALIZABLE, p1) }); - } - } - else if(f1 instanceof TApply) { - TApply apply2 = (TApply)parameter; - Type f2 = Types.canonical(apply2.function); - Type p2 = Types.canonical(apply2.parameter); - if(f2 instanceof TCon) { - TCon con = (TCon)f2; - /*if(con == Types.tupleConstructor(2)) { - return new Reduction( - new EConstant(Builtins.TUPLE_CONSTRUCTORS[2]new JavaConstructor("org/simantics/databoard/binding/impl/ArrayListBinding", Types.NO_EFFECTS, - Types.pred(Types.SERIALIZABLE, p1), Types.pred(Types.SERIALIZABLE, Types.list(p1)))), - Type.EMPTY_ARRAY, - new TPred[] { Types.pred(Types.SERIALIZABLE, p2), Types.pred(Types.SERIALIZABLE, p1) }); - }*/ - } - } - } - - // Default to a binding as an ObjectVariantBinding - // This can be applied only if the type is ground type (i.e. does not contain type variables), - // because otherwise the Serializable instance could be provided as a parameter to the function - if(parameter.isGround()) - return new Reduction( - new ELiteral(new JavaStaticField("org/simantics/databoard/Bindings", "OBJECT", Types.NO_EFFECTS, - TypeDesc.forClass("org/simantics/databoard/binding/VariantBinding"), Types.pred(Types.SERIALIZABLE, parameter), -1)), - Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); - } + if(constraint.typeClass == Types.SERIALIZABLE) + return ReduceSerializable.reduceSerializable(constraint.parameters[0]); // Typeable else if(constraint.typeClass == Types.TYPEABLE) { diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/elaboration/constraints/ConstraintSolver.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/elaboration/constraints/ConstraintSolver.java index dd91efb70..15ac6d664 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/elaboration/constraints/ConstraintSolver.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/elaboration/constraints/ConstraintSolver.java @@ -155,7 +155,7 @@ public class ConstraintSolver { } for(Constraint constraint : group) { Reduction reduction = environment.reduce(constraint.constraint); - if(reduction.parameters.length > 0) + if(reduction.demands.length > 0) throw new InternalCompilerError(); constraint.setGenerator(Constraint.STATE_HAS_INSTANCE, reduction.generator, reduction.parameters); diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/elaboration/constraints/ReduceSerializable.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/elaboration/constraints/ReduceSerializable.java new file mode 100644 index 000000000..ea0276e35 --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/elaboration/constraints/ReduceSerializable.java @@ -0,0 +1,147 @@ +package org.simantics.scl.compiler.internal.elaboration.constraints; + +import org.cojen.classfile.TypeDesc; +import org.simantics.scl.compiler.constants.ClassConstant; +import org.simantics.scl.compiler.constants.Constant; +import org.simantics.scl.compiler.constants.JavaConstructor; +import org.simantics.scl.compiler.constants.JavaStaticField; +import org.simantics.scl.compiler.constants.JavaStaticMethod; +import org.simantics.scl.compiler.elaboration.expressions.EApply; +import org.simantics.scl.compiler.elaboration.expressions.EApplyType; +import org.simantics.scl.compiler.elaboration.expressions.ELiteral; +import org.simantics.scl.compiler.elaboration.expressions.Expression; +import org.simantics.scl.compiler.elaboration.expressions.Expressions; +import org.simantics.scl.compiler.types.TCon; +import org.simantics.scl.compiler.types.TPred; +import org.simantics.scl.compiler.types.TVar; +import org.simantics.scl.compiler.types.Type; +import org.simantics.scl.compiler.types.Types; +import org.simantics.scl.compiler.types.kinds.Kinds; +import org.simantics.scl.compiler.types.util.MultiApply; + +import gnu.trove.map.hash.THashMap; + +public class ReduceSerializable { + + private static final TVar A = Types.var(Kinds.STAR); + private static final TVar B = Types.var(Kinds.STAR); + + private static final Type SERIALIZABLE_BOOLEAN = Types.pred(Types.SERIALIZABLE, Types.BOOLEAN); + private static final Type SERIALIZABLE_BYTE = Types.pred(Types.SERIALIZABLE, Types.BYTE); + private static final Type SERIALIZABLE_INTEGER = Types.pred(Types.SERIALIZABLE, Types.INTEGER); + private static final Type SERIALIZABLE_LONG = Types.pred(Types.SERIALIZABLE, Types.LONG); + private static final Type SERIALIZABLE_FLOAT = Types.pred(Types.SERIALIZABLE, Types.FLOAT); + private static final Type SERIALIZABLE_DOUBLE = Types.pred(Types.SERIALIZABLE, Types.DOUBLE); + private static final Type SERIALIZABLE_STRING = Types.pred(Types.SERIALIZABLE, Types.STRING); + + private static final Type SERIALIZABLE_BOOLEAN_ARRAY = Types.pred(Types.SERIALIZABLE, Types.BOOLEAN_ARRAY); + private static final Type SERIALIZABLE_BYTE_ARRAY = Types.pred(Types.SERIALIZABLE, Types.BYTE_ARRAY); + private static final Type SERIALIZABLE_INTEGER_ARRAY = Types.pred(Types.SERIALIZABLE, Types.INTEGER_ARRAY); + private static final Type SERIALIZABLE_LONG_ARRAY = Types.pred(Types.SERIALIZABLE, Types.LONG_ARRAY); + private static final Type SERIALIZABLE_FLOAT_ARRAY = Types.pred(Types.SERIALIZABLE, Types.FLOAT_ARRAY); + private static final Type SERIALIZABLE_DOUBLE_ARRAY = Types.pred(Types.SERIALIZABLE, Types.DOUBLE_ARRAY); + + private static final Type SERIALIZABLE_BOOLEAN_VECTOR = Types.pred(Types.SERIALIZABLE, Types.pred(Types.VECTOR, Types.BOOLEAN)); + private static final Type SERIALIZABLE_BYTE_VECTOR = Types.pred(Types.SERIALIZABLE, Types.pred(Types.VECTOR, Types.BYTE)); + private static final Type SERIALIZABLE_INTEGER_VECTOR = Types.pred(Types.SERIALIZABLE, Types.pred(Types.VECTOR, Types.INTEGER)); + private static final Type SERIALIZABLE_LONG_VECTOR = Types.pred(Types.SERIALIZABLE, Types.pred(Types.VECTOR, Types.LONG)); + private static final Type SERIALIZABLE_FLOAT_VECTOR = Types.pred(Types.SERIALIZABLE, Types.pred(Types.VECTOR, Types.FLOAT)); + private static final Type SERIALIZABLE_DOUBLE_VECTOR = Types.pred(Types.SERIALIZABLE, Types.pred(Types.VECTOR, Types.DOUBLE)); + + private static final Type SERIALIZABLE_TUPLE0 = Types.pred(Types.SERIALIZABLE, Types.tuple()); + private static final Type SERIALIZABLE_VARIANT = Types.pred(Types.SERIALIZABLE, Types.VARIANT); + + private static final TCon MLIST = Types.con("MList", "T"); + private static final TCon MAP = Types.con("Map", "T"); + private static final TCon MMAP = Types.con("MMap", "T"); + + private static Constant GET_BINDING = + new JavaStaticMethod("org/simantics/databoard/Bindings", "getBinding", Types.NO_EFFECTS, + Types.pred(Types.SERIALIZABLE, A), Types.pred(Types.VEC_COMP, A)); + + private static final THashMap BINDING_CONSTANTS0 = new THashMap(); + static { + BINDING_CONSTANTS0.put(Types.DOUBLE, new JavaStaticField("org/simantics/databoard/Bindings", "DOUBLE", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/DoubleBinding"), SERIALIZABLE_DOUBLE, -1)); + BINDING_CONSTANTS0.put(Types.STRING, new JavaStaticField("org/simantics/databoard/Bindings", "STRING", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/StringBinding"), SERIALIZABLE_STRING, -1)); + BINDING_CONSTANTS0.put(Types.INTEGER, new JavaStaticField("org/simantics/databoard/Bindings", "INTEGER", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/IntegerBinding"), SERIALIZABLE_INTEGER, -1)); + BINDING_CONSTANTS0.put(Types.BOOLEAN, new JavaStaticField("org/simantics/databoard/Bindings", "BOOLEAN", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/BooleanBinding"), SERIALIZABLE_BOOLEAN, -1)); + BINDING_CONSTANTS0.put(Types.BYTE, new JavaStaticField("org/simantics/databoard/Bindings", "BYTE", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ByteBinding"), SERIALIZABLE_BYTE, -1)); + BINDING_CONSTANTS0.put(Types.FLOAT, new JavaStaticField("org/simantics/databoard/Bindings", "FLOAT", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/FloatBinding"), SERIALIZABLE_FLOAT, -1)); + BINDING_CONSTANTS0.put(Types.LONG, new JavaStaticField("org/simantics/databoard/Bindings", "LONG", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/LongBinding"), SERIALIZABLE_LONG, -1)); + BINDING_CONSTANTS0.put(Types.UNIT, new JavaStaticField("org/simantics/databoard/Bindings", "VOID", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/Binding"), SERIALIZABLE_TUPLE0, -1)); + BINDING_CONSTANTS0.put(Types.VARIANT, new JavaStaticField("org/simantics/databoard/Bindings", "VARIANT", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/VariantBinding"), SERIALIZABLE_VARIANT, -1)); + BINDING_CONSTANTS0.put(Types.DOUBLE_ARRAY, new JavaStaticField("org/simantics/databoard/Bindings", "DOUBLE_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_DOUBLE_ARRAY, -1)); + BINDING_CONSTANTS0.put(Types.INTEGER_ARRAY, new JavaStaticField("org/simantics/databoard/Bindings", "INTEGER_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_INTEGER_ARRAY, -1)); + BINDING_CONSTANTS0.put(Types.BOOLEAN_ARRAY, new JavaStaticField("org/simantics/databoard/Bindings", "BOOLEAN_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_BOOLEAN_ARRAY, -1)); + BINDING_CONSTANTS0.put(Types.BYTE_ARRAY, new JavaStaticField("org/simantics/databoard/Bindings", "BYTE_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_BYTE_ARRAY, -1)); + BINDING_CONSTANTS0.put(Types.FLOAT_ARRAY, new JavaStaticField("org/simantics/databoard/Bindings", "FLOAT_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_FLOAT_ARRAY, -1)); + BINDING_CONSTANTS0.put(Types.LONG_ARRAY, new JavaStaticField("org/simantics/databoard/Bindings", "LONG_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_LONG_ARRAY, -1)); + } + + private static final THashMap BINDING_CONSTANTS1 = new THashMap(); + static { + BINDING_CONSTANTS1.put(Types.LIST, new JavaConstructor("org/simantics/databoard/binding/impl/ArrayListBinding", Types.NO_EFFECTS, Types.pred(Types.SERIALIZABLE, Types.list(A)), Types.pred(Types.SERIALIZABLE, A))); + BINDING_CONSTANTS1.put(Types.MAYBE, new JavaConstructor("org/simantics/databoard/binding/impl/OptionalBindingDefault", Types.NO_EFFECTS, Types.pred(Types.SERIALIZABLE, Types.apply(Types.MAYBE, A)), Types.pred(Types.SERIALIZABLE, A))); + BINDING_CONSTANTS1.put(Types.VECTOR, new JavaStaticMethod("org/simantics/databoard/Bindings", "getArrayBinding", Types.NO_EFFECTS, Types.pred(Types.SERIALIZABLE, Types.pred(Types.VECTOR, A)), Types.pred(Types.SERIALIZABLE, A))); + BINDING_CONSTANTS1.put(MLIST, new JavaConstructor("org/simantics/databoard/binding/impl/ArrayListBinding", Types.NO_EFFECTS, Types.pred(Types.SERIALIZABLE, Types.apply(MLIST, A)), Types.pred(Types.SERIALIZABLE, A))); + } + + private static final THashMap VECTOR_BINDING_CONSTANTS = new THashMap(); + static { + VECTOR_BINDING_CONSTANTS.put(Types.DOUBLE, new JavaStaticField("org/simantics/databoard/Bindings", "DOUBLE_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_DOUBLE_VECTOR, -1)); + VECTOR_BINDING_CONSTANTS.put(Types.INTEGER, new JavaStaticField("org/simantics/databoard/Bindings", "INTEGER_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_INTEGER_VECTOR, -1)); + VECTOR_BINDING_CONSTANTS.put(Types.BOOLEAN, new JavaStaticField("org/simantics/databoard/Bindings", "BOOLEAN_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_BOOLEAN_VECTOR, -1)); + VECTOR_BINDING_CONSTANTS.put(Types.BYTE, new JavaStaticField("org/simantics/databoard/Bindings", "BYTE_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_BYTE_VECTOR, -1)); + VECTOR_BINDING_CONSTANTS.put(Types.FLOAT, new JavaStaticField("org/simantics/databoard/Bindings", "FLOAT_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_FLOAT_VECTOR, -1)); + VECTOR_BINDING_CONSTANTS.put(Types.LONG, new JavaStaticField("org/simantics/databoard/Bindings", "LONG_ARRAY", Types.NO_EFFECTS, TypeDesc.forClass("org/simantics/databoard/binding/ArrayBinding"), SERIALIZABLE_LONG_VECTOR, -1)); + } + + private static final THashMap BINDING_CONSTANTS2 = new THashMap(); + static { + BINDING_CONSTANTS2.put(MAP, new JavaConstructor("org/simantics/databoard/binding/impl/HashMapBinding", Types.NO_EFFECTS, Types.pred(Types.SERIALIZABLE, Types.apply(MAP, A, B)), Types.pred(Types.SERIALIZABLE, A), Types.pred(Types.SERIALIZABLE, B))); + BINDING_CONSTANTS2.put(MMAP, new JavaConstructor("org/simantics/databoard/binding/impl/HashMapBinding", Types.NO_EFFECTS, Types.pred(Types.SERIALIZABLE, Types.apply(MMAP, A, B)), Types.pred(Types.SERIALIZABLE, A), Types.pred(Types.SERIALIZABLE, B))); + } + + public static Reduction reduceSerializable(Type parameter) { + MultiApply apply = Types.matchApply(parameter); + if(!(apply.constructor instanceof TCon)) + return null; + + switch(apply.parameters.length) { + case 0: { + Constant constant = BINDING_CONSTANTS0.get(apply.constructor); + if(constant != null) + return new Reduction(new ELiteral(constant), Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); + } break; + case 1: { + Type parameter0 = apply.parameters[0]; + if(apply.constructor == Types.VECTOR) { + Constant vecConstant = VECTOR_BINDING_CONSTANTS.get(parameter0); + if(vecConstant != null) + return new Reduction(new ELiteral(vecConstant), Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); + } + Constant constant = BINDING_CONSTANTS1.get(apply.constructor); + if(constant != null) + return new Reduction(new EApplyType(new ELiteral(constant), parameter0), Type.EMPTY_ARRAY, new TPred[] { Types.pred(Types.SERIALIZABLE, parameter0) }); + } break; + case 2: { + Constant constant = BINDING_CONSTANTS2.get(apply.constructor); + if(constant != null) + return new Reduction(Expressions.applyTypes(new ELiteral(constant), apply.parameters), + Type.EMPTY_ARRAY, + new TPred[] { Types.pred(Types.SERIALIZABLE, apply.parameters[0]), Types.pred(Types.SERIALIZABLE, apply.parameters[1]) }); + } break; + } + + // Default to a binding based on the class of the type. + // This can be applied only if the type is ground type (i.e. does not contain type variables), + // because otherwise the Serializable instance could be provided as a parameter to the function + if(parameter.isGround()) { + Expression bindingGenerator = new EApplyType(new ELiteral(GET_BINDING), parameter); + Expression bindingExpression = new EApply(bindingGenerator, new ELiteral(new ClassConstant(Types.pred(Types.VEC_COMP, parameter), parameter))); + return new Reduction(bindingExpression, Type.EMPTY_ARRAY, TPred.EMPTY_ARRAY); + } + + return null; + } +} diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/Types.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/Types.java index 5e8c54887..e6454fc0c 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/Types.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/types/Types.java @@ -556,7 +556,15 @@ public class Types { parameters.add(Types.canonical(apply.parameter)); type = canonical(apply.function); } - return new MultiApply(type, parameters.toArray(new Type[parameters.size()])); + Type[] parametersArray; + if(parameters.isEmpty()) + parametersArray = Type.EMPTY_ARRAY; + else { + parametersArray = new Type[parameters.size()]; + for(int i=0,j=parametersArray.length-1;i { if(rightVal==null) rightResult = rightTemp.toString(); else rightResult = rightVal; } - - if ((leftResult instanceof String) || (rightResult instanceof String)) { + if ((leftResult instanceof String) && (rightResult instanceof String)) { String leftString = (leftResult.toString()).toLowerCase(); String rightString = (rightResult.toString()).toLowerCase(); if("<".equals(astRelation.op.trim())) return leftString.compareTo(rightString) < 0; else if(">".equals(astRelation.op.trim())) return leftString.compareTo(rightString) > 0; - // empty string should equal zero (TODO: this is a hack, the proper fix should be somewhere earlier so other cases would work as well) - else if("=".equals(astRelation.op.trim())) return leftString.compareTo(rightString) == 0 || emptyAndZero(leftString, rightString) || emptyAndZero(rightString, leftString); + else if("=".equals(astRelation.op.trim())) return leftString.compareTo(rightString) == 0; else if("<>".equals(astRelation.op.trim())) return leftString.compareTo(rightString) != 0 ; else if("<=".equals(astRelation.op.trim())) return leftString.compareTo(rightString) <= 0 ; else if(">=".equals(astRelation.op.trim())) return leftString.compareTo(rightString) >= 0 ; else throw new IllegalStateException(); } else { - Number leftNumber = SpreadsheetGraphUtils.asNumber(leftResult); - Number rightNumber = SpreadsheetGraphUtils.asNumber(rightResult); + Number leftNumber = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(leftResult); + Number rightNumber = SpreadsheetGraphUtils.asDoubleWhereEmptyStringIsZero(rightResult); + if(leftNumber==null || rightNumber==null) return false; if("<".equals(astRelation.op.trim())) return leftNumber.doubleValue() < rightNumber.doubleValue(); else if(">".equals(astRelation.op.trim())) return leftNumber.doubleValue() > rightNumber.doubleValue(); else if("=".equals(astRelation.op.trim())) return leftNumber.doubleValue() == rightNumber.doubleValue(); @@ -208,15 +207,6 @@ public class CellValueVisitor implements AstValueVisitor { } } - private static boolean emptyAndZero(String a, String b) { - try { - return a.isEmpty() && (Double.parseDouble(b) == 0); - } - catch (NumberFormatException e) { - return false; - } - } - Object leftValueWithPrefix(Object result, AstValue value, String prefix, boolean forceNumber) { if(result == null) { Object obj = value.accept(this); diff --git a/bundles/org.simantics.tests.modelled.ontology/graph.tg b/bundles/org.simantics.tests.modelled.ontology/graph.tg index 6ffc03aa5..b80a9c8cd 100644 Binary files a/bundles/org.simantics.tests.modelled.ontology/graph.tg and b/bundles/org.simantics.tests.modelled.ontology/graph.tg differ diff --git a/bundles/org.simantics.tests.modelled.ui.ontology/graph.tg b/bundles/org.simantics.tests.modelled.ui.ontology/graph.tg index c8b53e56c..862131139 100644 Binary files a/bundles/org.simantics.tests.modelled.ui.ontology/graph.tg and b/bundles/org.simantics.tests.modelled.ui.ontology/graph.tg differ diff --git a/bundles/org.simantics.tests.modelled.ui.ontology/graph/TestsUI.pgraph b/bundles/org.simantics.tests.modelled.ui.ontology/graph/TestsUI.pgraph index 6f7d89c2d..b51213dc8 100644 --- a/bundles/org.simantics.tests.modelled.ui.ontology/graph/TestsUI.pgraph +++ b/bundles/org.simantics.tests.modelled.ui.ontology/graph/TestsUI.pgraph @@ -29,9 +29,9 @@ MBC = TestsUI.BrowseContext : VP.BrowseContext MBC @VP.namedRelationChildRule TestsUI.Contributions.STSSuites L0.Entity L0.ConsistsOf TESTS.STSSuite - @VP.namedConstantImageRule TestsUI.Contributions.ModuleImage TESTS.STSSuite TestsUI.testSuiteImage + @VP.namedConstantImageRule TestsUI.Contributions.SuiteImage TESTS.STSSuite TestsUI.testSuiteImage @VP.namedRelationChildRule TestsUI.Contributions.STSTests L0.Entity L0.ConsistsOf TESTS.STSTest - @VP.namedConstantImageRule TestsUI.Contributions.ModuleImage TESTS.STSTest TestsUI.testImage + @VP.namedConstantImageRule TestsUI.Contributions.TestImage TESTS.STSTest TestsUI.testImage MAC = TestsUI.ModelingActionContext : VP.BrowseContext VP.BrowseContext.IsIncludedIn PROJECT.ProjectActionContext diff --git a/bundles/org.simantics.tests.modelled.ui.ontology/src/org/simantics/tests/modelled/ui/ontology/TestsUIResource.java b/bundles/org.simantics.tests.modelled.ui.ontology/src/org/simantics/tests/modelled/ui/ontology/TestsUIResource.java index 4aa14a70a..897d42c9d 100644 --- a/bundles/org.simantics.tests.modelled.ui.ontology/src/org/simantics/tests/modelled/ui/ontology/TestsUIResource.java +++ b/bundles/org.simantics.tests.modelled.ui.ontology/src/org/simantics/tests/modelled/ui/ontology/TestsUIResource.java @@ -12,11 +12,12 @@ public class TestsUIResource { public final Resource BrowseContext; public final Resource Contributions; - public final Resource Contributions_ModuleImage; public final Resource Contributions_NewSTSSuite; public final Resource Contributions_NewSTSTest; public final Resource Contributions_STSSuites; public final Resource Contributions_STSTests; + public final Resource Contributions_SuiteImage; + public final Resource Contributions_TestImage; public final Resource ModelingActionContext; public final Resource ModelingActionContext_Actions; public final Resource ModelingActionContext_Actions_NewSTSSuite; @@ -29,11 +30,12 @@ public class TestsUIResource { public static class URIs { public static final String BrowseContext = "http://www.simantics.org/TestsUI-1.0/BrowseContext"; public static final String Contributions = "http://www.simantics.org/TestsUI-1.0/Contributions"; - public static final String Contributions_ModuleImage = "http://www.simantics.org/TestsUI-1.0/Contributions/ModuleImage"; public static final String Contributions_NewSTSSuite = "http://www.simantics.org/TestsUI-1.0/Contributions/NewSTSSuite"; public static final String Contributions_NewSTSTest = "http://www.simantics.org/TestsUI-1.0/Contributions/NewSTSTest"; public static final String Contributions_STSSuites = "http://www.simantics.org/TestsUI-1.0/Contributions/STSSuites"; public static final String Contributions_STSTests = "http://www.simantics.org/TestsUI-1.0/Contributions/STSTests"; + public static final String Contributions_SuiteImage = "http://www.simantics.org/TestsUI-1.0/Contributions/SuiteImage"; + public static final String Contributions_TestImage = "http://www.simantics.org/TestsUI-1.0/Contributions/TestImage"; public static final String ModelingActionContext = "http://www.simantics.org/TestsUI-1.0/ModelingActionContext"; public static final String ModelingActionContext_Actions = "http://www.simantics.org/TestsUI-1.0/ModelingActionContext/Actions"; public static final String ModelingActionContext_Actions_NewSTSSuite = "http://www.simantics.org/TestsUI-1.0/ModelingActionContext/Actions/NewSTSSuite"; @@ -56,11 +58,12 @@ public class TestsUIResource { public TestsUIResource(ReadGraph graph) { BrowseContext = getResourceOrNull(graph, URIs.BrowseContext); Contributions = getResourceOrNull(graph, URIs.Contributions); - Contributions_ModuleImage = getResourceOrNull(graph, URIs.Contributions_ModuleImage); Contributions_NewSTSSuite = getResourceOrNull(graph, URIs.Contributions_NewSTSSuite); Contributions_NewSTSTest = getResourceOrNull(graph, URIs.Contributions_NewSTSTest); Contributions_STSSuites = getResourceOrNull(graph, URIs.Contributions_STSSuites); Contributions_STSTests = getResourceOrNull(graph, URIs.Contributions_STSTests); + Contributions_SuiteImage = getResourceOrNull(graph, URIs.Contributions_SuiteImage); + Contributions_TestImage = getResourceOrNull(graph, URIs.Contributions_TestImage); ModelingActionContext = getResourceOrNull(graph, URIs.ModelingActionContext); ModelingActionContext_Actions = getResourceOrNull(graph, URIs.ModelingActionContext_Actions); ModelingActionContext_Actions_NewSTSSuite = getResourceOrNull(graph, URIs.ModelingActionContext_Actions_NewSTSSuite); diff --git a/bundles/org.simantics.utils/src/org/simantics/utils/format/FormattingUtil.java b/bundles/org.simantics.utils/src/org/simantics/utils/format/FormattingUtil.java index ae17537f0..a1a525d80 100644 --- a/bundles/org.simantics.utils/src/org/simantics/utils/format/FormattingUtil.java +++ b/bundles/org.simantics.utils/src/org/simantics/utils/format/FormattingUtil.java @@ -25,6 +25,8 @@ public class FormattingUtil { private int floatDigits; private int doubleDigits; + private double lowLimit; + private double highLimit; private DecimalFormatSymbols decimalFormatSymbols; private DecimalFormat flow; @@ -38,20 +40,28 @@ public class FormattingUtil { public FormattingUtil(int floatDigits, int doubleDigits) { this(floatDigits, doubleDigits, Locale.getDefault()); } - + public FormattingUtil(int floatDigits, int doubleDigits, Locale locale) { - this.floatDigits = floatDigits; + this(floatDigits, doubleDigits, 0.01, 1e6, locale); + } + + public FormattingUtil(int floatDigits, int doubleDigits, double lowLimit, double highLimit, Locale locale) { + this.floatDigits = floatDigits; this.doubleDigits = doubleDigits; - this.decimalFormatSymbols = DecimalFormatSymbols.getInstance(locale); + this.lowLimit = lowLimit; + this.highLimit = highLimit; + this.decimalFormatSymbols = DecimalFormatSymbols.getInstance(locale != null ? locale : Locale.getDefault()); initFormats(); } private void initFormats() { this.flow = createLowFormat(floatDigits); - this.fmiddles = createMiddleFormats(6, floatDigits); + double exp = Math.log10(highLimit); + int formatCount = Math.max(1, (int) Math.ceil(exp)); + this.fmiddles = createMiddleFormats(formatCount, floatDigits); this.fhigh = createHighFormat(floatDigits); this.dlow = createLowFormat(doubleDigits); - this.dmiddles = createMiddleFormats(6, doubleDigits); + this.dmiddles = createMiddleFormats(formatCount, doubleDigits); this.dhigh = createHighFormat(doubleDigits); } @@ -66,7 +76,8 @@ public class FormattingUtil { private DecimalFormat createHighFormat(int digitCount) { StringBuilder fmt = new StringBuilder(); - fmt.append("##0."); + fmt.append("##0"); + if (digitCount > 3) fmt.append("."); for (int i = 3; i < digitCount; ++i) fmt.append('#'); fmt.append("E0"); @@ -74,8 +85,8 @@ public class FormattingUtil { return new DecimalFormat(fmt.toString(), decimalFormatSymbols); } - private static DecimalFormat[] createMiddleFormats(int formatCount, int digitCount) { - DecimalFormat[] middles = new DecimalFormat[6]; + private DecimalFormat[] createMiddleFormats(int formatCount, int digitCount) { + DecimalFormat[] middles = new DecimalFormat[formatCount]; for (int exp10 = 0; exp10 < formatCount; ++exp10) { StringBuilder fmt = new StringBuilder(); int digits = digitCount; @@ -83,11 +94,11 @@ public class FormattingUtil { fmt.append('#'); fmt.append('0'); --digits; - fmt.append('.'); + if (digits > 0) fmt.append('.'); for (; digits > 0; --digits) fmt.append('#'); //System.out.println(fmt.toString()); - middles[exp10] = new DecimalFormat(fmt.toString(), DecimalFormatSymbols.getInstance(Locale.US)); + middles[exp10] = new DecimalFormat(fmt.toString(), decimalFormatSymbols); } return middles; } @@ -145,16 +156,16 @@ public class FormattingUtil { } } - private String formatNumber(float v) { + public String formatNumber(float v) { if (Float.isInfinite(v)) { return (v == Float.POSITIVE_INFINITY) ? "\u221E" : "-\u221E"; } else if (Float.isNaN(v)) { return "NaN"; } else { float abs = Math.abs(v); - if (abs < 1.0f && abs >= 0.01f) { + if (abs < 1.0f && abs >= (float)lowLimit) { return flow.format(v); - } else if (abs >= 1.0f && abs < 1e6f) { + } else if (abs >= 1.0f && abs < (float)highLimit) { double exp = Math.log10(abs); int expi = (int) Math.floor(exp); // System.out.println("format(" + v + ")"); @@ -168,16 +179,16 @@ public class FormattingUtil { } } - private String formatNumber(double v) { + public String formatNumber(double v) { if (Double.isInfinite(v)) { return (v == Double.POSITIVE_INFINITY) ? "\u221E" : "-\u221E"; } else if (Double.isNaN(v)) { return "NaN"; } else { double abs = Math.abs(v); - if (abs < 1.0 && abs >= 0.01) { + if (abs < 1.0 && abs >= lowLimit) { return dlow.format(v); - } else if (abs >= 1.0 && abs < 1e6) { + } else if (abs >= 1.0 && abs < highLimit) { double exp = Math.log10(abs); int expi = (int) Math.floor(exp); // System.out.println("format(" + v + ")");