Commits

Konstantin Baierer  committed ba55d3a

basic support for Graphs (classes that encapsulate a model)

  • Participants
  • Parent commits b00d670

Comments (0)

Files changed (7)

 *.tmp
 Makefile.PL
 MooseX-Semantic*
+cover_db/

File lib/MooseX/Semantic/Role/Graph.pm

+package MooseX::Semantic::Role::Graph;
+use Moose::Role;
+use MooseX::Semantic::Types qw(TrineModel TrineBlankOrUndef TrineNode TrineResource );
+
+with qw(
+    MooseX::Semantic::Role::Resource
+);
+
+=head1 NAME
+
+MooseX::Semantic::Role::Graph - Role for Moose objects that represent named graphs
+
+=cut
+
+has rdf_graph => (
+    traits => ['Semantic'],
+    is => 'rw',
+    isa => TrineModel,
+    coerce => 1,
+    default => sub { TrineModel->coerce },
+    # lazy => 1,
+);
+
+# around 'export_to_model' => sub {
+#     my $orig = shift;
+#     my $self = shift;
+#     my %args = @_;
+
+#     unless ($args{context}) {
+#     }
+# };
+
+1;

File lib/MooseX/Semantic/Role/RdfExport.pm

                 my ($attr, $stash) = @_;
                 my $iter = $stash->{attr_val}->as_stream;
                 while (my $stmt = $iter->next) {
+                    if ($self->does('MooseX::Semantic::Role::Graph')) {
+                        $stmt->[3] = $self->rdf_about;
+                    }
                     $model->add_statement($stmt);
                 }
             },
     
     my $req = HTTP::Request->new(POST => $uri);
     $req->header(Content_Type => $type);
-    my $model = $self->export_to_model($opts{model});
+    my $model = $self->export_to_model($opts{model}, $opts{context});
     $req->content( $ser->serialize_model_to_string($model) );
     
     my $res = $self->_user_agent->request($req);
 =cut
 
 sub export_to_hash {
+    # warn Dumper [@_];<>;
     my ($self, %opts) = @_;
     my $self_hash = {};
     $opts{max_recursion} //= 0;
             my ($attr, $stash) = @_;
             $self->_attr_to_hash( $self_hash, $attr, $stash->{attr_val}, %opts);
         },
+        model => sub {
+            my ($attr, $stash) = @_;
+            $self_hash->{$attr->name} = $self->{$attr->name}->as_hashref;
+        },
         resource => sub {
             my ($attr, $stash) = @_;
             my $self_hash_value = $self->export_to_hash( $stash->{attr_val}, %opts );

File lib/MooseX/Semantic/Types.pm

 use RDF::Trine qw(iri);
 use RDF::Trine::Namespace qw(xsd);
 use MooseX::Types -declare => [qw( 
+    TrineNode
     TrineBlank
     TrineResource 
+    TrineModel
+    TrineLiteral
+    TrineStore
+    TrineLiteralOrTrineResorce
+    TrineBlankOrUndef 
     ArrayOfTrineResources
+    ArrayOfTrineNodes
+    ArrayOfTrineLiterals
     HashOfTrineResources
-    TrineNode
-    ArrayOfTrineNodes
     HashOfTrineNodes
-    TrineLiteral
-    TrineBlankOrUndef 
-    ArrayOfTrineLiterals
     HashOfTrineLiterals
     CPAN_URI
     UriStr
-    TrineModel
-    TrineStore
     )];
 use MooseX::Types::URI Uri => { -as => 'MooseX__Types__URI__Uri' };
 use MooseX::Types::Moose qw{:all};

File lib/MooseX/Semantic/Util/TypeConstraintWalker.pm

 sub _find_parent_type_for_type_constraint {
     my ($self, $type_constraint, $needle, %opts) = @_;
     # warn Dumper $type_constraint->name;
-    warn Dumper $needle;
+    # warn Dumper $needle;
     $opts{max_depth} = 9999 unless defined $opts{max_depth};
     $opts{max_width} = 9999 unless defined $opts{max_width};
     $opts{current_depth} = 0 unless $opts{current_depth};
             $callback_name = 'literal';
         }
         elsif ($self->_find_parent_type($attr_type, 'Object')
-            || $self->_find_parent_type($attr_type, 'ClassName'))
+            # || $self->_find_parent_type($attr_type, 'ClassName')
+            )
         {
             $callback_name = 'resource';
-            if ($attr_type =~ /^RDF::Trine::Model/) {
+            # die Dumper $self->$attr_name;
+            if ($self->$attr_name->isa('RDF::Trine::Model')) {
+                # die "It's amodel";
                 $callback_name = 'model';
             }
         }
             }
         }
         else {
-            warn Dumper $attr_type->has_parent;
+            # warn Dumper $attr_type->has_parent;
             # warn Dumper $attr_type->parent;
-            warn Dumper ref $attr_type;
-            warn Dumper $self->_find_parent_type($attr_type, 'Object');
+            # warn Dumper ref $attr_type;
+            # warn Dumper $self->_find_parent_type($attr_type, 'Object');
             warn "Can't handle this attribute: $attr_name";
+            next;
         }
         $cb->{$callback_name}->($attr, $stash, @_);
     }
