0

StoryModel でいくつかのメソッドを作成しました。コードに誤りがある場合は、訂正していただければ幸いです。

public class StoryModel {

    String dbName = "DB3";
    String sns =  "http://somebook/SemBook#";
    String ns;
    Dataset ds;
    OntModel om;

    // create model and connect to database
    public StoryModel(String storyName){

        ns = sns + storyName;
        ds = TDBFactory.createDataset(dbName);
        om = ModelFactory.createOntologyModel();

    }

    // create classes in model
    public void initModel() {

        om.createClass(ns + "Person");
        om.createClass(ns + "Event");
        om.createClass(ns + "Place");
        om.createClass(ns + "Time");
        om.createClass(ns + "Object");
        saveModel();
    }   

    //Read & write in database
    //Display it
    public void saveModel() {

        ds.begin(ReadWrite.WRITE);
        om.write(System.out, "RDF/XML-ABBREV");

    }

    // Create resource to class in model
    public Resource createResource(String resourceName, String clsName) {

        OntResource resource = om.createOntResource(ns + resourceName);
        String ruri = ns + resource;
        OntClass clsuri = om.getOntClass(ns + clsName);

        Individual i = om.createIndividual(ruri, clsuri);

        return i;

    }

    /*
     * Return an RDF resource object given the URI of the resource as a string. The
     * URI can be represented in full, or as a "prefix:localName".
     * @param resourceName The full URI of the resource
     * @return An RDF resource object
     */

    public Resource stringToResource( String resourceName ) {

        String resourceURI = om.expandPrefix( resourceName );

        return om.getResource( resourceURI );

    }

    // Delete the resource
    public void deleteResource(String resourceName) {

        try {
            OntResource resource = om.getOntResource(ns + resourceName);

            if (resource != null) 
            {
               resource.remove();
            }
            else {
                System.out.println("Resource not present");
            }   
        }
        catch (Exception e) { }         
    }
}

SemBookMain では、モデルを作成し、StoryModel で作成されたメソッドを使用してリソースにクラスを初期化し、それを表示します。

public class SemBookMain {

    public static void main(String[] args) {

        // create and initialize a model
        StoryModel sm = new StoryModel("alice");
        //sm.saveModel();
        sm.initModel();

        // add resources
        String clsName = "Person";
        String[] ar = {"Alice", "Peter", "Ben", "Robin"};

        for (String r : ar) {

            Resource res = sm.createResource(r, clsName);

        }
        sm.saveModel();

    }

}

以下のコメントとして出力を追加したことを本当に申し訳ありませんので、私のコメントをご覧ください。

ERROR 1 & 2 とリソースが表示されない理由がわかりません。欠けているものがありますが、それを理解できませんでした。

4

1 に答える 1

2

ds.begin(ReadWrite.WRITE);でもいいえds.commit

initModelコールsaveModelしてから、後でsaveModelもう一度コールします。

次のコード パターンに従ってください。

http://jena.apache.org/documentation/tdb/tdb_transactions.html#write-transactions

于 2013-03-15T13:35:38.877 に答える