第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在 Android Studio 中加載 Turtle 文件時(shí)出錯(cuò)

在 Android Studio 中加載 Turtle 文件時(shí)出錯(cuò)

回首憶惘然 2021-11-17 14:50:39
我正在嘗試將 Turtle 文件加載到 Android Studio 中,并使用 Androjena 庫(kù)對(duì) Turtle 文件運(yùn)行查詢。我可以在 Eclipse 中使用 JavaFX 毫無(wú)問(wèn)題地執(zhí)行此操作。但是,在 Intellij IDE 中,我收到了一個(gè) FATAL 錯(cuò)誤,這顯然會(huì)導(dǎo)致我的應(yīng)用程序崩潰。我有一個(gè)名為 runQuery() 的方法,它被調(diào)用以在文件上運(yùn)行查詢:public String runQuery(){             String stringQuery = "PREFIX foaf: <http://xmlns.com/foaf/0.1/> \n" +            "PREFIX dbo: <http://dbpedia.org/ontology/> \n" +            "SELECT ?birthDate WHERE { \n" +            "?barack foaf:name \"Barack Obama\"@en .\n" +            "?barack dbo:birthDate ?birthDate \n" +            "}";             String answer = "";             Model model = FileManager.get().loadModel("sample_pres.ttl", "TTL");             Query query = QueryFactory.create(stringQuery);             try {                  QueryExecution qexec = QueryExecutionFactory.create(query, model);                  ResultSet results = qexec.execSelect();                  while(results.hasNext()) {                        QuerySolution soln = results.nextSolution();                        Literal answerLiteral = soln.getLiteral("birthDate");                        answer = answerLiteral.toString();        }    }            catch(Exception ignore) {    }    this.answer = answer;    return answer;}給我?guī)?lái)問(wèn)題的代碼行是 FileManager.get().loadModel() 行。這是我得到的異常:com.hp.hpl.jena.shared.NotFoundException: Not found: sample_pres.ttl所以我收集到 Android 沒(méi)有找到該文件,盡管該文件在我的 Assets 文件夾中。我假設(shè)我不/不能使用 AssetManager,因?yàn)槲覜](méi)有嘗試包含 FileInputStream。所以我在這一點(diǎn)上被困住了。這是我的項(xiàng)目結(jié)構(gòu)的圖片:我在我的項(xiàng)目結(jié)構(gòu)中添加了 app/src/main 下的資產(chǎn)文件夾。我對(duì) Android Studio 比較陌生,我知道在 Eclipse 的 JavaFX 中,我可以簡(jiǎn)單地使用文件的絕對(duì)路徑來(lái)訪問(wèn)它,我知道這顯然在 Android Studio 中不起作用。但是,我找不到從 Android 項(xiàng)目(我的資產(chǎn)文件夾)中的本地源加載 Turtle 文件并執(zhí)行查詢的示例。此站點(diǎn)上的每個(gè)示例或問(wèn)題似乎都與通過(guò) Internet 連接從外部端點(diǎn)運(yùn)行查詢有關(guān)。所以這就是我感到困惑的部分原因。我不確定如何從 Android Studio 中的本地源運(yùn)行查詢并從我的資產(chǎn)文件夾中引用 Turtle 文件以避免 com.hp.hpl.jena.shared.NotFoundException
查看完整描述

2 回答

?
Smart貓小萌

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊

您可以通過(guò)僅獲取InputStream提供者AssetManager.open()并將其傳遞給較新的RDFParserAPI來(lái)簡(jiǎn)化您的代碼,例如


InputStream inputStream = AssetManager.open("sample_3.ttl");

Model model = ModelFactory.createDefaultModel();

RDFParser.create().source(inputStream).lang(Lang.TTL).parse(model);

這避免了不必要的讀入和寫回文件。


然而,它確實(shí)需要使用該jena-arq庫(kù)以及使用最新版本的 Jena(您似乎正在使用 Jena 2 的某些變體,您將需要 Jena 3.7 或更高版本才能使上述內(nèi)容正常工作)


查看完整回答
反對(duì) 回復(fù) 2021-11-17
?
慕容3067478

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個(gè)贊

我找到了答案。問(wèn)題是 android studio 中的資產(chǎn)文件無(wú)法在 android studio 中讀取。它們必須轉(zhuǎn)換為 FileOutputStream,即使在使用 .ttl 文件并在烏龜中讀取它們時(shí)也是如此。這是代碼的示例:


 String filePath = context.getFilesDir() + File.separator + "my_turtle.ttl";

    File destinationFile = new File(filePath);

    FileOutputStream outputStream = new FileOutputStream(destinationFile);

    AssetManager assetManager = context.getAssets();

    InputStream inputStream = assetManager.open("sample_3.ttl");

    byte[] buffer = new byte[1024];

    int length = 0;

    while((length = inputStream.read(buffer)) != -1){

        outputStream.write(buffer,0,length);

    }

    inputStream.close();

    outputStream.close();




    Model model = null;


    model = FileManager.get().loadModel(filePath,"TTL");


    Query query = QueryFactory.create(stringQuery);


    QueryExecution qexec = QueryExecutionFactory.create(query, model);

    ResultSet results = qexec.execSelect();

    while(results.hasNext()) {

            QuerySolution soln = results.nextSolution();

            Literal answerLiteral = soln.getLiteral("abstract");

            answer = answerLiteral.toString();

            System.out.println(answer);



        }




    if(!answer.equals("")){

        this.answer = answer;

        return answer;

    }

    else{

        return "I could not find an answer";

    }



查看完整回答
反對(duì) 回復(fù) 2021-11-17
  • 2 回答
  • 0 關(guān)注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)