You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过org/topbraid/shacl/1.3.2执行SHACL JS扩展遇Nashorn缺失求助

解决SHACL JS扩展在JDK15+环境下的Nashorn缺失问题

你不需要强制降级到JDK 8,有两种可行的解决路径:

方案1:引入独立Nashorn引擎

JDK15移除自带Nashorn后,Oracle提供了独立维护的Nashorn核心库,可通过依赖管理工具引入,让TopBraid SHACL自动调用该引擎执行JS约束。

以Maven为例,添加以下依赖到pom.xml:

<dependency>
    <groupId>org.openjdk.nashorn</groupId>
    <artifactId>nashorn-core</artifactId>
    <version>15.4</version>
</dependency>

方案2:升级至适配新JS引擎的SHACL版本

TopBraid SHACL的1.4.x及后续版本已适配GraalVM JavaScript引擎(JDK官方推荐替代Nashorn的方案),直接升级到对应版本后,可在JDK19环境下原生支持SHACL JS扩展,无需额外引入依赖。


可运行示例代码

Java 验证程序

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.topbraid.shacl.validation.ValidationUtil;

import java.io.FileInputStream;
import java.io.InputStream;

public class SHACLJSValidationDemo {
    public static void main(String[] args) throws Exception {
        // 加载包含JS约束的形状图
        Model shapesModel = ModelFactory.createDefaultModel();
        try (InputStream shapesStream = new FileInputStream("shapes.ttl")) {
            shapesModel.read(shapesStream, null, "TTL");
        }

        // 加载待验证的数据图
        Model dataModel = ModelFactory.createDefaultModel();
        try (InputStream dataStream = new FileInputStream("data.ttl")) {
            dataModel.read(dataStream, null, "TTL");
        }

        // 启用JS扩展并执行验证
        Model validationReport = ValidationUtil.validateModel(dataModel, shapesModel, true);

        // 输出验证结果
        validationReport.write(System.out, "TTL");
    }
}

形状图文件 shapes.ttl

@prefix ex: <http://example.org/> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

ex:germanLabelShape
    a sh:NodeShape ;
    sh:targetClass rdfs:Resource ;
    sh:jsConstraint """
        function(node, context) {
            var label = node.get('http://www.w3.org/2000/01/rdf-schema#label');
            if(label) {
                var lang = label.language;
                return lang === 'de' || lang === 'de-DE';
            }
            return false;
        }
    """ ;
    sh:message "所有资源必须包含德语标签" .

数据图文件 data.ttl

@prefix ex: <http://example.org/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

ex:ValidResource
    rdfs:label "Willkommen"@de ;
    a rdfs:Resource .

ex:InvalidResource
    rdfs:label "Welcome"@en ;
    a rdfs:Resource .

内容的提问来源于stack exchange,提问作者Livio Robaldo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 01:20:08