RML Mapper用于json中的数组数组

时间:2018-06-14 13:12:43

标签: rdf r2rml

我正在尝试将此JSON文件映射到RDF,但我可能无法正确迭代以获取“值”的值,这些值位于度量数组中。

JSON:

{
    "status": 0,
    "body": {
        "updatetime": 1528904042,
        "timezone": "Europe\/Rome",
        "measuregrps": [{
                "grpid": 1154218424,
                "attrib": 2,
                "date": 1528902698,
                "category": 1,
                "brand": 1,
                "modified": 1528902700,
                "deviceid": null,
                "measures": [{
                    "value": 7000,
                    "type": 11,
                    "unit": -2,
                    "algo": 0,
                    "fw": 0,
                    "fm": 131
                }]
            },
            {
                "grpid": 1154218987,
                "attrib": 2,
                "date": 1528902745,
                "category": 1,
                "brand": 1,
                "modified": 1528902747,
                "deviceid": null,
                "measures": [{
                    "value": 7200,
                    "type": 11,
                    "unit": -2,
                    "algo": 0,
                    "fw": 0,
                    "fm": 131
                }]
            }
        ]
    }
}

RML:

@prefix rr: <http://www.w3.org/ns/r2rml#>.
@prefix rml: <http://semweb.mmlab.be/ns/rml#> .
@prefix ql: <http://semweb.mmlab.be/ns/ql#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
@prefix fo: <http://purl.org/ifo/#>.

###### Fitbit MAPPING #######


<#FitbitRestingHeartRate>

rml:logicalSource [
    rml:source "provaJson.json";
    rml:referenceFormulation ql:JSONPath;
    rml:iterator "$.body.measuregrps";
];

rr:subjectMap [
    rr:template "http://ifo.com/{grpid}";
    rr:class fo:HeartRate;
];

rr:predicateObjectMap [
    rr:predicate fo:hasTemporalRelationshipToPhysicalActivity;
    rr:objectMap [
        rr:constant fo:AtRest;
    ];
];

rr:predicateObjectMap [
    rr:predicate fo:hasMeasure;
    rr:objectMap [ 
        rr:parentTriplesMap <#MeasureHeartRate>;
    ];
].



<#MeasureHeartRate>

rml:logicalSource [
    rml:source "provaJson.json";
    rml:referenceFormulation ql:JSONPath;
    rml:iterator "$.body.measuregrps";
];

rr:subjectMap [
    rr:template "http://ifo.com/{grpid}"; 
    rr:class fo:Measure;
    rml:iterator "$.body.measuregrps";

];

rr:predicateObjectMap [
    rr:predicate fo:hasNumericalValue;
    rr:objectMap [
        rml:reference "@.measures.value";
        rr:datatype xsd:float;
    ];
];

rr:predicateObjectMap [
    rr:predicate fo:hasDescriptiveStatistic;
    rr:objectMap [
        rr:constant fo:average;
    ];
];

rr:predicateObjectMap [
    rr:predicate fo:hasUnit;
    rr:objectMap [
        rr:constant fo:bpm;
    ];
].

感谢您的帮助, Chiara的

1 个答案:

答案 0 :(得分:2)

  1. 适应JSON Path表达式使RML处理器能够检索心脏 费率值。 对于两个TriplesMap,我将它们更改为$.body.measuregrps.[*] 在每个measuregrps条目上。

  2. 我为每个TriplesMap添加了a rr:TriplesMap,以确保RML 处理器知道RDF描述了TriplesMap:

<#MeasureHeartRate>
    a rr:TriplesMap;
  1. 所有心率值均与每次测量匹配。 我添加了rr:joinCondition,它仅将心率值与 grpid值相等时进行正确的测量:
rr:predicateObjectMap [
    rr:predicate fo:hasMeasure;
    rr:objectMap [ 
        rr:parentTriplesMap <#MeasureHeartRate>;
        rr:joinCondition [
            rr:child "grpid";
            rr:parent "grpid";
        ];
    ];
].

  1. 我更改了心率测量图的IRI,因为IRI为 不唯一导致循环:
<http://ifo.com/1154218987> <http://purl.org/ifo/#hasMeasure> <http://ifo.com/1154218987>

