This file is indexed.

/usr/lib/python2.7/dist-packages/swap/llyn.py is in python-swap 1.2.1-7.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
"""
$Id: llyn.py,v 1.185 2007/12/11 21:18:08 syosi Exp $


RDF Store and Query engine

Logic Lookup: Yet another Name

(also, in Wales, a lake - a storage area at the centre of the valley?)

This is an engine which knows a certian amount of stuff and can manipulate it.
It is a (forward chaining) query engine, not an (backward chaining) inference engine:
that is, it will apply all rules it can
but won't figure out which ones to apply to prove something.  It is not
optimized particularly.

Used by cwm - the closed world machine.
See:  http://www.w3.org/DesignIssues/Notation3

Interfaces
==========

This store stores many formulae, where one formula is what in
straight RDF implementations is known as a "triple store".
So look at the Formula class for a triple store interface.

See also for comparison, a python RDF API for the Redland library (in C):
   http://www.redland.opensource.ac.uk/docs/api/index.html 
and the redfoot/rdflib interface, a python RDF API:
   http://rdflib.net/latest/doc/triple_store.html

    
Copyright ()  2000-2004 World Wide Web Consortium, (Massachusetts Institute
of Technology, European Research Consortium for Informatics and Mathematics,
Keio University). All Rights Reserved. This work is distributed under the
W3C Software License [1] in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.
"""

# emacsbug="""emacs got confused by long string above"""

from __future__ import generators
# see http://www.amk.ca/python/2.2/index.html#SECTION000500000000000000000

from set_importer import Set, ImmutableSet

import types
import string

import re
import StringIO
import sys
import time, xml
from warnings import warn


import urllib # for log:content
import md5, binascii  # for building md5 URIs

import uripath
from uripath import canonical

from sax2rdf import XMLtoDOM
import xml.dom.minidom

from why import smushedFormula, Premise, newTopLevelFormula, isTopLevel

import notation3    # N3 parsers and generators, and RDF generator
from webAccess import webget

import diag  # problems importing the tracking flag,
             # and chatty_flag must be explicit it seems: use diag.tracking

from diag import progress, verbosity, tracking
from term import BuiltIn, LightBuiltIn, RDFBuiltIn, HeavyBuiltIn, Function, \
    MultipleFunction, ReverseFunction, MultipleReverseFunction, \
    Literal, XMLLiteral, Symbol, Fragment, FragmentNil, Term, LabelledNode, \
    CompoundTerm, List, EmptyList, NonEmptyList, AnonymousNode, N3Set, \
    UnknownType
from formula import Formula, StoredStatement
import reify

from weakref import WeakValueDictionary

from query import think, applyRules, testIncludes
import webAccess
from webAccess import DocumentAccessError
from local_decimal import Decimal

from RDFSink import Logic_NS, RDFSink, forSomeSym, forAllSym
from RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4
from RDFSink import N3_nil, N3_first, N3_rest, OWL_NS, N3_Empty, N3_List, \
                    N3_li, List_NS
from RDFSink import RDF_NS_URI

from RDFSink import FORMULA, LITERAL, LITERAL_DT, LITERAL_LANG, ANONYMOUS, SYMBOL

from pretty import Serializer

LITERAL_URI_prefix = "data:application/rdf+n3-literal;"
Delta_NS = "http://www.w3.org/2004/delta#"
cvsRevision = "$Revision: 1.185 $"


# Magic resources we know about

from RDFSink import RDF_type_URI, DAML_sameAs_URI

from why import Because, BecauseBuiltIn, BecauseOfRule, \
    BecauseOfExperience, becauseSubexpression, BecauseMerge ,report

STRING_NS_URI = "http://www.w3.org/2000/10/swap/string#"
META_NS_URI = "http://www.w3.org/2000/10/swap/meta#"
INTEGER_DATATYPE = "http://www.w3.org/2001/XMLSchema#integer"
FLOAT_DATATYPE = "http://www.w3.org/2001/XMLSchema#double"
DECIMAL_DATATYPE = "http://www.w3.org/2001/XMLSchema#decimal"
BOOL_DATATYPE = "http://www.w3.org/2001/XMLSchema#boolean"

#reason=Namespace("http://www.w3.org/2000/10/swap/reason#")

META_mergedWith = META_NS_URI + "mergedWith"
META_source = META_NS_URI + "source"
META_run = META_NS_URI + "run"

doMeta = 0  # wait until we have written the code! :-)
    

class DataObject:
    """The info about a term in the context of a specific formula
    It is created by being passed the formula and the term, and is
    then accessed like a python dictionary of sequences of values. Example:
    
    F = myWorkingFormula
    x = F.theObject(pred=rdfType obj=fooCar)
    for y in x[color][label]
    """
    def __init__(context, term):
        self.context = context
        self.term = term
        
    def __getItem__(pred):   #   Use . or [] ?
        values = context.objects(pred=pred, subj=self.term)
        for v in value:
            yield DataObject(self.context, v)


def arg_hash(arg):
    if isinstance(arg, dict):
        g = []
        for k, v in arg.items():
            g.append((arg_hash(k), arg_hash(v)))
        return hash(tuple(g))
    if isinstance(arg, (tuple, list)):
        g = []
        for k in arg:
            g.append(arg_hash(k))
        return hash(tuple(g))
    if isinstance(arg, Set):
        g = []
        for k in arg:
            g.append(arg_hash(k))
        return hash(ImmutableSet(g))
    return hash(arg)

def memoize(f):
    mymap = {}
    def k(*args, **keywords):
        n = arg_hash((args, keywords))
        if n not in mymap:
            mymap[n] = f(*args, **keywords)
        else:
            if diag.chatty_flag > 10:
                progress("momoizing helped!")
        return mymap[n]
    return k


####
###  BCHAIN @@@ Is this the right way to do it?
####
BACKWARD_CHAINING = False
class VARHASH(object):
    def __repr__(self): return 'VAR'
    def __str__(self): return 'VAR'
VARHASH = VARHASH()
####
###  /BCHAIN
####


###################################### Forumula
#
class IndexedFormula(Formula):
    """A formula which has indexes to facilitate queries.
    
    A formula is either open or closed.  Initially, it is open. In this
    state is may be modified - for example, triples may be added to it.
    When it is closed, note that a different interned version of itself
    may be returned. From then on it is a constant.
    
    Only closed formulae may be mentioned in statements in other formuale.
    
    There is a reopen() method but it is not recommended, and if desperate should
    only be used immediately after a close().
    """
    def __init__(self, store, uri=None):
        Formula.__init__(self, store, uri)
#       self._redirections = {}
        self.descendents = None   # Placeholder for list of closure under subcontext
#       self.collector = None # Object collecting evidence, if any 
        self._newRedirections = {}  # not subsituted yet
        self._index = {}
        self._index[(None,None,None)] = self.statements

        self._closureMode = ""
        self._closureAgenda = []
        self._closureAlready = []
        self.reallyCanonical = False


    def statementsMatching(self, pred=None, subj=None, obj=None):
        """Return a READ-ONLY list of StoredStatement objects matching the parts given
        
        For example:
        for s in f.statementsMatching(pred=pantoneColor):
            print "We've got one which is ", `s[OBJ]`
            
        If none, returns []
        """
        return self._index.get((pred, subj, obj), [])

    def contains(self, pred=None, subj=None, obj=None):
        """Return boolean true iff formula contains statement(s) matching the parts given
        
        For example:
        if f.contains(pred=pantoneColor):
            print "We've got one statement about something being some color"
        """
        x =  self._index.get((pred, subj, obj), [])
        if x : return 1
        return 0


    def any(self, subj=None, pred=None, obj=None):
        """Return None or the value filing the blank in the called parameters.
        
        Specifiy exactly two of the arguments.
        color = f.any(pred=pantoneColor, subj=myCar)
        somethingRed = f.any(pred=pantoneColor, obj=red)
        
        Note difference from the old store.any!!
        Note SPO order not PSO.
        To aboid confusion, use named parameters.
        """
        hits = self._index.get((pred, subj, obj), [])
        if not hits: return None
        s = hits[0]
        if pred is None: return s[PRED]
        if subj is None: return s[SUBJ]
        if obj is None: return s[OBJ]
        raise ParameterError("You must give one wildcard")


    def the(self, subj=None, pred=None, obj=None):
        """Return None or the value filing the blank in the called parameters
        
        This is just like any() except it checks that there is only
        one answer in the store. It wise to use this when you expect only one.
        
        color = f.the(pred=pantoneColor, subj=myCar)
        redCar = f.the(pred=pantoneColor, obj=red)
        """
        hits = self._index.get((pred, subj, obj), [])
        if not hits: return None
        assert len(hits) == 1, """There should only be one match for (%s %s %s).
            Found: %s""" %(subj, pred, obj, self.each(subj, pred, obj))
        s = hits[0]
        if pred is None: return s[PRED]
        if subj is None: return s[SUBJ]
        if obj is None: return s[OBJ]
        raise parameterError("You must give one wildcard using the()")

    def each(self, subj=None, pred=None, obj=None):
        """Return a list of values value filing the blank in the called parameters
        
        Examples:
        colors = f.each(pred=pantoneColor, subj=myCar)
        
        for redthing in f.each(pred=pantoneColor, obj=red): ...
        
        """
        hits = self._index.get((pred, subj, obj), [])
        if hits == []: return []
        if pred is None: wc = PRED
        elif subj is None: wc = SUBJ
        elif obj is None: wc = OBJ
        else: raise ParameterError("You must give one wildcard None for each()")
        res = []
        for s in hits:
            res.append(s[wc])   # should use yeild @@ when we are ready
        return res

    def searchable(self, subj=None, pred=None, obj=None):
        """A pair of the difficulty of searching and a statement iterator of found statements
        
        The difficulty is a store-portable measure of how long the store
        thinks (in arbitrary units) it will take to search.
        This will only be used for choisng which part of the query to search first.
        If it is 0 there is no solution to the query, we know now.
        
        In this implementation, we use the length of the sequence to be searched."""
        res = self._index.get((pred, subj, obj), [])
