TM::Serializable::CSV - Topic Maps, trait for parsing (and later dumping) CSV stream


TM documentation Contained in the TM distribution.

Index


Code Index:

NAME

Top

TM::Serializable::CSV - Topic Maps, trait for parsing (and later dumping) CSV stream

SYNOPSIS

Top

   # 1) bare bones
   my $tm = .....;  # get a map from somewhere (can be empty)
   Class::Trait->apply ($tm, "TM::Serializable::CSV");

   use Perl6::Slurp;
   $tm->deserialize (slurp 'myugly.csv');

   # 2) exploiting the timed sync in/out mechanism
   my $tm = new TM::.... (url => 'file:myugly.csv'); # get a RESOURCEABLE map from somewhere
   $tm->sync_in;

DESCRIPTION

Top

This trait provides parsing and dumping from CSV formatted text streams.

INTERFACE

Top

Methods

deserialize

$tm->deserialize ($text)

This method consumes the text string passed in and interprets it as CSV formatted information. What topic map information is generated, depends on the header line (the first line):

If the header line contains a field called association-type, then all rows will be interpreted as assertions. In that the remaining header fields (in that order) are interpreted as roles (role types). For all rows in the CSV stream, the position where the association-type field was is ignored. The other fields (in that order) are affiliated with the corresponding roles.

Example:

   association-type,location,bio-unit
   is-born,gold-coast,rumsti
   is-born,vienna,ramsti

Scoping cannot be controlled. Also all players and roles (obviously) are directly interpreted as identifiers. Subject identifiers and locators are not (yet) implemented.

If the header line contains a field called id, then all further rows will be interpreted as topic characteristics, with each topic on one line. The column position where the id field in the header is will be interpreted as toplet identifier.

All further columns will be interpreted according to the following:

If the header column is named name, the values will be used as topic names.

Otherwise if the value looks like a URI, an occurrence with that URI value will be be added to the topic.

Otherwise an occurrence with a string value will be added to the topic.

Example:

   name,id,location,homepage
   "Rumsti",rumsti,gold-coast,http://rumsti.com
   "Ramsti",ramsti,vienna,http://ramsti.com

serialize

$tm->serialize

[Since TM 1.53] This method serializes a fragment of a topic map into CSV. Which fragment can be controlled with the header line and options (see constructor).

header_line (only for serialization)

This string contains a comma separated list (CSV parseable) of headings. If one of the headings is association-type, then the generated CSV content will contain associations only. Nothing else is implemented yet. The other headings control which roles (and in which order) should be included in the CSV content. If a particular role type has more than one player, then all players are included.

NOTE: As this is inconsistent, this will have to change.

type (only for serialization)

If existing, then this controls which association type is to be taken.

baseuri (only for serialization)

If existing and non-zero, the base URI of the map will remain in the identifiers. Otherwise it will be removed.

specification

If existing (and when selecting only associations), this specification will be interpreted in the sense of asserts (see TM).

Example:

    $tm->serialize (header_line => 'association-type,location,bio-unit',
                    type        => 'is-born',
                    baseuri     => 0);

SEE ALSO

Top

TM, TM::Serializable

AUTHOR INFORMATION

Top

Copyright 2010 Robert Barta.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. http://www.perl.com/perl/misc/Artistic.html


TM documentation Contained in the TM distribution.
package TM::Serializable::CSV;

use strict;
use warnings;

use Class::Trait 'base';
use Class::Trait 'TM::Serializable';

use Data::Dumper;


