]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PropertyInfo.java
Merge branch 'master' into private/antti2
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / request / PropertyInfo.java
1 /*******************************************************************************
2  * Copyright (c) 2019 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.layer0.request;
13
14 import java.util.Collection;
15 import java.util.Map;
16 import java.util.Set;
17
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;
29
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;
47                 this.name = name;
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;
60         }
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 {
62
63                 Layer0 L0 = Layer0.getInstance(graph);
64                 if(literalRange != null) {
65                         Collection<Resource> dts = graph.getAssertedObjects(literalRange, L0.HasDataType);
66                         if(dts.size() == 1) {
67                                 Datatype dt = graph.getPossibleValue(dts.iterator().next(), Bindings.DATATYPE);
68                                 if(requiredDatatype == null) requiredDatatype = dt;
69                         }
70                 }
71                 
72                 Binding defaultBinding = requiredDatatype != null ? Bindings.getBinding(requiredDatatype) : null;
73                 
74                 return new PropertyInfo(predicate, name, isFunctional, isHasProperty, classifications, builder, literalRange, requiredDatatype, definedUnit, requiredValueType, defaultBinding, subliteralPredicates, valueAccessor, hasEnumerationRange);
75
76         }
77         public boolean hasClassification(String classification) {
78                 return classifications.contains(classification);
79         }
80         @Override
81         public String toString() {
82                 StringBuilder sb = new StringBuilder();
83                 sb.append("PropertyInfo [")
84                 .append(name)
85                 .append(" : ")
86                 .append(requiredDatatype)
87                 .append(" :: ")
88                 .append(requiredValueType)
89                 .append(", predicate=")
90                 .append(predicate)
91                 .append(", isFunctional=")
92                 .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)
103                 .append("]");
104                 return sb.toString();
105         }
106     @Override
107     public int hashCode() {
108         final int prime = 31;
109         int result = 1;
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());
124         return result;
125     }
126     @Override
127     public boolean equals(Object obj) {
128         if (this == obj)
129             return true;
130         if (obj == null)
131             return false;
132         if (getClass() != obj.getClass())
133             return false;
134         PropertyInfo other = (PropertyInfo) obj;
135         if (builder == null) {
136             if (other.builder != null)
137                 return false;
138         } else if (!builder.equals(other.builder))
139             return false;
140         if (classifications == null) {
141             if (other.classifications != null)
142                 return false;
143         } else if (!classifications.equals(other.classifications))
144             return false;
145         if (defaultBinding == null) {
146             if (other.defaultBinding != null)
147                 return false;
148         } else if (!defaultBinding.equals(other.defaultBinding))
149             return false;
150         if (definedUnit == null) {
151             if (other.definedUnit != null)
152                 return false;
153         } else if (!definedUnit.equals(other.definedUnit))
154             return false;
155         if (hasEnumerationRange != other.hasEnumerationRange)
156             return false;
157         if (isFunctional != other.isFunctional)
158             return false;
159         if (isHasProperty != other.isHasProperty)
160             return false;
161         if (literalRange == null) {
162             if (other.literalRange != null)
163                 return false;
164         } else if (!literalRange.equals(other.literalRange))
165             return false;
166         if (name == null) {
167             if (other.name != null)
168                 return false;
169         } else if (!name.equals(other.name))
170             return false;
171         if (predicate == null) {
172             if (other.predicate != null)
173                 return false;
174         } else if (!predicate.equals(other.predicate))
175             return false;
176         if (requiredDatatype == null) {
177             if (other.requiredDatatype != null)
178                 return false;
179         } else if (!requiredDatatype.equals(other.requiredDatatype))
180             return false;
181         if (requiredValueType == null) {
182             if (other.requiredValueType != null)
183                 return false;
184         } else if (!requiredValueType.equals(other.requiredValueType))
185             return false;
186         if (subliteralPredicates == null) {
187             if (other.subliteralPredicates != null)
188                 return false;
189         } else if (!subliteralPredicates.equals(other.subliteralPredicates))
190             return false;
191         if (valueAccessor == null) {
192             if (other.valueAccessor != null)
193                 return false;
194         } else if (!valueAccessor.equals(other.valueAccessor))
195             return false;
196         return true;
197     }
198 }