Skip to content

Commit 0bee390

Browse files
committed
Working version of JSON2RDF converter
1 parent cb627dd commit 0bee390

3 files changed

Lines changed: 113 additions & 20 deletions

File tree

Cargo.lock

Lines changed: 69 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
oxrdf = "0.1.7"
78
petgraph = "0.6.5"
8-
rio_api = "0.8.4"
9-
rio_turtle = "0.8.4"
9+
1010
serde = "1.0.203"
1111
serde_json = "1.0.117"

src/main.rs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
use oxrdf::{LiteralRef, NamedNodeRef, TermRef, TripleRef};
12
use petgraph::graph::{Graph, NodeIndex};
23
use petgraph::visit::EdgeRef;
3-
use rio_api::formatter::TriplesFormatter;
4-
use rio_api::model::{NamedNode, Triple};
5-
use rio_turtle::NTriplesFormatter;
4+
// use rio_api::formatter::TriplesFormatter;
5+
// use rio_api::model::{NamedNode, Triple};
6+
// use rio_turtle::NTriplesFormatter;
67
use serde_json::Value;
7-
use std::fs::File;
8-
use std::io::BufReader;
8+
use std::fmt::format;
9+
use std::fs::{read, File};
10+
use std::io::{BufReader, Write};
911

1012
fn json_to_graph(value: &Value, graph: &mut Graph<String, String>, parent: Option<NodeIndex>) {
1113
match value {
@@ -15,6 +17,10 @@ fn json_to_graph(value: &Value, graph: &mut Graph<String, String>, parent: Optio
1517
if let Some(parent_index) = parent {
1618
graph.add_edge(parent_index, node, "".to_string());
1719
}
20+
// let node = graph.add_node("".to_string());
21+
// if let Some(parent_index) = parent {
22+
// graph.add_edge(parent_index, node, key.to_string());
23+
// }
1824
json_to_graph(val, graph, Some(node));
1925
}
2026
}
@@ -27,6 +33,9 @@ fn json_to_graph(value: &Value, graph: &mut Graph<String, String>, parent: Optio
2733
let node = graph.add_node(value.to_string());
2834
if let Some(parent_index) = parent {
2935
graph.add_edge(parent_index, node, "".to_string());
36+
// let node = graph.add_node("".to_string());
37+
// if let Some(parent_index) = parent {
38+
// graph.add_edge(parent_index, node, value.to_string());
3039
}
3140
}
3241
}
@@ -35,9 +44,25 @@ fn json_to_graph(value: &Value, graph: &mut Graph<String, String>, parent: Optio
3544
fn graph_to_ttl(graph: &mut Graph<String, String>, file_path: &str) {
3645
let mut file = File::create(file_path);
3746

38-
let mut formatter = NTriplesFormatter::new(Vec::default());
47+
let mut triples_graph = oxrdf::Graph::default();
3948

40-
for edge in graph.edge_references() {}
49+
for edge in graph.edge_references() {
50+
let val = format!("http://www.decisym.ai/data#{}", &graph[edge.source()]);
51+
52+
let subject_triple = NamedNodeRef::new(val.as_str()).unwrap();
53+
54+
let predicate_triple = NamedNodeRef::new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
55+
56+
let object_triples = TermRef::Literal(LiteralRef::new_simple_literal(
57+
graph[edge.target()].as_str(),
58+
));
59+
60+
let rdf_triple = TripleRef::new(subject_triple, predicate_triple.unwrap(), object_triples);
61+
62+
triples_graph.insert(rdf_triple);
63+
}
64+
65+
writeln!(file.unwrap(), "{}", triples_graph.to_string()).unwrap();
4166
}
4267

4368
fn main() -> serde_json::Result<()> {
@@ -50,5 +75,15 @@ fn main() -> serde_json::Result<()> {
5075

5176
json_to_graph(&json_value, &mut graph, None);
5277

78+
// for edge in graph.edge_references() {
79+
// println!("{:?}", graph[edge.source()]);
80+
// println!("{:?}", graph[edge.target()]);
81+
82+
// println!("------------")
83+
// //println!("{:?}", edge.id());
84+
// }
85+
86+
graph_to_ttl(&mut graph, "/home/bharath/test.ttl");
87+
5388
Ok(())
5489
}

0 commit comments

Comments
 (0)