sub deserialize {
    my $self = shift;
    my $stream = shift;
    my %options = @_;

    $stream =~ s/\r//g;                                                                      # F...ing M$
    my @lines = split /\n/, $stream;

    use Text::CSV;
    my $csv = Text::CSV->new({ always_quote => 1 });

    #-- first line -------------------------
    $csv->parse (shift @lines); # and warn $csv->error_diag ();
    my @headers = map { s/ /-/g; $_ }                                                        # get rid of blanks, I hate blanks
                  $csv->fields();

    if (defined (my $pos = _find_and_kill (\@headers, 'association-type'))) {
	foreach my $line (@lines) {
	    $csv->parse ($line) ; #and warn  $csv->error_diag ();
	    my @players = $csv->fields();
	    my $type    = splice @players, $pos, 1;

	    if (my $p = $options{preprocess}) {                                              # if the application wants to modify the values before adding
		foreach my $i (0..$#players) {                                               # clumsily step through the player/header lists
		    $players[$i] = &$p ( $headers[$i], $players[$i] );                       # apply the change
		}
	    }
#	    warn "$type: ".Dumper (\@headers, \@players);
	    
	    $self->assert (
		Assertion->new (kind    => TM->ASSOC,
				type    => $type,
				scope   => 'us',
				roles   => \@headers,
				players => \@players)
		);
	}
	sub _find_and_kill {
	    my $roles = shift;
	    my $kill  = shift;
	    foreach my $i (0 .. $#$roles) {
		if ($roles->[$i] eq $kill) {
		    splice @$roles, $i, 1;  # remove that from the roles list, but ...
		    return $i;              # we memorize its position
		}
	    }
	    return undef;
	}
    } elsif (defined ($pos = _find_and_kill (\@headers, 'id'))) {
	foreach my $line (@lines) {
	    $csv->parse ($line) ; #and warn  $csv->error_diag ();
	    my @attrs = $csv->fields();
	    my $id    = splice @attrs, $pos, 1;

	    $self->internalize ($id);

	    use Regexp::Common qw /URI/;
	    use TM::Literal;
	    foreach my $i (0..$#attrs) {
		if (my $p = $options{preprocess}) {
		    eval {
			$attrs[$i] = &$p ( $headers[$i], $attrs[$i] );                    # user-defined transformation of the value
		    };
		    next if $@;                                                           # on die, we simply skip that attribute
		}

		if ($headers[$i] eq 'name') {
		    $self->assert (
			Assertion->new (kind    => TM->NAME,
					type    => 'name',
					scope   => 'us',
					roles   => [ 'topic', 'value'],
					players => [ $id,    new TM::Literal ($attrs[$i], TM::Literal->STRING)  ])
			);

		} elsif ($attrs[$i] =~ /$RE{URI}/) {
		    $self->assert (
			Assertion->new (kind    => TM->OCC,
					type    => $headers[$i],
					scope   => 'us',
					roles   => [ 'thing', 'value'],
					players => [ $id,  new TM::Literal ($attrs[$i], TM::Literal->URI) ])
			);

		} else {
		    $self->assert (
			Assertion->new (kind    => TM->OCC,
					type    => $headers[$i],
					scope   => 'us',
					roles   => [ 'thing', 'value'],
					players => [ $id,   new TM::Literal ($attrs[$i], TM::Literal->STRING) ])
			);
		}
	    }
	    
	}
    } else {
	die;
    }

}

sub serialize {
    my $self    = shift;
    my $headers = shift;
    my %options = @_;

    my $bu = $self->baseuri; # we may need it later

    use Text::CSV;
    my $csv = Text::CSV->new();

    $csv->parse ($headers);
    my @headers = $csv->fields;

    my $content = ( join ",", @headers ) . "\n";
    if (grep { $_ eq 'association-type' } @headers) {
	my @as;
	if ($options{type}) {
	    @as = $self->match_forall (type => $self->tids ($options{type}));

	} elsif ($options{specification}) {
	    @as = $self->asserts (\ $options{specification});

	} else {
	    my $spec = \ '+associations';
	    @as = $self->asserts ($spec);
	}

	foreach my $a ( @as ) {
	    my @vs;
	    foreach my $h (@headers) {
		if ($h eq 'association-type') {
		    push @vs, $a->[TM->TYPE];
		} else {
		    push @vs, TM::get_players ($self, $a, $self->tids ($h));
		}
	    }
	    @vs = map { $_ =~ s/^$bu// ; $_ } @vs unless ($options{baseuri});
	    
	    $content .= (join ",", @vs) . "\n";
	}

    } else {
	die "not yet implemented";
    }
    return $content;
}

our $VERSION = 0.02;

1;