File: DBInsert.pm

package info (click to toggle)
polymake 3.2r4-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 22,564 kB
  • sloc: cpp: 153,464; perl: 40,590; ansic: 2,829; java: 2,654; python: 589; sh: 219; xml: 117; makefile: 63
file content (357 lines) | stat: -rw-r--r-- 13,554 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#  Copyright (c) 1997-2018
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
#  http://www.polymake.org
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-------------------------------------------------------------------------------
#
#  This file is part of the polymake database interface polyDB.
#
#   @author Silke Horn, Andreas Paffenholz
#   http://solros.de
#   http://www.mathematik.tu-darmstadt.de/~paffenholz
#

use strict;

package PolyDB::DBInsert;

sub add_properties_at_level {
   my ($input_object, $property_mask,$initial,$m_initial, $multiple) = @_;

   my $required_properties = [];
   my $required_multiple = {};
   my $required_attachments = [];

   foreach my $pn ( keys %$property_mask ) {
      if ( $pn eq "attachments" ) {
         foreach my $at ( keys %{$property_mask->{$pn}} ) {
            push @$required_attachments, $initial eq "" ? $at : $initial.".".$at;
         }
      } else {
         my $new_initial = $initial eq "" ? $pn : $initial.".".$pn;
         my $new_m_initial = $m_initial eq "" ? $pn : $m_initial.".".$pn;
         my $read_request;
         if ( eval { $read_request = $input_object->type->encode_read_request($new_initial) } ) {
            if ( ref $property_mask->{$pn} eq "HASH" ) {
               my $prop = local_pop($read_request->[0]);
               if ( $prop->flags & $Polymake::Core::Property::is_subobject ) {
                  my $is_multiple = $prop->flags & $Polymake::Core::Property::is_multiple;
                  my ($rp,$rm,$ra) = add_properties_at_level($input_object, $property_mask->{$pn},$new_initial, $is_multiple ? "" : $new_m_initial, $is_multiple ? 1 : $multiple);
                  push @$required_properties, @$rp;

                  push @$required_attachments, @$ra;
                  if ( $is_multiple ) {
                     $required_multiple->{$new_m_initial} = $rm;
                  } elsif ( $multiple ) {
                     $required_multiple = { %$required_multiple, %$rm };
                  }
               } else {
                  push @$required_properties, $new_initial;
                  if ( $multiple ) {
                     $required_multiple->{$new_m_initial} = 1;
                  }
               }
            } else {
               push @$required_properties, $new_initial;
               if ( $multiple ) {
                  $required_multiple->{$new_m_initial} = 1;
               }
            }
         }
      }
   }
   return $required_properties, $required_multiple, $required_attachments;
}

# assuming that the mask for desired properties is a nested hash map, e.g.
# $mask = { 'VERTICES' => 1, 'TRIANGULATION' => { 'FACETS' => 1, ... }, ... };

sub create_filter_for_mask {
   my ($property_mask, $original_property_mask)=@_;
   sub {
      my ($parent, $pv, $property)=@_;
      $property //= $pv->property; # if $property is defined then do nothing, else replace it with $pv->property
      my $mask_element = $property_mask->{$property->name};
      my $original_mask_element = $original_property_mask->{$property->name};

      if ( defined($mask_element) || defined($original_mask_element) ) {
         if ( (ref($mask_element) && $property->flags & $Core::Property::is_subobject) && ( !defined($original_mask_element) || ( ref($original_mask_element) && $property->flags & $Core::Property::is_subobject) ) ) {
            # recursively copy selected properties of a subobject or of all instances of a multiple subobject
            ($parent, $pv->copy($parent, $property, create_filter_for_mask($mask_element, $original_mask_element)));
         } else {
         # either an atomic property or the entire subobject shall be copied
           ($parent, $pv->copy($parent, $property));
         }
       } else {
          # property does not occur in the mask: empty return suppresses copying
          ()
       }
   }
}

sub copy_object_with_mask_filter {
  my ($object, $property_mask,$original_property_mask) = @_;
  $object->copy(undef, undef, create_filter_for_mask($property_mask, $original_property_mask));
}

