SheafSystem  0.0.0.0
refinable_poset.cc
Go to the documentation of this file.
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
20 
21 #include "SheafSystem/refinable_poset.h"
22 
23 #include "SheafSystem/namespace_poset.impl.h"
24 #include "SheafSystem/abstract_poset_member.h"
25 #include "SheafSystem/implicit_crg_interval.h"
26 class index_iterator;
27 #include "SheafSystem/poset_dof_map.h"
28 #include "SheafSystem/poset_handle_factory.h"
29 #include "SheafSystem/poset_member_iterator.h"
30 #include "SheafSystem/poset_state.h"
31 #include "SheafSystem/poset_type.h"
32 #include "SheafSystem/schema_poset_member.h"
33 #include "SheafSystem/subposet.h"
34 
35 using namespace std;
36 
37 // ===========================================================
38 // REFINABLE_POSET FACET
39 // ===========================================================
40 
41 // PUBLIC FUNCTIONS
42 
45 new_table(namespace_type& xns, const poset_path& xpath, const poset_path& xschema_path, bool xauto_access)
46 {
47  // cout << endl << "Entering refinable_poset::new_table." << endl;
48 
49  // Preconditions:
50 
51  require(xns.state_is_auto_read_write_accessible(xauto_access));
52 
53  require(!xpath.empty());
54  require(!xns.contains_path(xpath, xauto_access));
55 
56  require(xschema_path.full());
57  require(xns.path_is_auto_read_accessible(xschema_path, xauto_access));
58 
59  // Body:
60 
61  // Create the table; have to new it because namespace keeps a pointer.
62 
63  typedef refinable_poset table_type;
64 
65  table_type* ltable = new table_type();
66 
67  // Create a handle of the right type for the schema member.
68 
69  schema_poset_member lschema(&xns, xschema_path, xauto_access);
70 
71  if(xauto_access)
72  {
73  lschema.get_read_access();
74  }
75 
76  // Create the table dof map; must be newed
77  // because poset_state::_table keeps a pointer to it.
78  // No table dofs to set.
79 
80  array_poset_dof_map* lmap = new array_poset_dof_map(&lschema, true);
81 
82  // Create the state.
83 
84  ltable->new_state(xns, xpath, lschema, *lmap);
85 
86  if(xauto_access)
87  {
88  lschema.release_access();
89  }
90 
91  refinable_poset& result = *ltable;
92 
93  // Postconditions:
94 
95  ensure(xns.owns(result, xauto_access));
96  ensure(result.path(true) == xpath);
97  ensure(result.state_is_not_read_accessible());
98  ensure(result.schema(true).path(xauto_access) == xschema_path);
99 
100  // Exit:
101 
102  // cout << "Leaving refinable_poset::new_table." << endl;
103  return result;
104 }
105 
106 void
108 put_version(int xversion)
109 {
110  // Preconditions:
111 
112  require( has_version(xversion) );
113 
114  // Body:
115 
116  // Just to change the visibility of put_version
117  // from protected to public.
118 
119  poset::put_version(xversion);
120 
121  // Postconditions:
122 
123  ensure(version() == xversion);
124 
125  // Exit
126 
127  return;
128 }
129 
133 {
134  // Preconditions:
135 
136  require(state_is_read_accessible());
137 
138  // Body:
139 
140  subposet& result = state_obj()->powerset()->coarsest_common_refinement();
141 
142  // Postconditions:
143 
144  ensure(result.state_is_read_accessible());
145 
146  // Exit
147 
148  return result;
149 }
150 
151 const sheaf::subposet&
154 {
155  // Preconditions:
156 
157  require(state_is_read_accessible());
158 
159  // Body:
160 
161  subposet& result = state_obj()->powerset()->coarsest_common_refinement();
162 
163  // Postconditions:
164 
165  ensure(result.state_is_read_accessible());
166 
167  // Exit
168 
169  return result;
170 }
171 
175 {
176  // Preconditions:
177 
178  require(state_is_read_accessible());
179 
180  // Body:
181 
182  subposet& result = state_obj()->powerset()->coarsest_common_refinement_jims();
183 
184  // Postconditions:
185 
186  ensure(result.state_is_read_accessible());
187 
188  // Exit
189 
190  return result;
191 }
192 
193 const sheaf::subposet&
196 {
197  // Preconditions:
198 
199  require(state_is_read_accessible());
200 
201  // Body:
202 
203  subposet& result = state_obj()->powerset()->coarsest_common_refinement_jims();
204 
205  // Postconditions:
206 
207  ensure(result.state_is_read_accessible());
208 
209  // Exit
210 
211  return result;
212 }
213 
214 void
216 begin_refine_mode(bool xauto_access)
217 {
218  // Preconditions:
219 
220  require(!xauto_access ? state_is_read_write_accessible() : true);
221 
222  if(xauto_access)
223  get_read_write_access(true);
224 
225 
226  // Body:
227 
228  define_old_variable(int old_version_ct = version_ct());
229 
230  poset_state_handle::begin_jim_edit_mode(false);
231 
232  int lnew_ver = version_ct();
233  if(lnew_ver == 1)
234  {
235  // We're moving from version 0 to version 1.
236  // Until this point, version 0 has been just
237  // an alias for the ccr. But now version 0 will
238  // be constant and ccr will continue to change,
239  // so we need to make separate subposets for
240  // version 0.
241 
242  // First, delete the version 0 aliases.
243 
244  string lname = version_to_name(0);
245  delete_subposet_name(lname, false);
246  delete_subposet_name(lname+"_jims", false);
247 
248  assertion(version_ct() == 0);
249 
250  // Now create a separate version 0.
251 
252  new_version(0);
253 
254  assertion(version_ct() == old_version_ct);
255  }
256 
257  new_version(lnew_ver);
258 
259  // Postconditions:
260 
261  ensure(state_is_read_write_accessible());
262  ensure(in_jim_edit_mode());
263  ensure(version_ct() == old_version_ct + 1);
264 
265  // Exit
266 
267  return;
268 }
269 
270 void
272 end_refine_mode(bool xensure_lattice_invariant, bool xauto_access)
273 {
274  // Preconditions:
275 
276  require(precondition_of(end_jim_edit_mode(xensure_lattice_invarinat, xauto_access)));
277 
278  // Body:
279 
280  end_jim_edit_mode(xensure_lattice_invariant, xauto_access);
281 
282  // Postconditions:
283 
284  ensure(postcondition_of(end_jim_edit_mode(xensure_lattice_invarinat, xauto_access)));
285 
286  // Exit:
287 
288  return;
289 }
290 
291 // PROTECTED FUNCTIONS
292 
295 {
296  // Preconditions:
297 
298  // Body:
299 
300  // Nothing to do, handled by base class
301 
302  // Postconditions:
303 
304  ensure(postcondition_of(poset::poset()));
305 }
306 
309 {
310  // Preconditions:
311 
312  // Body:
313 
314  // Nothing to do; base class does it all.
315  // Currently, this routine exists mostly for the precondition,
316  // which is the same as poset::~poset.
317  // With this precondition in place, the source of a precondition
318  // violation will be clearer to the client.
319 
320  // Postconditions:
321 
322  // Exit
323 
324  return;
325 }
326 
327 // PRIVATE FUNCTIONS
328 
329 void
330 sheaf::refinable_poset::
331 new_version(int xversion)
332 {
333  // Preconditions:
334 
335  require(state_is_read_write_accessible());
336  require(!includes_subposet(version_to_name(xversion), false));
337 
338  define_old_variable(int old_version_ct = version_ct());
339 
340  // Body:
341 
342  // Construct the names for the new version
343 
344  string whole_name = version_to_name(xversion);
345  string jims_name(whole_name);
346  jims_name += "_jims";
347 
348  // Create the new whole() subposet and copy
349  // the old membership to it.
350 
351  subposet old_whole(whole());
352  powerset().whole().new_state();
353  powerset().whole().put_name(whole_name, true, false);
354  powerset().whole().make_equal_to(&old_whole);
355 
356  // Create the new jims() subposet and copy
357  // the old membership to it.
358 
359  subposet old_jims(jims());
360  jims().new_state();
361  jims().put_name(jims_name, true, false);
362  jims().make_equal_to(&old_jims);
363 
364  old_whole.detach_from_state();
365  old_jims.detach_from_state();
366 
367  // Postconditions:
368 
369  ensure(version_ct() == old_version_ct + 1);
370 
371  // Exit
372 
373  return;
374 }
375 
376 
377 // ===========================================================
378 // POSET FACET
379 // ===========================================================
380 
381 // PUBLIC FUNCTIONS
382 
383 // PROTECTED FUNCTIONS
384 
385 // PRIVATE FUNCTIONS
386 
387 bool
388 sheaf::refinable_poset::
389 make_prototype()
390 {
391  // Preconditions:
392 
393  // Body:
394 
395  // refinable poset uses POSET_ID.
396 
397  poset_type ltype = POSET_ID;
398 
399  refinable_poset* lproto = new refinable_poset;
400 
401  factory().insert_prototype(lproto);
402  factory().insert_prototype(ltype, lproto);
403 
404  // Postconditions:
405 
406  // Exit:
407 
408  return true;
409 }
410 
411 
412 // ===========================================================
413 // POSET_STATE_HANDLE FACET
414 // ===========================================================
415 
416 // PUBLIC FUNCTIONS
417 
420 type_id() const
421 {
422  return REFINABLE_POSET_ID;
423 }
424 
425 const char*
427 class_name() const
428 {
429  // Preconditions:
430 
431  // Body:
432 
433  static const char* result = "refinable_poset";
434 
435  // Postconditions:
436 
437  // Exit:
438 
439  return result;
440 }
441 
444 new_member(bool xis_jim, pod_index_type xdof_tuple_id)
445 {
446  // Preconditions:
447 
448  require(xis_jim ? in_jim_edit_mode() : state_is_read_write_accessible());
449  require(contains_row_dof_tuple(xdof_tuple_id) || !is_valid(xdof_tuple_id));
450 
451 
452  // Body:
453 
454  define_old_variable(int old_member_ct = member_ct());
455 
456  // Create the new member
457 
458  pod_index_type result = poset::new_member(xis_jim, xdof_tuple_id);
459 
460  // Insert the new member in the ccr.
461 
462  coarsest_common_refinement().insert_member(result);
463  if(xis_jim)
464  {
465  coarsest_common_refinement_jims().insert_member(result);
466  }
467 
468  // Postconditions:
469 
470  ensure(invariant());
471  ensure(cover_is_empty(LOWER, result));
472  ensure(cover_is_empty(UPPER, result));
473 
474  // Exit
475 
476  return result;
477 }
478 
479 // PROTECTED FUNCTIONS
480 
481 void
483 new_member(const scoped_index& xindex, bool xis_jim, const scoped_index& xdof_tuple_id)
484 {
485  // Preconditions:
486 
487  require(precondition_of(poset::new_member(same args)));
488 
489  // Body:
490 
491  define_old_variable(int old_member_ct = member_ct());
492 
493  // Create the new member
494 
495  poset::new_member(xindex.hub_pod(), xis_jim, xdof_tuple_id);
496 
497  // Insert the new member in the ccr.
498 
499  coarsest_common_refinement().insert_member(xindex);
500  if(xis_jim)
501  {
502  coarsest_common_refinement_jims().insert_member(xindex);
503  }
504 
505  // Postconditions:
506 
507  ensure(invariant());
508  ensure(contains_member(xindex));
509  ensure(cover_is_empty(LOWER, xindex));
510  ensure(cover_is_empty(UPPER, xindex));
511 
512  // Exit
513 
514  return;
515 }
516 
519 new_member_interval(const std::string& xinterval_type, size_type xsize)
520 {
521  // Preconditions:
522 
523  require(in_jim_edit_mode());
524 
525  // Body:
526 
527  define_old_variable(const scoped_index old_member_index_ub = member_index_ub());
528  define_old_variable(size_type old_member_ct = member_ct());
529 
530  // Create the new member
531 
532  pod_index_type result = poset::new_member_interval(xinterval_type, xsize);
533 
534  implicit_crg_interval& linterval = *crg().implicit_member(result);
535 
536  // Insert the new member in the ccr.
537 
538  for(pod_index_type i=result; i<member_index_ub().pod(); ++i)
539  {
540  coarsest_common_refinement().insert_member(i);
541  if(linterval.is_jim(i))
542  {
543  coarsest_common_refinement_jims().insert_member(i);
544  }
545  }
546 
547  // Postconditions:
548 
549  ensure(postcondition_of(poset::new_member_interval(xinterval_type, xsize)));
550  ensure_for_range(pod_index_type i=result, i<member_index_ub().pod(), ++i,
551  coarsest_common_refinement().contains_member(i));
552 
553  // Exit
554 
555  return result;
556 }
557 
558 void
560 new_member_interval(const scoped_index& xindex, const std::string& xinterval_type, size_type xsize)
561 {
562  // Preconditions:
563 
564  require(precondition_of(poset::new_member_interval(same args)));
565 
566  // Body:
567 
568  define_old_variable(const scoped_index old_member_index_ub = member_index_ub());
569  define_old_variable(size_type old_member_ct = member_ct());
570 
571  // Create the new member
572 
573  poset::new_member_interval(xindex.hub_pod(), xinterval_type, xsize);
574 
575  implicit_crg_interval& linterval = *crg().implicit_member(xindex.hub_pod());
576 
577  // Insert the new member in the ccr.
578 
579  for(pod_index_type i = linterval.begin(); i < linterval.end(); ++i)
580  {
581  coarsest_common_refinement().insert_member(i);
582  if(linterval.is_jim(i))
583  {
584  coarsest_common_refinement_jims().insert_member(i);
585  }
586  }
587 
588  // Postconditions:
589 
590  ensure(postcondition_of(poset::new_member_interval(xindex, xinterval_type, xsize)));
591  ensure_for_range(pod_index_type i=0, i<xsize, ++i, coarsest_common_refinement().contains_member(xindex.hub_id() + i));
592 
593  // Exit
594 
595  return;
596 }
597 
598 // PRIVATE FUNCTIONS
599 
600 
601 // ===========================================================
602 // ANY FACET
603 // ===========================================================
604 
605 // PUBLIC FUNCTIONS
606 
607 bool
609 is_ancestor_of(const any* xother) const
610 {
611  bool result;
612 
613  // Preconditions:
614 
615  // Body:
616 
617  result = dynamic_cast<const refinable_poset*>(xother) != 0;
618 
619  // Postconditions:
620 
621  // Exit
622 
623  return result;
624 }
625 
628 clone() const
629 {
630  refinable_poset* result;
631 
632  // Preconditions:
633 
634  // Body:
635 
636  result = new refinable_poset;
637 
638  // Postconditions:
639 
640  ensure(result != 0);
641  ensure(!result->is_attached());
642 
643  // Exit
644 
645  return result;
646 }
647 
648 bool
650 invariant() const
651 {
652  bool result = true;
653 
654  result = result && poset::invariant();
655 
656  if(invariant_check())
657  {
658  disable_invariant_check();
659 
662 
663  enable_invariant_check();
664  }
665 
666  return result;
667 }
668 
669 // PROTECTED FUNCTIONS
670 
671 // PRIVATE FUNCTIONS
672 
virtual ~refinable_poset()
Destructor.
virtual poset_path path(bool xauto_access=true) const
The path of this poset.
bool state_is_auto_read_write_accessible(bool xauto_access) const
True if state is auto accessible for read and write, that is, if the state is already accessible for ...
A client handle for a subposet.
Definition: subposet.h:86
virtual bool is_ancestor_of(const any *other) const
True if other conforms to this.
bool full() const
True if both poset name and member name are not empty.
Definition: poset_path.cc:311
poset_path path(bool xauto_access=true) const
A path to this component.
The default name space; a poset which contains other posets as members.
bool state_is_read_accessible() const
True if this is attached and if the state is accessible for read or access control is disabled...
bool path_is_auto_read_accessible(const poset_path &xpath, bool xauto_access) const
True if the state referred to xpath exists and is auto read accessible.
poset_type
Identifiers for poset types.
Definition: poset_type.h:41
void put_version(int xversion)
Set the current level to xversion.
A path defined by a poset name and a member name separated by a forward slash (&#39;/&#39;). For example: "cell_definitions/triangle".
Definition: poset_path.h:48
STL namespace.
subposet & coarsest_common_refinement_jims()
The jims of the coarsest_common_refinement (mutable version).
virtual void get_read_access() const
Get read access to the state associated with this.
subposet & coarsest_common_refinement()
The coarsest common refinement of all versions.
virtual refinable_poset * clone() const
Virtual constructor; creates a new handle of the same actual type as this, attached to the same state...
Abstract implementation of crg_interval for an interval of implicit cover relation graph members...
virtual void release_access(bool xall=false) const
Release access. If xall is true, release all levels of access. Otherwise, release one level of access...
Abstract base class with useful features for all objects.
Definition: any.h:39
virtual schema_poset_member & schema()
The schema for this poset (mutable version).
A factory for instanting descendants of an abstract type T, given the class name of the descendant...
Definition: eval_family.h:49
virtual void end_refine_mode(bool xensure_lattice_invariant=true, bool xauto_access=true)
Prevent editing of jims and jim order relation; synonym for end_jim_edit_mode(xensure_lattice_invaria...
A client handle for a partially order set that can be refined, that is, a jim can be converted to a j...
bool owns(const poset_state_handle &xposet, bool xauto_access) const
True if and only if this contains the poset xposet. synonym for contains_poset(xposet.poset_path(true), xauto_access)
An index within the external ("client") scope of a given id space.
Definition: scoped_index.h:116
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
virtual pod_index_type new_member(bool xis_jim, pod_index_type xdof_tuple_id)
Create a disconnected member with is_jim == xis_jim and the dof tuple identified by xdof_tuple_id...
bool contains_path(const poset_path &xpath, bool xauto_access=true) const
True if this contains the poset or poset member specified by xpath.
const bool UPPER
Selector for upper cover.
Definition: sheaf.h:72
virtual poset_type type_id() const
Identifier for the type of this poset.
refinable_poset()
Default constructor; creates a new refinable_poset handle not attached to any state.
bool empty() const
True if both poset name and member name are empty.
Definition: poset_path.cc:291
static refinable_poset & new_table(namespace_type &xhost, const poset_path &xpath, const poset_path &xschema_path, bool xauto_access)
Creates a new poset in namespace xns with path xpath and schema specified by xschema_path.
virtual bool is_jim(pod_index_type xmbr_index) const =0
True if and only if the member with index xmbr_index is join-irreducible.
const bool LOWER
Selector for lower cover.
Definition: sheaf.h:67
virtual void detach_from_state()
Detach this handle from its state, if any.
virtual bool is_attached() const
True if this is attached to a state.
subposet & coarsest_common_refinement_jims()
The jims of the coarsest common refinement.
virtual void begin_refine_mode(bool xauto_access=true)
Create a new version and allow editing of jims and jim order relation.
scoped_index hub_id() const
This mapped to the hub id space.
Definition: scoped_index.h:358
poset_powerset_state * powerset() const
The set of subposets of host().
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
virtual pod_index_type new_member_interval(const std::string &xinterval_type, size_type xsize, const block< pod_index_type > &xdof_tuple_ids, const block< pod_index_type > &xdata)
Create a disconnected member interval of type xinterval_type with size xsize, dof tuple ids xdof_tupl...
void insert_prototype(T *xprototype)
Sets xprototype as the prototype for its client class.
Definition: factory.impl.h:144
An array representation of abstract class poset_dof_map.
subposet & coarsest_common_refinement()
The coarsest common refinement of all versions of this poset (mutable version).
SHEAF_DLL_SPEC bool is_valid(pod_index_type xpod_index)
True if an only if xpod_index is valid.
Definition: pod_types.cc:37
bool state_is_not_read_accessible() const
True if this is attached and if the state is accessible for read or if access control is disabled...
A client handle for a poset member which has been prepared for use as a schema.
virtual bool invariant() const
Class invariant.
pod_type hub_pod() const
The pod value of this mapped to the unglued hub id space.
Definition: scoped_index.h:710
virtual const char * class_name() const
The name of this class.