1 /*******************************************************************************
2 * Copyright (c) 2019 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.layer0.request;
14 import java.util.Collection;
18 import org.simantics.databoard.Bindings;
19 import org.simantics.databoard.accessor.reference.ChildReference;
20 import org.simantics.databoard.binding.Binding;
21 import org.simantics.databoard.type.Datatype;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.variable.ValueAccessor;
26 import org.simantics.db.layer0.variable.VariableBuilder;
27 import org.simantics.layer0.Layer0;
28 import org.simantics.utils.datastructures.Pair;
30 public class PropertyInfo {
31 public final Resource predicate;
32 public final String name;
33 public final boolean isImmutable;
34 public final boolean isHasProperty;
35 public final boolean isFunctional;
36 public final Set<String> classifications;
37 public final VariableBuilder builder;
38 public final Resource literalRange;
39 public final Datatype requiredDatatype;
40 public final String requiredValueType;
41 public final String definedUnit;
42 public final Binding defaultBinding;
43 public final Map<String,Pair<Resource, ChildReference>> subliteralPredicates;
44 public final ValueAccessor valueAccessor;
45 public final boolean hasEnumerationRange;
46 public PropertyInfo(Resource predicate, String name, boolean isImmutable, boolean isFunctional, boolean isHasProperty, Set<String> classifications, VariableBuilder builder, Resource literalRange, Datatype requiredDatatype, String definedUnit, String requiredValueType, Binding defaultBinding, Map<String,Pair<Resource, ChildReference>> subliteralPredicates, ValueAccessor valueAccessor, boolean hasEnumerationRange) {
47 this.predicate = predicate;
49 this.isImmutable = isImmutable;
50 this.isFunctional = isFunctional;
51 this.isHasProperty = isHasProperty;
52 this.classifications = classifications;
53 this.builder = builder;
54 this.literalRange = literalRange;
55 this.requiredDatatype = requiredDatatype;
56 this.definedUnit = definedUnit;
57 this.requiredValueType = requiredValueType;
58 this.defaultBinding = defaultBinding;
59 this.subliteralPredicates = subliteralPredicates;
60 this.valueAccessor = valueAccessor;
61 this.hasEnumerationRange = hasEnumerationRange;
63 public static PropertyInfo make(ReadGraph graph, Resource predicate, String name, boolean isFunctional, boolean isHasProperty, Set<String> classifications, VariableBuilder builder, Resource literalRange, Datatype requiredDatatype, String definedUnit, String requiredValueType, Map<String,Pair<Resource, ChildReference>> subliteralPredicates, ValueAccessor valueAccessor, boolean hasEnumerationRange) throws DatabaseException {
65 Layer0 L0 = Layer0.getInstance(graph);
66 if(literalRange != null) {
67 Collection<Resource> dts = graph.getAssertedObjects(literalRange, L0.HasDataType);
69 Datatype dt = graph.getPossibleValue(dts.iterator().next(), Bindings.DATATYPE);
70 if(requiredDatatype == null) requiredDatatype = dt;
74 Binding defaultBinding = requiredDatatype != null ? Bindings.getBinding(requiredDatatype) : null;
76 return new PropertyInfo(predicate, name, graph.isImmutable(predicate), isFunctional, isHasProperty, classifications, builder, literalRange, requiredDatatype, definedUnit, requiredValueType, defaultBinding, subliteralPredicates, valueAccessor, hasEnumerationRange);
79 public boolean hasClassification(String classification) {
80 return classifications.contains(classification);
83 public String toString() {
84 StringBuilder sb = new StringBuilder();
85 sb.append("PropertyInfo [")
88 .append(requiredDatatype)
90 .append(requiredValueType)
91 .append(", predicate=")
93 .append(", isFunctional=")
95 .append(", isHasProperty=")
96 .append(isHasProperty)
97 .append(", hasEnumerationRange=")
98 .append(hasEnumerationRange)
99 .append(", definedUnit=")
100 .append(definedUnit != null ? definedUnit : "<none>")
101 .append(", defaultBinding=")
102 .append(defaultBinding)
103 .append(", valueAccessor=")
104 .append(valueAccessor)
106 return sb.toString();
109 public int hashCode() {
110 final int prime = 31;
112 result = prime * result + ((predicate == null) ? 0 : predicate.hashCode());
114 result = prime * result + ((builder == null) ? 0 : builder.hashCode());
115 result = prime * result + ((classifications == null) ? 0 : classifications.hashCode());
116 result = prime * result + ((defaultBinding == null) ? 0 : defaultBinding.hashCode());
117 result = prime * result + ((definedUnit == null) ? 0 : definedUnit.hashCode());
118 result = prime * result + (hasEnumerationRange ? 1231 : 1237);
119 result = prime * result + (isFunctional ? 1231 : 1237);
120 result = prime * result + (isHasProperty ? 1231 : 1237);
121 result = prime * result + ((literalRange == null) ? 0 : literalRange.hashCode());
122 result = prime * result + ((name == null) ? 0 : name.hashCode());
123 result = prime * result + ((requiredDatatype == null) ? 0 : requiredDatatype.hashCode());
124 result = prime * result + ((requiredValueType == null) ? 0 : requiredValueType.hashCode());
125 result = prime * result + ((subliteralPredicates == null) ? 0 : subliteralPredicates.hashCode());
126 result = prime * result + ((valueAccessor == null) ? 0 : valueAccessor.hashCode());
131 public boolean equals(Object obj) {
136 if (getClass() != obj.getClass())
138 PropertyInfo other = (PropertyInfo) obj;
139 if (predicate == null) {
140 if (other.predicate != null)
142 } else if (!predicate.equals(other.predicate))
146 if (builder == null) {
147 if (other.builder != null)
149 } else if (!builder.equals(other.builder))
151 if (classifications == null) {
152 if (other.classifications != null)
154 } else if (!classifications.equals(other.classifications))
156 if (defaultBinding == null) {
157 if (other.defaultBinding != null)
159 } else if (!defaultBinding.equals(other.defaultBinding))
161 if (definedUnit == null) {
162 if (other.definedUnit != null)
164 } else if (!definedUnit.equals(other.definedUnit))
166 if (hasEnumerationRange != other.hasEnumerationRange)
168 if (isFunctional != other.isFunctional)
170 if (isHasProperty != other.isHasProperty)
172 if (literalRange == null) {
173 if (other.literalRange != null)
175 } else if (!literalRange.equals(other.literalRange))
178 if (other.name != null)
180 } else if (!name.equals(other.name))
182 if (requiredDatatype == null) {
183 if (other.requiredDatatype != null)
185 } else if (!requiredDatatype.equals(other.requiredDatatype))
187 if (requiredValueType == null) {
188 if (other.requiredValueType != null)
190 } else if (!requiredValueType.equals(other.requiredValueType))
192 if (subliteralPredicates == null) {
193 if (other.subliteralPredicates != null)
195 } else if (!subliteralPredicates.equals(other.subliteralPredicates))
197 if (valueAccessor == null) {
198 if (other.valueAccessor != null)
200 } else if (!valueAccessor.equals(other.valueAccessor))