File: rdf_model.c

package info (click to toggle)
redland 1.0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 27,592 kB
  • ctags: 12,328
  • sloc: ansic: 79,017; xml: 25,115; sh: 10,162; yacc: 5,985; lex: 3,682; makefile: 3,260; perl: 2,814; cpp: 59
file content (2301 lines) | stat: -rw-r--r-- 66,999 bytes parent folder | download
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
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
/* -*- Mode: c; c-basic-offset: 2 -*-
 *
 * rdf_model.c - RDF Graph (Model) interface
 *
 * $Id: rdf_model.c 12992 2007-10-22 12:10:11Z laalto $
 *
 * Copyright (C) 2000-2007, David Beckett http://purl.org/net/dajobe/
 * Copyright (C) 2000-2005, University of Bristol, UK http://www.bristol.ac.uk/
 * 
 * This package is Free Software and part of Redland http://librdf.org/
 * 
 * It is licensed under the following three licenses as alternatives:
 *   1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
 *   2. GNU General Public License (GPL) V2 or any newer version
 *   3. Apache License, V2.0 or any newer version
 * 
 * You may not use this file except in compliance with at least one of
 * the above three licenses.
 * 
 * See LICENSE.html or LICENSE.txt at the top of this package for the
 * complete terms and further detail along with the license texts for
 * the licenses in COPYING.LIB, COPYING and LICENSE-2.0.txt respectively.
 * 
 * 
 */


#ifdef HAVE_CONFIG_H
#include <rdf_config.h>
#endif

#ifdef WIN32
#include <win32_rdf_config.h>
#endif

#include <stdio.h>
#include <string.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h> /* for exit()  */
#endif

#include <redland.h>

#ifndef STANDALONE

/**
 * librdf_init_model:
 * @world: redland world object
 *
 * INTERNAL - Initialise the model module.
 *
 **/
void
librdf_init_model(librdf_world *world)
{
  /* Always have model storage - must always be the default model */
  librdf_init_model_storage(world);
}


/**
 * librdf_finish_model:
 * @world: redland world object
 *
 * INTERNAL - Terminate the model module.
 *
 **/
void
librdf_finish_model(librdf_world *world)
{
  if(world->models) {
    raptor_free_sequence(world->models);
    world->models=NULL;
  }
}


/*
 * librdf_model_supports_contexts - helper function to determine if this model supports contexts
 **/
static REDLAND_INLINE int
librdf_model_supports_contexts(librdf_model* model) {
  return model->supports_contexts;
}



/* class methods */

static void
librdf_free_model_factory(librdf_model_factory* factory)
{
  if(factory->name)
    LIBRDF_FREE(librdf_model_factory, factory->name);
  if(factory->label)
    LIBRDF_FREE(librdf_model_factory, factory->label);
  LIBRDF_FREE(librdf_model_factory, factory);
}


/**
 * librdf_model_register_factory:
 * @world: redland world object
 * @name: the model factory name
 * @label: the storage factory label
 * @factory: pointer to function to call to register the factory
 *
 * Register a model factory.
 * 
 **/
void
librdf_model_register_factory(librdf_world *world, 
                              const char *name, const char *label,
                              void (*factory) (librdf_model_factory*)) 
{
  librdf_model_factory *model;
  int i;

  librdf_world_open(world);

#if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1
  LIBRDF_DEBUG2("Received registration for model %s\n", name);
#endif

  if(!world->models) {
    world->models=raptor_new_sequence((raptor_sequence_free_handler *)librdf_free_model_factory, NULL);
    if(!world->models)
      goto oom;
  }
  
  for(i=0;
      (model=(librdf_model_factory*)raptor_sequence_get_at(world->models, i));
      i++) {
    if(!strcmp(model->name, name)) {
      librdf_log(world,
                 0, LIBRDF_LOG_ERROR, LIBRDF_FROM_MODEL, NULL,
                 "model %s already registered", model->name);
      return;
    }
  }

  model=(librdf_model_factory*)LIBRDF_CALLOC(librdf_model_factory, 1,
                                             sizeof(librdf_model_factory));
  if(!model)
    goto oom;

  model->name=(char*)LIBRDF_MALLOC(cstring, strlen(name)+1);
  if(!model->name)
    goto oom_tidy;
  strcpy(model->name, name);

  model->label=(char*)LIBRDF_MALLOC(cstring, strlen(label)+1);
  if(!model->label)
    goto oom_tidy;
  strcpy(model->label, label);

  if(raptor_sequence_push(world->models, model))
    goto oom;

  /* Call the model registration function on the new object */
  (*factory)(model);
  
#if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 1
  LIBRDF_DEBUG3("%s has context size %d\n", name, model->context_length);
#endif

  return;

  oom_tidy:
  librdf_free_model_factory(model);
  oom:
  LIBRDF_FATAL1(world, LIBRDF_FROM_MODEL, "Out of memory");
}


/**
 * librdf_get_model_factory:
 * @world: redland world object
 * @name: the factory name or NULL for the default factory
 *
 * Get a model factory by name.
 * 
 * Return value: the factory object or NULL if there is no such factory
 **/
librdf_model_factory*
librdf_get_model_factory(librdf_world* world, const char *name) 
{
  librdf_model_factory *factory;

  librdf_world_open(world);

  /* return 1st model if no particular one wanted - why? */
  if(!name) {
    factory=(librdf_model_factory *)raptor_sequence_get_at(world->models, 0);
    if(!factory) {
      LIBRDF_DEBUG1("No (default) models registered\n");
      return NULL;
    }
  } else {
    int i;
    
    for(i=0;
        (factory=(librdf_model_factory*)raptor_sequence_get_at(world->models, i));
        i++) {
      if(!strcmp(factory->name, name))
        break;
    }
    /* else FACTORY name not found */
    if(!factory) {
      LIBRDF_DEBUG2("No model with name %s found\n", name);
      return NULL;
    }
  }
        
  return factory;
}


/**
 * librdf_model_enumerate:
 * @world: redland world object
 * @counter: index into the list of models
 * @name: pointer to store the name of the model (or NULL)
 * @label: pointer to store syntax readable label (or NULL)
 *
 * Get information on models.
 * 
 * Return value: non 0 on failure of if counter is out of range
 **/
int
librdf_model_enumerate(librdf_world* world,
                       const unsigned int counter,
                       const char **name, const char **label)
{
  librdf_model_factory *factory;
  
  librdf_world_open(world);

  factory=(librdf_model_factory*)raptor_sequence_get_at(world->models,
                                                        counter);
  if(!factory)
    return 1;
  
  if(name)
    *name=factory->name;
  if(label)
    *label=factory->label;
  return 0;
}


/**
 * librdf_new_model:
 * @world: redland world object
 * @storage: #librdf_storage to use
 * @options_string: options to initialise model
 *
 * Constructor - create a new storage #librdf_model object.
 *
 * The options are encoded as described in librdf_hash_from_string()
 * and can be NULL if none are required.
 *
 * Return value: a new #librdf_model object or NULL on failure
 */
librdf_model*
librdf_new_model (librdf_world *world,
                  librdf_storage *storage, const char *options_string) {
  librdf_hash* options_hash;
  librdf_model *model;

  librdf_world_open(world);

  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(storage, librdf_storage, NULL);

  if(!storage)
    return NULL;
  
  options_hash=librdf_new_hash(world, NULL);
  if(!options_hash)
    return NULL;
  
  if(librdf_hash_from_string(options_hash, options_string)) {
    librdf_free_hash(options_hash);
    return NULL;
  }

  model=librdf_new_model_with_options(world, storage, options_hash);
  librdf_free_hash(options_hash);
  return model;
}


/**
 * librdf_new_model_with_options:
 * @world: redland world object
 * @storage: #librdf_storage storage to use
 * @options: #librdf_hash of options to use
 * 
 * Constructor - Create a new #librdf_model with storage.
 *
 * Options are presently not used.
 *
 * Return value: a new #librdf_model object or NULL on failure
 **/