+let SessionLoad = 1
+if &cp | set nocp | endif
+let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0
+let v:this_session=expand("<sfile>:p")
+silent only
+cd /code/perl/p5-moosex-semantic
+if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''
+  let s:wipebuf = bufnr('%')
+endif
+set shortmess=aoO
+badd +12 lib/MooseX/Semantic/Role/RdfExport.pm
+badd +18 lib/MooseX/Semantic/Role/Graph.pm
+badd +0 lib/MooseX/Semantic/Util/TypeConstraintWalker.pm
+badd +0 t/rdf_export/graph.t
+silent! argdel *
+edit lib/MooseX/Semantic/Role/RdfExport.pm
+set splitbelow splitright
+wincmd _ | wincmd |
+vsplit
+1wincmd h
+wincmd w
+set nosplitbelow
+set nosplitright
+wincmd t
+set winheight=1 winwidth=1
+exe 'vert 1resize ' . ((&columns * 30 + 98) / 197)
+exe 'vert 2resize ' . ((&columns * 166 + 98) / 197)
+argglobal
+enew
+file NERD_tree_1
+setlocal fdm=marker
+setlocal fde=0
+setlocal fmr={{{,}}}
+setlocal fdi=#
+setlocal fdl=0
+setlocal fml=1
+setlocal fdn=20
+setlocal fen
+wincmd w
+argglobal
+setlocal fdm=marker
+setlocal fde=0
+setlocal fmr={{{,}}}
+setlocal fdi=#
+setlocal fdl=0
+setlocal fml=1
+setlocal fdn=20
+setlocal fen
+let s:l = 12 - ((11 * winheight(0) + 24) / 48)
+if s:l < 1 | let s:l = 1 | endif
+exe s:l
+normal! zt
+12
+normal! 0
+wincmd w
+exe 'vert 1resize ' . ((&columns * 30 + 98) / 197)
+exe 'vert 2resize ' . ((&columns * 166 + 98) / 197)
+tabedit lib/MooseX/Semantic/Role/Graph.pm
+set splitbelow splitright
+set nosplitbelow
+set nosplitright
+wincmd t
+set winheight=1 winwidth=1
+argglobal
+setlocal fdm=marker
+setlocal fde=0
+setlocal fmr={{{,}}}
+setlocal fdi=#
+setlocal fdl=0
+setlocal fml=1
+setlocal fdn=20
+setlocal fen
+let s:l = 15 - ((14 * winheight(0) + 24) / 48)
+if s:l < 1 | let s:l = 1 | endif
+exe s:l
+normal! zt
+15
+normal! 017l
+tabedit lib/MooseX/Semantic/Util/TypeConstraintWalker.pm
+set splitbelow splitright
+set nosplitbelow
+set nosplitright
+wincmd t
+set winheight=1 winwidth=1
+argglobal
+setlocal fdm=marker
+setlocal fde=0
+setlocal fmr={{{,}}}
+setlocal fdi=#
+setlocal fdl=0
+setlocal fml=1
+setlocal fdn=20
+setlocal fen
+let s:l = 1 - ((0 * winheight(0) + 24) / 48)
+if s:l < 1 | let s:l = 1 | endif
+exe s:l
+normal! zt
+1
+normal! 0
+tabedit t/rdf_export/graph.t
+set splitbelow splitright
+set nosplitbelow
+set nosplitright
+wincmd t
+set winheight=1 winwidth=1
+argglobal
+setlocal fdm=marker
+setlocal fde=0
+setlocal fmr={{{,}}}
+setlocal fdi=#
+setlocal fdl=0
+setlocal fml=1
+setlocal fdn=20
+setlocal fen
+let s:l = 20 - ((19 * winheight(0) + 24) / 48)
+if s:l < 1 | let s:l = 1 | endif
+exe s:l
+normal! zt
+20
+normal! 025l
+tabnext 4
+if exists('s:wipebuf')
+  silent exe 'bwipe ' . s:wipebuf
+endif
+unlet! s:wipebuf
+set winheight=1 winwidth=20 shortmess=filnxtToO
+let s:sx = expand("<sfile>:p:r")."x.vim"
+if file_readable(s:sx)
+  exe "source " . fnameescape(s:sx)
+endif
+let &so = s:so_save | let &siso = s:siso_save
+doautoall SessionLoadPost
+unlet SessionLoad
+" vim: set ft=vim :

File t/rdf_export/graph.t

+#=======================================================================
+# graph.t
+#=======================================================================
+use common::sense;
+use Test::Most tests => 1;
+use RDF::Trine qw(iri statement);
+use Data::Dumper;
+
+{
+    package GraphPackage;
+    use Moose;
+    with qw( MooseX::Semantic::Role::Graph MooseX::Semantic::Role::RdfExport );
+    has 'timestamp' => (
+        traits => ['Semantic'],
+        is => 'rw',
+        default => '1234',
+        uri => 'dc:date',
+    );
+}
+
+my $g = GraphPackage->new;
+# warn Dumper $g->rdf_graph;
+$g->rdf_graph->add_statement(statement iri('A'), iri('B'), iri('C') );
+# warn Dumper $g->rdf_graph;
+# warn Dumper ref $g->rdf_graph;
+# warn Dumper $g->export_to_hash;
+warn Dumper $g->export_to_string(format=>'nquads');
+