Skip to content

Commit

Permalink
Add types for RDF triple/quad and their terms
Browse files Browse the repository at this point in the history
  • Loading branch information
zmughal committed Oct 20, 2023
1 parent a7a2d57 commit 142be2c
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 2 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ t/store-simpletriple.t
t/term-map.t
t/term.t
t/treerewrite.t
t/types-general.t
t/types-iri.t
xt/dawg11-memory.t
xt/eval-sparql-star-memory-simpleeval.t
Expand Down
1 change: 1 addition & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ requires 'Sub::Util' => 1.40;
requires 'Test::Modern' => 0.012;
requires 'Test::Moose' => 0;
requires 'Test::Roo' => 0;
requires 'Test::TypeTiny' => 0;
requires 'Text::CSV' => 0;
requires 'Text::Table' => 0;
requires 'Try::Tiny' => 0;
Expand Down
91 changes: 89 additions & 2 deletions lib/Types/Attean.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ package Types::Attean;
use strict;
use warnings;

use Type::Library -base, -declare => qw( AtteanIRI );
use Types::Standard qw( Str InstanceOf ScalarRef );
use Type::Library -base, -declare => qw(
AtteanIRI
AtteanBlank
AtteanLiteral
AtteanSubject AtteanPredicate AtteanObject
AtteanGraph
AtteanTriple
AtteanQuad
);
use Types::Standard qw( Str InstanceOf ConsumerOf ScalarRef );
use Types::URI qw( Uri Iri );
use Types::Namespace qw( Namespace );
use Types::Path::Tiny qw( Path );
Expand Down Expand Up @@ -56,6 +64,46 @@ L<XML::Namespace> and strings.
Additionally, a C<ScalarRef> can be coerced into a C<data> URI.
=item C<< AtteanBlank >>
A role type for L<Attean::API::Blank>.
=item C<< AtteanLiteral >>
A role type for L<Attean::API::Literal>.
=item C<< AtteanSubject >>
A role type for a term that can be used as a subject
in a triple or quad
(i.e., L<Attean::API::BlankOrIRI>).
=item C<< AtteanPredicate >>
A role type for a term that can be used as a predicate
in a triple or quad
(i.e., L<Attean::API::IRI>).
=item C<< AtteanObject >>
A role type for a term that can be used as an object
in a triple or quad
(i.e., L<Attean::API::Term>).
=item C<< AtteanGraph >>
A role type for a term that can be used as a graph
in a quad
(i.e., L<Attean::API::BlankOrIRI>).
=item C<< AtteanTriple >>
A role type for L<Attean::API::Triple>.
=item C<< AtteanQuad >>
A role type for L<Attean::API::Quad>.
=back
=head1 OTHER COERCIONS
Expand Down Expand Up @@ -89,5 +137,44 @@ AtteanIRI->coercion->add_type_coercions(

require Attean::IRI;

__PACKAGE__->add_type(
name => AtteanBlank,
parent => ConsumerOf['Attean::API::Blank']
);

__PACKAGE__->add_type(
name => AtteanLiteral,
parent => ConsumerOf['Attean::API::Literal']
);

__PACKAGE__->add_type(
name => AtteanSubject,
parent => ConsumerOf['Attean::API::BlankOrIRI']
);

__PACKAGE__->add_type(
name => AtteanPredicate,
parent => ConsumerOf['Attean::API::IRI']
);

__PACKAGE__->add_type(
name => AtteanObject,
parent => ConsumerOf['Attean::API::Term']
);

__PACKAGE__->add_type(
name => AtteanGraph,
parent => ConsumerOf['Attean::API::BlankOrIRI']
);

__PACKAGE__->add_type(
name => AtteanTriple,
parent => ConsumerOf['Attean::API::Triple']
);

__PACKAGE__->add_type(
name => AtteanQuad,
parent => ConsumerOf['Attean::API::Quad']
);

1;
59 changes: 59 additions & 0 deletions t/types-general.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use Test::TypeTiny;
use Attean;
use Test::Requires { 'Attean::IRI' => '0.023' };
use Attean::RDF qw(
iri
blank
literal
triple
quad
);
use Types::Attean qw(
AtteanIRI
AtteanBlank
AtteanLiteral
AtteanSubject AtteanPredicate AtteanObject
AtteanGraph
AtteanTriple
AtteanQuad
);

my $iri = iri('http://www.example.net/');
my $blank = blank('b0');
my $literal = literal('foo');

my $triple = triple( $blank, $iri, $literal );
my $quad = quad( $blank, $iri, $literal, blank('g0') );

should_pass( $iri , AtteanIRI );
should_pass( $blank , AtteanBlank );
should_pass( $literal, AtteanLiteral );

note 'IRI can be in any position';
should_pass( $iri , AtteanSubject );
should_pass( $iri , AtteanPredicate );
should_pass( $iri , AtteanObject );
should_pass( $iri , AtteanGraph );

should_pass( $blank , AtteanSubject );
should_fail( $blank , AtteanPredicate , 'blank can not be a predicate');
should_pass( $blank , AtteanObject );
should_pass( $blank , AtteanGraph );

should_fail( $literal, AtteanSubject );
should_fail( $literal, AtteanPredicate );
should_pass( $literal, AtteanObject , 'literal can only be an object');
should_fail( $literal, AtteanGraph );

should_pass( $triple , AtteanTriple );
should_fail( $triple , AtteanQuad , 'triple is not a quad');

should_pass( $quad , AtteanTriple , 'quad is also a triple');
should_pass( $quad , AtteanQuad );

done_testing;

0 comments on commit 142be2c

Please sign in to comment.