#        progress("searchable  %s, %s" %(self.statements, (pred, subj, obj))
        return len(res), res


    def add(self, subj, pred, obj, why=None):
        """Add a triple to the formula.
        
        The formula must be open.
        subj, pred and obj must be objects as for example generated by Formula.newSymbol()
        and newLiteral(), or else literal values which can be interned.
        why     may be a reason for use when a proof will be required.
        """
        if self.canonical != None:
            raise RuntimeError("Attempt to add statement to closed formula "+`self`)
        store = self.store
        
        if not isinstance(subj, Term): subj = store.intern(subj)
        if not isinstance(pred, Term): pred = store.intern(pred)
        if not isinstance(obj, Term): obj = store.intern(obj)
        newBindings = {}

#       Smushing of things which are equal into a single node
#       Even if we do not do this with owl:sameAs, we do with lists

        subj = subj.substituteEquals(self._redirections, newBindings)
        pred = pred.substituteEquals(self._redirections, newBindings)
        obj = obj.substituteEquals(self._redirections, newBindings)
            
        if diag.chatty_flag > 90:
            progress(u"Add statement (size before %i, %i statements) to %s:\n {%s %s %s}" % (
                self.store.size, len(self.statements),`self`,  `subj`, `pred`, `obj`) )
        if self.statementsMatching(pred, subj, obj):
            if diag.chatty_flag > 97:
                progress("Add duplicate SUPPRESSED %s: {%s %s %s}" % (
                    self,  subj, pred, obj) )
            return 0  # Return no change in size of store
            
        assert not isinstance(pred, Formula) or pred.canonical is pred, "pred Should be closed"+`pred`
        assert (not isinstance(subj, Formula)
                or subj is self
                or subj.canonical is subj), "subj Should be closed or self"+`subj`
        assert not isinstance(obj, Formula) or obj.canonical is obj, "obj Should be closed"+`obj`+`obj.canonical`
        store.size = store.size+1 # rather nominal but should be monotonic

        if False and isTopLevel(self):
            if isinstance(subj, Formula) and not subj.reallyCanonical:
                raise RuntimeError(subj.debugString())
            if isinstance(obj, Formula) and not obj.reallyCanonical:
                raise RuntimeError(obj.debugString())

# We collapse lists from the declared daml first,rest structure into List objects.
# To do this, we need a bnode with (a) a first; (b) a rest, and (c) the rest being a list.
# We trigger list collapse on any of these three becoming true.
# @@@ we don't reverse this on remove statement.  Remove statement is really not a user call.

# (Not clear: how t smush symbols without smushing variables. Need separate python class
# for variables I guess as everyone has been saying.
# When that happens, expend smushing to symbols.)

        if pred is store.rest:
            if isinstance(obj, List) and  subj in self._existentialVariables:
                ss = self.statementsMatching(pred=store.first, subj=subj)
                if ss:
                    s = ss[0]
                    self.removeStatement(s)
                    first = s[OBJ]
                    list = obj.prepend(first)
                    self._noteNewList(subj, list, newBindings)
                    self.substituteEqualsInPlace(newBindings, why=why)
                    return 1  # Added a statement but ... it is hidden in lists
    
        elif pred is store.first  and  subj in self._existentialVariables:
            ss = self.statementsMatching(pred=store.rest, subj=subj)
            if ss:
                s = ss[0]
                rest = s[OBJ]
                if isinstance(rest, List):
                    list = rest.prepend(obj)
                    self.removeStatement(s)
                    self._noteNewList(subj, list, newBindings)
                    self.substituteEqualsInPlace(newBindings)
                    return 1

        if pred is store.owlOneOf:
            if isinstance(obj, List) and subj in self._existentialVariables:
                new_set = store.newSet(obj)
                self._noteNewSet(subj, new_set, newBindings)
                self.substituteEqualsInPlace(newBindings)
                return 1

        if "e" in self._closureMode:
            if pred is store.sameAs:
                if subj is obj: return 0 # ignore a = a
                if obj in self.existentials() and subj not in self.existentials():
                    var, val = obj, subj
                elif ((subj in self.existentials() and obj not in self.existentials())
                    or (subj.generated() and not obj.generated())
                    or Term.compareAnyTerm(obj, subj) < 0): var, val = subj, obj
                else: var, val = obj, subj
                newBindings[var] = val
                if diag.chatty_flag > 90: progress("Equality: %s = %s" % (`var`, `val`))
                self.substituteEqualsInPlace(newBindings)               
                return 1

        if "T" in self._closureMode:
            if pred is store.type and obj is store.Truth:
                assert isinstance(subj, Formula), "What are we doing concluding %s is true?" % subj
                subj.resetRenames(True)
                self.loadFormulaWithSubstitution(subj.renameVars())
                subj.resetRenames(False)

#########
        if newBindings != {}:
            self.substituteEqualsInPlace(newBindings)
#######


        s = StoredStatement((self, pred, subj, obj))
        
        if diag.tracking:
            if (why is None): raise RuntimeError(
                "Tracking reasons but no reason given for"+`s`)
            report(s, why)

        # Build 8 indexes.