sub copy_all_attachments {
   my ($input_object,$output_object) = @_;

   $output_object->copy_attachments($input_object);
   foreach my $pv (@{$input_object->contents}) {
      if ($pv->property->flags & $Core::Property::is_subobject) {
         copy_all_attachments($input_object->{$pv->{$property}},$output_object->{$property});
      }
   }
}

sub provide_multiple_properties {

   my ($input_object, $required_multiple) = @_;
   foreach my $propstring (keys %$required_multiple) {
      if ( ref($required_multiple->{$propstring}) ) {
         my $input_subobject;
         my $prop;
         if ( $propstring =~ /\./ ) {
            (my $initial,$prop) = split(/\.([^\.]+)$/, $propstring);
            $input_subobject = $input_object->give($initial);
         } else {
            $prop = $propstring;
            $input_subobject = $input_object;
         }
         foreach my $multiple_subobject (@{$input_subobject->give($prop)}) {
            provide_multiple_properties($multiple_subobject,$required_multiple->{$propstring});
         }
      } else {
         $input_object->provide($propstring);
      }
   }
}

sub adjust_properties {

   my ($input_object, $options ) = @_;

   if ( defined($options->{'type_information'}) ) {
      my $output_object;
      my $property_mask = $options->{'type_information'}->{"property_mask"};
      my $original_property_mask = {};

      if ( $options->{"keep_all_props"}) {
         my @known_props = $input_object->list_properties(1);
         foreach my $propstring (@known_props) {
            my @props = split(/\./,$propstring);
            foreach (@props) {
               s/\[.*\]$//;
            }
            my $mask_pointer = $original_property_mask;
            my $last = pop(@props);
            while (my $x = shift(@props) ) {
               if ( defined($mask_pointer->{$x})) {
                  $mask_pointer = $mask_pointer->{$x};
               } else {
                  $mask_pointer->{$x} = {};
                  $mask_pointer = $mask_pointer->{$x};
               }
            }
            $mask_pointer->{$last} = 1;
         }
      }

      my ($required_properties,$required_multiple,$required_attachments) = add_properties_at_level($input_object, $property_mask,"", "", 0);

      $input_object->provide(@$required_properties) unless $options->{"nonew"};
      provide_multiple_properties($input_object,$required_multiple) unless $options->{"nonew"};
      $output_object = copy_object_with_mask_filter($input_object,$property_mask,$original_property_mask);

      foreach my $at (@$required_attachments) {
         if ( $at =~ /\./ ) {
            my ($prop,$attachment) = split(/\.([^\.]+)$/, $at);
            my $sub_in = $input_object;
            my $sub_out = $output_object;
            while ( $prop =~ /\./ ) {
               (my $p,$prop) = split(/\./, $prop,2);
               $sub_in = $sub_in->give($p);
               $sub_out = $sub_out->give($p);
            }
            $sub_in = $sub_in->give($prop);
            $sub_out = $sub_out->give($prop);
            if ( exists($sub_in->attachments->{$attachment}) ) {
               $sub_out->attach($attachment,$sub_in->get_attachment($attachment));
            } else {
               if ( !$options->{"nonew"} ) {
                  croak("attachment ".$at." required but not provided\n");
               }
            }
         } else {
            if ( exists($input_object->attachments->{$at}) ) {
               $output_object->attach($at,$input_object->get_attachment($at));
            } else {
               if ( !$options->{"nonew"} ) {
                  croak("attachment ".$at." required but not provided\n");
               }
            }
         }
      }
      return $output_object;
   } else {
      return $input_object;
   }
}

sub prepare_xml {
   my ($obj) = @_;

   $obj->remove_attachment("polyDB");
   my $xml = save Core::XMLstring($obj);
   return $xml;
}

# the current date as a string in the form yyyy-mm-dd
sub get_date {
   my $now = `date '+%Y-%m-%d'`;
   chomp $now;
   return $now;
}

