Skip to content

Commit fa99485

Browse files
committed
useBlankNode as option
1 parent df04b0e commit fa99485

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

app.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ def dlftLidoFile():
2121
return Path('./defaultLido.xml')
2222

2323

24-
def convert_lido_str(lido_str, x3ml_str, format='turtle'):
24+
def convert_lido_str(lido_str, x3ml_str,**kw):
2525
'''Converts LIDO XML string to RDF using the provided X3ML mapping string.
2626
Returns the RDF string in the specified format.'''
2727
if lido_str:
28-
converter = LRC.LidoRDFConverter.from_str(x3ml_str)
28+
format = kw.get('format','turtle')
29+
converter = LRC.LidoRDFConverter.from_str(x3ml_str, **kw)
2930
graph = converter.parse_string(lido_str)
3031
return graph.serialize(format=format)
3132
return ''
@@ -95,8 +96,9 @@ def run_mappings():
9596
if js := parm.get('x3ml'):
9697
model = X3ml.fromJSON(js)
9798
format = parm.get('format','turtle')
99+
useBlankNode = parm.get('useBlankNode',True)
98100
response_object = {'status': 'success', 'message': 'Mappings applied to Lido!', 'format':format}
99-
response_object['text'] = convert_lido_str(lido_data, model.to_str(),format=format)
101+
response_object['text'] = convert_lido_str(lido_data, model.to_str(),format=format, useBlankNode = useBlankNode)
100102
return jsonify(response_object)
101103
return jsonify({'status': 'failed', 'message': 'No Lido data provided!'})
102104

@@ -120,7 +122,7 @@ def convert():
120122
lido_data = request.get_data()
121123
format = 'turtle'
122124
try:
123-
rdf_str = convert_lido_str(lido_data, mapping_data, format=format)
125+
rdf_str = convert_lido_str(lido_data, mapping_data, format=format, useBlankNode = False)
124126
response = make_response(rdf_str, 200)
125127
response.mime_type = f"text/{format}"
126128
except Exception as e:

libs/LidoRDFConverter.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,12 @@ def make_id_node(id_str: str, **kw) -> RF.URIRef:
119119
'''Creates an RDF node (BNode or URIRef) from id string and a mode'''
120120
'''Gets the ID mode and graph from kw arguments'''
121121
mode = kw.get('mode', x3ml.IDMode.LIDO_ID)
122+
useBlankNode = kw.get('useBlankNode',False)
122123
if mode == x3ml.IDMode.PATH:
123-
return RF.BNode(hash(id_str))
124+
if useBlankNode:
125+
return RF.BNode(hash(id_str))
126+
else:
127+
return NAMESPACE_MAP['n4o'][f"{hash(id_str)}"]
124128
uri = f'n4o:{hash(id_str)}'
125129
try:
126130
nsm = kw.get('graph').namespace_manager
@@ -194,14 +198,15 @@ def get_ns(elem):
194198
class LidoRDFConverter():
195199
'''Converts LIDO XML files to RDF graphs using X3ML mappings'''
196200

197-
def __init__(self, file_path):
201+
def __init__(self, file_path, useBlankNode = False):
198202
self.mappings = x3ml.Mappings.from_file(file_path)
203+
self.useBlankNode = useBlankNode
199204

200205
Graph = RF.Graph
201206

202207
@classmethod
203-
def from_str(cls, mapping_str):
204-
obj = cls('')
208+
def from_str(cls, mapping_str,**kw):
209+
obj = cls('',kw.get('useBlankNode',False))
205210
obj.mappings = x3ml.Mappings.from_str(mapping_str)
206211
return obj
207212

@@ -278,4 +283,4 @@ def _process_lido_element(self, elem, graph) -> None:
278283
for data in [m.evaluate(elem) for m in self.mappings]:
279284
for i, mapping_data in enumerate(data):
280285
if mapping_data.valid:
281-
add_triples(graph, mapping_data, recID, index=i)
286+
add_triples(graph, mapping_data, recID, index=i, useBlankNode = self.useBlankNode)

templates/index.html

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,11 @@ <h5 class="modal-title" id="uploadLidoModalLabel">Upload LIDO/XML file</h5>
125125
<div class="row">
126126
<div class="col-5">
127127
<div> Mappings (x3ml):<br>
128-
<button type="button" class="btn btn-primary btn-sm " @click="addMapping" title="Add a new mapping rule to the list.">Add mapping</button>
129-
<button type="button" class="btn btn-primary btn-sm" @click="clearMappings" title="Remove all mapping rules.">Clear all</button>
130-
<button type="button" class="btn btn-primary btn-sm" @click="resetMappings" title="Load the default mappings.">Load default</button>
131-
<button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#uploadX3mlModal" title="Upload a customer mapping.">Upload</button>
132-
<button type="button" class="btn btn-primary btn-sm" @click="downloadMappings" title="Download the current mapping.">Download</button>
128+
<button type="button" class="btn btn-primary btn-sm " @click="addMapping" title="Add new mapping.">Add mapping</button>
129+
<button type="button" class="btn btn-primary btn-sm" @click="clearMappings" title="Remove all mappings.">Clear all</button>
130+
<button type="button" class="btn btn-primary btn-sm" @click="resetMappings" title="Load default mappings.">Load default</button>
131+
<button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#uploadX3mlModal" title="Upload mappings.">Upload</button>
132+
<button type="button" class="btn btn-primary btn-sm" @click="downloadMappings" title="Download mappings.">Download</button>
133133
</div>
134134
<div id="acc" class="accordion accordion-flush" style="overflow-y: scroll; height:1000px; width: 100%;">
135135
<div class="accordion-item" v-for="(mapping, i) in x3ml.mappings" :key="i">
@@ -201,6 +201,11 @@ <h2 class="accordion-header" :id="'L' + j">
201201
</option>
202202
</select>
203203
</div>
204+
<div class="form-check">
205+
<input class="form-check-input" type="checkbox" v-model="useBlankNode" id="flexCheckDefault">
206+
<label class="form-check-label" for="flexCheckDefault">Use BNodes</label>
207+
</div>
208+
204209
</div>
205210
<p>
206211
<div id="rdfEditor"></div>
@@ -274,7 +279,8 @@ <h2 class="accordion-header" :id="'L' + j">
274279
mode: '',
275280
modeFormatKeys: new Map(),
276281
tmp: ["abc", "abcd", "xxxx"],
277-
formatKey: 'n3'
282+
formatKey: 'n3',
283+
useBlankNode: false
278284
}
279285
},
280286
delimiters: ["${", "}$"], // for global
@@ -284,7 +290,7 @@ <h2 class="accordion-header" :id="'L' + j">
284290
let attrs = event.target.attributes;
285291
let i = parseInt(attrs.id.value);
286292
let j = parseInt(attrs.pid.value);
287-
let link = this.x3ml.mappings[j].links[i];
293+
let link = this.x3ml.mappings[j].links[i];
288294
link.range.sourceNode.text = event.target.value
289295
},
290296
saveX3mlUrl(event) {
@@ -500,7 +506,7 @@ <h2 class="accordion-header" :id="'L' + j">
500506
// Triggers the transformation lido + x3ml => ttl
501507
// and loads the result into the RDF editor.
502508
format = app.formatKey
503-
data = { type: '/run_mappings', data: lidoEditor.getValue(), x3ml: app.x3ml, format: format }
509+
data = { type: '/run_mappings', data: lidoEditor.getValue(), x3ml: app.x3ml, format: format, useBlankNode: app.useBlankNode }
504510
post('run_mappings', data)
505511
.then(res => res.json())
506512
.then(s => {

0 commit comments

Comments
 (0)