]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/PropertyInfo.java
Simupedia tuning step 2
[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 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;
48                 this.name = name;
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;
62         }
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
65                 Layer0 L0 = Layer0.getInstance(graph);
66                 if(literalRange != null) {
67                         Collection<Resource> dts = graph.getAssertedObjects(literalRange, L0.HasDataType);
68                         if(dts.size() == 1) {
69                                 Datatype dt = graph.getPossibleValue(dts.iterator().next(), Bindings.DATATYPE);
70                                 if(requiredDatatype == null) requiredDatatype = dt;
71                         }
72                 }
73                 
74                 Binding defaultBinding = requiredDatatype != null ? Bindings.getBinding(requiredDatatype) : null;
75                 
76                 return new PropertyInfo(predicate, name, graph.isImmutable(predicate), isFunctional, isHasProperty, classifications, builder, literalRange, requiredDatatype, definedUnit, requiredValueType, defaultBinding, subliteralPredicates, valueAccessor, hasEnumerationRange);
77
78         }
79         public boolean hasClassification(String classification) {
80                 return classifications.contains(classification);
81         }
82         @Override
83         public String toString() {
84                 StringBuilder sb = new StringBuilder();
85                 sb.append("PropertyInfo [")
86                 .append(name)
87                 .append(" : ")
88                 .append(requiredDatatype)
89                 .append(" :: ")
90                 .append(requiredValueType)
91                 .append(", predicate=")
92                 .append(predicate)
93                 .append(", isFunctional=")
94                 .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)
105                 .append("]");
106                 return sb.toString();
107         }
108     @Override
109     public int hashCode() {
110         final int prime = 31;
111         int result = 1;
112         result = prime * result + ((predicate == null) ? 0 : predicate.hashCode());
113         if(!isImmutable) { 
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());
127         }
128         return result;
129     }
130     @Override
131     public boolean equals(Object obj) {
132         if (this == obj)
133             return true;
134         if (obj == null)
135             return false;
136         if (getClass() != obj.getClass())
137             return false;
138         PropertyInfo other = (PropertyInfo) obj;
139         if (predicate == null) {
140             if (other.predicate != null)
141                 return false;
142         } else if (!predicate.equals(other.predicate))
143             return false;
144         if(isImmutable)
145             return true;
146         if (builder == null) {
147             if (other.builder != null)
148                 return false;
149         } else if (!builder.equals(other.builder))
150             return false;
151         if (classifications == null) {
152             if (other.classifications != null)
153                 return false;
154         } else if (!classifications.equals(other.classifications))
155             return false;
156         if (defaultBinding == null) {
157             if (other.defaultBinding != null)
158                 return false;
159         } else if (!defaultBinding.equals(other.defaultBinding))
160             return false;
161         if (definedUnit == null) {
162             if (other.definedUnit != null)
163                 return false;
164         } else if (!definedUnit.equals(other.definedUnit))
165             return false;
166         if (hasEnumerationRange != other.hasEnumerationRange)
167             return false;
168         if (isFunctional != other.isFunctional)
169             return false;
170         if (isHasProperty != other.isHasProperty)
171             return false;
172         if (literalRange == null) {
173             if (other.literalRange != null)
174                 return false;
175         } else if (!literalRange.equals(other.literalRange))
176             return false;
177         if (name == null) {
178             if (other.name != null)
179                 return false;
180         } else if (!name.equals(other.name))
181             return false;
182         if (requiredDatatype == null) {
183             if (other.requiredDatatype != null)
184                 return false;
185         } else if (!requiredDatatype.equals(other.requiredDatatype))
186             return false;
187         if (requiredValueType == null) {
188             if (other.requiredValueType != null)
189                 return false;
190         } else if (!requiredValueType.equals(other.requiredValueType))
191             return false;
192         if (subliteralPredicates == null) {
193             if (other.subliteralPredicates != null)
194                 return false;
195         } else if (!subliteralPredicates.equals(other.subliteralPredicates))
196             return false;
197         if (valueAccessor == null) {
198             if (other.valueAccessor != null)
199                 return false;
200         } else if (!valueAccessor.equals(other.valueAccessor))
201             return false;
202         return true;
203     }
204 }