#  Copyright (c) 1997-2022
#  Ewgenij Gawrilow, Michael Joswig, and the polymake team
#  Technische Universität Berlin, Germany
#  https://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.
#-------------------------------------------------------------------------------

#  The aim of all these methods is to convert the obsolete properties read from a plain data file
#  to the corresponding sub-object properties.

sub translate_plain_file_type : method {
   (undef, my $legacy_type)=@_;
   ($legacy_type && eval("typeof $legacy_type"), typeof SimplicialComplex)
}

object SimplicialComplex {

   # the graph and dual graph related properties

   method upgrade_plain_GRAPH {
      my ($this, $value)=@_;
      $this->take("GRAPH.ADJACENCY", $value);
   }

   method upgrade_plain_DUAL_GRAPH {
      my ($this, $value)=@_;
      $this->take("DUAL_GRAPH.ADJACENCY", $value);
   }
   
   sub parse_graph_with_edge_attributes {
      my ($value, $attr_re)=@_;
      my $adjacency="";
      my @map;
      my $from=0;
      foreach (split /\n/, $value) {
         s{\(\s*(\d+)\s+($attr_re)\s*\)}{
            if ($from>=$1) {
               push @map, $2; $1
            } else {
               ""
            }
         }eg;
         $adjacency.="$_\n";
         ++$from;
      }
      return ($adjacency, \@map);
   }

   method upgrade_plain_MIXED_GRAPH {
      my ($this, $value)=@_;
      my ($adjacency, $weights)=parse_graph_with_edge_attributes($value, qr/\d+(\.\d+)?/);
      $this->take("MIXED_GRAPH.ADJACENCY", $adjacency);
      $this->take("MIXED_GRAPH.EDGE_WEIGHTS", $weights);
   }
   
   method upgrade_plain_VERTEX_DEGREES {
      my ($this, $value)=@_;
      $this->take("GRAPH.NODE_DEGREES", $value);
   }

   method upgrade_plain_CONNECTED {
      my ($this, $value, $boolean_value)=@_;
      $this->take("GRAPH.CONNECTED", $boolean_value);
   }

   method upgrade_plain_DUAL_CONNECTED {
      my ($this, $value, $boolean_value)=@_;
      $this->take("DUAL_GRAPH.CONNECTED", $boolean_value);
   }

   method upgrade_plain_CONNECTIVITY {
      my ($this, $value, $boolean_value)=@_;
      $this->take("GRAPH.CONNECTIVITY", $boolean_value);
   }

   method upgrade_plain_DUAL_CONNECTIVITY {
      my ($this, $value, $boolean_value)=@_;
      $this->take("DUAL_GRAPH.CONNECTIVITY", $boolean_value);
   }

   method upgrade_plain_CONNECTED_COMPONENTS {
      my ($this, $value)=@_;
      $this->take("GRAPH.CONNECTED_COMPONENTS", $value);
   }

   method upgrade_plain_DUAL_CONNECTED_COMPONENTS {
      my ($this, $value)=@_;
      $this->take("DUAL_GRAPH.CONNECTED_COMPONENTS", $value);
   }

   method upgrade_plain_N_CONNECTED_COMPONENTS {
      my ($this, $value)=@_;
      $this->take("GRAPH.N_CONNECTED_COMPONENTS", $value);
   }

   method upgrade_plain_MAX_CLIQUES {
      my ($this, $value)=@_;
      $this->take("GRAPH.MAX_CLIQUES", $value);
   }

   method upgrade_plain_DUAL_MAX_CLIQUES {
      my ($this, $value)=@_;
      $this->take("DUAL_GRAPH.MAX_CLIQUES", $value);
   }

   method upgrade_plain_BIPARTITE {
      my ($this, $value, $boolean_value)=@_;
      $this->take("GRAPH.BIPARTITE", $boolean_value);
   }

   method upgrade_plain_DUAL_BIPARTITE {
      my ($this, $value, $boolean_value)=@_;
      $this->take("DUAL_GRAPH.BIPARTITE", $boolean_value);
   }

   method upgrade_plain_GRAPH_SIGNATURE {
      my ($this, $value)=@_;
      $this->take("GRAPH.SIGNATURE", $value);
   }

   method upgrade_plain_DUAL_GRAPH_SIGNATURE {
      my ($this, $value)=@_;
      $this->take("DUAL_GRAPH.SIGNATURE", $value);
   }

   method upgrade_plain_COLORING {
      my ($this, $value)=@_;
      $this->take("COLORING",$value);
   }
   
   method upgrade_plain_ORIENTATION {
      my ($this, $value)=@_;
      my $o="";
      foreach (split /\ /, $value) {
        if ($_==-1) { $o.="false ";}
	else {$o.="true ";}
      }
      $this->take("ORIENTATION", $o);
   }

   method upgrade_plain_DUAL_COLORING {
      my ($this, $value)=@_;
      $this->take("DUAL_GRAPH.COLORING", $value);
   }

   method upgrade_plain_HASSE_DIAGRAM {
      my ($this, $value)=@_;
      $this->HASSE_DIAGRAM=graph::FaceLattice_from_plain_text($value);
   }

   method upgrade_plain_INTERSECTION_FORM {
      my ($this, $value)=@_;
      $value =~ s/[()]//g;
      $this->take("INTERSECTION_FORM", $value);
   }
 
   method upgrade_plain_BOUNDARY_OF_PSEUDO_MANIFOLD {
      my ($this, $value)=@_;
      my $res = squeeze_faces($value);
      $this->take("BOUNDARY.FACETS", $res->first);
      $this->take("BOUNDARY.VERTEX_MAP", $res->second);
   }
   
   method upgrade_plain_ODD_SUBCOMPLEX {
      my ($this, $value)=@_;
      $this->take("ODD_SUBCOMPLEX.INPUT_FACES", $value);
   }
   
   method upgrade_plain_VERTICES { }  # discard
}

# Local Variables:
# mode: perl
# c-basic-offset:3
# End:
