2013-10-23 2 views
1

RDF 온톨로지는 약 20MB입니다. 아래 코드와 같이 개인을 추가하려고 시도했습니다.기존 RDF 온톨로지에 개인을 더 추가

FileManager.get().addLocatorClassLoader(RDFWriter.class.getClassLoader()); 
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_RDFS_INF); 
model.read("Ontology/LocationOntology_New2.owl"); 
String preFix = "LocationOntology_New.owl#"; 
OntClass Region = model.getOntClass(preFix+"Region"); 
Individual sabara = model.createIndividual(preFix+"Sabaragamuwa",Region); 
try { 
    PrintStream p = new PrintStream("Ontology/LocationOntology_New2.owl"); 
    model.write(p,null); 
    p.close(); 
} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
} 

그러나이 코드는 모델을로드 된 파일에 다시 기록하는 데 많은 시간이 걸립니다. 처음부터 모든 것을 쓴 것처럼 보입니다 (기존 파일을 업데이트하지 않음). 아무도이 문제를 해결하는 방법을 알고 있습니까?

+0

이것은 당신이 메모리 리소스를 구축하기 위해 전체 파일을 읽을 필요가 시스템의 모든 종류의 경우가 될 것입니다. 데이터를 N-triples에 저장하는 경우, 한 줄에 세 번만 있으면됩니다. 새 정보로 _new_ 모델을 만든 다음 기존 파일에 _append_하면됩니다. 그것은 여전히 ​​잘 형성 될 것이고, 꽤 빠를 것이다. 귀하의 예제에서 기존 모델의 데이터를 사용하지 않으므로이 작업은 정상적으로 작동합니다. 당신은 새로운 트리플을 만들뿐입니다. –

답변

1

나는 이것을 풀 수 없다고 생각합니다. 그것은 예나가 당신이 어떤 변화를했는지 결정해야한다는 것을 의미합니다. 사실 새 인스턴스 만 추가 한 경우 파일에 추가하면 충분합니다. 그러나 수퍼 클래스를 일부 클래스에 추가하는 등의 변경을 한 다음이 클래스 정의를 업데이트해야 할 수도 있습니다.

+0

sparql 업데이트에 대해 어떻게 생각합니까?이 문서는 sparql을 사용하여 기존 rdf 문서를 업데이트 할 수있는 것으로 보입니다. http://www.w3.org/Submission/SPARQL-Update/ – Proceso

+0

SPARQL 업데이트를 사용하면 RDF 그래프는 문서와 동일하지 않습니다. Jena가 변경 사항을 반영하도록 문서를 업데이트하는 방법을 결정하는 방법을 구현 한 경우 Jena에 대해 충분히 알지 못합니다. – Rhand

+2

주어진 형식에 대해 일반적으로 가능한 여러 직렬화가 있기 때문에 텍스트 형식의 문서 형식에서는 본질적으로 불가능합니다. – RobV

1

나는 일반적으로, 이것은 당신이 (일반 RDF 반대) OWL에서 작업하는 경우 OWL 온톨로지는 RDF로 직렬화하는 경우, 당신은이 작업을 수행 할 수 있습니다 있는 할 곤란하다, RobV's point에 동의하는 동안 N-Triples에서 연재. 다음 코드 (주석 포함)는 어떻게 수행 할 수 있는지 보여줍니다.

새로운 콘텐츠를 추가하는 경우와 RDF를 한 줄에 하나씩 넣는 형식을 사용하는 경우 문제없이 콘텐츠에 새 트리플을 간단히 추가 할 수 있습니다. 내가 보여준 첫 번째 모델은 디스크상의 온톨로지 모델과 같습니다. 여기서는 온톨로지의 클래스 선언이 하나의 트리플을 사용한다는 것을 보여주기 위해 그것을 만들었습니다. Region a owl:Class. 하지만 지역은 IRI에 의해 식별됩니다. IRI를 알고있는 한 자원을 참조하기 위해 전체 온톨로지가 필요하지 않습니다. 새로운 모델에서는 Region 유형의 개체를 만들 수 있으며 해당 모델의 트리플을 디스크의 파일에 간단하게 추가 할 수 있습니다.

import com.hp.hpl.jena.ontology.Individual; 
import com.hp.hpl.jena.ontology.OntClass; 
import com.hp.hpl.jena.ontology.OntModel; 
import com.hp.hpl.jena.ontology.OntModelSpec; 
import com.hp.hpl.jena.rdf.model.ModelFactory; 

public class IncrementalOWLUpdates { 
    public static void main(String[] args) { 
     final String NS = "http://example.org/"; 

     // This is like the model on disk, and contains the class declaration 
     // that you wouldn't want to write out each time. 
     System.out.println("=== content of ontology on disk ==="); 
     final OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); 
     final OntClass Region = model.createClass(NS+"Region"); 
     model.write(System.out, "N-Triples"); 

     // This is the new model that you would build to contain the new triples 
     // that you want to add to the original model. Note that it _doesn't_ 
     // contain the class declaration, but only the new triples about the 
     // new individual. If you open the original ontology file and append this 
     // output, you've updated the ontology without reading it all into memory. 
     System.out.println("=== new content to append ==="); 
     final OntModel update = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); 
     final Individual newHampshire = update.createIndividual(NS+"NewHampshire", Region); 
     newHampshire.addLabel("New Hampshire", "en"); 
     update.write(System.out, "N-Triples"); 
    } 
} 
=== content of ontology on disk === 
<http://example.org/Region> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> . 
=== new content to append === 
<http://example.org/NewHampshire> <http://www.w3.org/2000/01/rdf-schema#label> "New Hampshire"@en . 
<http://example.org/NewHampshire> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/Region> . 
+0

이 특별한 문제에 대한 아주 좋은 해결책입니다. 필요한 경우 모델을 참조하여 원본 파일의 일부를 사용할 수도 있습니다. – Rhand

관련 문제