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 {
64 Layer0 L0 = Layer0.getInstance(graph);
65 if(literalRange != null) {
66 Collection<Resource> dts = graph.getAssertedObjects(literalRange, L0.HasDataType);
68 Datatype dt = graph.getPossibleValue(dts.iterator().next(), Bindings.DATATYPE);
69 if(requiredDatatype == null) requiredDatatype = dt;
72 Binding defaultBinding = requiredDatatype != null ? Bindings.getBinding(requiredDatatype) : null;
73 return new PropertyInfo(predicate, name, graph.isImmutable(predicate), isFunctional, isHasProperty, classifications, builder, literalRange, requiredDatatype, definedUnit, requiredValueType, defaultBinding, subliteralPredicates, valueAccessor, hasEnumerationRange);
75 public boolean hasClassification(String classification) {
76 return classifications.contains(classification);
79 public String toString() {
80 StringBuilder sb = new StringBuilder();
81 sb.append("PropertyInfo [")
84 .append(requiredDatatype)
86 .append(requiredValueType)
87 .append(", predicate=")
89 .append(", isFunctional=")
91 .append(", isHasProperty=")
92 .append(isHasProperty)
93 .append(", hasEnumerationRange=")
94 .append(hasEnumerationRange)
95 .append(", definedUnit=")
96 .append(definedUnit != null ? definedUnit : "<none>")
97 .append(", defaultBinding=")
98 .append(defaultBinding)
99 .append(", valueAccessor=")
100 .append(valueAccessor)
102 return sb.toString();
105 public int hashCode() {
106 final int prime = 31;
108 result = prime * result + ((predicate == null) ? 0 : predicate.hashCode());
110 result = prime * result + ((builder == null) ? 0 : builder.hashCode());
111 result = prime * result + ((classifications == null) ? 0 : classifications.hashCode());
112 result = prime * result + ((defaultBinding == null) ? 0 : defaultBinding.hashCode());
113 result = prime * result + ((definedUnit == null) ? 0 : definedUnit.hashCode());
114 result = prime * result + (hasEnumerationRange ? 1231 : 1237);
115 result = prime * result + (isFunctional ? 1231 : 1237);
116 result = prime * result + (isHasProperty ? 1231 : 1237);
117 result = prime * result + ((literalRange == null) ? 0 : literalRange.hashCode());
118 result = prime * result + ((name == null) ? 0 : name.hashCode());
119 result = prime * result + ((requiredDatatype == null) ? 0 : requiredDatatype.hashCode());
120 result = prime * result + ((requiredValueType == null) ? 0 : requiredValueType.hashCode());
121 result = prime * result + ((subliteralPredicates == null) ? 0 : subliteralPredicates.hashCode());
122 result = prime * result + ((valueAccessor == null) ? 0 : valueAccessor.hashCode());
127 public boolean equals(Object obj) {
132 if (getClass() != obj.getClass())
134 PropertyInfo other = (PropertyInfo) obj;
135 if (predicate == null) {
136 if (other.predicate != null)
138 } else if (!predicate.equals(other.predicate))
142 if (builder == null) {
143 if (other.builder != null)
145 } else if (!builder.equals(other.builder))
147 if (classifications == null) {
148 if (other.classifications != null)
150 } else if (!classifications.equals(other.classifications))
152 if (defaultBinding == null) {
153 if (other.defaultBinding != null)
155 } else if (!defaultBinding.equals(other.defaultBinding))
157 if (definedUnit == null) {
158 if (other.definedUnit != null)
160 } else if (!definedUnit.equals(other.definedUnit))
162 if (hasEnumerationRange != other.hasEnumerationRange)
164 if (isFunctional != other.isFunctional)
166 if (isHasProperty != other.isHasProperty)
168 if (literalRange == null) {
169 if (other.literalRange != null)
171 } else if (!literalRange.equals(other.literalRange))
174 if (other.name != null)
176 } else if (!name.equals(other.name))
178 if (requiredDatatype == null) {
179 if (other.requiredDatatype != null)
181 } else if (!requiredDatatype.equals(other.requiredDatatype))
183 if (requiredValueType == null) {
184 if (other.requiredValueType != null)
186 } else if (!requiredValueType.equals(other.requiredValueType))
188 if (subliteralPredicates == null) {
189 if (other.subliteralPredicates != null)
191 } else if (!subliteralPredicates.equals(other.subliteralPredicates))
193 if (valueAccessor == null) {
194 if (other.valueAccessor != null)
196 } else if (!valueAccessor.equals(other.valueAccessor))