-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgen_lattice.pl
More file actions
executable file
·365 lines (306 loc) · 9.86 KB
/
gen_lattice.pl
File metadata and controls
executable file
·365 lines (306 loc) · 9.86 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env perl
use strict;
use warnings;
use DBI qw(:sql_types); # libdbi-perl
use DBD::SQLite::Constants qw(:file_open); # libdbd-sqlite3-perl
use JSON; # libjson-perl
use Data::Dumper;
use File::Path qw(make_path);
use File::Basename qw(dirname);
use Cwd qw(cwd);
use Digest::MD5 qw(md5_hex); # libdigest-md5-perl
BEGIN {
my $conexp_cp = dirname($0) . '/../bin/conexp-1.3/*';
if (!defined $ENV{'CLASSPATH'} || $ENV{'CLASSPATH'} eq '') {
$ENV{'CLASSPATH'} = $conexp_cp;
} else {
$ENV{'CLASSPATH'} .= ';' . $conexp_cp;
}
}
# libinline-java-perl
use Inline (
Java => 'study',
autostudy => 1,
study => [
'conexp.core.Context',
'conexp.frontend.ContextDocumentModel',
'java.lang.System'
]
);
sub sqlFetch {
my $dbh = shift;
my $context_name = shift;
my $lang = shift;
my $sql = shift;
my $sth = $dbh->prepare($sql);
die $DBI::errstr if !defined $sth;
$sth->bind_param(1, $context_name, SQL_VARCHAR);
$sth->bind_param(2, $lang, SQL_VARCHAR);
my $rv = $sth->execute();
die $sth->errstr if !defined $rv;
return $sth->fetchall_arrayref({});
}
sub importContextAsContext {
my $dbfile = shift;
my $context_name = shift;
my $lang = shift;
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile", '', '',
{ sqlite_open_flags => SQLITE_OPEN_READONLY });
die $DBI::errstr if !defined $dbh;
my $attr_rh = sqlFetch($dbh, $context_name, $lang,
'SELECT Attribute FROM v_attribute_context ' .
'WHERE Context = ? AND Lang = ?');
my @attributes = map { $_->{'Attribute'} } @$attr_rh;
my $numAttributes = scalar @attributes;
my $obj_rh = sqlFetch($dbh, $context_name, $lang,
'SELECT Code AS Id, Object AS Name FROM v_object_context ' .
'WHERE Context = ? AND Lang = ?');
my @objects = map { $_->{'Id'} } @$obj_rh;
my $numObjects = scalar @objects;
my %objectsByName = map { $_->{'Name'} => $_->{'Id'} } @$obj_rh;
my $assig_rh = sqlFetch($dbh, $context_name, $lang,
'SELECT ObjectCode AS ObjectId, Attribute FROM v_context_assignments ' .
'WHERE x = 1 AND Context = ? AND Lang = ?');
my $context = new conexp::core::Context($numObjects, $numAttributes);
for (my $i = 0; $i < $numObjects; $i++) {
my $id = $objects[$i];
$context->getObject($i)->setName($obj_rh->[$i]{'Name'});
for (my $j = 0; $j < $numAttributes; $j++) {
my $attr = $attributes[$j];
$context->getAttribute($j)->setName($attr) if $i == 1;
if (scalar(grep { $_->{'ObjectId'} eq $id && $_->{'Attribute'} eq $attr } @$assig_rh) > 0) {
$context->setRelationAt($i, $j, 1);
}
}
}
return {
'model' => new conexp::frontend::ContextDocumentModel($context),
'context' => $context,
'attributes' => \@attributes,
'objects' => \@objects,
'objectsByName' => \%objectsByName
};
}
sub objectSetToList {
my $set = shift;
my $extractor = shift;
my $iterator = shift;
$iterator = $set->iterator() if !defined $iterator;
my @list = ();
while ($iterator->hasNext()) {
my $element = $iterator->next();
push @list, &$extractor($element);
}
return \@list;
}
sub writeAttributeSet {
my $set = shift;
my $attrs = shift;
for (my $i = 0; $i < $set->size(); $i++) {
if ($set->in($i)) {
print STDERR $attrs->[$i], ',';
}
}
}
sub getObjectHash {
my $object = shift;
return java::lang::System->identityHashCode($object);
}
sub debugContext {
my $context = shift;
my $objects = shift;
my @attributes = ();
print STDERR 'Attributes (', $context->getAttributeCount(), '): ';
for (my $i = 0; $i < $context->getAttributeCount(); $i++) {
my $attribute = $context->getAttribute($i);
my $name = $attribute->getName();
push @attributes, $name;
print STDERR $name, ', ';
}
print STDERR "\n\n";
print STDERR 'Objects (', $context->getObjectCount(), "):\n\n";
for (my $i = 0; $i < $context->getObjectCount(); $i++) {
my $object = $context->getObject($i);
print STDERR $object->getName(), ' (id: ', $objects->[$i], ') ';
for (my $j = 0; $j < $context->getAttributeCount(); $j++) {
if ($context->getRelationAt($i, $j)) {
print STDERR $attributes[$j], " ";
}
}
print STDERR "\n\n";
}
}
sub debugConcepts {
my $concepts = shift;
my $attributes = shift;
my $objects = shift;
for (my $i = 0; $i < $concepts->conceptsCount(); $i++) {
print STDERR 'Concept ', $i, "\n";
my $concept = $concepts->conceptAt($i);
my $intent = $concept->getAttribs();
print STDERR 'Intent (size ', $intent->length(), '): ';
for (my $j = 0; $j < $intent->size(); $j++) {
if ($intent->in($j)) {
print STDERR $attributes->[$j], ' ';
}
}
print STDERR "\n";
my $extent = $concept->getObjects();
print STDERR 'Extent (size ', $extent->length(), '): ';
for (my $j = 0; $j < $extent->size(); $j++) {
if ($extent->in($j)) {
print STDERR $objects->[$j], ' ';
}
}
print STDERR "\n\n";
}
}
sub debugImplications {
my $implications = shift;
my $attributes = shift;
print STDERR 'Implications (', $implications->getSize(), ")\n";
for (my $i = 0; $i < $implications->getSize(); $i++) {
my $implication = $implications->getImplication($i);
print STDERR $i, ' <', $implication->getObjectCount(), '> ';
writeAttributeSet($implication->getPremise(), $attributes);
print STDERR ' ==> ';
writeAttributeSet($implication->getConclusion(), $attributes);
print STDERR "\n";
}
print STDERR "\n";
}
sub debugAssociations {
my $associations = shift;
my $attributes = shift;
print STDERR 'Associations (', $associations->getSize(), ")\n";
for (my $i = 0; $i < $associations->getSize(); $i++) {
my $association = $associations->getDependency($i);
print STDERR $i, ' <', $association->getPremiseSupport(), '> ';
writeAttributeSet($association->getPremise(), $attributes);
print STDERR ' [', $association->getConfidence(), '] ';
print STDERR ' ==> ';
print STDERR ' <', $association->getRuleSupport(), '> ';
writeAttributeSet($association->getConclusion(), $attributes);
print STDERR "\n";
}
print STDERR "\n";
}
my $DBFILE = $ARGV[0];
my $CONTEXT = $ARGV[1];
my $LANG = $ARGV[2];
my $DEBUG = $ARGV[3];
my $result = importContextAsContext($DBFILE, $CONTEXT, $LANG);
my $model = $result->{'model'};
my $context = $result->{'context'};
my $attributes = $result->{'attributes'};
my $objects = $result->{'objects'};
my $objectsByName = $result->{'objectsByName'};
debugContext($context, $objects) if $DEBUG;
$model->resetLatticeComponents();
my $latticecomp = $model->getLatticeComponent(0);
$latticecomp->calculateLattice();
my $lattice = $latticecomp->getLattice();
my $concepts = $lattice;
$model->findAssociations();
my $associations = $model->getAssociationRules();
$model->findImplications();
my $implications = $model->getImplications();
debugConcepts($concepts, $attributes, $objects) if $DEBUG;
debugImplications($implications, $attributes) if $DEBUG;
debugAssociations($associations, $attributes) if $DEBUG;
# Convert context to an associative array
my %acontext = ();
for (my $i = 0; $i < $context->getObjectCount(); $i++) {
my $object = $context->getObject($i);
my @attributesList = ();
for (my $j = 0; $j < $context->getAttributeCount(); $j++) {
if ($context->getRelationAt($i, $j)) {
push @attributesList, $attributes->[$j];
}
}
my $aobject = {
'name' => $object->getName(),
'id' => $objects->[$i],
'attributes' => \@attributesList
};
$acontext{$objects->[$i]} = $aobject;
}
# Convert concept table to a list of associative arrays
my @lconcepts = ();
for (my $i = 0; $i < $concepts->conceptsCount(); $i++) {
my $concept = $concepts->conceptAt($i);
my @intentList = ();
my $intent = $concept->getAttribs();
for (my $j = 0; $j < $intent->size(); $j++) {
push @intentList, $attributes->[$j] if $intent->in($j);
}
my @extentList = ();
my $extent = $concept->getObjects();
for (my $j = 0; $j < $extent->size(); $j++) {
push @extentList, $objects->[$j] if $extent->in($j);
}
my $md5 = md5_hex(join('', @intentList, @extentList));
my $aconcept = {
'intent' => \@intentList,
'extent' => \@extentList,
'md5' => $md5
};
push @lconcepts, $aconcept;
}
# Convert nodes to a list of associative arrays
sub getAttribName {
my $attribObject = shift;
return $attribObject->getName();
}
sub getObjectId {
my $object = shift;
return $objectsByName->{$object->getName()};
}
my %anodes = ();
my @lnodes = ();
for (my $i = 0; $i < $concepts->conceptsCount(); $i++) {
my $node = $concepts->elementAt($i);
my @parents = ();
for (my $j = 0; $j < $node->getSuccCount(); $j++) {
push @parents, getObjectHash($node->getSucc($j));
}
my @children = ();
for (my $j = 0; $j < $node->getPredCount(); $j++) {
push @children, getObjectHash($node->getPred($j));
}
my $hash = getObjectHash($node);
my $anode = {
'hash' => $hash,
'idx' => $i,
'level' => $node->getHeight(),
'concept' => $lconcepts[$i],
'parents' => \@parents,
'children' => \@children,
'labelObjects' => objectSetToList(undef, \&getObjectId, $node->ownObjectsIterator()),
'labelAttributes' => objectSetToList(undef, \&getAttribName, $node->ownAttribsIterator())
};
$anodes{$hash} = $anode;
push @lnodes, $anode;
}
print STDERR Dumper(\%anodes) if $DEBUG;
my @links = ();
foreach my $node (@lnodes) {
foreach my $child (@{$node->{'children'}}) {
push @links, {
'source' => $node->{'idx'},
'target' => $anodes{$child}->{'idx'}
};
}
}
print STDERR Dumper(\@links) if $DEBUG;
my $json = JSON->new()
->pretty
->canonical
->encode(
{
'context' => \%acontext,
'concepts' => \@lconcepts,
'nodes' => \@lnodes,
'links' => \@links
});
print $json;