File: Dump.pm

package info (click to toggle)
libowl-directsemantics-perl 0.001-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 664 kB
  • sloc: perl: 5,519; makefile: 7; sh: 1
file content (101 lines) | stat: -rw-r--r-- 2,308 bytes parent folder | download | duplicates (3)
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
package OWL::DirectSemantics::Writer::Dump;

BEGIN {
	$OWL::DirectSemantics::Writer::Dump::AUTHORITY = 'cpan:TOBYINK';
	$OWL::DirectSemantics::Writer::Dump::VERSION   = '0.001';
};

use 5.008;

use Moose::Role;

requires qw[element_name];

sub dump
{
	my ($self, $indent) = @_;
	$indent = '' unless defined $indent;
	
	my @keyvalue;
	foreach ($self->meta->get_all_attributes)
	{
		my $attr = $_->name;
		next if $attr eq 'annotations';
		next if $attr eq 'axioms';
		next if $attr eq 'metadata';
		my $val = $self->$attr;
		$val = [$val] unless ref($val) eq 'ARRAY';
		push @keyvalue, sprintf('%s=%s', $attr,
			(join ',',
				map {
					if (blessed($_) and $_->isa('RDF::Trine::Node'))
						{ $_->as_ntriples }
					elsif (!defined $_)
						{ 'null' }
					elsif (ref $_)
						{ ref $_ }
					else
						{ $_ }
					} @$val
				)
			)||'null';
	}
	
	my $keyvalue = join ' ', sort @keyvalue;
	
	my @anns   = $self->annotations;
	my @axioms = $self->axioms if $self->can('axioms');
	my $str = '';
	if (@anns || @axioms)
	{
		$str .= sprintf("%s%s(\n", $indent, $self->element_name);
		$str .= sprintf("%s\t%s\n", $indent, $keyvalue);
		$str .= $_->dump("\t${indent}") foreach @anns; 
		$str .= $_->dump("\t${indent}") foreach @axioms; 
		$str .= sprintf("%s\t)\n", $indent);
	}
	else
	{
		$str .= sprintf("%s%s( %s )\n", $indent, $self->element_name, $keyvalue);
	}
	
	return $str;
}

1;

=head1 NAME

OWL::DirectSemantics::Writer::Dump - Moose::Role providing reasonably readable output

=head1 DESCRIPTION

This Moose::Role provides a C<dump> method to output an element for debugging.

It requires the object or class it is composed with to provide an C<element_name> method.

C<element_name> is essentially the name of the "function" the object represents, e.g.
'ClassAssertion'.

=head1 SEE ALSO

L<OWL::DirectSemantics>,
L<OWL::DirectSemantics::Element>.

=head1 AUTHOR

Toby Inkster E<lt>tobyink@cpan.orgE<gt>.

=head1 COPYRIGHT

Copyright 2011-2012 Toby Inkster

This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=head1 DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.