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 isHasProperty;
34 public final boolean isFunctional;
35 public final Set<String> classifications;
36 public final VariableBuilder builder;
37 public final Resource literalRange;
38 public final Datatype requiredDatatype;
39 public final String requiredValueType;
40 public final String definedUnit;
41 public final Binding defaultBinding;
42 public final Map<String,Pair<Resource, ChildReference>> subliteralPredicates;
43 public final ValueAccessor valueAccessor;
44 public final boolean hasEnumerationRange;
45 public PropertyInfo(Resource predicate, String name, 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) {
46 this.predicate = predicate;
48 this.isFunctional = isFunctional;
49 this.isHasProperty = isHasProperty;
50 this.classifications = classifications;
51 this.builder = builder;
52 this.literalRange = literalRange;
53 this.requiredDatatype = requiredDatatype;
54 this.definedUnit = definedUnit;
55 this.requiredValueType = requiredValueType;
56 this.defaultBinding = defaultBinding;
57 this.subliteralPredicates = subliteralPredicates;
58 this.valueAccessor = valueAccessor;
59 this.hasEnumerationRange = hasEnumerationRange;
61 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 {
63 Layer0 L0 = Layer0.getInstance(graph);
64 if(literalRange != null) {
65 Collection<Resource> dts = graph.getAssertedObjects(literalRange, L0.HasDataType);
67 Datatype dt = graph.getPossibleValue(dts.iterator().next(), Bindings.DATATYPE);
68 if(requiredDatatype == null) requiredDatatype = dt;
72 Binding defaultBinding = requiredDatatype != null ? Bindings.getBinding(requiredDatatype) : null;
74 return new PropertyInfo(predicate, name, isFunctional, isHasProperty, classifications, builder, literalRange, requiredDatatype, definedUnit, requiredValueType, defaultBinding, subliteralPredicates, valueAccessor, hasEnumerationRange);
77 public boolean hasClassification(String classification) {
78 return classifications.contains(classification);
81 public String toString() {
82 StringBuilder sb = new StringBuilder();
83 sb.append("PropertyInfo [")
86 .append(requiredDatatype)
88 .append(requiredValueType)
89 .append(", predicate=")
91 .append(", isFunctional=")
93 .append(", isHasProperty=")
94 .append(isHasProperty)
95 .append(", hasEnumerationRange=")
96 .append(hasEnumerationRange)
97 .append(", definedUnit=")
98 .append(definedUnit != null ? definedUnit : "<none>")
99 .append(", defaultBinding=")
100 .append(defaultBinding)
101 .append(", valueAccessor=")
102 .append(valueAccessor)
104 return sb.toString();
107 public int hashCode() {
108 final int prime = 31;
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 + ((predicate == null) ? 0 : predicate.hashCode());
120 result = prime * result + ((requiredDatatype == null) ? 0 : requiredDatatype.hashCode());
121 result = prime * result + ((requiredValueType == null) ? 0 : requiredValueType.hashCode());
122 result = prime * result + ((subliteralPredicates == null) ? 0 : subliteralPredicates.hashCode());
123 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 (builder == null) {
136 if (other.builder != null)
138 } else if (!builder.equals(other.builder))
140 if (classifications == null) {
141 if (other.classifications != null)
143 } else if (!classifications.equals(other.classifications))
145 if (defaultBinding == null) {
146 if (other.defaultBinding != null)
148 } else if (!defaultBinding.equals(other.defaultBinding))
150 if (definedUnit == null) {
151 if (other.definedUnit != null)
153 } else if (!definedUnit.equals(other.definedUnit))
155 if (hasEnumerationRange != other.hasEnumerationRange)
157 if (isFunctional != other.isFunctional)
159 if (isHasProperty != other.isHasProperty)
161 if (literalRange == null) {
162 if (other.literalRange != null)
164 } else if (!literalRange.equals(other.literalRange))
167 if (other.name != null)
169 } else if (!name.equals(other.name))
171 if (predicate == null) {
172 if (other.predicate != null)
174 } else if (!predicate.equals(other.predicate))
176 if (requiredDatatype == null) {
177 if (other.requiredDatatype != null)
179 } else if (!requiredDatatype.equals(other.requiredDatatype))
181 if (requiredValueType == null) {
182 if (other.requiredValueType != null)
184 } else if (!requiredValueType.equals(other.requiredValueType))
186 if (subliteralPredicates == null) {
187 if (other.subliteralPredicates != null)
189 } else if (!subliteralPredicates.equals(other.subliteralPredicates))
191 if (valueAccessor == null) {
192 if (other.valueAccessor != null)
194 } else if (!valueAccessor.equals(other.valueAccessor))