新的IRI格式还支持心脏以外的其他类型的测量 率。

映射规则

@base <http://example.org> .
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix rml: <http://semweb.mmlab.be/ns/rml#> .
@prefix ql: <http://semweb.mmlab.be/ns/ql#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix fo: <http://purl.org/ifo/#> .

<#FitbitRestingHeartRate>
    a rr:TriplesMap;
    rml:logicalSource [
        rml:source "provaJson.json";
        rml:referenceFormulation ql:JSONPath;
        rml:iterator "$.body.measuregrps.[*]";
    ];

    rr:subjectMap [
        rr:template "http://ifo.com/{grpid}";
        rr:class fo:HeartRate;
    ];

    rr:predicateObjectMap [
        rr:predicate fo:hasTemporalRelationshipToPhysicalActivity;
        rr:objectMap [
            rr:constant fo:AtRest;
        ];
    ];

    rr:predicateObjectMap [
        rr:predicate fo:hasMeasure;
        rr:objectMap [ 
            rr:parentTriplesMap <#MeasureHeartRate>;
            rr:joinCondition [
                rr:child "grpid";
                rr:parent "grpid";
            ];
        ];
    ].

<#MeasureHeartRate>
    a rr:TriplesMap;
    rml:logicalSource [
        rml:source "provaJson.json";
        rml:referenceFormulation ql:JSONPath;
        rml:iterator "$.body.measuregrps.[*]";
    ];

    rr:subjectMap [
        rr:template "http://ifo.com/{grpid}/{date}/{measures.[*].type}"; 
        rr:class fo:Measure;
    ];

    rr:predicateObjectMap [
        rr:predicate fo:hasNumericalValue;
        rr:objectMap [
            rml:reference "measures.[*].value";
            rr:datatype xsd:float;
        ];
    ];

    rr:predicateObjectMap [
        rr:predicate fo:hasDescriptiveStatistic;
        rr:objectMap [
            rr:constant fo:average;
        ];
    ];

    rr:predicateObjectMap [
        rr:predicate fo:hasUnit;
        rr:objectMap [
            rr:constant fo:bpm;
        ];
    ].

输出

<http://ifo.com/1154218424> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/ifo/#HeartRate>.
<http://ifo.com/1154218424> <http://purl.org/ifo/#hasTemporalRelationshipToPhysicalActivity> <http://purl.org/ifo/#AtRest>.
<http://ifo.com/1154218424> <http://purl.org/ifo/#hasMeasure> <http://ifo.com/1154218424/1528902698/11>.
<http://ifo.com/1154218987> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/ifo/#HeartRate>.
<http://ifo.com/1154218987> <http://purl.org/ifo/#hasTemporalRelationshipToPhysicalActivity> <http://purl.org/ifo/#AtRest>.
<http://ifo.com/1154218987> <http://purl.org/ifo/#hasMeasure> <http://ifo.com/1154218987/1528902745/11>.
<http://ifo.com/1154218424/1528902698/11> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/ifo/#Measure>.
<http://ifo.com/1154218424/1528902698/11> <http://purl.org/ifo/#hasNumericalValue> "7000"^^<http://www.w3.org/2001/XMLSchema#float>.
<http://ifo.com/1154218424/1528902698/11> <http://purl.org/ifo/#hasDescriptiveStatistic> <http://purl.org/ifo/#average>.
<http://ifo.com/1154218424/1528902698/11> <http://purl.org/ifo/#hasUnit> <http://purl.org/ifo/#bpm>.
<http://ifo.com/1154218987/1528902745/11> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/ifo/#Measure>.
<http://ifo.com/1154218987/1528902745/11> <http://purl.org/ifo/#hasNumericalValue> "7200"^^<http://www.w3.org/2001/XMLSchema#float>.
<http://ifo.com/1154218987/1528902745/11> <http://purl.org/ifo/#hasDescriptiveStatistic> <http://purl.org/ifo/#average>.
<http://ifo.com/1154218987/1528902745/11> <http://purl.org/ifo/#hasUnit> <http://purl.org/ifo/#bpm>.

注意:我为RML及其技术做出了贡献。