pike.ida.liu.se
Home>Projects>Semantic Web


code examples
This page collects cookbook-type examples of how to do OWL/RDF stuff in Pike.

Listing OWL classes
This program reads an OWL file and lists all OWL classes that occurred in the file.
  int main(int argc, array argv)
  {
    object owlset = Web.OWL();
    string input = Stdio.read_file(argv[1]);
    owlset->parse_xml(input);
    write("Classes:\n");

    foreach(owlset->find_statements(0,
                                    owlset->rdf_type,
                                    owlset->owl_Class);
            array statement)
       write("- %O\n", statement[0]);

    return 0;
  }      


Listing RDF N-triples
This program reads an RDF (or OWL) file, loops over all statements in the resulting dataset, and prints each statement in N-triple form.
  int main(int argc, array argv)
  {
    object dataset = Web.RDF();
    string input = Stdio.read_file(argv[1]);
    dataset->parse_xml(input);

    foreach(dataset->find_statements(0, 0, 0),
             [Resource subj, Resource pred, Resource obj])
      write(subj->get_n_triple_name() + " " +
            pred->get_n_triple_name() + " " +
            obj ->get_n_triple_name() + "\n");

    return 0;
  }