File Coverage

blib/lib/ODO/Query/Simple/Mapper.pm
Criterion Covered Total %
statement 58 58 100.0
branch 11 12 91.6
condition n/a
subroutine 16 16 100.0
pod 4 4 100.0
total 89 90 98.8


line stmt bran cond sub pod time code
1             #
2             # Copyright (c) 2005-2006 IBM Corporation.
3             #
4             # All rights reserved. This program and the accompanying materials
5             # are made available under the terms of the Eclipse Public License v1.0
6             # which accompanies this distribution, and is available at
7             # http://www.eclipse.org/legal/epl-v10.html
8             #
9             # File: $Source: /var/lib/cvs/ODO/lib/ODO/Query/Simple/Mapper.pm,v $
10             # Created by: Stephen Evanchik( evanchik@us.ibm.com )
11             # Created on: 01/17/2005
12             # Revision: $Id: Mapper.pm,v 1.2 2009-11-25 17:53:53 ubuntu Exp $
13             #
14             # Contributors:
15             # IBM Corporation - initial API and implementation
16             #
17             package ODO::Query::Simple::Mapper;
18              
19 6     6   3377 use strict;
  6         13  
  6         196  
20 6     6   34 use warnings;
  6         14  
  6         156  
21 6     6   31 use vars qw /$VERSION/;
  6         12  
  6         516  
22             $VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /: (\d+)\.(\d+)/;
23 6     6   37 use ODO::Node;
  6         13  
  6         271  
24 6     6   40 use ODO::Statement;
  6         15  
  6         236  
25              
26 6     6   31 use base qw/ODO/;
  6         19  
  6         1921  
27              
28             __PACKAGE__->mk_accessors(qw/mapping/);
29              
30             =head1 NAME
31              
32             ODO::Query::Simple::Mapper
33              
34             =head1 SYNOPSIS
35              
36             Synopsis.
37              
38             =head1 DESCRIPTION
39              
40             Description.
41              
42             =head1 METHODS
43              
44             =over
45              
46             =item new( $source_simple_query, $dest_simple_query )
47              
48             This object creates a mapping between the variables of two ODO::Query::Simple
49             objects's. For example, if T1 contains the variable named 'A' in its subject
50             and T2 contains the variable 'A' in its object then the new
51             ODO::Query::Simple::Mapper object will return 'object' when its subject
52             method is invoked, i.e:
53              
54             ($mapper->subject() eq 'object' == 1)
55              
56             This can be used to compare statements with respect to their variables.
57              
58             =cut
59              
60              
61             sub new {
62 1     1 1 3 my ($self, $source, $dest) = @_;
63 1         12 return $self->SUPER::new(source=> $source, dest=> $dest);
64             }
65              
66              
67             =item compare( $source, $dest )
68              
69             compare does the following:
70              
71             1. Determine if the (s, p, o) of the source statement maps on to
72             either the s, p, or o of the destination statement.
73             2. If it does, compare the source statement's component to the
74             destination statement's component.
75             3. If they do not match return false
76             4. Return true if we make it through the loop
77              
78             =cut
79              
80             sub compare {
81 3     3 1 5 my ($self, $source, $dest) = @_;
82            
83 3         7 foreach my $component ('s', 'p', 'o') {
84            
85 7         18 my $destComponent = $self->$component();
86            
87             next
88 7 100       50 unless($destComponent);
89            
90             # The component's variable matches now see if the value
91             # matches
92 3 100       11 return 0
93             unless($source->$component()->equal($dest->$destComponent()));
94             }
95            
96 1         4 return 1;
97             }
98              
99              
100             =item find_var( $statementMatch, $variableNode )
101              
102             Looks for a variable in one of the components of the destination
103             statement.
104              
105             =cut
106              
107             sub find_var {
108 3     3 1 18 my ($self, $tm, $var) = @_;
109            
110 3         5 foreach my $c ( 's', 'p', 'o') {
111              
112 7 100       62 return $c
113             if($tm->$c()->value() eq $var->value());
114             }
115            
116 2         23 return undef;
117             }
118              
119              
120             sub BEGIN {
121              
122 6     6   39 no strict 'refs';
  6         11  
  6         742  
123            
124 6     6   17 foreach my $comp ('s', 'p', 'o') {
125            
126 18         54 my $fn = __PACKAGE__ . "::$comp";
127            
128             *$fn = sub {
129 7     7   10 my $self = shift;
130            
131             # A mapping may not exists for this particular statement match
132             # component
133             return undef
134 7 100       18 unless(exists($self->mapping()->{ $comp }));
135              
136 3         20 return $self->mapping()->{ $comp };
137 18         239 };
138             }
139            
140 6     6   35 use strict;
  6         12  
  6         129  
141             }
142              
143              
144 6     6   32 no warnings;
  6         12  
  6         495  
145              
146             *subject = \&s;
147             *predicate = \&p;
148             *object = \&o;
149              
150 6     6   30 use warnings;
  6         11  
  6         990  
151              
152              
153             sub init {
154 1     1 1 26 my ($self, $config) = @_;
155            
156 1         3 my $source = $config->{'source'};
157 1         3 my $dest = $config->{'dest'};
158              
159 1         6 $self->mapping( {} );
160            
161 1         19 foreach my $component ('s', 'p', 'o') {
162            
163             next
164 3 50       16 unless(UNIVERSAL::isa($source->$component(), 'ODO::Node::Variable'));
165            
166 3         26 my $mapped_component = $self->find_var($dest, $source->$component());
167            
168             next
169 3 100       36 unless($mapped_component);
170            
171             # At this point make it so that $self->subject() points to
172             # $dest->(subject|predicate|object)
173 1         5 $self->mapping()->{ $component } = $mapped_component;
174             }
175            
176 1         3 return $self;
177             }
178              
179              
180             =back
181              
182             =head1 AUTHOR
183              
184             IBM Corporation
185              
186             =head1 COPYRIGHT
187              
188             Copyright (c) 2004-2006 IBM Corporation.
189              
190             All rights reserved. This program and the accompanying materials
191             are made available under the terms of the Eclipse Public License v1.0
192             which accompanies this distribution, and is available at
193             http://www.eclipse.org/legal/epl-v10.html
194              
195             =cut
196              
197             1;
198              
199             __END__