#       This now takes a lot of the time in a typical  cwm run! :-(
#       I honestly think the above line is a bit pessemistic. The below lines scale.
#       The above lines do not (removeStatement does not scale)

        if subj is self:  # Catch variable declarations
            if pred is self.store.forAll:
                if obj not in self._universalVariables:
                    if diag.chatty_flag > 50: progress("\tUniversal ", obj)
                    self._universalVariables.add(obj)
                return 1
            if pred is self.store.forSome:
                if obj not in self._existentialVariables:
                    if diag.chatty_flag > 50: progress("\tExistential ", obj)
                    self._existentialVariables.add(obj)
                return 1
            raise ValueError("You cannot use 'this' except as subject of forAll or forSome")

        self.statements.append(s)
       
        list = self._index.get((None, None, obj), None)
        if list is None: self._index[(None, None, obj)]=[s]
        else: list.append(s)

        list = self._index.get((None, subj, None), None)
        if list is None: self._index[(None, subj, None)]=[s]
        else: list.append(s)

        list = self._index.get((None, subj, obj), None)
        if list is None: self._index[(None, subj, obj)]=[s]
        else: list.append(s)

        list = self._index.get((pred, None, None), None)
        if list is None: self._index[(pred, None, None)]=[s]
        else: list.append(s)

        rein = self.newSymbol('http://dig.csail.mit.edu/2005/09/rein/network#requester')
        list = self._index.get((pred, None, obj), None)
        if list is None: self._index[(pred, None, obj)]=[s]
        else: list.append(s)

        list = self._index.get((pred, subj, None), None)
        if list is None: self._index[(pred, subj, None)]=[s]
        else: list.append(s)

        list = self._index.get((pred, subj, obj), None)
        if list is None: self._index[(pred, subj, obj)]=[s]
        else: list.append(s)

        if self._closureMode != "":
            self.checkClosure(subj, pred, obj)

        try:
            if self.isWorkingContext and diag.chatty_flag > 20:
                progress("adding",  (subj, pred, obj))
        except:
            pass

        return 1  # One statement has been added  @@ ignore closure extras from closure
                    # Obsolete this return value? @@@ 

                
    def removeStatement(self, s):
        """Removes a statement The formula must be open.
        
        This implementation is alas slow, as removal of items from tha hash is slow.
        The above statement is false. Removing items from a hash is easily over five times
        faster than removing them from a list.
        Also, truth mainainance is not done.  You can't undeclare things equal.
        This is really a low-level method, used within add() and for cleaning up the store
        to save space in purge() etc.
        """
        assert self.canonical is None, "Cannot remove statement from canonnical"+`self`
        self.store.size = self.store.size-1
        if diag.chatty_flag > 97:  progress("removing %s" % (s))
        context, pred, subj, obj = s.quad
        self.statements.remove(s)
        self._index[(None, None, obj)].remove(s)
        self._index[(None, subj, None)].remove(s)
        self._index[(None, subj, obj)].remove(s)
        self._index[(pred, None, None)].remove(s)
        self._index[(pred, None, obj)].remove(s)
        self._index[(pred, subj, None)].remove(s)
        self._index[(pred, subj, obj)].remove(s)
        #raise RuntimeError("The triple is %s: %s %s %s"%(context, pred, subj, obj))
        return

    def newCanonicalize(F):  ## Horrible name!
        if self._hashval is not None:
            return
        from term import Existential, Universal
        statements = []
        def convert(n):
            if isinstance(n, Universal):
                return Universal
            if isinstance(n, Existential):
                return Existential
            return n
        for statement in self.statements:
            statements.append(tuple([convert(x) for x in statement]))
        self._hashval = hash(ImmutableSet(statements))
    
    def canonicalize(F, cannon=False):
        """If this formula already exists, return the master version.
        If not, record this one and return it.
        Call this when the formula is in its final form, with all its
        statements.  Make sure no one else has a copy of the pointer to the
        smushed one.  In canonical form,
         - the statments are ordered
         - the lists are all internalized as lists
         
        Store dependency: Uses store._formulaeOfLength
        """
        if diag.chatty_flag > 70:
            progress('I got here')
        store = F.store
        if F.canonical != None:
            if diag.chatty_flag > 70:
                progress("End formula -- @@ already canonical:"+`F`)
            return F.canonical
        if F.stayOpen:
            if diag.chatty_flag > 70:
                progress("Canonicalizion ignored: @@ Knowledge base mode:"+`F`)
            return F

        F.store._equivalentFormulae.add(F)

        if (diag.tracking and isTopLevel(F)) or (F._renameVarsMaps and not cannon):  ## we are sitting in renameVars --- don't bother
            F.canonical = F
            return F
 
        fl = F.statements
        l = len(fl), len(F.universals()), len(F.existentials()) 
        possibles = store._formulaeOfLength.get(l, None)  # Any of same length

        if possibles is None:
            store._formulaeOfLength[l] = [F]
            if diag.chatty_flag > 70:
                progress("End formula - first of length", l, F)
            F.canonical = F
            F.reallyCanonical = True
 
            return F
        if diag.chatty_flag > 70:
            progress('I got here, possibles = ', possibles)
        fl.sort()
        fe = F.existentials()
        #fe.sort(Term.compareAnyTerm)
        fu = F.universals ()
        #fu.sort(Term.compareAnyTerm)

        for G in possibles:
        
#           progress("Just checking.\n",
#                   "\n", "_"*80, "\n", F.debugString(),
#                   "\n", "_"*80, "\n", G.debugString(),
#                   )
            gl = G.statements
            gkey = len(gl), len(G.universals()), len(G.existentials())
            if gkey != l: raise RuntimeError("@@Key of %s is %s instead of %s"
                %(G, `gkey`, `l`))

            gl.sort()
            for se, oe, in  ((fe, G.existentials()),
                             (fu, G.universals())):
                if se != oe:
                    break
            
            for i in range(l[0]):
                for p in PRED, SUBJ, OBJ:
                    if (fl[i][p] is not gl[i][p]):
#                       progress("""Mismatch on part %i on statement %i.
#           Becaue  %s  is not   %s"""
#                       % (p, i, `fl[i][p].uriref()`, `gl[i][p].uriref()`),
#                               )
#                       for  x in (fl[i][p], gl[i][p]):
#                           progress("Class %s, id=%i" %(x.__class__, id(x)))
#
#                       if Formula.unify(F,G):
#                           progress("""Unifies but did not match on part %i on statement %i.
#                           Because  %s  is not   %s"""
#                                       % (p, i, `fl[i][p].uriref()`, `gl[i][p].uriref()`),
#                                   "\n", "_"*80, "\n", F.debugString(),
#                                   "\n", "_"*80, "\n", G.debugString()
#                                   )
#                           raise RuntimeError("foundOne")

                        break # mismatch
                else: #match one statement
                    continue
                break
            else: #match
#               if not Formula.unify(F,G):
#                   raise RuntimeError("Look the same but don't unify")
                if tracking: smushedFormula(F,G)
                if diag.chatty_flag > 70: progress(
                    "** End Formula: Smushed new formula %s giving old %s" % (F, G))
                F.canonical = G
                del(F)  # Make sure it ain't used again
                return G



        possibles.append(F)
        F.canonical = F
        F.reallyCanonical = True
        if diag.chatty_flag > 70:
            progress("End formula, a fresh one:"+`F`)
##            for k in possibles:
##                print 'one choice is'
##                print k.n3String()
##                print '--------\n--------'
##            raise RuntimeError(F.n3String())
        return F


    def reopen(self):
        """Make a formula which was once closed oopen for input again.
        
        NOT Recommended.  Dangers: this formula will be, because of interning,
        the same objet as a formula used elsewhere which happens to have the same content.
        You mess with this one, you mess with that one.
        Much better to keep teh formula open until you don't needed it open any more.
        The trouble is, the parsers close it at the moment automatically. To be fixed."""
        return self.store.reopen(self)

    def setClosureMode(self, x):
        self._closureMode = x

    def checkClosure(self, subj, pred, obj):
        """Check the closure of the formula given new contents
        
        The s p o flags cause llyn to follow those parts of the new statement.
        i asks it to follow owl:imports
        r ask it to follow doc:rules
        """
        firstCall = (self._closureAgenda == [])
        if "s" in self._closureMode: self.checkClosureOfSymbol(subj)
        if "p" in self._closureMode: self.checkClosureOfSymbol(pred)
        if ("o" in self._closureMode or
            "t" in self._closureMode and pred is self.store.type):
            self.checkClosureOfSymbol(obj)
        if (("r" in self._closureMode and
              pred is self.store.docRules) or
            ("i" in self._closureMode and
              pred is self.store.imports)):   # check subject? @@@  semantics?
            self.checkClosureDocument(obj)
        if firstCall:
            while self._closureAgenda != []:
                x = self._closureAgenda.pop()
                self._closureAlready.append(x)
                x.dereference("m" + self._closureMode, self)
    
    def checkClosureOfSymbol(self, y):
        if not isinstance(y, Fragment): return
        return self.checkClosureDocument(y.resource)

    def checkClosureDocument(self, x):
        if x != None and x not in self._closureAlready and x not in self._closureAgenda:
            self._closureAgenda.append(x)


    def outputStrings(self, channel=None, relation=None):
        """Fetch output strings from store, sort and output

        To output a string, associate (using the given relation) with a key
        such that the order of the keys is the order in which you want the corresponding
        strings output.
        """
        if channel is None:
            channel = sys.stdout
        if relation is None:
            relation = self.store.intern((SYMBOL, Logic_NS + "outputString"))
        list = self.statementsMatching(pred=relation)  # List of things of (subj, obj) pairs
        pairs = []
        for s in list:
            pairs.append((s[SUBJ], s[OBJ]))
        pairs.sort(comparePair)
        for key, str in pairs:
            if not hasattr(str, "string"):
                print `str`
            channel.write(str.string.encode('utf-8'))


    def debugString(self, already=[]):
        """A simple dump of a formula in debug form.
        
        This formula is dumped, using ids for nested formula.
        Then, each nested formula mentioned is dumped."""
        red = ""
        if self._redirections != {}: red = " redirections:" + `self._redirections`
        str = `self`+ red + unicode(id(self)) + " is {"
        for vv, ss in ((self.universals().copy(), "@forAll"),(self.existentials().copy(), "@forSome")):
            if vv != Set():
                str = str + " " + ss + " " + `vv.pop()`
                for v in vv:
                    str = str + ", " + `v`
                str = str + "."
        todo = []
        for s in self.statements:
            subj, pred, obj = s.spo()
            str = str + "\n%28s  %20s %20s ." % (`subj`, `pred`, `obj`)
            for p in PRED, SUBJ, OBJ:
                if (isinstance(s[p], CompoundTerm)
                    and s[p] not in already and s[p] not in todo and s[p] is not self):
                    todo.append(s[p])
        str = str+ "}.\n"
        already = already + todo + [ self ]
        for f in todo:
            str = str + "        " + f.debugString(already)
        return str

    def _noteNewList(self,  bnode, list, newBindings):
        """Note that we have a new list.
        
        Check whether this new list (given as bnode) causes other things to become lists.
        Set up redirection so the list is used from now on instead of the bnode.        
        Internal function.

        This function is extraordinarily slow, .08 seconds per call on reify/reify3.n3"""
        if diag.chatty_flag > 80: progress("New list was %s, now %s = %s"%(`bnode`, `list`, `list.value()`))
        if isinstance(bnode, List): return  ##@@@@@ why is this necessary? weid.
        newBindings[bnode] = list
        if diag.chatty_flag > 80: progress("...New list newBindings %s"%(`newBindings`))
        self._existentialVariables.discard(bnode)
        possibles = self.statementsMatching(pred=self.store.rest, obj=bnode)  # What has this as rest?
        for s in possibles[:]:
            L2 = s[SUBJ]
            ff = self.statementsMatching(pred=self.store.first, subj=L2)
            if ff != []:
                first = ff[0][OBJ]
                self.removeStatement(s) 
                self.removeStatement(ff[0])
                list2 = list.prepend(first)
                self._noteNewList(L2, list2, newBindings)
        possibleSets = self.statementsMatching(pred=self.store.owlOneOf, obj=bnode)
        if possibleSets:
            new_set = self.store.newSet(list)
        for s in possibleSets[:]:
            s2 = s[SUBJ]
            if s2 in self._existentialVariables:
                self.removeStatement(s)
                self._noteNewSet(s2, new_set, newBindings)
        return

    def _noteNewSet(self, bnode, set, newBindings):
        newBindings[bnode] = set
        if diag.chatty_flag > 80: progress("...New set newBindings %s"%(`newBindings`))
        self._existentialVariables.discard(bnode)

    def substituteEqualsInPlace(self, redirections, why=None):
        """Slow ... does not use indexes"""
        bindings = redirections
        while bindings != {}:
            self._redirections.update(bindings)
            newBindings = {}
            for s in self.statements[:]:  # take a copy!
                changed = 0
                quad = [self, s[PRED], s[SUBJ], s[OBJ]]
                for p in PRED, SUBJ, OBJ:
                    x = s[p]
                    y = x.substituteEquals(bindings, newBindings)
                    if y is not x:
                        if diag.chatty_flag>90: progress("Substituted %s -> %s in place" %(x, y))
                        changed = 1
                        quad[p] = y
                if changed:
                    self.removeStatement(s)
                    self.add(subj=quad[SUBJ], pred=quad[PRED], obj=quad[OBJ], why=why)
            bindings = newBindings
            if diag.chatty_flag>70: progress("Substitions %s generated %s" %(bindings, newBindings))
        return

##    def unify(self, other, vars=Set([]), existentials=Set([]),  bindings={}):
    def unifySecondary(self, other, env1, env2, vars,
                       universals, existentials,
                       n1Source, n2Source):
        if self.canonical and other.canonical and self.store._equivalentFormulae.connected(self, other):
            yield (env1, env2)
        else:
            from query import n3Equivalent, testIncludes
            freeVars = self.freeVariables()   ## We can't use these
            retVal = n3Equivalent(self, other, env1, env2, vars,
                       universals, existentials,
                       n1Source, n2Source) # \
    ##               and n3Entails(self, other, vars=vars, existentials=existentials, bindings=bindings)
            for (env11, env12) in retVal:
                if env11 == env1 and env12 == env2:
                    self.store._equivalentFormulae.merge(self, other)
                yield (env11, env12)

##    unify = memoize(unify)



def comparePair(self, other):
    "Used only in outputString"
    for i in 0,1:
        x = self[i].compareAnyTerm(other[i])
        if x != 0:
            return x





###############################################################################################
#
#                       C W M - S P E C I A L   B U I L T - I N s
#
###########################################################################
    
# Equivalence relations

class BI_EqualTo(LightBuiltIn,Function, ReverseFunction):
    def eval(self,  subj, obj, queue, bindings, proof, query):
        return (subj is obj)   # Assumes interning

    def evalObj(self, subj, queue, bindings, proof, query):
        return subj

    def evalSubj(self, obj, queue, bindings, proof, query):
        return obj

class BI_notEqualTo(LightBuiltIn):
    def eval(self, subj, obj, queue, bindings, proof, query):
        return (subj is not obj)   # Assumes interning

BI_SameAs = BI_EqualTo

#### I hope this is never really added
##class BI_RunAsPython(LightBuiltIn, Function):
##    def evaluateObject(self, subject):
##        return eval(subject)

# Functions 
    
class BI_uri(LightBuiltIn, Function, ReverseFunction):

    def evalObj(self, subj, queue, bindings, proof, query):
        type, value = subj.asPair()
        if type == SYMBOL:
            return self.store.intern((LITERAL, value))

    def evaluateSubject(self, object):
        """Return the object which has this string as its URI
        
        #@@hm... check string for URI syntax?
        # or at least for non-uri chars, such as space?
        Note that relative URIs can be OK as the whole process
        has a base, which may be irrelevant. Eg see roadmap-test in retest.sh
        """
        store = self.store
        if ':' not in object:
            progress("Warning: taking log:uri of non-abs: %s" % object)
            return None
        #except (TypeError, AttributeError):
        #    return None
        return store.intern((SYMBOL, object))


class BI_dtlit(LightBuiltIn, Function):
    """built a datatype literal from a string and a uri"""
     
    def evaluateObject(self, subj_py):
        lex, dt = subj_py
        if dt is self.store.symbol("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral"):
            try:
                dom = XMLtoDOM(lex)
            except SyntaxError, e:
                raise UnknownType # really malformed literal
            return self.store.newXMLLiteral(dom)
        else:
            return self.store.newLiteral(lex, dt)


class BI_rawUri(BI_uri):
    """This is like  uri except that it allows you to get the internal
    identifiers for anonymous nodes and formuale etc."""
     
    def evalObj(self, subj, queue, bindings, proof, query):
        type, value = subj.asPair()
        return self.store.intern((LITERAL, value))


class BI_rawType(LightBuiltIn, Function):
    """
    The raw type is a type from the point of view of the langauge: is
    it a formula, list, and so on. Needed for test for formula in finding subformulae
    eg see test/includes/check.n3 
    """

    def evalObj(self, subj,  queue, bindings, proof, query):
        store = self.store
        if isinstance(subj, Literal): y = store.Literal
        elif isinstance(subj, Formula): y = store.Formula
        elif isinstance(subj, List): y = store.List
        elif isinstance(subj, N3Set): y = store.Set
        elif isinstance(subj, AnonymousNode): y = store.Blank
        else: y = store.Other  #  None?  store.Other?
        if diag.chatty_flag > 91:
            progress("%s  rawType %s." %(`subj`, y))
        return y
        

class BI_racine(LightBuiltIn, Function):    # The resource whose URI is the same up to the "#" 

    def evalObj(self, subj,  queue, bindings, proof, query):
        if isinstance(subj, Fragment):
            return subj.resource
        else:
            return subj

# Heavy Built-ins

class BI_includes(HeavyBuiltIn):
    """Check that one formula does include the other.
    This limits the ability to bind a variable by searching inside another
    context. This is quite a limitation in some ways. @@ fix
    """
    def eval(self, subj, obj, queue, bindings, proof, query):
        store = subj.store
        if isinstance(subj, Formula) and isinstance(obj, Formula):
            return testIncludes(subj, obj, bindings=bindings) # No (relevant) variables
        return 0
            
    
class BI_notIncludes(HeavyBuiltIn):
    """Check that one formula does not include the other.

    notIncludes is a heavy function not only because it may take more time than
    a simple search, but also because it must be performed after other work so that
    the variables within the object formula have all been subsituted.  It makes no sense
    to ask a notIncludes question with variables, "Are there any ?x for which
    F does not include foo bar ?x" because of course there will always be an
    infinite number for any finite F.  So notIncludes can only be used to check, when a
    specific case has been found, that it does not exist in the formula.
    This means we have to know that the variables do not occur in obj.

    As for the subject, it does make sense for the opposite reason.  If F(x)
    includes G for all x, then G would have to be infinite.  
    """
    def eval(self, subj, obj, queue, bindings, proof, query):
        store = subj.store
        if isinstance(subj, Formula) and isinstance(obj, Formula):
            return not testIncludes(subj, obj,  bindings=bindings, interpretBuiltins=0) # No (relevant) variables
        return 0   # Can't say it *doesn't* include it if it ain't a formula

class BI_notIncludesWithBuiltins(HeavyBuiltIn):
    def eval(self, subj, obj, queue, bindings, proof, query):
        store = subj.store
        if isinstance(subj, Formula) and isinstance(obj, Formula):
            return not testIncludes(subj, obj,  bindings=bindings,
                    interpretBuiltins=1) # No (relevant) variables
        return 0   # Can't say it *doesn't* include it if it ain't a formula



class BI_semantics(HeavyBuiltIn, Function):
    """ The semantics of a resource are its machine-readable meaning, as an
    N3 forumula.  The URI is used to find a representation of the resource in
    bits which is then parsed according to its content type."""
    def evalObj(self, subj, queue, bindings, proof, query):
        store = subj.store
        if isinstance(subj, Fragment): doc = subj.resource
        else: doc = subj
        F = store.any((store._experience, store.semantics, doc, None))
        if F != None:
            if diag.chatty_flag > 10:
                progress("Already read and parsed "+`doc`+" to "+ `F`)
            return F

        if diag.chatty_flag > 10: progress("Reading and parsing " + doc.uriref())
        inputURI = doc.uriref()
