]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/PartialIC.java
Multiple readers and variable optimization
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / PartialIC.java
1 package org.simantics.modeling;
2
3 import java.util.Arrays;
4
5 import org.simantics.databoard.Bindings;
6 import org.simantics.databoard.binding.Binding;
7 import org.simantics.databoard.binding.mutable.Variant;
8 import org.simantics.db.WriteGraph;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.layer0.variable.RVI;
11 import org.simantics.db.layer0.variable.Variable;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 public class PartialIC {
16
17     private static final Logger LOGGER = LoggerFactory.getLogger(PartialIC.class);
18         public static Binding BINDING = Bindings.getBindingUnchecked(PartialIC.class);
19         
20         public RVI[] rvis = {};
21         public Variant[] variants = {};
22         
23         //public TreeMap<RVI, Variant> values = new TreeMap<RVI, Variant>(); 
24         
25         public void add(RVI rvi, Variant value) {
26             rvis = Arrays.copyOf(rvis, rvis.length + 1);
27             rvis[rvis.length - 1] = rvi;
28             variants = Arrays.copyOf(variants, variants.length + 1);
29         variants[rvis.length - 1] = value;
30         
31                 //values.put(rvi, value);
32         }
33         
34         public void apply(WriteGraph graph, Variable base) throws DatabaseException {
35 //              for(Map.Entry<RVI,Variant> entry : values.entrySet()) {
36 //                      RVI rvi = entry.getKey();
37 //                      Variant variant = entry.getValue();
38 //                      Variable v = rvi.resolvePossible(graph, base);
39 //                      if(v != null) {
40 ////                            LOGGER.warn("apply " + v.getURI(graph) + " => " + variant);
41 //                              v.setValue(graph, variant.getValue(), variant.getBinding());
42 //                      } else {
43 //                              LOGGER.warn("failed to resolve " + rvi);
44 //                      }
45 //              }
46             
47             if (rvis.length != variants.length) {
48                 throw new DatabaseException("PartialIC has broken RVI and Variant data arrays. Tried to apply to " + base.getURI(graph));
49             }
50             
51             for (int i = 0; i < rvis.length; i++) {
52                 RVI rvi = rvis[i];
53                 Variant variant = variants[i];
54                 Variable v = rvi.resolvePossible(graph, base);
55                 if (v != null) {
56 //              LOGGER.warn("apply " + v.getURI(graph) + " => " + variant);
57                 v.setValue(graph, variant.getValue(), variant.getBinding());
58             } else {
59                 LOGGER.warn("failed to resolve " + rvi);
60             }
61         }
62             
63         }
64         
65 }