/[suikacvs]/webroot/swe/lib/SWE/Object/Repository.pm
Suika

Contents of /webroot/swe/lib/SWE/Object/Repository.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations) (download)
Mon Sep 21 09:10:40 2009 UTC (16 years, 10 months ago) by wakaba
Branch: MAIN
Changes since 1.3: +24 -0 lines
++ swe/lib/SWE/DB/ChangeLog	21 Sep 2009 09:05:45 -0000
2009-09-21  Wakaba  <wakaba@suika.fam.cx>

	* Lock.pm (check_lockability): Don't allow the same level of lock
	type being locked twice to avoid deadlocks caused by same level of
	locks.

++ swe/lib/SWE/Object/ChangeLog	21 Sep 2009 09:10:06 -0000
	* Document.pm (repo, prop_untainted, untainted_prop, save_prop,
	locked): New method.  Introduced the concept of "tainted" such
	that we can access to the property in the locked code fragment
	without being afraid to update the property using old values.
	(get_or_create_graph_node): Updated to utilize |prop| family of
	method with locks.

	* Graph.pm (repo, lock, unlock): New methods.
	(add_nodes, create_node, schelling_update): Locks the database
	before the modifications.

	* Repository.pm (graph, get_document_by_id): New methods.

	* Node.pm (repo): New method.

2009-09-21  Wakaba  <wakaba@suika.fam.cx>

++ swe/lib/suikawiki/ChangeLog	21 Sep 2009 09:10:27 -0000
	* main.pl: Made the graph node view to lock the database.

2009-09-21  Wakaba  <wakaba@suika.fam.cx>

1 wakaba 1.1 package SWE::Object::Repository;
2     use strict;
3     use warnings;
4 wakaba 1.4 use Scalar::Util qw/weaken/;
5 wakaba 1.1
6     sub new ($%) {
7     my $class = shift;
8     my $self = bless {@_}, $class;
9    
10     return $self;
11     }
12    
13     sub db ($) { $_[0]->{db} }
14    
15 wakaba 1.4 sub graph ($) {
16     my $self = shift;
17     return $self->{graph} ||= do {
18     require SWE::Object::Graph;
19     my $g = SWE::Object::Graph->new (repo => $self, db => $self->db);
20     weaken $g->{repo};
21     $g;
22     };
23     } # graph
24    
25     sub get_document_by_id ($$) {
26     my ($self, $doc_id) = @_;
27    
28     return $self->{document}->{$doc_id} ||= do {
29     require SWE::Object::Document;
30     my $doc = SWE::Object::Document->new
31     (repo => $self, db => $self->db, id => $doc_id);
32     weaken $doc->{repo};
33     $doc;
34     };
35     } # get_document_by_id
36    
37     # XXX
38 wakaba 1.1 my $weight_file_name = 'data/weight.txt';
39    
40     sub term_weight_vector ($) {
41     my $self = shift;
42    
43     ## TODO: lock
44    
45     ## TODO: use global props
46    
47     return $self->{term_weight_vector} ||= do {
48     require SWE::Data::FeatureVector;
49    
50     my $w;
51     if (-f $weight_file_name) {
52     local $/ = undef;
53     open my $file, '<:encoding(utf8)', $weight_file_name or die "$0: $weight_file_name: $!";
54     $w = SWE::Data::FeatureVector->parse_stringref (\<$file>);
55     close $file;
56     } else {
57     $w = SWE::Data::FeatureVector->new;
58     }
59     delete $self->{term_weight_vector_modified};
60     $w;
61     };
62     } # term_weight_vector
63    
64     sub save_term_weight_vector ($) {
65     my $self = shift;
66     return unless $self->{term_weight_vector_modified};
67    
68     ## TODO: use global props
69    
70     open my $file, '>:encoding(utf8)', $weight_file_name or die "$0: $weight_file_name: $!";
71     print $file $self->{term_weight_vector}->stringify;
72     close $file;
73     } # save_term_weight_vector
74    
75     sub are_related_ids ($$$;$) {
76     my ($self, $id1, $id2, $answer) = @_;
77    
78     my $w = $self->term_weight_vector;
79    
80     my $tfidf_db = $self->db->id_tfidf;
81 wakaba 1.2
82     ## TODO: cache
83 wakaba 1.1
84     require SWE::Data::FeatureVector;
85     my $fv1 = SWE::Data::FeatureVector->parse_stringref
86 wakaba 1.3 ($tfidf_db->get_data ($id1) // return undef);
87 wakaba 1.1 my $fv2 = SWE::Data::FeatureVector->parse_stringref
88 wakaba 1.3 ($tfidf_db->get_data ($id2) // return undef);
89 wakaba 1.1
90     my $diff = $fv1->subtract ($fv2);
91 wakaba 1.2
92     my $i = 0;
93     A: {
94     my $wx = $diff->multiply ($w)->component_sum;
95     my $y = $wx >= 0 ? 1 : -1;
96    
97     if (defined $answer and $y * $answer < 0) {
98     $w = $y > 0 ? $w->subtract ($diff) : $w->add ($diff);
99     $self->{term_weight_vector} = $w;
100     $self->{term_weight_vector_modified} = 1;
101     $i++;
102     redo A unless $i > 20;
103     }
104    
105     return $y > 0;
106 wakaba 1.1 }
107     } # are_related_ids
108    
109     1;

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24