-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.ui.workbench.editor.input;\r
-\r
-import java.util.Arrays;\r
-\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.request.ParametrizedRead;\r
-import org.simantics.db.common.request.Queries;\r
-import org.simantics.db.common.request.UnaryRead;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.request.combinations.Combinators;\r
-import org.simantics.db.layer0.request.combinations.Combinators.SynchronizationProcedure;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.ui.workbench.IResourceEditorInput;\r
-\r
-/**\r
- * Composable database read operations designed to be used for defining\r
- * validation strategies for resource editor inputs through composition.\r
- * \r
- * <p>\r
- * For example one validation criterion might be that the editor input or one of\r
- * its neighboring resources must have a proper URI.\r
- * \r
- * @author Tuukka Lehtonen\r
- */\r
-public final class InputValidationCombinators {\r
-\r
- /**\r
- * Returns a function:\r
- * <pre>\r
- * = subject -> exists(URI(subject))\r
- * | null -> false\r
- * </pre>.\r
- */\r
- public static ParametrizedRead<Resource, Boolean> hasURI() {\r
- return HAS_URI;\r
- }\r
-\r
- /**\r
- * Returns a function:\r
- * <pre>\r
- * = input -> resource(input)\r
- * | null -> null\r
- * </pre>.\r
- */\r
- public static ParametrizedRead<IResourceEditorInput, Resource> extractInputResource() {\r
- return EXTRACT_INPUT_RESOURCE;\r
- }\r
-\r
- /**\r
- * Returns a function:\r
- * <pre>\r
- * = subject -> singleObject(subject, resource(relationURI))\r
- * | null -> null\r
- * </pre>.\r
- */\r
- public static ParametrizedRead<Resource,Resource> completeFunction(String relationURI) {\r
- return new CompleteFunctionURI(relationURI);\r
- }\r
-\r
- /**\r
- * Returns a function:\r
- * <pre>\r
- * = subject -> possibleObject(subject, resource(relationURI))\r
- * | null -> null\r
- * </pre>.\r
- */\r
- public static ParametrizedRead<Resource,Resource> partialFunction(String relationURI) {\r
- return new PartialFunctionURI(relationURI);\r
- }\r
-\r
- /**\r
- * Returns a function:\r
- * <pre>\r
- * = subject -> true if any of conditions is true for subject, false otherwise\r
- * | null -> true\r
- * </pre>.\r
- */\r
- @SuppressWarnings("unchecked")\r
- public static ParametrizedRead<Resource, Boolean> or(ParametrizedRead<Resource, Boolean> c1,\r
- ParametrizedRead<Resource, Boolean> c2) {\r
- return new FunctionOr(new ParametrizedRead[] { c1, c2 });\r
- }\r
-\r
- // ------------------------------------------------------------------------\r
-\r
- private static class Or extends UnaryRead<Resource, Boolean> {\r
- ParametrizedRead<Resource, Boolean>[] reads;\r
- public Or(Resource resource, ParametrizedRead<Resource, Boolean>... reads) {\r
- super(resource);\r
- this.reads = reads;\r
- }\r
- @Override\r
- public Boolean perform(ReadGraph graph) throws DatabaseException {\r
- for (ParametrizedRead<Resource, Boolean> r : reads) {\r
- Read<Boolean> read = r.get(parameter);\r
- Boolean value = graph.syncRequest( read );\r
- if (value)\r
- return Boolean.TRUE;\r
- }\r
- return Boolean.FALSE;\r
- }\r
- @Override\r
- public int hashCode() {\r
- return super.hashCode() * 31 + Arrays.hashCode(reads);\r
- }\r
- @Override\r
- public boolean equals(Object object) {\r
- if (this == object) return true;\r
- else if (object == null || getClass() != object.getClass()) return false;\r
- Or other = (Or) object;\r
- return super.equals(object) && Arrays.equals(reads, other.reads);\r
- }\r
- }\r
-\r
- // ------------------------------------------------------------------------\r
-\r
- private static class FunctionOr implements ParametrizedRead<Resource, Boolean> {\r
- ParametrizedRead<Resource, Boolean>[] reads;\r
- public FunctionOr(ParametrizedRead<Resource, Boolean>... reads) {\r
- this.reads = reads;\r
- }\r
- @Override\r
- public Read<Boolean> get(Resource subject) {\r
- if (subject == null || reads.length == 0)\r
- return Combinators.constant(Boolean.TRUE);\r
- return new Or(subject, reads);\r
- }\r
- @Override\r
- public int hashCode() {\r
- return getClass().hashCode() + 31 * Arrays.hashCode(reads);\r
- }\r
- @Override\r
- public boolean equals(Object obj) {\r
- if(obj == this) return true;\r
- if(obj == null || obj.getClass() != getClass()) return false;\r
- FunctionOr other = (FunctionOr)obj;\r
- return Arrays.equals(reads, other.reads);\r
- }\r
- }\r
-\r
- // ------------------------------------------------------------------------\r
-\r
- private static class HasURI extends UnaryRead<Resource, Boolean> {\r
- public HasURI(Resource resource) {\r
- super(resource);\r
- }\r
- @Override\r
- public Boolean perform(ReadGraph graph) throws DatabaseException {\r
- if (parameter == null)\r
- return Boolean.FALSE;\r
-\r
- String uri = graph.syncRequest(Queries.possibleUri(parameter));\r
- //System.out.println("uri(" + parameter + "): " + uri);\r
- if (uri == null)\r
- return Boolean.FALSE;\r
-\r
- // FIXME: URI request will return invalid URIs, like\r
- // null/Configuration/MyComposite after deleting the parenting model\r
- // For this reason we try to reverse lookup the URI back into a\r
- // resource which must be equal to the original parameter resource.\r
-\r
- Resource reverseLookup = graph.getPossibleResource(uri);\r
- //System.out.println("resource(" + uri + "): " + reverseLookup);\r
- return parameter.equals(reverseLookup);\r
- }\r
- }\r
-\r
- public static Read<Boolean> hasURI(Resource resource) {\r
- return new HasURI(resource);\r
- }\r
-\r
- // ------------------------------------------------------------------------\r
-\r
- private static class ExtractResource extends UnaryRead<IResourceEditorInput, Resource> {\r
- public ExtractResource(IResourceEditorInput input) {\r
- super(input);\r
- }\r
- @Override\r
- public Resource perform(ReadGraph graph) throws DatabaseException {\r
- return parameter != null ? parameter.getResource() : null;\r
- }\r
- }\r
-\r
- public static Read<Resource> extractResource(IResourceEditorInput input) {\r
- return new ExtractResource(input);\r
- }\r
-\r
- // ------------------------------------------------------------------------\r
-\r
- private static class PossibleObjectURI implements Read<Resource> {\r
- Resource subject;\r
- String relationURI;\r
- public PossibleObjectURI(Resource subject, String relationURI) {\r
- this.subject = subject;\r
- this.relationURI = relationURI;\r
- }\r
- @Override\r
- public Resource perform(ReadGraph graph) throws DatabaseException {\r
- SynchronizationProcedure<Resource> procedure = new SynchronizationProcedure<Resource>();\r
- Resource relation = graph.getResource(relationURI);\r
- graph.forPossibleObject(subject, relation, procedure);\r
- return procedure.getResult();\r
- }\r
- @Override\r
- public int hashCode() {\r
- return subject.hashCode() + 31 * relationURI.hashCode();\r
- }\r
- @Override\r
- public boolean equals(Object object) {\r
- if (this == object) return true;\r
- else if (object == null || getClass() != object.getClass()) return false;\r
- PossibleObjectURI other = (PossibleObjectURI)object;\r
- return subject.equals(other.subject) && relationURI.equals(other.relationURI);\r
- }\r
- }\r
-\r
- /**\r
- * Returns a read request that reads an object possibly connected to the subject by the relation.\r
- */\r
- public static Read<Resource> possibleObject(Resource subject, String relationURI) {\r
- return new PossibleObjectURI(subject, relationURI);\r
- }\r
-\r
- // ------------------------------------------------------------------------\r
-\r
- private static class PartialFunctionURI implements ParametrizedRead<Resource, Resource> {\r
- String relationURI;\r
- public PartialFunctionURI(String relationURI) {\r
- this.relationURI = relationURI;\r
- }\r
- @Override\r
- public Read<Resource> get(Resource subject) {\r
- if (subject == null)\r
- return Combinators.constant(null);\r
- return possibleObject(subject, relationURI);\r
- }\r
- @Override\r
- public int hashCode() {\r
- return getClass().hashCode() + 31 * relationURI.hashCode();\r
- }\r
- @Override\r
- public boolean equals(Object obj) {\r
- if(obj == this) return true;\r
- if(obj == null || obj.getClass() != getClass()) return false;\r
- PartialFunctionURI other = (PartialFunctionURI)obj;\r
- return relationURI.equals(other.relationURI);\r
- }\r
- }\r
-\r
- // ------------------------------------------------------------------------\r
-\r
- private static class SingleObjectURI implements Read<Resource> {\r
- Resource subject;\r
- String relationURI;\r
- public SingleObjectURI(Resource subject, String relationURI) {\r
- this.subject = subject;\r
- this.relationURI = relationURI;\r
- }\r
- @Override\r
- public Resource perform(ReadGraph graph) throws DatabaseException {\r
- SynchronizationProcedure<Resource> procedure = new SynchronizationProcedure<Resource>();\r
- Resource relation = graph.getResource(relationURI);\r
- graph.forSingleObject(subject, relation, procedure);\r
- return procedure.getResult();\r
- }\r
- @Override\r
- public int hashCode() {\r
- return subject.hashCode() + 31 * relationURI.hashCode();\r
- }\r
- @Override\r
- public boolean equals(Object object) {\r
- if (this == object) return true;\r
- else if (object == null || getClass() != object.getClass()) return false;\r
- SingleObjectURI other = (SingleObjectURI) object;\r
- return subject.equals(other.subject) && relationURI.equals(other.relationURI);\r
- }\r
- }\r
-\r
- /**\r
- * Returns a read request that reads a single object connected to the\r
- * subject by the relation.\r
- */\r
- public static Read<Resource> singleObject(Resource subject, String relationURI) {\r
- return new SingleObjectURI(subject, relationURI);\r
- }\r
-\r
- // ------------------------------------------------------------------------\r
-\r
- private static class CompleteFunctionURI implements ParametrizedRead<Resource, Resource> {\r
- String relationURI;\r
- public CompleteFunctionURI(String relationURI) {\r
- this.relationURI = relationURI;\r
- }\r
- @Override\r
- public Read<Resource> get(Resource subject) {\r
- if (subject == null)\r
- return Combinators.constant(null);\r
- return singleObject(subject, relationURI);\r
- }\r
- @Override\r
- public int hashCode() {\r
- return getClass().hashCode() + 31 * relationURI.hashCode();\r
- }\r
- @Override\r
- public boolean equals(Object obj) {\r
- if(obj == this) return true;\r
- if(obj == null || obj.getClass() != getClass()) return false;\r
- CompleteFunctionURI other = (CompleteFunctionURI)obj;\r
- return relationURI.equals(other.relationURI);\r
- }\r
- }\r
-\r
- // ------------------------------------------------------------------------\r
-\r
- private static final ParametrizedRead<IResourceEditorInput, Resource> EXTRACT_INPUT_RESOURCE = new ParametrizedRead<IResourceEditorInput, Resource>() {\r
- @Override\r
- public Read<Resource> get(IResourceEditorInput input) {\r
- return extractResource(input);\r
- }\r
- };\r
-\r
- // ------------------------------------------------------------------------\r
-\r
- private static final ParametrizedRead<Resource, Boolean> HAS_URI = new ParametrizedRead<Resource, Boolean>() {\r
- @Override\r
- public Read<Boolean> get(Resource resource) {\r
- return hasURI(resource);\r
- }\r
- };\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2018 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.ui.workbench.editor.input;
+
+import java.util.Arrays;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.ParametrizedRead;
+import org.simantics.db.common.request.Queries;
+import org.simantics.db.common.request.UnaryRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.request.combinations.Combinators;
+import org.simantics.db.request.Read;
+import org.simantics.ui.workbench.IResourceEditorInput;
+
+/**
+ * Composable database read operations designed to be used for defining
+ * validation strategies for resource editor inputs through composition.
+ *
+ * <p>
+ * For example one validation criterion might be that the editor input or one of
+ * its neighboring resources must have a proper URI.
+ *
+ * @author Tuukka Lehtonen
+ */
+public final class InputValidationCombinators {
+
+ /**
+ * Returns a function:
+ * <pre>
+ * = subject -> exists(URI(subject))
+ * | null -> false
+ * </pre>.
+ */
+ public static ParametrizedRead<Resource, Boolean> hasURI() {
+ return HAS_URI;
+ }
+
+ /**
+ * Returns a function:
+ * <pre>
+ * = input -> resource(input)
+ * | null -> null
+ * </pre>.
+ */
+ public static ParametrizedRead<IResourceEditorInput, Resource> extractInputResource() {
+ return EXTRACT_INPUT_RESOURCE;
+ }
+
+ /**
+ * Returns a function:
+ * <pre>
+ * = subject -> singleObject(subject, resource(relationURI))
+ * | null -> null
+ * </pre>.
+ */
+ public static ParametrizedRead<Resource,Resource> completeFunction(String relationURI) {
+ return new CompleteFunctionURI(relationURI);
+ }
+
+ /**
+ * Returns a function:
+ * <pre>
+ * = subject -> possibleObject(subject, resource(relationURI))
+ * | null -> null
+ * </pre>.
+ */
+ public static ParametrizedRead<Resource,Resource> partialFunction(String relationURI) {
+ return new PartialFunctionURI(relationURI);
+ }
+
+ /**
+ * Returns a function:
+ * <pre>
+ * = subject -> true if any of conditions is true for subject, false otherwise
+ * | null -> true
+ * </pre>.
+ */
+ @SuppressWarnings("unchecked")
+ public static ParametrizedRead<Resource, Boolean> or(ParametrizedRead<Resource, Boolean> c1,
+ ParametrizedRead<Resource, Boolean> c2) {
+ return new FunctionOr(new ParametrizedRead[] { c1, c2 });
+ }
+
+ // ------------------------------------------------------------------------
+
+ private static class Or extends UnaryRead<Resource, Boolean> {
+ ParametrizedRead<Resource, Boolean>[] reads;
+ public Or(Resource resource, ParametrizedRead<Resource, Boolean>... reads) {
+ super(resource);
+ this.reads = reads;
+ }
+ @Override
+ public Boolean perform(ReadGraph graph) throws DatabaseException {
+ for (ParametrizedRead<Resource, Boolean> r : reads) {
+ Read<Boolean> read = r.get(parameter);
+ Boolean value = graph.syncRequest( read );
+ if (value)
+ return Boolean.TRUE;
+ }
+ return Boolean.FALSE;
+ }
+ @Override
+ public int hashCode() {
+ return super.hashCode() * 31 + Arrays.hashCode(reads);
+ }
+ @Override
+ public boolean equals(Object object) {
+ if (this == object) return true;
+ else if (object == null || getClass() != object.getClass()) return false;
+ Or other = (Or) object;
+ return super.equals(object) && Arrays.equals(reads, other.reads);
+ }
+ }
+
+ // ------------------------------------------------------------------------
+
+ private static class FunctionOr implements ParametrizedRead<Resource, Boolean> {
+ ParametrizedRead<Resource, Boolean>[] reads;
+ public FunctionOr(ParametrizedRead<Resource, Boolean>... reads) {
+ this.reads = reads;
+ }
+ @Override
+ public Read<Boolean> get(Resource subject) {
+ if (subject == null || reads.length == 0)
+ return Combinators.constant(Boolean.TRUE);
+ return new Or(subject, reads);
+ }
+ @Override
+ public int hashCode() {
+ return getClass().hashCode() + 31 * Arrays.hashCode(reads);
+ }
+ @Override
+ public boolean equals(Object obj) {
+ if(obj == this) return true;
+ if(obj == null || obj.getClass() != getClass()) return false;
+ FunctionOr other = (FunctionOr)obj;
+ return Arrays.equals(reads, other.reads);
+ }
+ }
+
+ // ------------------------------------------------------------------------
+
+ private static class HasURI extends UnaryRead<Resource, Boolean> {
+ public HasURI(Resource resource) {
+ super(resource);
+ }
+ @Override
+ public Boolean perform(ReadGraph graph) throws DatabaseException {
+ if (parameter == null)
+ return Boolean.FALSE;
+
+ String uri = graph.syncRequest(Queries.possibleUri(parameter));
+ //System.out.println("uri(" + parameter + "): " + uri);
+ if (uri == null)
+ return Boolean.FALSE;
+
+ // FIXME: URI request will return invalid URIs, like
+ // null/Configuration/MyComposite after deleting the parenting model
+ // For this reason we try to reverse lookup the URI back into a
+ // resource which must be equal to the original parameter resource.
+
+ Resource reverseLookup = graph.getPossibleResource(uri);
+ //System.out.println("resource(" + uri + "): " + reverseLookup);
+ return parameter.equals(reverseLookup);
+ }
+ }
+
+ public static Read<Boolean> hasURI(Resource resource) {
+ return new HasURI(resource);
+ }
+
+ // ------------------------------------------------------------------------
+
+ private static class ExtractResource extends UnaryRead<IResourceEditorInput, Resource> {
+ public ExtractResource(IResourceEditorInput input) {
+ super(input);
+ }
+ @Override
+ public Resource perform(ReadGraph graph) throws DatabaseException {
+ return parameter != null ? parameter.getResource() : null;
+ }
+ }
+
+ public static Read<Resource> extractResource(IResourceEditorInput input) {
+ return new ExtractResource(input);
+ }
+
+ // ------------------------------------------------------------------------
+
+ private static class PossibleObjectURI implements Read<Resource> {
+ Resource subject;
+ String relationURI;
+ public PossibleObjectURI(Resource subject, String relationURI) {
+ this.subject = subject;
+ this.relationURI = relationURI;
+ }
+ @Override
+ public Resource perform(ReadGraph graph) throws DatabaseException {
+ Resource relation = graph.getResource(relationURI);
+ return graph.getPossibleObject(subject, relation);
+ }
+ @Override
+ public int hashCode() {
+ return subject.hashCode() + 31 * relationURI.hashCode();
+ }
+ @Override
+ public boolean equals(Object object) {
+ if (this == object) return true;
+ else if (object == null || getClass() != object.getClass()) return false;
+ PossibleObjectURI other = (PossibleObjectURI)object;
+ return subject.equals(other.subject) && relationURI.equals(other.relationURI);
+ }
+ }
+
+ /**
+ * Returns a read request that reads an object possibly connected to the subject by the relation.
+ */
+ public static Read<Resource> possibleObject(Resource subject, String relationURI) {
+ return new PossibleObjectURI(subject, relationURI);
+ }
+
+ // ------------------------------------------------------------------------
+
+ private static class PartialFunctionURI implements ParametrizedRead<Resource, Resource> {
+ String relationURI;
+ public PartialFunctionURI(String relationURI) {
+ this.relationURI = relationURI;
+ }
+ @Override
+ public Read<Resource> get(Resource subject) {
+ if (subject == null)
+ return Combinators.constant(null);
+ return possibleObject(subject, relationURI);
+ }
+ @Override
+ public int hashCode() {
+ return getClass().hashCode() + 31 * relationURI.hashCode();
+ }
+ @Override
+ public boolean equals(Object obj) {
+ if(obj == this) return true;
+ if(obj == null || obj.getClass() != getClass()) return false;
+ PartialFunctionURI other = (PartialFunctionURI)obj;
+ return relationURI.equals(other.relationURI);
+ }
+ }
+
+ // ------------------------------------------------------------------------
+
+ private static class SingleObjectURI implements Read<Resource> {
+ Resource subject;
+ String relationURI;
+ public SingleObjectURI(Resource subject, String relationURI) {
+ this.subject = subject;
+ this.relationURI = relationURI;
+ }
+ @Override
+ public Resource perform(ReadGraph graph) throws DatabaseException {
+ Resource relation = graph.getResource(relationURI);
+ return graph.getSingleObject(subject, relation);
+ }
+ @Override
+ public int hashCode() {
+ return subject.hashCode() + 31 * relationURI.hashCode();
+ }
+ @Override
+ public boolean equals(Object object) {
+ if (this == object) return true;
+ else if (object == null || getClass() != object.getClass()) return false;
+ SingleObjectURI other = (SingleObjectURI) object;
+ return subject.equals(other.subject) && relationURI.equals(other.relationURI);
+ }
+ }
+
+ /**
+ * Returns a read request that reads a single object connected to the
+ * subject by the relation.
+ */
+ public static Read<Resource> singleObject(Resource subject, String relationURI) {
+ return new SingleObjectURI(subject, relationURI);
+ }
+
+ // ------------------------------------------------------------------------
+
+ private static class CompleteFunctionURI implements ParametrizedRead<Resource, Resource> {
+ String relationURI;
+ public CompleteFunctionURI(String relationURI) {
+ this.relationURI = relationURI;
+ }
+ @Override
+ public Read<Resource> get(Resource subject) {
+ if (subject == null)
+ return Combinators.constant(null);
+ return singleObject(subject, relationURI);
+ }
+ @Override
+ public int hashCode() {
+ return getClass().hashCode() + 31 * relationURI.hashCode();
+ }
+ @Override
+ public boolean equals(Object obj) {
+ if(obj == this) return true;
+ if(obj == null || obj.getClass() != getClass()) return false;
+ CompleteFunctionURI other = (CompleteFunctionURI)obj;
+ return relationURI.equals(other.relationURI);
+ }
+ }
+
+ // ------------------------------------------------------------------------
+
+ private static final ParametrizedRead<IResourceEditorInput, Resource> EXTRACT_INPUT_RESOURCE = new ParametrizedRead<IResourceEditorInput, Resource>() {
+ @Override
+ public Read<Resource> get(IResourceEditorInput input) {
+ return extractResource(input);
+ }
+ };
+
+ // ------------------------------------------------------------------------
+
+ private static final ParametrizedRead<Resource, Boolean> HAS_URI = new ParametrizedRead<Resource, Boolean>() {
+ @Override
+ public Read<Boolean> get(Resource resource) {
+ return hasURI(resource);
+ }
+ };
+
+}