1+ use oxrdf:: { LiteralRef , NamedNodeRef , TermRef , TripleRef } ;
12use petgraph:: graph:: { Graph , NodeIndex } ;
23use 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;
67use 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
1012fn 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
3544fn 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
4368fn 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