#       if diag.tracking: flags="B"   # @@@@@@@@@@@ Yuk
#       else: flags=""
        F = self.store.load(inputURI, why=becauseSubexpression)
        if diag.chatty_flag>10: progress("    semantics: %s" % (F))
        return F.canonicalize()

class BI_semanticsWithImportsClosure(HeavyBuiltIn, Function):

    """ The semantics of a resource are its machine-readable meaning,
    as an N3 forumula.  The URI is used to find a representation of
    the resource in bits which is then parsed according to its content
    type.  Extension : It also loads all imported URIs"""
  
    def evalObj(self, subj, queue, bindings, proof, query):
        store = subj.store
        if isinstance(subj, Fragment): doc = subj.resource
        else: doc = subj
        F = store.any((store._experience, store.semanticsWithImportsClosure, doc, None))
        if F != None:
            if diag.chatty_flag > 10: progress("Already read and parsed "+`doc`+" to "+ `F`)
            return F
  
        if diag.chatty_flag > 10: progress("Reading and parsing with closure " + doc.uriref())
        inputURI = doc.uriref()
  
        F = store.newFormula()
        F.setClosureMode("i")
        F = store.load(uri=inputURI, openFormula=F)
          
        if diag.chatty_flag>10: progress("Reading and parsing with closure done.    semantics: %s" % (F))
#       if diag.tracking:
#            proof.append(F.collector)
        F = F.close()
        store.storeQuad((store._experience, store.semanticsWithImportsClosure, doc, F))
        return F
        
import httplib    
class BI_semanticsOrError(BI_semantics):
    """ Either get and parse to semantics or return an error message on any error """
    def evalObj(self, subj, queue, bindings, proof, query):
        import xml.sax._exceptions # hmm...
        store = subj.store
        x = store.any((store._experience, store.semanticsOrError, subj, None))
        if x != None:
            if diag.chatty_flag > 10: progress(`store._experience`+`store.semanticsOrError`+": Already found error for "+`subj`+" was: "+ `x`)
            return x
        try:
            return BI_semantics.evalObj(self, subj, queue, bindings, proof, query)
        except (IOError, SyntaxError, DocumentAccessError,
                xml.sax._exceptions.SAXParseException, httplib.BadStatusLine):
            message = sys.exc_info()[1].__str__()
            result = store.intern((LITERAL, message))
            if diag.chatty_flag > 0: progress(`store.semanticsOrError`+": Error trying to access <" + `subj` + ">: "+ message) 
            store.storeQuad((store._experience,
                             store.semanticsOrError,
                             subj,
                             result))
            return result
    

def loadToStore(term, types):
    """load content from the web and keep it in the store's experience.
    return resulting literal term
    
    raises IOError
    
    <DanC> the log:content built-in could keep an HTTP response
           object around a la tabulator too.
    <timbl> yes.
    <timbl> You learn a lot from a recode.
    """

    if isinstance(term, Fragment): doc = term.resource # i.e. racine
    else: doc = term
    store = term.store #hmm... separate store from term?
    C = store.any((store._experience, store.content, doc, None))
    if C != None:
        if diag.chatty_flag > 10: progress("already read " + `doc`)
        return C

    if diag.chatty_flag > 10: progress("Reading " + `doc`)
    inputURI = doc.uriref()

    netStream = webget(inputURI, types)

    #@@ get charset from headers
    str = netStream.read().decode('utf-8')
    C = store.intern((LITERAL, str))
    store.storeQuad((store._experience,
                     store.content,
                     doc,
                     C))
    return C

class BI_content(HeavyBuiltIn, Function):
    def evalObj(self, subj, queue, bindings, proof, query):
        try:
            return loadToStore(subj, [])
        except IOError:
            return None # hmm... is built-in API evolving to support exceptions?


class BI_xmlTree(HeavyBuiltIn, Function):
    def evalObj(self, subj, queue, bindings, proof, query):
        x= BI_content.evalObj(self, subj, queue, bindings, proof, query)
        dom = XMLtoDOM(x.value())
        return subj.store.intern((XMLLITERAL, dom))

class BI_parsedAsN3(HeavyBuiltIn, Function):
    def evalObj(self, subj, queue, bindings, proof, query):
        store = subj.store
        if isinstance(subj, Literal):
            F = store.any((store._experience, store.parsedAsN3, subj, None))
            if F != None: return F
            if diag.chatty_flag > 10: progress("parsing " + subj.string[:30] + "...")

            inputURI = subj.asHashURI() # iffy/bogus... rather asDataURI? yes! but make more efficient
            p = notation3.SinkParser(store)
            p.startDoc()
            p.feed(subj.string.encode('utf-8')) #@@ catch parse errors
            F = p.endDoc()
            F = F.close()
            store._experience.add(subj=subj, pred=store.parsedAsN3, obj=F)
            return F

class BI_conclusion(HeavyBuiltIn, Function):
    """ Deductive Closure

    Closure under Forward Inference, equivalent to cwm's --think function.
    This is a function, so the object is calculated from the subject.
    """
    def evalObj(self, subj, queue, bindings, proof, query):
        store = subj.store
        if isinstance(subj, Formula):
            assert subj.canonical != None
            F = self.store.any((store._experience, store.cufi, subj, None))  # Cached value?
            if F != None:
                if diag.chatty_flag > 10: progress("Bultin: " + `subj`+ " cached log:conclusion " + `F`)
                return F

            F = self.store.newFormula()
            newTopLevelFormula(F)
            if diag.tracking:
                reason = Premise("Assumption of builtin", (subj, self))
#               reason = BecauseMerge(F, subj)
#               F.collector = reason
#               proof.append(reason)
            else: reason = None
            if diag.chatty_flag > 10: progress("Bultin: " + `subj`+ " log:conclusion " + `F`)
            self.store.copyFormula(subj, F, why=reason) # leave open
            think(F)
            F = F.close()
            assert subj.canonical != None
            
            self.store.storeQuad((store._experience, store.cufi, subj, F),
                    why=BecauseOfExperience("conclusion"))  # Cache for later
            return F

class BI_supports(HeavyBuiltIn):
    """A more managable version of log:conclusion
The real version of this should appear in query.py
    """
    def eval(self, subj, obj, queue, bindings, proof, query):
        pass

class BI_filter(LightBuiltIn, Function):
    """Filtering of formulae

    """
    def evalObj(self, subj, queue, bindings, proof, query):
        store = subj.store
        if not isinstance(subj, List):
            raise ValueError('I need a list of two formulae')
        list = [x for x in subj] 
        if len(list) != 2:
            raise ValueError('I need a list of TWO formulae')
        if diag.chatty_flag > 30:
            progress("=== begin filter of:" + `list`)
        # list = [bindings.get(a,a) for a in list]
        base, filter = list
        F = self.store.newFormula()
        if diag.tracking:
            pass
        else: reason = None
        applyRules(base, filter, F)
        F = F.close()
        if diag.chatty_flag > 30:
            progress("=== end filter of:" + `list` + "we got: " + `F`)
        return F

class BI_vars(LightBuiltIn, Function):
    """Get only the variables from a formula

    """
    def evalObj(self, subj, queue, bindings, proof, query):
        F = self.store.newFormula()
        #F.existentials().update(subj.existentials())
        F.universals().update(subj.universals())
        return F.close()

class BI_universalVariableName(RDFBuiltIn): #, MultipleFunction):
    """Is the object the name of a universal variable in the subject?
    Runs even without interpretBuitins being set.  
    Used internally in query.py for testing for 
    Can be used as a test, or returns a sequence of values."""

    def eval(self, subj, obj, queue, bindings, proof, query):
        if not isinstance(subj, Formula): return None
        s = str(obj)
        if diag.chatty_flag > 180:
            progress(`subj.universals()`)
        return obj in subj.universals()
        for v in subj.universals():
            if v.uriref() == s: return 1
        return 0

    def evalObj(self,subj, queue, bindings, proof, query):
        if not isinstance(subj, Formula): return None
        return [subj.newLiteral(x.uriref()) for x in subj.universals()]

class BI_existentialVariableName(RDFBuiltIn): #, MultipleFunction):
    """Is the object the name of a existential variable in the subject?
    Can be used as a test, or returns a sequence of values.
    Currently gives BNode names too.  Maybe we make sep function for that?"""
    def eval(self, subj, obj, queue, bindings, proof, query):
        if not isinstance(subj, Formula): return None
        s = str(obj)
        if obj not in subj.existentials() and diag.chatty_flag > 25:
            progress('Failed, which is odd. Subj="%s", Obj="%s"' % (subj.debugString(), obj.debugString()))
        return obj in subj.existentials()
        for v in subj.existentials():
            if v.uriref() == s: return 1
        return 0

    def evalObj(self,subj, queue, bindings, proof, query):
        if not isinstance(subj, Formula): return None
        rea = None
        return [subj.newLiteral(x.uriref()) for x in subj.existentials()]


class BI_enforceUniqueBinding(RDFBuiltIn):
    """Is the mapping from the variable in the subject to the name in the object unique?

    """
    def eval(self, subj, obj, queue, bindings, proof, query):
        if not isinstance(subj, Formula): return None
        s = str(obj)
        if subj not in query.backwardMappings:
            query.backwardMappings[subj] = s
            return True
        return query.backwardMappings[subj] == s

class BI_conjunction(LightBuiltIn, Function):      # Light? well, I suppose so.
    """ The conjunction of a set of formulae is the set of statements which is
    just the union of the sets of statements
    modulo non-duplication of course."""
    def evalObj(self, subj, queue, bindings, proof, query):
        subj_py = subj.value()
        if diag.chatty_flag > 50:
            progress("Conjunction input:"+`subj_py`)
            for x in subj_py:
                progress("    conjunction input formula %s has %i statements" 
                                                % (x, x.size()))
        F = self.store.newFormula()
        if diag.tracking:
            reason = Because("I said so #4")
            #reason = BecauseMerge(F, subj_py)
        else: reason = None
        for x in subj_py:
            if not isinstance(x, Formula): return None # Can't
            if (x.canonical == None): # Not closed! !!
                F.canonical != None
                progress("Conjunction input NOT CLOSED:"+`x`) #@@@
            self.store.copyFormula(x, F, why=reason)   #  No, that is 
            if diag.chatty_flag > 74:
                progress("    Formula %s now has %i" % (`F`,len(F.statements)))
        return F.canonicalize()

class BI_n3String(LightBuiltIn, Function):      # Light? well, I suppose so.
    """ The n3 string for a formula is what you get when you
    express it in the N3 language without using any URIs.
    Note that there is no guarantee that two implementations will
    generate the same thing, but whatever they generate should
    parse back using parsedAsN3 to exaclty the same original formula.
    If we *did* have a canonical form it would be great for signature
    A canonical form is possisble but not simple."""
    def evalObj(self, subj, queue, bindings, proof, query):
        if diag.chatty_flag > 50:
            progress("Generating N3 string for:"+`subj`)
        if isinstance(subj, Formula):
            return self.store.intern((LITERAL, subj.n3String()))

class BI_reification(HeavyBuiltIn, Function, ReverseFunction):
    """



    """
    def evalSubj(self, obj, queue, bindings, proof, query):
        f = obj.store.newFormula()
        return reify.dereification(obj, f, obj.store)

    def evalObj(self, subj, queue, bindings, proof, query):
        f = subj.store.newFormula()
        q = subj.reification(f, {}, why=None)
        f=f.close()
        self.store.storeQuad((self.store._experience, self.store.type, f, 3), why=BecauseOfExperience("SomethingOrOther"))
        return q

import weakref

class Disjoint_set(object):
    class disjoint_set_node(object):
        def __init__(self, val):
            self.value = weakref.ref(val)
            self.parent = None
            self.rank = 0
        def link(self, other):
            if other.parent or self.parent:
                raise RuntimeError
            if self.rank > other.rank:
                other.parent = self
            else:
                self.parent = other
                if self.rank == other.rank:
                    other.rank += 1
        def find_set(self):
            if self.parent is None:
                return self
            self.parent = self.parent.find_set()
            return self.parent
        def disjoint(self, other):
            return self() is not other()
        def connected(self, other):
            return self() is other()
        __call__ = find_set

    def __init__(self):
        self.map = weakref.WeakKeyDictionary()

    def add(self, f):
        self.map[f] = self.disjoint_set_node(f)

    def connected(self, f, g):
        return self.map[f].connected(self.map[g])

    def merge(self, s1, s2):
        s1 = self.map[s1]()
        s2 = self.map[s2]()
        s1.link(s2)
    
################################################################################################

class RDFStore(RDFSink) :
    """ Absorbs RDF stream and saves in triple store
    """

    def clear(self):
        "Remove all formulas from the store     @@@ DOESN'T ACTUALLY DO IT/BROKEN"
        self.resources = WeakValueDictionary()    # Hash table of URIs for interning things
#        self.formulae = []     # List of all formulae        
        self._experience = None   #  A formula of all the things program run knows from direct experience
        self._formulaeOfLength = {} # A dictionary of all the constant formuale in the store, lookup by length key.
        self._formulaeOfLengthPerWorkingContext = {}
        self.size = 0
        self._equivalentFormulae = Disjoint_set()
        
    def __init__(self, genPrefix=None, metaURI=None, argv=None, crypto=0):
        RDFSink.__init__(self, genPrefix=genPrefix)
        self.clear()
        self.argv = argv     # List of command line arguments for N3 scripts

        run = uripath.join(uripath.base(), ".RUN/") + `time.time()`  # Reserrved URI @@

        if metaURI != None: meta = metaURI
        else: meta = run + "meta#formula"
        self.reset(meta)


        # Constants, as interned:
        
        self.forSome = self.symbol(forSomeSym)
        self.integer = self.symbol(INTEGER_DATATYPE)
        self.float  = self.symbol(FLOAT_DATATYPE)
        self.boolean = self.symbol(BOOL_DATATYPE)
        self.decimal = self.symbol(DECIMAL_DATATYPE)
        self.forAll  = self.symbol(forAllSym)
        self.implies = self.symbol(Logic_NS + "implies")
        self.insertion = self.symbol(Delta_NS + "insertion")
        self.deletion  = self.symbol(Delta_NS + "deletion")
        self.means = self.symbol(Logic_NS + "means")
        self.asserts = self.symbol(Logic_NS + "asserts")
        
# Register Light Builtins:

        log = self.symbol(Logic_NS[:-1])   # The resource without the hash

# Functions:        

        log.internFrag("racine", BI_racine)  # Strip fragment identifier from string
        log.internFrag("dtlit", BI_dtlit)

        self.rawType =  log.internFrag("rawType", BI_rawType) # syntactic type, oneOf:
        log.internFrag("rawUri", BI_rawUri)
        self.Literal =  log.internFrag("Literal", Fragment) # syntactic type possible value - a class
        self.List =     log.internFrag("List", Fragment) # syntactic type possible value - a class
        self.Set =     log.internFrag("Set", Fragment) # syntactic type possible value - a class
        self.Formula =  log.internFrag("Formula", Fragment) # syntactic type possible value - a class
        self.Blank   =  log.internFrag("Blank", Fragment)
        self.Other =    log.internFrag("Other", Fragment) # syntactic type possible value - a class
        self.filter  =  log.internFrag("filter", BI_filter) # equivilent of --filter
        self.vars    =  log.internFrag("vars", BI_vars) # variables of formula
        
        self.universalVariableName = log.internFrag(
                            "universalVariableName", BI_universalVariableName)
        self.existentialVariableName = log.internFrag(
                            "existentialVariableName", BI_existentialVariableName)
        self.enforceUniqueBinding = log.internFrag(
                            "enforceUniqueBinding", BI_enforceUniqueBinding)
        log.internFrag("conjunction", BI_conjunction)
        
# Bidirectional things:
        log.internFrag("uri", BI_uri)
        log.internFrag("equalTo", BI_EqualTo)
        log.internFrag("notEqualTo", BI_notEqualTo)
        log.internFrag("reification", BI_reification)

        owl = self.symbol(OWL_NS[:-1])
        self.sameAs = owl.internFrag("sameAs", BI_SameAs)

# Heavy relational operators:

        self.includes =         log.internFrag( "includes", BI_includes)
        self.supports =         log.internFrag( "supports", BI_supports)

#        log.internFrag("directlyIncludes", BI_directlyIncludes)
        self.notIncludes = log.internFrag("notIncludes", BI_notIncludes)
        self.smartNotIncludes = log.internFrag("notIncludesWithBuiltins", BI_notIncludesWithBuiltins)
#        log.internFrag("notDirectlyIncludes", BI_notDirectlyIncludes)

#Heavy functions:

#        log.internFrag("resolvesTo", BI_semantics) # obsolete
        self.semantics = log.internFrag("semantics", BI_semantics)
        self.cufi = log.internFrag("conclusion", BI_conclusion)
        self.semanticsOrError = log.internFrag("semanticsOrError", BI_semanticsOrError)
        self.semanticsWithImportsClosure = log.internFrag("semanticsWithImportsClosure", BI_semanticsWithImportsClosure)
        self.content = log.internFrag("content", BI_content)
        self.parsedAsN3 = log.internFrag("parsedAsN3",  BI_parsedAsN3)
        self.n3ExprFor = log.internFrag("n3ExprFor",  BI_parsedAsN3) ## Obsolete
        log.internFrag("n3String",  BI_n3String)

# Remote service flag in metadata:

        self.definitiveService = log.internFrag("definitiveService", Fragment)
        self.definitiveDocument = log.internFrag("definitiveDocument", Fragment)
        self.pointsAt = log.internFrag("pointsAt", Fragment)  # This was EricP's

# Constants:

        self.Truth = self.symbol(Logic_NS + "Truth")
        self.Falsehood = self.symbol(Logic_NS + "Falsehood")
        self.type = self.symbol(RDF_type_URI)
        self.Chaff = self.symbol(Logic_NS + "Chaff")
        self.docRules = self.symbol("http://www.w3.org/2000/10/swap/pim/doc#rules")
        self.imports = self.symbol("http://www.w3.org/2002/07/owl#imports")
        self.owlOneOf = self.symbol('http://www.w3.org/2002/07/owl#oneOf')

# List stuff - beware of namespace changes! :-(

        from cwm_list import BI_first, BI_rest
        rdf = self.symbol(List_NS[:-1])
        self.first = rdf.internFrag("first", BI_first)
        self.rest = rdf.internFrag("rest", BI_rest)
        self.nil = self.intern(N3_nil, FragmentNil)
        self.Empty = self.intern(N3_Empty)
        self.li = self.intern(N3_li)
        self.List = self.intern(N3_List)

        import cwm_string  # String builtins
        import cwm_os      # OS builtins
        import cwm_time    # time and date builtins
        import cwm_math    # Mathematics
        import cwm_trigo   # Trignometry
        import cwm_times    # time and date builtins
        import cwm_maths   # Mathematics, perl/string style
        import cwm_list    # List handling operations
        import cwm_set     # Set operations
        import cwm_sparql  # builtins for sparql
        import cwm_xml     # XML Document Object Model operations
        cwm_string.register(self)
        cwm_math.register(self)
        cwm_trigo.register(self)
        cwm_maths.register(self)
        cwm_os.register(self)
        cwm_time.register(self)
        cwm_times.register(self)
        cwm_list.register(self)
        cwm_set.register(self)
        cwm_sparql.register(self)
        cwm_xml.register(self)
        import cwm_crypto  # Cryptography
        if crypto:
            if cwm_crypto.USE_PKC == 0:
                raise RuntimeError("Try installing pycrypto, and make sure it is in you PYTHONPATH")
        else:
            cwm_crypto.USE_PKC = 0       
        cwm_crypto.register(self)  # would like to anyway to catch bug if used but not available

    def newLiteral(self, str, dt=None, lang=None):
        "Interned version: generate new literal object as stored in this store"
        key = (str, dt, lang)
        result = self.resources.get(key, None)
        if result != None: return result
#       if dt is not None: dt = self.newSymbol(dt)
        assert dt is None or isinstance(dt, LabelledNode)
        if dt is not None and not isinstance(dt, Fragment):
            progress("Warning: <%s> is not a fragment!" % dt)
        result = Literal(self, str, dt, lang)
        self.resources[key] = result
        return result
        
    def newXMLLiteral(self, dom):
        # We do NOT intern these so they will NOT have 'is' same as '=='
        return XMLLiteral(self, dom)
        
    def newFormula(self, uri=None):
        return IndexedFormula(self, uri)

    def newSymbol(self, uri):
        return self.intern(RDFSink.newSymbol(self, uri))

    def newSet(self, iterator=[], context=None):
        new_set = N3Set(iterator)
        Term.__init__(new_set, self)
        return new_set

    def newBlankNode(self, context, uri=None, why=None):
        """Create or reuse, in the default store, a new unnamed node within the given
        formula as context, and return it for future use"""
        return context.newBlankNode(uri=uri)

    def newExistential(self, context, uri=None, why=None):
        """Create or reuse, in the default store, a new named variable
        existentially qualified within the given
        formula as context, and return it for future use"""
        return self.intern(RDFSink.newExistential(self, context, uri, why=why))
    
    def newUniversal(self, context, uri=None, why=None):
        """Create or reuse, in the default store, a named variable
        universally qualified within the given
        formula as context, and return it for future use"""
        return self.intern(RDFSink.newUniversal(self, context, uri, why=why))



###################

    def reset(self, metaURI): # Set the metaURI
        self._experience = self.newFormula(metaURI + "_formula")
        assert isinstance(self._experience, Formula)

    def load(store, uri=None, openFormula=None, asIfFrom=None, contentType=None, remember=1,
                    flags="", referer=None, why=None, topLevel=False):
        """Get and parse document.  Guesses format if necessary.

        uri:      if None, load from standard input.
        remember: if 1, store as metadata the relationship between this URI and this formula.
        
        Returns:  top-level formula of the parsed document.
        Raises:   IOError, SyntaxError, DocumentError
        
        This was and could be an independent function, as it is fairly independent
        of the store. However, it is natural to call it as a method on the store.
        And a proliferation of APIs confuses.
        """
        baseURI = uripath.base()
        givenOpenFormula = openFormula
        if openFormula is None:
            openFormula = store.newFormula()
        if topLevel:
            newTopLevelFormula(openFormula)
        if uri != None and openFormula==None and remember:
            addr = uripath.join(baseURI, uri) # Make abs from relative
            source = store.newSymbol(addr)
            F = store._experience.the(source, store.semantics)
            if F != None:
                if diag.chatty_flag > 40: progress("Using cached semantics for",addr)
                return F 
            F = webAccess.load(store, uri, openFormula, asIfFrom, contentType, flags, referer, why)  
            store._experience.add(
                    store.intern((SYMBOL, addr)), store.semantics, F,
                    why=BecauseOfExperience("load document"))
            return F
            
        return webAccess.load(store, uri, openFormula, asIfFrom, contentType, flags, \
                              referer=referer, why=why)  

    



    def loadMany(self, uris, openFormula=None, referer=None):
        """Get, parse and merge serveral documents, given a list of URIs. 
        
        Guesses format if necessary.
        Returns top-level formula which is the parse result.
        Raises IOError, SyntaxError
        """
        assert type(uris) is type([])
        if openFormula is None: F = self.newFormula()
        else:  F = openFormula
        f = F.uriref()
        for u in uris:
            F.reopen()  # should not be necessary
            self.load(u, openFormula=F, remember=0, referer=referer)
        return F.close()

    def genId(self):
        """Generate a new identifier
        
        This uses the inherited class, but also checks that we haven't for some pathalogical reason
        ended up generating the same one as for example in another run of the same system. 
        """
        while 1:
            uriRefString = RDFSink.genId(self)
            hash = string.rfind(uriRefString, "#")
            if hash < 0 :     # This is a resource with no fragment
                return uriRefString # ?!
            resid = uriRefString[:hash]
            r = self.resources.get(resid, None)
            if r is None: return uriRefString
            fragid = uriRefString[hash+1:]
            f = r.fragments.get(fragid, None)
            if f is None: return uriRefString
            if diag.chatty_flag > 70:
                progress("llyn.genid Rejecting Id already used: "+uriRefString)
                
    def checkNewId(self, urirefString):
        """Raise an exception if the id is not in fact new.
        
        This is useful because it is usfeul
        to generate IDs with useful diagnostic ways but this lays them
        open to possibly clashing in pathalogical cases."""
        hash = string.rfind(urirefString, "#")
        if hash < 0 :     # This is a resource with no fragment
            result = self.resources.get(urirefString, None)
            if result is None: return
        else:
            r = self.resources.get(urirefString[:hash], None)
            if r is None: return
            f = r.fragments.get(urirefString[hash+1:], None)
            if f is None: return
        raise ValueError("Ooops! Attempt to create new identifier hits on one already used: %s"%(urirefString))
        return


    def internURI(self, str, why=None):
        warn("use symbol()", DeprecationWarning, stacklevel=3)
        return self.intern((SYMBOL,str), why)

    def symbol(self, str, why=None):
        """Intern a URI for a symvol, returning a symbol object"""
        return self.intern((SYMBOL,str), why)

    
    def _fromPython(self, x, queue=None):
        """Takem a python string, seq etc and represent as a llyn object"""
        if isinstance(x, tuple(types.StringTypes)):
            return self.newLiteral(x)
        elif type(x) is types.LongType or type(x) is types.IntType:
            return self.newLiteral(str(x), self.integer)
        elif isinstance(x, Decimal):
            return self.newLiteral(str(x), self.decimal)
        elif isinstance(x, bool):
            return self.newLiteral(x and 'true' or 'false', self.boolean)
        elif isinstance(x, xml.dom.minidom.Document):
            return self.newXMLLiteral(x)
        elif type(x) is types.FloatType:
            if `x`.lower() == "nan":  # We can get these form eg 2.math:asin
                return None
            return self.newLiteral(`x`, self.float)
        elif isinstance(x, Set) or isinstance(x, ImmutableSet):
            return self.newSet([self._fromPython(y) for y in x])
        elif isinstance(x, Term):
            return x
        elif hasattr(x,'__getitem__'): #type(x) == type([]):
            return self.nil.newList([self._fromPython(y) for y in x])
        return x

    def intern(self, what, dt=None, lang=None, why=None, ):
        """find-or-create a Fragment or a Symbol or Literal or list as appropriate

        returns URISyntaxError if, for example, the URIref has
        two #'s.
        
        This is the way they are actually made.
        """

        if isinstance(what, Term): return what # Already interned.  @@Could mask bugs
        if type(what) is not types.TupleType:
            if isinstance(what, tuple(types.StringTypes)):
                return self.newLiteral(what, dt, lang)
#           progress("llyn1450 @@@ interning non-string", `what`)
            if type(what) is types.LongType:
                return self.newLiteral(str(what),  self.integer)
            if type(what) is types.IntType:
                return self.newLiteral(`what`,  self.integer)
            if type(what) is types.FloatType:
                return self.newLiteral(repr(what),  self.float)
            if isinstance(what,Decimal):
                return self.newLiteral(str(what), self.decimal)
            if isinstance(what, bool):
                return self.newLiteral(what and 'true' or 'false', self.boolean)
            if type(what) is types.ListType: #types.SequenceType:
                return self.newList(what)
            raise RuntimeError("Eh?  can't intern "+`what`+" of type: "+`what.__class__`)

        typ, urirefString = what

        if typ == LITERAL:
            return self.newLiteral(urirefString, dt, lang)
        if typ == LITERAL_DT:
            return self.newLiteral(urirefString[0], self.intern(SYMBOL, urirefString[1]))
        if typ == LITERAL_LANG:
            return self.newLiteral(urirefString[0], None, urirefString[1])
        else:
            urirefString = canonical(urirefString)
            assert ':' in urirefString, "must be absolute: %s" % urirefString


            hash = string.rfind(urirefString, "#")
            if hash < 0 :     # This is a resource with no fragment
                assert typ == SYMBOL, "If URI <%s>has no hash, must be symbol" % urirefString
                result = self.resources.get(urirefString, None)
                if result != None: return result
                result = Symbol(urirefString, self)
                self.resources[urirefString] = result
            
            else :      # This has a fragment and a resource
                resid = urirefString[:hash]
                if string.find(resid, "#") >= 0:
                    raise URISyntaxError("Hash in document ID - can be from parsing XML as N3! -"+resid)
                r = self.symbol(resid)
                if typ == SYMBOL:
                    if urirefString == N3_nil[1]:  # Hack - easier if we have a different classs
                        result = r.internFrag(urirefString[hash+1:], FragmentNil)
                    else:
                        result = r.internFrag(urirefString[hash+1:], Fragment)
                elif typ == ANONYMOUS:
                    result = r.internFrag(urirefString[hash+1:], AnonymousNode)
                elif typ == FORMULA:
                    raise RuntimeError("obsolete")
                    result = r.internFrag(urirefString[hash+1:], IndexedFormula)
                else: raise RuntimeError, "did not expect other type:"+`typ`
        return result

    def newList(self, value, context=None):
        return self.nil.newList(value)

#    def deleteFormula(self,F):
#        if diag.chatty_flag > 30: progress("Deleting formula %s %ic" %
#                                            ( `F`, len(F.statements)))
#        for s in F.statements[:]:   # Take copy
#            self.removeStatement(s)


    def reopen(self, F):
        if F.canonical is None:
            if diag.chatty_flag > 50:
                progress("reopen formula -- @@ already open: "+`F`)
            return F # was open
        if diag.chatty_flag > 00:
            progress("warning - reopen formula:"+`F`)
        key = len(F.statements), len(F.universals()), len(F.existentials())
        try:
            self._formulaeOfLength[key].remove(F)  # Formulae of same length
        except (KeyError, ValueError):
            pass
        F.canonical = None
        return F


    def bind(self, prefix, uri):

        if prefix != "":   #  Ignore binding to empty prefix
            return RDFSink.bind(self, prefix, uri) # Otherwise, do as usual.
    
    def makeStatement(self, tuple, why=None):
        """Add a quad to the store, each part of the quad being in pair form."""
        q = ( self.intern(tuple[CONTEXT]),
              self.intern(tuple[PRED]),
              self.intern(tuple[SUBJ]),
              self.intern(tuple[OBJ]) )
        if q[PRED] is self.forSome and isinstance(q[OBJ], Formula):
            if diag.chatty_flag > 97:  progress("Makestatement suppressed")
            return  # This is implicit, and the same formula can be used un >1 place
        self.storeQuad(q, why)
                    
    def makeComment(self, str):
        pass        # Can't store comments


    def any(self, q):
        """Query the store for the first match.
        
        Quad contains one None as wildcard. Returns first value
        matching in that position.
        """
        list = q[CONTEXT].statementsMatching(q[PRED], q[SUBJ], q[OBJ])
        if list == []: return None
        for p in ALL4:
            if q[p] is None:
                return list[0].quad[p]


    def storeQuad(self, q, why=None):
        """ intern quads, in that dupliates are eliminated.

        subject, predicate and object are terms - or atomic values to be interned.
        Builds the indexes and does stuff for lists.
        Deprocated: use Formula.add()         
        """
        
        context, pred, subj, obj = q
        assert isinstance(context, Formula), "Should be a Formula: "+`context`
        return context.add(subj=subj, pred=pred, obj=obj, why=why)
        

    def startDoc(self):
        pass

    def endDoc(self, rootFormulaPair):
        return




##########################################################################
#
# Output methods:
#
    def dumpChronological(self, context, sink):
        "Fast as possible. Only dumps data. No formulae or universals."
        pp = Serializer(context, sink)
        pp. dumpChronological()
        del(pp)
        
    def dumpBySubject(self, context, sink, sorting=1):
        """ Dump by order of subject except forSome's first for n3=a mode"""
        pp = Serializer(context, sink, sorting=sorting)
        pp. dumpBySubject()
        del(pp)
        

    def dumpNested(self, context, sink, flags=""):
        """ Iterates over all URIs ever seen looking for statements
        """
        pp = Serializer(context, sink, flags=flags)
        pp. dumpNested()
        del(pp)



##################################  Manipulation methods:
#
#  Note when we move things, then the store may shrink as they may
# move on top of existing entries and we don't allow duplicates.
#
#   @@@@ Should automatically here rewrite any variable name clashes
#  for variable names which occur in the other but not as the saem sort of variable
# Must be done by caller.

    def copyFormula(self, old, new, why=None):
        new.loadFormulaWithSubstitution(old, why=why)
        return
##      bindings = {old: new}
##      for v in old.universals():
##          new.declareUniversal(bindings.get(v,v))
##      for v in old.existentials():
##          new.declareExistential(bindings.get(v,v))
##        for s in old.statements[:] :   # Copy list!
##            q = s.quad
##            for p in CONTEXT, PRED, SUBJ, OBJ:
##                x = q[p]
##                if x is old:
##                    q = q[:p] + (new,) + q[p+1:]
##            self.storeQuad(q, why)
                

    def purge(self, context, boringClass=None):
        """Clean up intermediate results

    Statements in the given context that a term is a Chaff cause
    any mentions of that term to be removed from the context.
    """
        if boringClass is None:
            boringClass = self.Chaff
        for subj in context.subjects(pred=self.type, obj=boringClass):
            self.purgeSymbol(context, subj)

    def purgeSymbol(self, context, subj):
        """Purge all triples in which a symbol occurs.
        """
        total = 0
        for t in context.statementsMatching(subj=subj)[:]:
                    context.removeStatement(t)    # SLOW
                    total = total + 1
        for t in context.statementsMatching(pred=subj)[:]:
                    context.removeStatement(t)    # SLOW
                    total = total + 1
        for t in context.statementsMatching(obj=subj)[:]:
                    context.removeStatement(t)    # SLOW
                    total = total + 1
        if diag.chatty_flag > 30:
            progress("Purged %i statements with %s" % (total,`subj`))
        return total


#    def removeStatement(self, s):
#        "Remove statement from store"
#       return s[CONTEXT].removeStatement(s)

    def purgeExceptData(self, context):
        """Remove anything which can't be expressed in plain RDF"""
        uu = context.universals()
        for s in context.statements[:]:
            for p in PRED, SUBJ, OBJ:
                x = s[p]
                if x in uu or isinstance(x, Formula):
                    context.removeStatement(s)
                    break
        context._universalVariables.clear()  # Cheat! @ use API



class URISyntaxError(ValueError):
    """A parameter is passed to a routine that requires a URI reference"""
    pass



def isString(x):
    # in 2.2, evidently we can test for isinstance(types.StringTypes)
    #    --- but on some releases, we need to say tuple(types.StringTypes)
    return type(x) is type('') or type(x) is type(u'')

#####################  Register this module

from myStore import setStoreClass
setStoreClass(RDFStore)

#ends