如何将变量从java传递到turtle文件?

发布于 2025-01-17 06:36:31 字数 1716 浏览 1 评论 0原文

我有一个具有以下结构的项目:

-config
  -repositories
    -test-repo.ttl

test-repo.ttl 文件如下所示:

@prefix lookup: <http://www.metaphacts.com/ontologies/platform/repository/lookup#> .
@prefix pathfinder: <http://www.metaphacts.com/ontologies/platform/service/pathfinder/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rep: <http://www.openrdf.org/config/repository#> .
@prefix sail: <http://www.openrdf.org/config/sail#> .
@prefix sr: <http://www.openrdf.org/config/repository/sail#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix mph: <http://www.metaphacts.com/ontologies/platform/repository#> .
@prefix ephedra: <http://www.metaphacts.com/ontologies/platform/ephedra#> .
@prefix fedsail: <http://www.openrdf.org/config/sail/federation#> .
@prefix sparqlr: <http://www.openrdf.org/config/repository/sparql#> .

[] a rep:Repository;
  rep:repositoryID "test-repo";
  rdfs:label "Repository to access data.";
  rep:repositoryImpl [
      rep:repositoryType "openrdf:SailRepository";
      sr:sailImpl [
          sail:sailType "metaphacts:RESTService";
          ephedra:serviceURL "https://request-to-access-data.com";
          ephedra:implementsService ephedra:test-ephedra;
          ephedra:httpMethod "GET";
          ephedra:httpHeader [
              ephedra:name "Authorization";
              ephedra:value "my_secret_token"
            ]
        ]
    ] .

每次运行项目时,字符串 my_secret_token 都会发生变化,因此我需要从 java 文件输入其值。我是java的绝对初学者。是否可以有一个 java 文件,在其中创建 my_secret_token 变量并为其分配值“123”,然后将该值传递给海龟文件中的 ephedra:value 字段?

如果是这样,你能帮助我实现这一目标吗?

I have a project with the structure:

-config
  -repositories
    -test-repo.ttl

The test-repo.ttl file looks like this:

@prefix lookup: <http://www.metaphacts.com/ontologies/platform/repository/lookup#> .
@prefix pathfinder: <http://www.metaphacts.com/ontologies/platform/service/pathfinder/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rep: <http://www.openrdf.org/config/repository#> .
@prefix sail: <http://www.openrdf.org/config/sail#> .
@prefix sr: <http://www.openrdf.org/config/repository/sail#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix mph: <http://www.metaphacts.com/ontologies/platform/repository#> .
@prefix ephedra: <http://www.metaphacts.com/ontologies/platform/ephedra#> .
@prefix fedsail: <http://www.openrdf.org/config/sail/federation#> .
@prefix sparqlr: <http://www.openrdf.org/config/repository/sparql#> .

[] a rep:Repository;
  rep:repositoryID "test-repo";
  rdfs:label "Repository to access data.";
  rep:repositoryImpl [
      rep:repositoryType "openrdf:SailRepository";
      sr:sailImpl [
          sail:sailType "metaphacts:RESTService";
          ephedra:serviceURL "https://request-to-access-data.com";
          ephedra:implementsService ephedra:test-ephedra;
          ephedra:httpMethod "GET";
          ephedra:httpHeader [
              ephedra:name "Authorization";
              ephedra:value "my_secret_token"
            ]
        ]
    ] .

The string my_secret_token changes every time the project is run, so I would need to imput its value from a java file. I am an absolute beginner in java. Is it possible to have a java file in which I create the my_secret_token variable and assign it the value of "123" for example, and then to pass this value to the ephedra:value field in the turtle file?

If so, can you help me to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月隐月明月朦胧 2025-01-24 06:36:31

我不确定我完全理解你想要做什么,但如果纯粹是为了更改 Turtle 文件中的文字字符串,我想一种简单的方法是将文件读入内存中的 RDf 模型,更改值,然后再次将模型写回文件。像这样的东西(未经测试,但希望它给出了总体思路):

// read the file into an RDF4J Model
Model model = Rio.parse("/path/to/test-repo.ttl", RDFFormat.TURTLE);

// create an IRI object for the 'ephedra:value' property
IRI ephedraValue = Values.iri("http://www.metaphacts.com/ontologies/platform/ephedra#value");

// find the statement in the RDF model we want to change the object of
model.getStatements(null, ephedraValue, null)
   .forEach(st -> {
        Value object = st.getObject();
        if (object.isLiteral()) { 
            // remove the old value
            model.remove(st);
            Literal newObject = Values.literal("my_NEW_secret_token");
            // add the new value
            model.add(st.getSubject, ephedraValue, newObject());
        }
);
// write modified model to file
Rio.write(model, new FileOutputStream("/path/to/file.ttle"), RDFFormat.TURTLE);

I am not sure I fully understand what you're trying to do, but if it's purely about changing that literal string in your Turtle file, I guess one simple way to do is to read the file into an in-memory RDf model, change the value, then write the model back to file again. Something like this (untested, but it gives the general idea hopefully):

// read the file into an RDF4J Model
Model model = Rio.parse("/path/to/test-repo.ttl", RDFFormat.TURTLE);

// create an IRI object for the 'ephedra:value' property
IRI ephedraValue = Values.iri("http://www.metaphacts.com/ontologies/platform/ephedra#value");

// find the statement in the RDF model we want to change the object of
model.getStatements(null, ephedraValue, null)
   .forEach(st -> {
        Value object = st.getObject();
        if (object.isLiteral()) { 
            // remove the old value
            model.remove(st);
            Literal newObject = Values.literal("my_NEW_secret_token");
            // add the new value
            model.add(st.getSubject, ephedraValue, newObject());
        }
);
// write modified model to file
Rio.write(model, new FileOutputStream("/path/to/file.ttle"), RDFFormat.TURTLE);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文