librdf_model*
librdf_new_model_with_options(librdf_world *world,
                              librdf_storage *storage, librdf_hash* options)
{
  librdf_model *model;
  librdf_uri *uri;
  
  librdf_world_open(world);

  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(storage, librdf_storage, NULL);

  if(!storage)
    return NULL;
  
  model=(librdf_model*)LIBRDF_CALLOC(librdf_model, 1, sizeof(librdf_model));
  if(!model)
    return NULL;
  
  model->world=world;

  model->factory=librdf_get_model_factory(world, "storage");
  if(!model->factory) {
    LIBRDF_FREE(librdf_model, model);
    return NULL;
  }
    
  model->context=LIBRDF_CALLOC(data, 1, model->factory->context_length);

  if(!model->context || model->factory->create(model, storage, options)) {
    if(model->context)
      LIBRDF_FREE(data, model->context);
    LIBRDF_FREE(librdf_model, model);
    return NULL;
  }

  uri=librdf_new_uri(world, (const unsigned char*)LIBRDF_MODEL_FEATURE_CONTEXTS);
  if(uri) {
    librdf_node *node=librdf_model_get_feature(model, uri);
    if(node) {
      model->supports_contexts=atoi((const char*)librdf_node_get_literal_value(node));
      librdf_free_node(node);
    }
    librdf_free_uri(uri);
  }

  model->usage=1;

  return model;
}


/**
 * librdf_new_model_from_model:
 * @model: the existing #librdf_model
 *
 * Copy constructor - create a new librdf_model from an existing one.
 * 
 * Creates a new model as a copy of the existing model in the same
 * storage context.
 * 
 * Return value: a new #librdf_model or NULL on failure
 **/
librdf_model*
librdf_new_model_from_model(librdf_model* model)
{
  librdf_model *new_model;

  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, NULL);

  new_model=model->factory->clone(model);
  if(new_model) {
    new_model->supports_contexts=model->supports_contexts;
    new_model->usage=1;
  }
  return new_model;
}


/**
 * librdf_free_model:
 * @model: #librdf_model model to destroy
 *
 * Destructor - Destroy a #librdf_model object.
 * 
 **/
void
librdf_free_model(librdf_model *model)
{
  librdf_iterator* iterator;
  librdf_model* m;

  LIBRDF_ASSERT_OBJECT_POINTER_RETURN(model, librdf_model);

  if(--model->usage)
    return;
  
  if(model->sub_models) {
    iterator=librdf_list_get_iterator(model->sub_models);
    if(iterator) {
      while(!librdf_iterator_end(iterator)) {
        m=(librdf_model*)librdf_iterator_get_object(iterator);
        if(m)
          librdf_free_model(m);
        librdf_iterator_next(iterator);
      }
      librdf_free_iterator(iterator);
    }
    librdf_free_list(model->sub_models);
  } else {
    model->factory->destroy(model);
  }
  LIBRDF_FREE(data, model->context);

  LIBRDF_FREE(librdf_model, model);
}


void
librdf_model_add_reference(librdf_model *model)
{
  model->usage++;
}

void
librdf_model_remove_reference(librdf_model *model)
{
  model->usage--;
}


/* methods */

/**
 * librdf_model_size:
 * @model: #librdf_model object
 *
 * Get the number of statements in the model.
 * 
 * WARNING: Not all underlying stores can return the size of the graph
 * In which case the return value will be negative.
 *
 * Return value: the number of statements or <0 if not possible
 **/
int
librdf_model_size(librdf_model* model)
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, -1);

  return model->factory->size(model);
}


/**
 * librdf_model_add_statement:
 * @model: model object
 * @statement: statement object
 *
 * Add a statement to the model.
 * 
 * The passed-in statement is copied when added to the model, not
 * shared with the model.  It must be a complete statement - all
 * of subject, predicate, object parts must be present.
 *
 * Only statements that are legal RDF can be added: URI or blank subject,
 * URI predicate and URI or blank or literal object (i.e. anything).
 *
 * If the statement already exists in the model, it is not added.
 * Duplicate statements can be added when used with Redland Contexts
 * such as with #librdf_model_context_add_statement
 *
 * Return value: non 0 on failure
 **/
int
librdf_model_add_statement(librdf_model* model, librdf_statement* statement)
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(statement, librdf_statement, 1);

  if(!librdf_statement_is_complete(statement))
    return 1;

  /* Do not add duplicate statements */
  if(librdf_model_contains_statement(model, statement))
    return 0;
  
  return model->factory->add_statement(model, statement);
}


/**
 * librdf_model_add_statements:
 * @model: model object
 * @statement_stream: stream of statements to use
 *
 * Add a stream of statements to the model.
 * 
 * If any of the statements are illegal RDF statements they will
 * be skipped and not added.  See #librdf_model_add_statement for the detail.
 *
 * If any of the statements already exists in the store, they are not
 * added unless Redland contexts are being used.  See also
 * #librdf_model_context_add_statements
 *
 * Return value: non 0 on failure
 **/
int
librdf_model_add_statements(librdf_model* model, librdf_stream* statement_stream)
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(statement_stream, librdf_statement, 1);

  return model->factory->add_statements(model, statement_stream);
}


/**
 * librdf_model_add:
 * @model: model object
 * @subject: #librdf_node of subject
 * @predicate: #librdf_node of predicate
 * @object: #librdf_node of object (literal or resource)
 *
 * Create and add a new statement about a resource to the model.
 * 
 * After this method, the #librdf_node objects become owned by the model.
 * All of subject, predicate and object must be non-NULL.
 * 
 * Return value: non 0 on failure
 **/
int
librdf_model_add(librdf_model* model, librdf_node* subject, 
		 librdf_node* predicate, librdf_node* object)
{
  librdf_statement *statement;
  int result;

  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(subject, librdf_node, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(predicate, librdf_node, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(object, librdf_node, 1);

  if(!subject ||
     (!librdf_node_is_resource(subject) && !librdf_node_is_blank(subject)))
    return 1;

  if(!predicate || !librdf_node_is_resource(predicate))
     return 1;

  if(!object)
    return 1;

  statement=librdf_new_statement(model->world);
  if(!statement)
    return 1;

  librdf_statement_set_subject(statement, subject);
  librdf_statement_set_predicate(statement, predicate);
  librdf_statement_set_object(statement, object);

  result=librdf_model_add_statement(model, statement);
  librdf_free_statement(statement);
  
  return result;
}


/**
 * librdf_model_add_typed_literal_statement:
 * @model: model object
 * @subject: #librdf_node of subject
 * @predicate: #librdf_node of predicate
 * @literal: string literal content
 * @xml_language: language of literal
 * @datatype_uri: datatype #librdf_uri
 *
 * Create and add a new statement about a typed literal to the model.
 * 
 * After this method, the #librdf_node subject and predicate become
 * owned by the model.
 * 
 * The language can be set to NULL if not used.
 * All of subject, predicate and literal must be non-NULL.
 *
 * Return value: non 0 on failure
 **/
int
librdf_model_add_typed_literal_statement(librdf_model* model, 
                                         librdf_node* subject, 
                                         librdf_node* predicate, 
                                         const unsigned char* literal,
                                         const char *xml_language,
                                         librdf_uri *datatype_uri)
{
  librdf_node* object;

  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(subject, librdf_node, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(predicate, librdf_node, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(literal, string, 1);

  if(!subject ||
     (!librdf_node_is_resource(subject) && !librdf_node_is_blank(subject)))
    return 1;

  if(!predicate || !librdf_node_is_resource(predicate))
     return 1;

  if(!literal)
    return 1;

  object=librdf_new_node_from_typed_literal(model->world,
                                            literal, xml_language, 
                                            datatype_uri);
  if(!object)
    return 1;
  
  return librdf_model_add(model, subject, predicate, object);
}


/**
 * librdf_model_add_string_literal_statement:
 * @model: model object
 * @subject: #librdf_node of subject
 * @predicate: #librdf_node of predicate
 * @literal: string literal conten
 * @xml_language: language of literal
 * @is_wf_xml: literal is XML
 *
 * Create and add a new statement about a literal to the model.
 * 
 * The language can be set to NULL if not used.
 * All of subject, predicate and literal must be non-NULL.
 *
 * 0.9.12: xml_space argument deleted
 *
 * Return value: non 0 on failure
 **/
int
librdf_model_add_string_literal_statement(librdf_model* model, 
                                          librdf_node* subject, 
                                          librdf_node* predicate, 
                                          const unsigned char* literal,
                                          const char *xml_language,
                                          int is_wf_xml)
{
  librdf_node* object;
  int result;
  
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(subject, librdf_node, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(predicate, librdf_node, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(literal, string, 1);

  if(!subject ||
     (!librdf_node_is_resource(subject) && !librdf_node_is_blank(subject)))
    return 1;

  if(!predicate || !librdf_node_is_resource(predicate))
     return 1;

  if(!literal)
    return 1;

  object=librdf_new_node_from_literal(model->world,
                                      literal, xml_language, 
                                      is_wf_xml);
  if(!object)
    return 1;
  
  result=librdf_model_add(model, subject, predicate, object);
  if(result)
    librdf_free_node(object);
  
  return result;
}


/**
 * librdf_model_remove_statement:
 * @model: the model object
 * @statement: the statement
 *
 * Remove a known statement from the model.
 *
 * It must be a complete statement - all of subject, predicate, object
 * parts must be present and a legal RDF triple.
 *
 * Return value: non 0 on failure
 **/
int
librdf_model_remove_statement(librdf_model* model, librdf_statement* statement)
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(statement, librdf_statement, 1);

  if(!librdf_statement_is_complete(statement))
    return 1;

  return model->factory->remove_statement(model, statement);
}


/**
 * librdf_model_contains_statement:
 * @model: the model object
 * @statement: the statement
 *
 * Check for a statement in the model.
 * 
 * It must be a complete statement - all of subject, predicate,
 * object parts must be present and a legal RDF triple.  Use
 * librdf_model_find_statements to search for partial statement
 * matches.
 *
 * WARNING: librdf_model_contains_statement may not work correctly
 * with stores using contexts.  In this case, a search using
 * librdf_model_find_statements for a non-empty list will
 * return the correct result.
 *
 * Return value: non 0 if the model contains the statement (>0 if the statement is illegal)
 **/
int
librdf_model_contains_statement(librdf_model* model, librdf_statement* statement)
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, 0);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(statement, librdf_statement, 0);

  if(!librdf_statement_is_complete(statement))
    return 1;

  return model->factory->contains_statement(model, statement);
}


/**
 * librdf_model_as_stream:
 * @model: the model object
 *
 * List the model contents as a stream of statements.
 * 
 * Return value: a #librdf_stream or NULL on failure
 **/
librdf_stream*
librdf_model_as_stream(librdf_model* model)
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, NULL);

  return model->factory->serialise(model);
}


/**
 * librdf_model_serialise:
 * @model: the model object
 *
 * Serialise the entire model as a stream (DEPRECATED).
 * 
 * DEPRECATED to reduce confusion with the librdf_serializer class.
 * Please use librdf_model_as_stream.
 *
 * Return value: a #librdf_stream or NULL on failure
 **/
librdf_stream*
librdf_model_serialise(librdf_model* model)
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, NULL);

  return model->factory->serialise(model);
}


/**
 * librdf_model_find_statements:
 * @model: the model object
 * @statement: the partial statement to match
 *
 * Find matching statements in the model.
 * 
 * The partial statement is a statement where the subject, predicate
 * and/or object can take the value NULL which indicates a match with
 * any value in the model
 * 
 * Return value: a #librdf_stream of statements (can be empty) or NULL
 * on failure.
 **/
librdf_stream*
librdf_model_find_statements(librdf_model* model, 
                             librdf_statement* statement)
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(statement, librdf_statement, NULL);

  return model->factory->find_statements(model, statement);
}


/**
 * librdf_model_get_sources:
 * @model: #librdf_model object
 * @arc: #librdf_node arc
 * @target: #librdf_node target
 *
 * Return the sources (subjects) of arc in an RDF graph given arc (predicate) and target (object).
 * 
 * Searches the model for arcs matching the given arc and target
 * and returns a list of the source #librdf_node objects as an iterator
 * 
 * Return value:  #librdf_iterator of #librdf_node objects (may be empty) or NULL on failure
 **/
librdf_iterator*
librdf_model_get_sources(librdf_model *model,
                         librdf_node *arc, librdf_node *target) 
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(arc, librdf_node, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(target, librdf_node, NULL);

  return model->factory->get_sources(model, arc, target);
}


/**
 * librdf_model_get_arcs:
 * @model: #librdf_model object
 * @source: #librdf_node source
 * @target: #librdf_node target
 *
 * Return the arcs (predicates) of an arc in an RDF graph given source (subject) and target (object).
 * 
 * Searches the model for arcs matching the given source and target
 * and returns a list of the arc #librdf_node objects as an iterator
 * 
 * Return value:  #librdf_iterator of #librdf_node objects (may be empty) or NULL on failure
 **/
librdf_iterator*
librdf_model_get_arcs(librdf_model *model,
                      librdf_node *source, librdf_node *target) 
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(source, librdf_node, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(target, librdf_node, NULL);

  return model->factory->get_arcs(model, source, target);
}


/**
 * librdf_model_get_targets:
 * @model: #librdf_model object
 * @source: #librdf_node source
 * @arc: #librdf_node arc
 *
 * Return the targets (objects) of an arc in an RDF graph given source (subject) and arc (predicate).
 * 
 * Searches the model for targets matching the given source and arc
 * and returns a list of the source #librdf_node objects as an iterator
 * 
 * Return value:  #librdf_iterator of #librdf_node objects (may be empty) or NULL on failure
 **/
librdf_iterator*
librdf_model_get_targets(librdf_model *model,
                         librdf_node *source, librdf_node *arc) 
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(source, librdf_node, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(arc, librdf_node, NULL);

  return model->factory->get_targets(model, source, arc);
}


/**
 * librdf_model_get_source:
 * @model: #librdf_model object
 * @arc: #librdf_node arc
 * @target: #librdf_node target
 *
 * Return one source (subject) of arc in an RDF graph given arc (predicate) and target (object).
 * 
 * Searches the model for arcs matching the given arc and target
 * and returns one #librdf_node object
 * 
 * Return value:  a new #librdf_node object or NULL on failure
 **/
librdf_node*
librdf_model_get_source(librdf_model *model,
                        librdf_node *arc, librdf_node *target) 
{
  librdf_iterator *iterator;
  librdf_node *node;

  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(arc, librdf_node, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(target, librdf_node, NULL);

  iterator=librdf_model_get_sources(model, arc, target);
  if(!iterator)
    return NULL;
  
  node=(librdf_node*)librdf_iterator_get_object(iterator);
  if(node)
    node=librdf_new_node_from_node(node);
  librdf_free_iterator(iterator);
  return node;
}


/**
 * librdf_model_get_arc:
 * @model: #librdf_model object
 * @source: #librdf_node source
 * @target: #librdf_node target
 *
 * Return one arc (predicate) of an arc in an RDF graph given source (subject) and target (object).
 * 
 * Searches the model for arcs matching the given source and target
 * and returns one #librdf_node object
 * 
 * Return value:  a new #librdf_node object or NULL on failure
 **/
librdf_node*
librdf_model_get_arc(librdf_model *model,
                     librdf_node *source, librdf_node *target) 
{
  librdf_iterator *iterator;
  librdf_node *node;

  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(source, librdf_node, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(target, librdf_node, NULL);

  iterator=librdf_model_get_arcs(model, source, target);
  if(!iterator)
    return NULL;
  
  node=(librdf_node*)librdf_iterator_get_object(iterator);
  if(node)
    node=librdf_new_node_from_node(node);
  librdf_free_iterator(iterator);
  return node;
}


/**
 * librdf_model_get_target:
 * @model: #librdf_model object
 * @source: #librdf_node source
 * @arc: #librdf_node arc
 *
 * Return one target (object) of an arc in an RDF graph given source (subject) and arc (predicate).
 * 
 * Searches the model for targets matching the given source and arc
 * and returns one #librdf_node object
 * 
 * Return value:  a new #librdf_node object or NULL on failure
 **/
librdf_node*
librdf_model_get_target(librdf_model *model,
                        librdf_node *source, librdf_node *arc) 
{
  librdf_iterator *iterator;
  librdf_node *node;

  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(source, librdf_node, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(arc, librdf_node, NULL);

  iterator=librdf_model_get_targets(model, source, arc);
  if(!iterator)
    return NULL;
  
  node=(librdf_node*)librdf_iterator_get_object(iterator);
  if(node)
    node=librdf_new_node_from_node(node);
  librdf_free_iterator(iterator);
  return node;
}


/**
 * librdf_model_add_submodel:
 * @model: the model object
 * @sub_model: the sub model to add
 *
 * Add a sub-model to the model.
 * 
 * FIXME: Not tested
 * 
 * Return value: non 0 on failure
 **/
int
librdf_model_add_submodel(librdf_model* model, librdf_model* sub_model)
{
  librdf_list *l=model->sub_models;
  
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(sub_model, librdf_model, 1);

  if(!l) {
    l=librdf_new_list(model->world);
    if(!l)
      return 1;
    model->sub_models=l;
  }
  
  if(librdf_list_add(l, sub_model))
    return 1;
  
  return 0;
}



/**
 * librdf_model_remove_submodel:
 * @model: the model object
 * @sub_model: the sub model to remove
 *
 * Remove a sub-model from the model.
 * 
 * FIXME: Not tested
 * 
 * Return value: non 0 on failure
 **/
int
librdf_model_remove_submodel(librdf_model* model, librdf_model* sub_model)
{
  librdf_list *l=model->sub_models;
  
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(sub_model, librdf_model, 1);

  if(!l)
    return 1;
  if(!librdf_list_remove(l, sub_model))
    return 1;
  
  return 0;
}



/**
 * librdf_model_get_arcs_in:
 * @model: #librdf_model object
 * @node: #librdf_node resource node
 *
 * Return the properties pointing to the given resource.
 * 
 * Return value:  #librdf_iterator of #librdf_node objects (may be empty) or NULL on failure
 **/
librdf_iterator*
librdf_model_get_arcs_in(librdf_model *model, librdf_node *node) 
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(node, librdf_node, NULL);

  return model->factory->get_arcs_in(model, node);
}


/**
 * librdf_model_get_arcs_out:
 * @model: #librdf_model object
 * @node: #librdf_node resource node
 *
 * Return the properties pointing from the given resource.
 * 
 * Return value:  #librdf_iterator of #librdf_node objects (may be empty) or NULL on failure
 **/
librdf_iterator*
librdf_model_get_arcs_out(librdf_model *model, librdf_node *node) 
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(node, librdf_node, NULL);

  return model->factory->get_arcs_out(model, node);
}


/**
 * librdf_model_has_arc_in:
 * @model: #librdf_model object
 * @node: #librdf_node resource node
 * @property: #librdf_node property node
 *
 * Check if a node has a given property pointing to it.
 * 
 * Return value: non 0 if arc property does point to the resource node
 **/
int
librdf_model_has_arc_in(librdf_model *model, librdf_node *node, 
                        librdf_node *property) 
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, 0);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(node, librdf_node, 0);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(property, librdf_node, 0);

  return model->factory->has_arc_in(model, node, property);
}


/**
 * librdf_model_has_arc_out:
 * @model: #librdf_model object
 * @node: #librdf_node resource node
 * @property: #librdf_node property node
 *
 * Check if a node has a given property pointing from it.
 * 
 * Return value: non 0 if arc property does point from the resource node
 **/
int
librdf_model_has_arc_out(librdf_model *model, librdf_node *node,
                         librdf_node *property) 
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, 0);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(node, librdf_node, 0);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(property, librdf_node, 0);

  return model->factory->has_arc_out(model, node, property);
}




/**
 * librdf_model_print:
 * @model: the model object
 * @fh: the FILE stream to print to
 *
 * Print the model.
 * 
 * This method is for debugging and the format of the output should
 * not be relied on.
 **/
void
librdf_model_print(librdf_model *model, FILE *fh)
{
  librdf_stream* stream;
  
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN(model, librdf_model);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN(fh, FILE*);

  stream=librdf_model_as_stream(model);
  if(!stream)
    return;
  fputs("[[\n", fh);
  librdf_stream_print(stream, fh);
  fputs("]]\n", fh);
  librdf_free_stream(stream);
}


/**
 * librdf_model_context_add_statement:
 * @model: #librdf_model object
 * @context: #librdf_node context
 * @statement: #librdf_statement statement object
 *
 * Add a statement to a model with a context.
 * 
 * It must be a complete statement - all
 * of subject, predicate, object parts must be present.
 *
 * If @context is NULL, this is equivalent to librdf_model_add_statement
 *
 * Return value: Non 0 on failure
 **/
int
librdf_model_context_add_statement(librdf_model* model, 
                                   librdf_node* context,
                                   librdf_statement* statement) 
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(statement, librdf_statement, 1);

  if(!librdf_statement_is_complete(statement))
    return 1;

  if(!librdf_model_supports_contexts(model)) {
    librdf_log(model->world, 0, LIBRDF_LOG_WARN, LIBRDF_FROM_MODEL, NULL,
               "Model does not support contexts");
    return 1;
  }

  return model->factory->context_add_statement(model, context, statement);
}



/**
 * librdf_model_context_add_statements:
 * @model: #librdf_model object
 * @context: #librdf_node context
 * @stream: #librdf_stream stream object
 *
 * Add statements to a model with a context.
 * 
 * If @context is NULL, this is equivalent to librdf_model_add_statements
 *
 * Return value: Non 0 on failure
 **/
int
librdf_model_context_add_statements(librdf_model* model, 
                                    librdf_node* context,
                                    librdf_stream* stream) 
{
  int status=0;
  
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(stream, librdf_stream, 1);

  if(!stream)
    return 1;

  if(!librdf_model_supports_contexts(model)) {
    librdf_log(model->world, 0, LIBRDF_LOG_WARN, LIBRDF_FROM_MODEL, NULL,
               "Model does not support contexts");
    return 1;
  }

  if(model->factory->context_add_statements)
    return model->factory->context_add_statements(model, context, stream);

  while(!librdf_stream_end(stream)) {
    librdf_statement* statement=librdf_stream_get_object(stream);
    if(!statement)
      break;
    status=librdf_model_context_add_statement(model, context, statement);
    if(status)
      break;
    librdf_stream_next(stream);
  }

  return status;
}



/**
 * librdf_model_context_remove_statement:
 * @model: #librdf_model object
 * @context: #librdf_node context
 * @statement: #librdf_statement statement
 *
 * Remove a statement from a model in a context.
 * 
 * It must be a complete statement - all of subject, predicate, object
 * parts must be present.
 *
 * If @context is NULL, this is equivalent to librdf_model_remove_statement
 *
 * Return value: Non 0 on failure
 **/
int
librdf_model_context_remove_statement(librdf_model* model,
                                      librdf_node* context,
                                      librdf_statement* statement) 
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(statement, librdf_statement, 1);

  if(!librdf_statement_is_complete(statement))
    return 1;

  if(!librdf_model_supports_contexts(model)) {
    librdf_log(model->world, 0, LIBRDF_LOG_WARN, LIBRDF_FROM_MODEL, NULL,
               "Model does not support contexts");
    return 1;
  }

  return model->factory->context_remove_statement(model, context, statement);
}


/**
 * librdf_model_context_remove_statements:
 * @model: #librdf_model object
 * @context: #librdf_node context
 *
 * Remove statements from a model with the given context.
 * 
 * Return value: Non 0 on failure
 **/
int
librdf_model_context_remove_statements(librdf_model* model,
                                       librdf_node* context) 
{
  librdf_stream *stream;

  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, 1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(context, librdf_node, 1);

  if(!librdf_model_supports_contexts(model)) {
    librdf_log(model->world, 0, LIBRDF_LOG_WARN, LIBRDF_FROM_MODEL, NULL,
               "Model does not support contexts");
    return 1;
  }

  if(model->factory->context_remove_statements)
    return model->factory->context_remove_statements(model, context);

  stream=librdf_model_context_as_stream(model, context);
  if(!stream)
    return 1;

  while(!librdf_stream_end(stream)) {
    librdf_statement *statement=librdf_stream_get_object(stream);
    if(!statement)
      break;
    librdf_model_context_remove_statement(model, context, statement);
    librdf_stream_next(stream);
  }
  librdf_free_stream(stream);  
  return 0;
}


/**
 * librdf_model_context_as_stream:
 * @model: #librdf_model object
 * @context: #librdf_node context
 *
 * List all statements in a model context.
 * 
 * Return value: #librdf_stream of statements or NULL on failure
 **/
librdf_stream*
librdf_model_context_as_stream(librdf_model* model, librdf_node* context) 
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(context, librdf_node, NULL);

  if(!librdf_model_supports_contexts(model)) {
    librdf_log(model->world, 0, LIBRDF_LOG_WARN, LIBRDF_FROM_MODEL, NULL,
               "Model does not support contexts");
    return NULL;
  }

  return model->factory->context_serialize(model, context);
}


/**
 * librdf_model_context_serialize:
 * @model: #librdf_model object
 * @context: #librdf_node context
 *
 * List all statements in a model context.
 * 
 * DEPRECATED to reduce confusion with the librdf_serializer class.
 * Please use librdf_model_context_as_stream.
 *
 * Return value: #librdf_stream of statements or NULL on failure
 **/
librdf_stream*
librdf_model_context_serialize(librdf_model* model, librdf_node* context) 
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(context, librdf_node, NULL);

  if(!librdf_model_supports_contexts(model)) {
    librdf_log(model->world, 0, LIBRDF_LOG_WARN, LIBRDF_FROM_MODEL, NULL,
               "Model does not support contexts");
    return NULL;
  }

  return model->factory->context_serialize(model, context);
}


/**
 * librdf_model_query_execute:
 * @model: #librdf_model object
 * @query: #librdf_query object
 *
 * Execute a query against the model.
 * 
 * Run the given query against the model and return a #librdf_stream of
 * matching #librdf_statement objects
 * 
 * Return value: #librdf_query_results or NULL on failure
 **/
librdf_query_results*
librdf_model_query_execute(librdf_model* model, librdf_query* query) 
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(query, librdf_query, NULL);

  return model->factory->query_execute(model, query);
}


/**
 * librdf_model_sync:
 * @model: #librdf_model object
 *
 * Synchronise the model to the model implementation.
 * 
 * Return value: non-0 on failure
 **/
int
librdf_model_sync(librdf_model* model) 
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, 1);

  if(model->factory->sync)
    return model->factory->sync(model);

  return 0;
}


/**
 * librdf_model_get_storage:
 * @model: #librdf_model object
 *
 * Return the storage of this model.
 * 
 * Note: this can only return one storage, so model implementations
 * that have multiple #librdf_storage internally may chose not to
 * implement this.
 *
 * Return value:  #librdf_storage or NULL if this has no store
 **/
librdf_storage*
librdf_model_get_storage(librdf_model *model)
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, NULL);

  if(model->factory->get_storage)
    return model->factory->get_storage(model);
  else
    return NULL;
}


/**
 * librdf_model_find_statements_in_context:
 * @model: #librdf_model object
 * @statement: #librdf_statement partial statement to find
 * @context_node: context #librdf_node (or NULL)
 *
 * Search the model for matching statements in a given context.
 * 
 * Searches the model for a (partial) statement as described in
 * librdf_statement_match() in the given context and returns a
 * #librdf_stream of matching #librdf_statement objects.  If
 * context is NULL, this is equivalent to librdf_model_find_statements.
 * 
 * Return value: #librdf_stream of matching statements (may be empty) or NULL on failure
 **/
librdf_stream*
librdf_model_find_statements_in_context(librdf_model* model, librdf_statement* statement, librdf_node* context_node) 
{
  librdf_stream *stream;

  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(statement, librdf_statement, NULL);

  if(!librdf_model_supports_contexts(model)) {
    librdf_log(model->world, 0, LIBRDF_LOG_WARN, LIBRDF_FROM_MODEL, NULL,
               "Model does not support contexts");
    return NULL;
  }

  if(model->factory->find_statements_in_context)
    return model->factory->find_statements_in_context(model, statement, context_node);

  statement=librdf_new_statement_from_statement(statement);
  if(!statement)
    return NULL;

  stream=librdf_model_context_as_stream(model, context_node);
  if(!stream) {
    librdf_free_statement(statement);
    return librdf_new_empty_stream(model->world);
  }

  librdf_stream_add_map(stream,
                        &librdf_stream_statement_find_map,
                        (librdf_stream_map_free_context_handler)&librdf_free_statement, (void*)statement);

  return stream;
}


/**
 * librdf_model_get_contexts:
 * @model: #librdf_model object
 *
 * Return the list of contexts in the graph.
 * 
 * Returns an iterator of #librdf_node context nodes for each
 * context in the graph.
 *
 * Return value: #librdf_iterator of context nodes or NULL on failure or if contexts are not supported
 **/
librdf_iterator*
librdf_model_get_contexts(librdf_model* model) 
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, NULL);

  if(!librdf_model_supports_contexts(model)) {
    librdf_log(model->world, 0, LIBRDF_LOG_WARN, LIBRDF_FROM_MODEL, NULL,
               "Model does not support contexts");
    return NULL;
  }

  if(model->factory->get_contexts)
    return model->factory->get_contexts(model);
  else
    return NULL;
}


/**
 * librdf_model_get_feature:
 * @model: #librdf_model object
 * @feature: #librdf_uri feature property
 *
 * Get the value of a graph feature .
 * 
 * Return value: new #librdf_node feature value or NULL if no such feature
 * exists or the value is empty.
 **/
librdf_node*
librdf_model_get_feature(librdf_model* model, librdf_uri* feature)
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, NULL);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(feature, librdf_uri, NULL);

  if(model->factory->get_feature)
    return model->factory->get_feature(model, feature);
  return NULL;
}


/**
 * librdf_model_set_feature:
 * @model: #librdf_model object
 * @feature: #librdf_uri feature property
 * @value: #librdf_node feature property value
 *
 * Set the value of a graph feature.
 * 
 * Return value: non 0 on failure (negative if no such feature)
 **/
int
librdf_model_set_feature(librdf_model* model, librdf_uri* feature,
                         librdf_node* value)
{
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(model, librdf_model, -1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(feature, librdf_uri, -1);
  LIBRDF_ASSERT_OBJECT_POINTER_RETURN_VALUE(value, librdf_node, -1);

  if(model->factory->set_feature)
    return model->factory->set_feature(model, feature, value);
  return -1;
}


/**
 * librdf_model_find_statements_with_options:
 * @model: #librdf_model object
 * @statement: #librdf_statement partial statement to find
 * @context_node: #librdf_node context node or NULL.
 * @options: #librdf_hash of matching options or NULL
 *
 * Search the model for matching statements with match options.
 * 
 * Searches the model for a (partial) statement as described in
 * librdf_statement_match() and returns a #librdf_stream of
 * matching #librdf_statement objects.
 * 
 * If options is given then the match is made according to
 * the given options.  If options is NULL, this is equivalent
 * to librdf_model_find_statements_in_context.
 * 
 * Return value:  #librdf_stream of matching statements (may be empty) or NULL on failure
 **/
librdf_stream*
librdf_model_find_statements_with_options(librdf_model* model,
                                          librdf_statement* statement,
                                          librdf_node* context_node,
                                          librdf_hash* options) 
{
  if(context_node && !librdf_model_supports_contexts(model)) {
    librdf_log(model->world, 0, LIBRDF_LOG_WARN, LIBRDF_FROM_MODEL, NULL,
               "Model does not support contexts");
    return NULL;
  }

  if(model->factory->find_statements_with_options)
    return model->factory->find_statements_with_options(model, statement, context_node, options);
  else
    return librdf_model_find_statements_in_context(model, statement, context_node);
}


/**
 * librdf_model_load:
 * @model: #librdf_model object
 * @uri: the URI to read the content
 * @name: the name of the parser (or NULL)
 * @mime_type: the MIME type of the syntax (NULL if not used)
 * @type_uri: URI identifying the syntax (NULL if not used)
 *
 * Load content from a URI into the model.
 *
 * If the name field is NULL, the library will try to guess
 * the parser to use from the uri, mime_type and type_uri fields.
 * This is done via the raptor_guess_parser_name function.
 * 
 * Return value: non 0 on failure
 **/
int
librdf_model_load(librdf_model* model, librdf_uri *uri,
                  const char *name, const char *mime_type, 
                  librdf_uri *type_uri)
{
  int rc=0;
  librdf_parser* parser;

  if(name && !*name)
    name=NULL;
  if(mime_type && !*mime_type)
    mime_type=NULL;

  if(!name)
    name=raptor_guess_parser_name((raptor_uri*)type_uri, mime_type,
                                  NULL, 0, librdf_uri_as_string(uri));
  parser=librdf_new_parser(model->world, name, NULL, NULL);
  if(!parser)
    return 1;

  rc=librdf_parser_parse_into_model(parser, uri, NULL, model);
  librdf_free_parser(parser);
  return rc;
}


/**
 * librdf_model_to_counted_string:
 * @model: #librdf_model object
 * @uri: base URI to use in serializing (or NULL if not used)
 * @name: the name of the serializer (or NULL for default)
 * @mime_type: the MIME type of the syntax (NULL if not used)
 * @type_uri: URI identifying the syntax (NULL if not used)
 * @string_length_p: pointer to location to store string length (or NULL)
 *
 * Write serialized model to a string.
 *
 * If the name field is NULL, the default serializer will be used.
 *
 * Note: the returned string must be freed by the caller.
 *
 * Return value: new string or NULL on failure
 **/
unsigned char*
librdf_model_to_counted_string(librdf_model* model, librdf_uri *uri,
                               const char *name, const char *mime_type, 
                               librdf_uri *type_uri, size_t* string_length_p)
{
  unsigned char *string=NULL;
  librdf_serializer* serializer;
  
  if(name && !*name)
    name=NULL;
  if(mime_type && !*mime_type)
    mime_type=NULL;

  serializer=librdf_new_serializer(model->world, name, mime_type, type_uri);
  if(!serializer)
    return NULL;

  string=librdf_serializer_serialize_model_to_counted_string(serializer,
                                                             uri, model,
                                                             string_length_p);
  librdf_free_serializer(serializer);
  return string;
}


/**
 * librdf_model_to_string:
 * @model: #librdf_model object
 * @uri: base URI to use in serializing (or NULL if not used)
 * @name: the name of the serializer (or NULL for default)
 * @mime_type: the MIME type of the syntax (NULL if not used)
 * @type_uri: URI identifying the syntax (NULL if not used)
 *
 * Write serialized model to a string.
 *
 * If the name field is NULL, the default serializer will be used.
 *
 * Note: the returned string must be freed by the caller.
 *
 * Return value: new string or NULL on failure
 **/
unsigned char*
librdf_model_to_string(librdf_model* model, librdf_uri *uri,
                       const char *name, const char *mime_type, 
                       librdf_uri *type_uri) {
  return librdf_model_to_counted_string(model, uri, 
                                        name, mime_type, type_uri,
                                        NULL);
}


/**
 * librdf_model_contains_context:
 * @model: the model object
 * @context: the contest
 *
 * Check for a context in the model.
 * 
 * Return value: non 0 if the model contains the context node
 **/
int
librdf_model_contains_context(librdf_model* model, librdf_node* context) {
  librdf_stream* stream;
  int result;

  stream=librdf_model_context_as_stream(model, context);
  if(!stream)
    return 0;
  
  result=!librdf_stream_end(stream);
  librdf_free_stream(stream);

  return result;
}


/**
 * librdf_model_transaction_start:
 * @model: the model object
 * 
 * Start a transaction
 * 
 * Return value: non-0 on failure
 **/
int
librdf_model_transaction_start(librdf_model* model) 
{
  if(model->factory->transaction_start)
    return model->factory->transaction_start(model);
  else
    return 1;
}


/**
 * librdf_model_transaction_start_with_handle:
 * @model: the model object
 * @handle: the transaction object
 * 
 * Start a transaction using an existing external transaction object.
 * 
 * Return value: non-0 on failure
 **/
int
librdf_model_transaction_start_with_handle(librdf_model* model, void* handle)
{
  if(model->factory->transaction_start_with_handle)
    return model->factory->transaction_start_with_handle(model, handle);
  else
    return 1;
}


/**
 * librdf_model_transaction_commit:
 * @model: the model object
 * 
 * Commit a transaction.
 * 
 * Return value: non-0 on failure 
 **/
int
librdf_model_transaction_commit(librdf_model* model) 
{
  if(model->factory->transaction_commit)
    return model->factory->transaction_commit(model);
  else
    return 1;
}


/**
 * librdf_model_transaction_rollback:
 * @model: the model object
 * 
 * Rollback a transaction.
 * 
 * Return value: non-0 on failure 
 **/
int
librdf_model_transaction_rollback(librdf_model* model) 
{
  if(model->factory->transaction_rollback)
    return model->factory->transaction_rollback(model);
  else
    return 1;
}


/**
 * librdf_model_transaction_get_handle:
 * @model: the model object
 * 
 * Get the current transaction handle.
 * 
 * Return value: non-0 on failure 
 **/
void*
librdf_model_transaction_get_handle(librdf_model* model) 
{
  if(model->factory->transaction_get_handle)
    return model->factory->transaction_get_handle(model);
  else
    return NULL;
}

#endif


/* TEST CODE */


#ifdef STANDALONE

/* one more prototype */
int main(int argc, char *argv[]);

#define EX1_CONTENT \
"<?xml version=\"1.0\"?>\n" \
"<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n" \
"         xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n" \
"  <rdf:Description rdf:about=\"http://purl.org/net/dajobe/\">\n" \
"    <dc:title>Dave Beckett's Home Page</dc:title>\n" \
"    <dc:creator>Dave Beckett</dc:creator>\n" \
"    <dc:description>The generic home page of Dave Beckett.</dc:description>\n" \
"  </rdf:Description>\n" \
"</rdf:RDF>"

#define EX2_CONTENT \
"<?xml version=\"1.0\"?>\n" \
"<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n" \
"         xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n" \
"  <rdf:Description rdf:about=\"http://purl.org/net/dajobe/\">\n" \
"    <dc:title>Dave Beckett's Home Page</dc:title>\n" \
"    <dc:creator>Dave Beckett</dc:creator>\n" \
"    <dc:description>I do development-based research on RDF, metadata and web searching.</dc:description>\n" \
"    <dc:rights>Copyright &#169; 2002 Dave Beckett</dc:rights>\n" \
"  </rdf:Description>\n" \
"</rdf:RDF>"

int test_model_cloning(char const *program, librdf_world *);

int
main(int argc, char *argv[]) 
{
  librdf_storage* storage;
  librdf_model* model;
  librdf_statement *statement;
  librdf_parser* parser;
  librdf_stream* stream;
  const char *parser_name="rdfxml";
  #define URI_STRING_COUNT 2
  const unsigned char *file_uri_strings[URI_STRING_COUNT]={(const unsigned char*)"http://example.org/test1.rdf", (const unsigned char*)"http://example.org/test2.rdf"};
  const unsigned char *file_content[URI_STRING_COUNT]={(const unsigned char*)EX1_CONTENT, (const unsigned char*)EX2_CONTENT};
  librdf_uri* uris[URI_STRING_COUNT];
  librdf_node* nodes[URI_STRING_COUNT];
  int i;
  const char *program=librdf_basename((const char*)argv[0]);
  /* initialise dependent modules - all of them! */
  librdf_world *world=librdf_new_world();
  librdf_iterator *iterator, *second_iterator;
  librdf_node *n1, *n2;
  int count;
  int expected_count;
#define EXPECTED_BAD_STRING_LENGTH 317
  librdf_uri* base_uri;
  unsigned char *string;
  size_t string_length=0;
  int remove_count=0;

  librdf_world_open(world);

  /* Test model cloning first */
  if(test_model_cloning(program, world))
    return(1);
  
  fprintf(stderr, "%s: Creating storage\n", program);
  if(1) {
    /* test in memory */
    storage=librdf_new_storage(world, NULL, NULL, "contexts='yes'");
  } else {
    /* test on disk */
    storage=librdf_new_storage(world, "sqlite", "test", "new='yes',contexts='yes'");
    if(!storage)
      storage=librdf_new_storage(world, "hashes", "test", "hash-type='bdb',dir='.',write='yes',new='yes',contexts='yes'");
    if(!storage)
      storage=librdf_new_storage(world, NULL, NULL, "contexts='yes'");
  }
  if(!storage) {
    fprintf(stderr, "%s: Failed to create new storage\n", program);
    return(1);
  }
  fprintf(stderr, "%s: Creating model\n", program);
  model=librdf_new_model(world, storage, NULL);
  if(!model) {
    fprintf(stderr, "%s: Failed to create new model\n", program);
    return(1);
  }

  statement=librdf_new_statement(world);
  /* after this, nodes become owned by model */
  librdf_statement_set_subject(statement, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://www.dajobe.org/"));
  librdf_statement_set_predicate(statement, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://purl.org/dc/elements/1.1/creator"));

  if(!librdf_model_add_statement(model, statement)) {
    fprintf(stderr, "%s: librdf_model_add_statement unexpectedly succeeded adding a partial statement\n", program);
    return(1);
  }

  librdf_statement_set_object(statement, librdf_new_node_from_literal(world, (const unsigned char*)"Dave Beckett", NULL, 0));

  librdf_model_add_statement(model, statement);
  librdf_free_statement(statement);

  /* make it illegal */
  statement=librdf_new_statement(world);
  librdf_statement_set_subject(statement, librdf_new_node_from_literal(world, (const unsigned char*)"Bad Subject", NULL, 0));
  librdf_statement_set_predicate(statement, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/pred"));
  librdf_statement_set_object(statement, librdf_new_node_from_literal(world, (const unsigned char*)"Good Object", NULL, 0));

  if(!librdf_model_add_statement(model, statement)) {
    fprintf(stderr, "%s: librdf_model_add_statement unexpectedly succeeded adding an illegal triple\n", program);
    return(1);
  }
  librdf_free_statement(statement);

  fprintf(stderr, "%s: Printing model\n", program);
  librdf_model_print(model, stderr);
  
  parser=librdf_new_parser(world, parser_name, NULL, NULL);
  if(!parser) {
    fprintf(stderr, "%s: Failed to create new parser type %s\n", program,
            parser_name);
    exit(1);
  }

  for (i=0; i<URI_STRING_COUNT; i++) {
    uris[i]=librdf_new_uri(world, file_uri_strings[i]);
    nodes[i]=librdf_new_node_from_uri_string(world, file_uri_strings[i]);

    fprintf(stderr, "%s: Adding content from %s into statement context\n", program,
            librdf_uri_as_string(uris[i]));
    if(!(stream=librdf_parser_parse_string_as_stream(parser, 
                                                     file_content[i], uris[i]))) {
      fprintf(stderr, "%s: Failed to parse RDF from %s as stream\n", program,
              librdf_uri_as_string(uris[i]));
      exit(1);
    }
    librdf_model_context_add_statements(model, nodes[i], stream);
    librdf_free_stream(stream);

    fprintf(stderr, "%s: Printing model\n", program);
    librdf_model_print(model, stderr);
  }


  fprintf(stderr, "%s: Freeing Parser\n", program);
  librdf_free_parser(parser);


  /* sync - probably a NOP */
  librdf_model_sync(model);


  /* sources */
  n1=librdf_new_node_from_uri_string(world, (const unsigned char*)"http://purl.org/dc/elements/1.1/creator");
  n2=librdf_new_node_from_literal(world, (const unsigned char*)"Dave Beckett", NULL, 0);

  fprintf(stderr, "%s: Looking for sources of arc=", program);
  librdf_node_print(n1, stderr);
  fputs(" target=", stderr);
  librdf_node_print(n2, stderr);
  fputs("\n", stderr);

  iterator=librdf_model_get_sources(model, n1, n2);
  if(!iterator) {
    fprintf(stderr, "%s: librdf_model_get_sources failed\n", program);
    exit(1);
  }

  expected_count=3;
  for(count=0; !librdf_iterator_end(iterator); librdf_iterator_next(iterator), count++) {
    librdf_node* n=(librdf_node*)librdf_iterator_get_object(iterator);
    fputs("  ", stderr);
    librdf_node_print(n, stderr);
    fputs("\n", stderr);
  }
  librdf_free_iterator(iterator);
  if(count != expected_count) {
    fprintf(stderr, "%s: librdf_model_get_sources returned %d nodes, expected %d\n", program, count, expected_count);
    exit(1);
  }
  librdf_free_node(n1);
  librdf_free_node(n2);
  

  /* targets */
  n1=librdf_new_node_from_uri_string(world, (const unsigned char*)"http://purl.org/net/dajobe/");
  n2=librdf_new_node_from_uri_string(world, (const unsigned char*)"http://purl.org/dc/elements/1.1/description");

  fprintf(stderr, "%s: Looking for targets of source=", program);
  librdf_node_print(n1, stderr);
  fputs(" arc=", stderr);
  librdf_node_print(n2, stderr);
  fputs("\n", stderr);

  iterator=librdf_model_get_targets(model, n1, n2);
  if(!iterator) {
    fprintf(stderr, "%s: librdf_model_get_targets failed\n", program);
    exit(1);
  }

  expected_count=2;
  for(count=0; !librdf_iterator_end(iterator); librdf_iterator_next(iterator), count++) {
    librdf_node* n=(librdf_node*)librdf_iterator_get_object(iterator);
    fputs("  ", stderr);
    librdf_node_print(n, stderr);
    fputs("\n", stderr);
  }
  librdf_free_iterator(iterator);
  if(count != expected_count) {
    fprintf(stderr, "%s: librdf_model_get_targets returned %d nodes, expected %d\n", program, count, expected_count);
    exit(1);
  }
  librdf_free_node(n1);
  librdf_free_node(n2);
  

  /* arcs */
  n1=librdf_new_node_from_uri_string(world, (const unsigned char*)"http://purl.org/net/dajobe/");
  n2=librdf_new_node_from_literal(world, (const unsigned char*)"Dave Beckett", NULL, 0);

  fprintf(stderr, "%s: Looking for arcs of source=", program);
  librdf_node_print(n1, stderr);
  fputs(" target=", stderr);
  librdf_node_print(n2, stderr);
  fputs("\n", stderr);

  iterator=librdf_model_get_arcs(model, n1, n2);
  if(!iterator) {
    fprintf(stderr, "%s: librdf_model_get_arcs failed\n", program);
    exit(1);
  }

  expected_count=2;
  for(count=0; !librdf_iterator_end(iterator); librdf_iterator_next(iterator), count++) {
    librdf_node* n=(librdf_node*)librdf_iterator_get_object(iterator);
    fputs("  ", stderr);
    librdf_node_print(n, stderr);
    fputs("\n", stderr);
  }
  librdf_free_iterator(iterator);
  if(count != expected_count) {
    fprintf(stderr, "%s: librdf_model_get_arcs returned %d nodes, expected %d\n", program, count, expected_count);
    exit(1);
  }
  librdf_free_node(n1);
  librdf_free_node(n2);


  fprintf(stderr, "%s: Listing contexts\n", program);
  iterator=librdf_model_get_contexts(model);
  if(!iterator) {
    fprintf(stderr, "%s: librdf_model_get_contexts failed (optional method)\n", program);
  } else {
    expected_count=2;
    for(count=0; !librdf_iterator_end(iterator); librdf_iterator_next(iterator), count++) {
      librdf_node* n=(librdf_node*)librdf_iterator_get_object(iterator);
      fputs("  ", stderr);
      librdf_node_print(n, stderr);
      fputs("\n", stderr);
    }
    librdf_free_iterator(iterator);
    if(count != expected_count) {
      fprintf(stderr, "%s: librdf_model_get_contexts returned %d context nodes, expected %d\n", program, count, expected_count);
      exit(1);
    }
  }

#define TEST_CONTEXT_URI_INDEX 0

  if(librdf_model_contains_context(model, nodes[TEST_CONTEXT_URI_INDEX])) {
    fprintf(stderr, "%s: Model contains context %s\n", program, 
            librdf_uri_as_string(uris[TEST_CONTEXT_URI_INDEX]));
  } else {
    fprintf(stderr, "%s: librdf_model_contains_contexts failed to find context URI %s\n", 
            program, librdf_uri_as_string(uris[TEST_CONTEXT_URI_INDEX]));
    exit(1);
  }
  

  for (i=0; i<URI_STRING_COUNT; i++) {
    fprintf(stderr, "%s: Removing statement context %s\n", program, 
            librdf_uri_as_string(uris[i]));
    librdf_model_context_remove_statements(model, nodes[i]);

    fprintf(stderr, "%s: Printing model\n", program);
    librdf_model_print(model, stderr);
  }

  fprintf(stderr, "%s: Serializing model\n", program);
  base_uri=librdf_new_uri(world, (const unsigned char*)"http://example.org/base#");
  string=librdf_model_to_counted_string(model, base_uri,
                                        "rdfxml", 
                                        NULL, NULL,
                                        &string_length);
  if(string_length != EXPECTED_BAD_STRING_LENGTH) {
    fprintf(stderr, "%s: Serialising to RDF/XML returned string size %d, expected %d\n", program,
            (int)string_length, EXPECTED_BAD_STRING_LENGTH);
    return 1;
  }
  librdf_free_uri(base_uri);
  free(string);
  fprintf(stderr, "%s: Serialized OK\n", program);


#define TEST_SIMILAR_COUNT 9

  /* add some similar statements */
  fprintf(stderr, "%s: Adding %d similar statements\n", program, TEST_SIMILAR_COUNT);
  for(i=0; i < TEST_SIMILAR_COUNT; i++) {
    char literal[6];
    strncpy(literal, "DaveX", 6);
    literal[4]='0'+i;
    statement=librdf_new_statement(world);
    librdf_statement_set_subject(statement, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/"));
    librdf_statement_set_predicate(statement, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://purl.org/dc/elements/1.1/creator"));
    librdf_statement_set_object(statement, librdf_new_node_from_literal(world, (const unsigned char*)literal, NULL, 0));
    
    librdf_model_add_statement(model, statement);
    librdf_free_statement(statement);
  }

  /* targets */
  fprintf(stderr, "%s: iterating similar statements\n", program);
  n1=librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/");
  n2=librdf_new_node_from_uri_string(world, (const unsigned char*)"http://purl.org/dc/elements/1.1/creator");
  iterator=librdf_model_get_targets(model, n1, n2);
  if(!iterator) {
    fprintf(stderr, "%s: librdf_model_get_targets failed\n", program);
    exit(1);
  }

  fprintf(stderr, "%s: iterating similar statements again\n", program);
  second_iterator=librdf_model_get_targets(model, n1, n2);

  /* Deleting 2 statements but only 1 before counting */
  expected_count= TEST_SIMILAR_COUNT - 1;
  for(count=0;
      !librdf_iterator_end(iterator); 
      librdf_iterator_next(iterator), librdf_iterator_next(second_iterator), count++) {
    librdf_node* n;
    char literal[6];

    if(count == 2 ) {
      strncpy(literal, "DaveX", 6);
      literal[4]='0'+count;
      statement=librdf_new_statement(world);
      librdf_statement_set_subject(statement, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/"));
      librdf_statement_set_predicate(statement, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://purl.org/dc/elements/1.1/creator"));
      librdf_statement_set_object(statement, librdf_new_node_from_literal(world, (const unsigned char*)literal, NULL, 0));

      fprintf(stderr, "%s: Removing statement with literal '%s'\n",
              program, literal);
      
      librdf_model_remove_statement(model, statement);
      librdf_free_statement(statement);
      remove_count++;
    }
  
    n=(librdf_node*)librdf_iterator_get_object(iterator);
    fputs("  ", stderr);
    librdf_node_print(n, stderr);
    fputs("\n", stderr);

    if(count == 5 ) {
      strncpy(literal, "DaveX", 6);
      literal[4]='0'+count+remove_count;
      statement=librdf_new_statement(world);
      librdf_statement_set_subject(statement, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://example.org/"));
      librdf_statement_set_predicate(statement, librdf_new_node_from_uri_string(world, (const unsigned char*)"http://purl.org/dc/elements/1.1/creator"));
      librdf_statement_set_object(statement, librdf_new_node_from_literal(world, (const unsigned char*)literal, NULL, 0));

      fprintf(stderr, "%s: Removing statement with literal '%s'\n",
              program, literal);
      
      librdf_model_remove_statement(model, statement);
      librdf_free_statement(statement);
      remove_count++;
    }
  
  }
  librdf_free_iterator(iterator);
  if(count != expected_count) {
    fprintf(stderr, "%s: librdf_model_get_targets returned %d nodes, expected %d\n", program, count, expected_count);
    exit(1);
  }
  librdf_free_node(n1);
  librdf_free_node(n2);
  

  librdf_free_iterator(second_iterator);

  fprintf(stderr, "%s: Freeing URIs and Nodes\n", program);
  for (i=0; i<URI_STRING_COUNT; i++) {
    librdf_free_uri(uris[i]);
    librdf_free_node(nodes[i]);
  }
  
  fprintf(stderr, "%s: Freeing model\n", program);
  librdf_free_model(model);

  fprintf(stderr, "%s: Freeing storage\n", program);
  librdf_free_storage(storage);

  librdf_free_world(world);
  
  /* keep gcc -Wall happy */
  return(0);
}

int test_model_cloning(char const *program, librdf_world *world) {
  librdf_storage *storage;
  librdf_model *model1;
  librdf_model *model2;
  librdf_model *model3;

  fprintf(stderr, "%s: Testing model cloning\n", program);

  fprintf(stderr, "%s: Creating hashes storage for cloning\n", program);
  /* only the hashes storage supports cloning */
  storage=librdf_new_storage(world, "hashes", "test", "hash-type='bdb',dir='.',write='yes',new='yes',contexts='yes'");
  if(!storage) {
    fprintf(stderr, "%s: Failed to create new hashes storage\n", program);    
    return 1;
  }
  
  fprintf(stderr, "%s: Creating new model for cloning\n", program);
  model1=librdf_new_model(world, storage, NULL);
  if(!model1) {
    fprintf(stderr, "%s: Failed to create new model\n", program);
    /* ok to leak memory */    
    return 1;
  }
  
  fprintf(stderr, "%s: Cloning model\n", program);
  model2=librdf_new_model_from_model(model1);
  if(!model2) {
    fprintf(stderr, "%s: Failed to clone model\n", program);
    /* ok to leak memory */    
    return 1;
  }

  /* Free original model now so we can test whether the clone is self-sufficient */
  fprintf(stderr, "%s: Freeing original model\n", program);
  librdf_free_model(model1);

  fprintf(stderr, "%s: Cloning cloned model\n", program);
  model3=librdf_new_model_from_model(model2);
  if(!model3) {
    fprintf(stderr, "%s: Failed to clone cloned model\n", program);
    /* ok to leak memory */    
    return 1;
  }

  fprintf(stderr, "%s: Freeing cloned models\n", program);  
  librdf_free_model(model3);
  librdf_free_model(model2);
  
  fprintf(stderr, "%s: Freeing hashes storage\n", program);  
  librdf_free_storage(storage);
  
  return 0;
}

#endif