# prepare metadata
# need to define
# * creation_date
# * contributor
# * type_information_key
# * package->polymake with
#    * type
#    * app
#    * xml
# this will appear in the database as 'polyDB'-entry
sub prepare_metadata {

   my ($obj, $type_information, $options) = @_;

   my $metadata = {};
   # check if object has already polyDB attachment
   if ( defined( my $polydb_attachment = $obj->get_attachment("polyDB") ) ) {
      foreach (keys %{$polydb_attachment}) {
         $metadata->{$_} = $polydb_attachment->{$_};
      }
   }

   # set the contributor, precedence is
   # given in object > set as option > given in template
   if ( !defined( $metadata->{"contributor"}) ) {
      if ( defined($options->{"contributor"}) ) {
         $metadata->{"contributor"} = $options->{"contributor"};
      } else {
         if ( defined($type_information->{"contributor"}) ) {
            $metadata->{"contributor"} = $type_information->{"contributor"};
         }
      }
   }

   # set the creation date of the entry
   # either already stored in the object (e.g. when copying the db)
   # or we take the current day
   if ( !defined( $metadata->{"creation_date"}) ) {
      $metadata->{"creation_date"} = get_date();
   }

   # Potentially overwrite a template key from the options, if one is given in options and in the object
   if ( defined($options->{type_information_key}) ) {
      $metadata->{"type_information_key"} = $options->{type_information_key};
   }

   my $version = do {
      if ( defined($options->{'version'}) ) {
         $options->{'version'};
      } elsif ( defined($type_information->{'version'}) ) {
         $type_information->{'version'};
      } else {
         $Polymake::Version;
      }
   };
   # we replace version in the xml for the one given in options or type_information
   # this is potentially unsafe if the version used to create the document writes properties differently
   # however, this is necessary to provide accessibility of the data for older versions of polymake
   $metadata->{'package'}->{'polymake'} = {
      version => $version,
      type => $obj->type->qualified_name,
      tag => "object"
   };
   ($metadata->{"package"}->{"polymake"}->{"app"}) = $metadata->{"package"}->{"polymake"}->{"type"} =~ /^(.+?)(?=::)/;
   if ( !$options->{'noxml'} ) {
      my $xml = prepare_xml($obj);
      $xml =~ s/version="(\d+.)+\d+" xmlns/version=\"$version\" xmlns/g;
      $metadata->{'package'}->{'polymake'}->{'xml'} = $xml;
   }

   return $metadata;
}

# $obj: the original polymake object to insert
# $collection: handler for database/collection in the MongoDB
# $id: id for the object to insert
# $options: hash containing options, in particular
#   - template: the tamplate defining which properties to store in json and xml
#   - contributor:
sub insert_into_col {
   my ($obj, $collection_handler, $id, $options) = @_;

   my $use_type_information = 0;   # do we use a template file to filter the properties
   my $type_information;
   if ( defined($type_information = $options->{type_information}) ) {
      $use_type_information = 1;
   }

   my $polymake_object;
   if ( $use_type_information ) {
      # returns a new polymake object with the properties of $obj copied to it
      # if all should be kept or in template, properties required by template computed and nonoew not set
      $obj = adjust_properties($obj, $options);

      my $json_modifier = $type_information->{'modifier'} // $type_information->{'json_modifier'};

      # convert to perl array
      $polymake_object = PolymakeJsonConversion::polymake2perl($obj, {property_mask=>$type_information->{'property_mask'}, json_modifier=>$json_modifier, keep_all_props=>$options->{"keep_all_props"} });

      # additional data is a perl function that uses polymake to compute additional date from the polymake object $obj
      # this may add new properties to $obj, but this is okay as we have already stored the xml in $polymake_object
      # (it might also recompute properties that we removed during the call to adjust properties. But if, then most likely they should have been included in the properties stored in the xml)
      if ( defined($options->{'type_information'}->{'additional_data'}) ) {
         my $func = eval($options->{'type_information'}->{'additional_data'});
         $func->($obj, $polymake_object);
      }
   } else {

      # convert to perl array
      # save the given polymake object as is
      $polymake_object = PolymakeJsonConversion::polymake2perl($obj);
   }

   my $metadata = prepare_metadata($obj, $type_information, $options);
   $polymake_object->{"polyDB"} = $metadata;
   $polymake_object->{'_id'} = $id;

   # insert into database
   my $o = $options->{noinsert} || $collection_handler->insert_one($polymake_object);

   # return success
   return $o;
}

1;


# Local Variables:
# mode: perl
# cperl-indent-level:3
# indent-tabs-mode:nil
# End: