SQL> @ /scratch/sdas/DEMO/family_demo.sql SQL> -- Basic steps: SQL> -- After you have connected as a privileged user and called SQL> -- SEM_APIS.CREATE_SEM_NETWORK to enable RDF support, SQL> -- connect as a regular database user and do the following. SQL> -- 1. For each desired model, create a table to hold its data. SQL> -- 2. For each model, create a model (SEM_APIS.CREATE_RDF_MODEL). SQL> -- 3. For each table to hold semantic data, insert data into the table. SQL> -- 4. Use various subprograms and constructors. SQL> SQL> connect rdfuser/rdfuser Connected. SQL> SQL> -- Create the table to hold data for the model. SQL> CREATE TABLE family_rdf_data (id NUMBER, triple SDO_RDF_TRIPLE_S); Table created. SQL> SQL> -- Create the model. SQL> execute SEM_APIS.create_rdf_model('family', 'family_rdf_data', 'triple'); PL/SQL procedure successfully completed. SQL> select * from mdsys.rdf_model$; OWNER -------------------------------------------------------------------------------- MODEL_ID MODEL_NAME ---------- ------------------------- TABLE_NAME -------------------------------------------------------------------------------- COLUMN_NAME MODEL_TABLESPACE_NAME ------------------------------ ------------------------------ FLAGS ---------------------------------------- RDFUSER 67 FAMILY FAMILY_RDF_DATA TRIPLE SYSAUX M SQL> SQL> -- Insert rows into the table. These express the following information: SQL> ----------------- SQL> -- John and Janice have two children, Suzie and Matt. SQL> -- Matt married Martha, and they have two children: SQL> -- Tom (male, height 5.75) and Cindy (female, height 06.00). SQL> -- Suzie married Sammy, and they have two children: SQL> -- Cathy (height 5.8) and Jack (male, height 6). SQL> SQL> -- Person is a class that has two subslasses: Male and Female. SQL> -- parentOf is a property that has two subproperties: fatherOf and motherOf. SQL> -- siblingOf is a property that has two subproperties: brotherOf and sisterOf. SQL> -- The domain of the fatherOf and brotherOf properties is Male. SQL> -- The domain of the motherOf and sisterOf properties is Female. SQL> ------------------------ SQL> SQL> -- John is the father of Suzie. SQL> INSERT INTO family_rdf_data VALUES (1, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> commit; Commit complete. SQL> SQL> -- John is the father of Matt. SQL> INSERT INTO family_rdf_data VALUES (2, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> commit; Commit complete. SQL> SQL> -- Janice is the mother of Suzie. SQL> INSERT INTO family_rdf_data VALUES (3, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- Janice is the mother of Matt. SQL> INSERT INTO family_rdf_data VALUES (4, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- Sammy is the father of Cathy. SQL> INSERT INTO family_rdf_data VALUES (5, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- Sammy is the father of Jack. SQL> INSERT INTO family_rdf_data VALUES (6, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- Suzie is the mother of Cathy. SQL> INSERT INTO family_rdf_data VALUES (7, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- Suzie is the mother of Jack. SQL> INSERT INTO family_rdf_data VALUES (8, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- Matt is the father of Tom. SQL> INSERT INTO family_rdf_data VALUES (9, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- Matt is the father of Cindy SQL> INSERT INTO family_rdf_data VALUES (10, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- Martha is the mother of Tom. SQL> INSERT INTO family_rdf_data VALUES (11, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- Martha is the mother of Cindy. SQL> INSERT INTO family_rdf_data VALUES (12, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- Cathy is the sister of Jack. SQL> INSERT INTO family_rdf_data VALUES (13, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- Jack is male. SQL> INSERT INTO family_rdf_data VALUES (14, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- Tom is male. SQL> INSERT INTO family_rdf_data VALUES (15, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- Cindy is female. SQL> INSERT INTO family_rdf_data VALUES (16, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> commit; Commit complete. SQL> SQL> -- ===================== schema ================================== SQL> -- Person is a class. SQL> INSERT INTO family_rdf_data VALUES (17, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- Male is a subclass of Person. SQL> INSERT INTO family_rdf_data VALUES (18, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- Female is a subclass of Person. SQL> INSERT INTO family_rdf_data VALUES (19, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- siblingOf is a property. SQL> INSERT INTO family_rdf_data VALUES (20, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- parentOf is a property. SQL> INSERT INTO family_rdf_data VALUES (21, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- brotherOf is a subproperty of siblingOf. SQL> INSERT INTO family_rdf_data VALUES (22, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- sisterOf is a subproperty of siblingOf. SQL> INSERT INTO family_rdf_data VALUES (23, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- A brother is male. SQL> INSERT INTO family_rdf_data VALUES (24, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- A sister is female. SQL> INSERT INTO family_rdf_data VALUES (25, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- fatherOf is a subproperty of parentOf. SQL> INSERT INTO family_rdf_data VALUES (26, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- motherOf is a subproperty of parentOf. SQL> INSERT INTO family_rdf_data VALUES (27, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- A father is male. SQL> INSERT INTO family_rdf_data VALUES (28, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- A mother is female. SQL> INSERT INTO family_rdf_data VALUES (29, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> commit; Commit complete. SQL> SQL> -- Use SET ESCAPE OFF to prevent the caret (^) from being SQL> -- interpreted as an escape character. Two carets (^^) are SQL> -- used to represent typed literals. SQL> SET ESCAPE OFF; SQL> SQL> -- Cathy's height is 5.8 (decimal). SQL> INSERT INTO family_rdf_data VALUES (30, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '"5.8"^^xsd:decimal')); 1 row created. SQL> SQL> -- Jack's height is 6 (integer). SQL> INSERT INTO family_rdf_data VALUES (31, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '"6"^^xsd:integer')); 1 row created. SQL> SQL> -- Tom's height is 05.75 (decimal). SQL> INSERT INTO family_rdf_data VALUES (32, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '"05.75"^^xsd:decimal')); 1 row created. SQL> SQL> -- Cindy's height is 06.00 (decimal). SQL> INSERT INTO family_rdf_data VALUES (33, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '"06.00"^^xsd:decimal')); 1 row created. SQL> SQL> COMMIT; Commit complete. SQL> SQL> -- RDFS inferencing in the family model SQL> BEGIN 2 SEM_APIS.CREATE_ENTAILMENT( 3 'rdfs_rix_family', 4 SEM_Models('family'), 5 SEM_Rulebases('RDFS')); 6 END; 7 / PL/SQL procedure successfully completed. SQL> SQL> -- Select all males from the family model, without inferencing. SQL> SELECT m 2 FROM TABLE(SEM_MATCH( 3 '(?m rdf:type :Male)', 4 SEM_Models('family'), 5 null, 6 SEM_ALIASES(SEM_ALIAS('','http://www.example.org/family/')), 7 null)); M -------------------------------------------------------------------------------- http://www.example.org/family/Jack http://www.example.org/family/Tom SQL> SQL> -- Select all males from the family model, with RDFS inferencing. SQL> SELECT m 2 FROM TABLE(SEM_MATCH( 3 '(?m rdf:type :Male)', 4 SEM_Models('family'), 5 SDO_RDF_Rulebases('RDFS'), 6 SEM_ALIASES(SEM_ALIAS('','http://www.example.org/family/')), 7 null)); M -------------------------------------------------------------------------------- http://www.example.org/family/Jack http://www.example.org/family/Tom http://www.example.org/family/John http://www.example.org/family/Sammy http://www.example.org/family/Matt SQL> SQL> -- General inferencing in the family model SQL> SQL> EXECUTE SEM_APIS.CREATE_RULEBASE('family_rb'); PL/SQL procedure successfully completed. SQL> SQL> INSERT INTO mdsys.semr_family_rb VALUES( 2 'grandparent_rule', 3 '(?x :parentOf ?y) (?y :parentOf ?z)', 4 NULL, 5 '(?x :grandParentOf ?z)', 6 SEM_ALIASES(SEM_ALIAS('','http://www.example.org/family/'))); 1 row created. SQL> SQL> COMMIT; Commit complete. SQL> SQL> -- Because a new rulebase has been created, and it will be used in the SQL> -- entailment, drop the preceding entailment and then re-create it. SQL> EXECUTE SEM_APIS.DROP_ENTAILMENT ('rdfs_rix_family'); PL/SQL procedure successfully completed. SQL> SQL> -- Re-create the entailment. SQL> BEGIN 2 SEM_APIS.CREATE_ENTAILMENT( 3 'rdfs_rix_family', 4 SEM_Models('family'), 5 SEM_Rulebases('RDFS','family_rb')); 6 END; 7 / PL/SQL procedure successfully completed. SQL> SQL> -- Select all grandfathers and their grandchildren from the family model, SQL> -- without inferencing. (With no inferencing, no results are returned.) SQL> SELECT x grandfather, y grandchild 2 FROM TABLE(SEM_MATCH( 3 '(?x :grandParentOf ?y) (?x rdf:type :Male)', 4 SEM_Models('family'), 5 null, 6 SEM_ALIASES(SEM_ALIAS('','http://www.example.org/family/')), 7 null)); no rows selected SQL> SQL> -- Select all grandfathers and their grandchildren from the family model. SQL> -- Use inferencing from both the RDFS and family_rb rulebases. SQL> SELECT x grandfather, y grandchild 2 FROM TABLE(SEM_MATCH( 3 '(?x :grandParentOf ?y) (?x rdf:type :Male)', 4 SEM_Models('family'), 5 SEM_Rulebases('RDFS','family_rb'), 6 SEM_ALIASES(SEM_ALIAS('','http://www.example.org/family/')), 7 null)); GRANDFATHER -------------------------------------------------------------------------------- GRANDCHILD -------------------------------------------------------------------------------- http://www.example.org/family/John http://www.example.org/family/Cindy http://www.example.org/family/John http://www.example.org/family/Jack http://www.example.org/family/John http://www.example.org/family/Cathy http://www.example.org/family/John http://www.example.org/family/Tom SQL> SQL> -- Set up to find grandfathers of tall (>= 6) grandchildren SQL> -- from the family model, with RDFS inferencing and SQL> -- inferencing using the "family_rb" rulebase. SQL> SQL> UPDATE mdsys.semr_family_rb SET 2 antecedents = '(?x :parentOf ?y) (?y :parentOf ?z) (?z :height ?h)', 3 filter = '(h >= 6)', 4 aliases = SEM_ALIASES(SEM_ALIAS('','http://www.example.org/family/')) 5 WHERE rule_name = 'GRANDPARENT_RULE'; 1 row updated. SQL> SQL> -- Because the rulebase has been updated, drop the preceding entailment, SQL> -- and then re-create it. SQL> EXECUTE SEM_APIS.DROP_ENTAILMENT ('rdfs_rix_family'); PL/SQL procedure successfully completed. SQL> SQL> -- Re-create the entailment. SQL> BEGIN 2 SEM_APIS.CREATE_ENTAILMENT( 3 'rdfs_rix_family', 4 SEM_Models('family'), 5 SEM_Rulebases('RDFS','family_rb')); 6 END; 7 / PL/SQL procedure successfully completed. SQL> SQL> -- Find the entailment that was just created (that is, the SQL> -- one based on the specified model and rulebases). SQL> SELECT SEM_APIS.LOOKUP_ENTAILMENT(SEM_MODELS('family'), 2 SEM_RULEBASES('RDFS','family_rb')) AS lookup_entailment FROM DUAL; LOOKUP_ENTAILMENT -------------------------------------------------------------------------------- RDFS_RIX_FAMILY SQL> SQL> -- Select grandfathers of tall (>= 6) grandchildren, and their SQL> -- tall grandchildren. SQL> SELECT x grandfather, y grandchild 2 FROM TABLE(SEM_MATCH( 3 '(?x :grandParentOf ?y) (?x rdf:type :Male)', 4 SEM_Models('family'), 5 SEM_RuleBases('RDFS','family_rb'), 6 SEM_ALIASES(SEM_ALIAS('','http://www.example.org/family/')), 7 null)); GRANDFATHER -------------------------------------------------------------------------------- GRANDCHILD -------------------------------------------------------------------------------- http://www.example.org/family/John http://www.example.org/family/Cindy http://www.example.org/family/John http://www.example.org/family/Jack SQL> SQL> -- SQL> -- add some OWL property restrictions SQL> -- SQL> SQL> -- potato is a Vegetable SQL> INSERT INTO family_rdf_data VALUES (101, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 'rdf:type', 5 '')); 1 row created. SQL> SQL> -- restriction onProperty eat: allValuesFrom Vegetable SQL> INSERT INTO family_rdf_data VALUES (102, 2 SDO_RDF_TRIPLE_S('family', 3 '_:avfRestriction', 4 'rdf:type', 5 'owl:Restriction')); 1 row created. SQL> SQL> INSERT INTO family_rdf_data VALUES (103, 2 SDO_RDF_TRIPLE_S('family', 3 '_:avfRestriction', 4 'owl:onProperty', 5 '')); 1 row created. SQL> SQL> INSERT INTO family_rdf_data VALUES (104, 2 SDO_RDF_TRIPLE_S('family', 3 '_:avfRestriction', 4 'owl:allValuesFrom', 5 '')); 1 row created. SQL> SQL> INSERT INTO family_rdf_data VALUES (105, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 'rdfs:subClassOf', 5 '_:avfRestriction')); 1 row created. SQL> SQL> -- add data showing John eats Vegetable SQL> INSERT INTO family_rdf_data VALUES (51, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> commit; Commit complete. SQL> SQL> -- SQL> -- RDFS inference: re-create the entailment, but only with RDFS and some exclusions (see Sec. 2.2.6) SQL> -- SQL> EXECUTE SEM_APIS.DROP_ENTAILMENT ('rdfs_rix_family'); PL/SQL procedure successfully completed. SQL> SQL> BEGIN 2 SEM_APIS.CREATE_ENTAILMENT( 3 'rdfs_rix_family', 4 SEM_Models('family'), 5 SEM_Rulebases('RDFS') 6 ,0,'RDFS4A-, RDFS4B-, RDFS6-, RDFS8-,RDFS10-, RDFS13-','ENTAIL_ANYWAY=T, DISTANCE=T, USER_RULES=F'); 7 END; 8 / PL/SQL procedure successfully completed. SQL> SQL> -- OWLPrime inference SQL> BEGIN 2 SEM_APIS.CREATE_ENTAILMENT( 3 'owlprime_rix_family', 4 SEM_Models('family'), 5 SEM_Rulebases('owlprime')); 6 END; 7 / PL/SQL procedure successfully completed. SQL> SQL> -- Owl2rl inference SQL> BEGIN 2 SEM_APIS.CREATE_ENTAILMENT( 3 'owl2rl_rix_family', 4 SEM_Models('family'), 5 SEM_Rulebases('owl2rl')); 6 END; 7 / PL/SQL procedure successfully completed. SQL> SQL> -- query to see how :John has been classified SQL> SELECT y 2 FROM TABLE(SEM_MATCH( 3 '(:John rdf:type ?y)', 4 SEM_Models('family'), 5 SEM_RuleBases('rdfs'), 6 SEM_ALIASES(SEM_ALIAS('','http://www.example.org/family/')), 7 null)); Y -------------------------------------------------------------------------------- http://www.example.org/family/Male http://www.w3.org/2000/01/rdf-schema#Resource http://www.example.org/family/Person SQL> SQL> SELECT y 2 FROM TABLE(SEM_MATCH( 3 '(:John rdf:type ?y)', 4 SEM_Models('family'), 5 SEM_RuleBases('owlprime'), 6 SEM_ALIASES(SEM_ALIAS('','http://www.example.org/family/')), 7 null)); Y -------------------------------------------------------------------------------- http://www.example.org/family/Male http://www.example.org/family/Person SQL> SQL> SELECT y 2 FROM TABLE(SEM_MATCH( 3 '(:John rdf:type ?y)', 4 SEM_Models('family'), 5 SEM_RuleBases('owl2rl'), 6 SEM_ALIASES(SEM_ALIAS('','http://www.example.org/family/')), 7 null)); Y -------------------------------------------------------------------------------- http://www.example.org/family/Male http://www.example.org/family/Person SQL> SQL> -- explicitly identify :John as :Vegetarian and then add a food item that :John eats SQL> INSERT INTO family_rdf_data VALUES (51, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 'rdf:type', 5 '')); 1 row created. SQL> SQL> -- add data showing John eats some unknown category food SQL> INSERT INTO family_rdf_data VALUES (52, 2 SDO_RDF_TRIPLE_S('family', 3 '', 4 '', 5 '')); 1 row created. SQL> SQL> -- do entailments SQL> -- SQL> -- RDFS inference: re-create the entailment, but only with RDFS and some exclusions (see Sec. 2.2.6) SQL> -- SQL> BEGIN 2 SEM_APIS.CREATE_ENTAILMENT( 3 'rdfs_rix_family', 4 SEM_Models('family'), 5 SEM_Rulebases('RDFS') 6 ,0,'RDFS4A-, RDFS4B-, RDFS6-, RDFS8-,RDFS10-, RDFS13-','ENTAIL_ANYWAY=T, DISTANCE=T, USER_RULES=F'); 7 END; 8 / PL/SQL procedure successfully completed. SQL> SQL> -- OWLPrime inference SQL> BEGIN 2 SEM_APIS.CREATE_ENTAILMENT( 3 'owlprime_rix_family', 4 SEM_Models('family'), 5 SEM_Rulebases('owlprime') 6 ,options => 'ENTAIL_ANYWAY=T'); 7 END; 8 / PL/SQL procedure successfully completed. SQL> SQL> -- Owl2rl inference SQL> BEGIN 2 SEM_APIS.CREATE_ENTAILMENT( 3 'owl2rl_rix_family', 4 SEM_Models('family'), 5 SEM_Rulebases('owl2rl') 6 ,options => 'ENTAIL_ANYWAY=T'); 7 END; 8 / PL/SQL procedure successfully completed. SQL> SQL> -- query to see how :bean has been classified SQL> SELECT y 2 FROM TABLE(SEM_MATCH( 3 '(:bean rdf:type ?y)', 4 SEM_Models('family'), 5 SEM_RuleBases('rdfs'), 6 SEM_ALIASES(SEM_ALIAS('','http://www.example.org/food/item/')), 7 null)); no rows selected SQL> SQL> SELECT y 2 FROM TABLE(SEM_MATCH( 3 '(:bean rdf:type ?y)', 4 SEM_Models('family'), 5 SEM_RuleBases('owlprime'), 6 SEM_ALIASES(SEM_ALIAS('','http://www.example.org/food/item/')), 7 null)); Y -------------------------------------------------------------------------------- http://www.example.org/food/category/Vegetable SQL> SQL> SELECT y 2 FROM TABLE(SEM_MATCH( 3 '(:bean rdf:type ?y)', 4 SEM_Models('family'), 5 SEM_RuleBases('owl2rl'), 6 SEM_ALIASES(SEM_ALIAS('','http://www.example.org/food/item/')), 7 null)); Y -------------------------------------------------------------------------------- http://www.example.org/food/category/Vegetable SQL> SQL> -- SQL> -- cleanup the model (and entailment) and the application table SQL> -- SQL> SQL> exec sem_apis.drop_sem_model('family'); PL/SQL procedure successfully completed. SQL> drop table family_rdf_data; Table dropped. SQL> exec sem_apis.drop_rulebase('family_rb'); PL/SQL procedure successfully completed. SQL> spool off