skolemization of blank nodes and BASE in SPARQl query and update

master
Niko PLP 4 months ago
parent d03d15f9b7
commit 077ba6265e
  1. 51
      nextgraph/src/local_broker.rs
  2. 9
      ng-app/src-tauri/src/lib.rs
  3. 2
      ng-app/src/apps/SparqlUpdateEditor.svelte
  4. 2
      ng-app/src/lib/FullLayout.svelte
  5. 3
      ng-app/src/lib/Home.svelte
  6. 6
      ng-app/src/routes/NURI.svelte
  7. 2
      ng-app/src/routes/Shared.svelte
  8. 2
      ng-app/src/routes/Site.svelte
  9. 6
      ng-app/src/store.ts
  10. 2
      ng-app/src/tab.ts
  11. 30
      ng-app/src/wallet_emojis.ts
  12. 26
      ng-net/src/app_protocol.rs
  13. 3
      ng-oxigraph/src/oxrdf/parser.rs
  14. 2
      ng-sdk-js/README.md
  15. 42
      ng-sdk-js/app-node/index.js
  16. 40
      ng-sdk-js/src/lib.rs
  17. 52
      ng-verifier/src/commits/transaction.rs
  18. 31
      ng-verifier/src/request_processor.rs
  19. 13
      ng-verifier/src/verifier.rs

@ -1085,15 +1085,15 @@ impl LocalBroker {
} }
fn add_session(&mut self, session: Session) -> Result<SessionInfo, NgError> { fn add_session(&mut self, session: Session) -> Result<SessionInfo, NgError> {
let private_store_id = self let private_store_id = NuriV0::to_store_nuri_string(
.get_site_store_of_session(&session, SiteStoreType::Private)? &self.get_site_store_of_session(&session, SiteStoreType::Private)?,
.to_string(); );
let protected_store_id = self let protected_store_id = NuriV0::to_store_nuri_string(
.get_site_store_of_session(&session, SiteStoreType::Protected)? &self.get_site_store_of_session(&session, SiteStoreType::Protected)?,
.to_string(); );
let public_store_id = self let public_store_id = NuriV0::to_store_nuri_string(
.get_site_store_of_session(&session, SiteStoreType::Public)? &self.get_site_store_of_session(&session, SiteStoreType::Public)?,
.to_string(); );
let user_id = session.config.user_id(); let user_id = session.config.user_id();
@ -2216,9 +2216,9 @@ pub async fn session_start(config: SessionConfig) -> Result<SessionInfo, NgError
} }
if let Ok(AppResponse::V0(AppResponseV0::SessionStart(AppSessionStartResponse::V0(response)))) = res { if let Ok(AppResponse::V0(AppResponseV0::SessionStart(AppSessionStartResponse::V0(response)))) = res {
session_info.private_store_id = response.private_store.to_string(); session_info.private_store_id = NuriV0::to_store_nuri_string(&response.private_store);
session_info.protected_store_id = response.protected_store.to_string(); session_info.protected_store_id = NuriV0::to_store_nuri_string(&response.protected_store);
session_info.public_store_id = response.public_store.to_string(); session_info.public_store_id = NuriV0::to_store_nuri_string(&response.public_store);
} }
Ok(session_info) Ok(session_info)
@ -2245,15 +2245,24 @@ pub async fn session_start(config: SessionConfig) -> Result<SessionInfo, NgError
return Ok(SessionInfo { return Ok(SessionInfo {
session_id: *idx, session_id: *idx,
user: user_id, user: user_id,
private_store_id: broker private_store_id: NuriV0::to_store_nuri_string(
.get_site_store_of_session(sess, SiteStoreType::Private)? &broker.get_site_store_of_session(
.to_string(), sess,
protected_store_id: broker SiteStoreType::Private,
.get_site_store_of_session(sess, SiteStoreType::Protected)? )?,
.to_string(), ),
public_store_id: broker protected_store_id: NuriV0::to_store_nuri_string(
.get_site_store_of_session(sess, SiteStoreType::Public)? &broker.get_site_store_of_session(
.to_string(), sess,
SiteStoreType::Protected,
)?,
),
public_store_id: NuriV0::to_store_nuri_string(
&broker.get_site_store_of_session(
sess,
SiteStoreType::Public,
)?,
),
}); });
} }
} }

@ -537,12 +537,12 @@ async fn branch_history(session_id: u64, nuri: String) -> Result<AppHistoryJs, S
#[tauri::command(rename_all = "snake_case")] #[tauri::command(rename_all = "snake_case")]
async fn sparql_update(session_id: u64, sparql: String, nuri: String) -> Result<(), String> { async fn sparql_update(session_id: u64, sparql: String, nuri: String) -> Result<(), String> {
let nuri = NuriV0::new_from(&nuri).map_err(|e| e.to_string())?; let nuriv0 = NuriV0::new_from(&nuri).map_err(|e| e.to_string())?;
let request = AppRequest::V0(AppRequestV0 { let request = AppRequest::V0(AppRequestV0 {
command: AppRequestCommandV0::new_write_query(), command: AppRequestCommandV0::new_write_query(),
nuri, nuri: nuriv0,
payload: Some(AppRequestPayload::new_sparql_query(sparql)), payload: Some(AppRequestPayload::new_sparql_query(sparql, Some(nuri))),
session_id, session_id,
}); });
@ -560,6 +560,7 @@ async fn sparql_update(session_id: u64, sparql: String, nuri: String) -> Result<
async fn sparql_query( async fn sparql_query(
session_id: u64, session_id: u64,
sparql: String, sparql: String,
base: Option<String>,
nuri: Option<String>, nuri: Option<String>,
) -> Result<Value, String> { ) -> Result<Value, String> {
let nuri = if nuri.is_some() { let nuri = if nuri.is_some() {
@ -571,7 +572,7 @@ async fn sparql_query(
let request = AppRequest::V0(AppRequestV0 { let request = AppRequest::V0(AppRequestV0 {
command: AppRequestCommandV0::new_read_query(), command: AppRequestCommandV0::new_read_query(),
nuri, nuri,
payload: Some(AppRequestPayload::new_sparql_query(sparql)), payload: Some(AppRequestPayload::new_sparql_query(sparql, base)),
session_id, session_id,
}); });

@ -33,7 +33,7 @@
onMount(()=>{ onMount(()=>{
reset_in_memory(); reset_in_memory();
if (!$in_memory_discrete){ if (!$in_memory_discrete){
$in_memory_discrete = "INSERT DATA { \n <did:ng:test> <test:predicate> \"An example value\".\r}"; $in_memory_discrete = "INSERT DATA { \n <> <example:predicate> \"An example value\".\r}";
} }
}); });
const run = async () => { const run = async () => {

@ -558,7 +558,7 @@
await reset_toasts(); await reset_toasts();
let store_repo = $cur_tab.store.repo; let store_repo = $cur_tab.store.repo;
if (!store_repo) { if (!store_repo) {
store_repo = $all_tabs["o:"+$active_session.private_store_id].store.repo store_repo = $all_tabs[$active_session.private_store_id].store.repo
} }
let nuri = await ng.doc_create($active_session.session_id, get_class(class_name)["ng:crdt"], class_name, store_repo, destination); let nuri = await ng.doc_create($active_session.session_id, get_class(class_name)["ng:crdt"], class_name, store_repo, destination);
push("#/"+nuri); push("#/"+nuri);

@ -51,7 +51,7 @@
reset_in_memory(); reset_in_memory();
}); });
let nuri = $active_session && ("o:"+$active_session.private_store_id); let nuri = $active_session && $active_session.private_store_id;
</script> </script>
<FullLayout withoutNavBar={true}> <FullLayout withoutNavBar={true}>
@ -119,6 +119,7 @@
<Square3Stack3d tabindex="-1" class="mt-1 flex-none w-7 h-7 mr-1 focus:outline-none "/><div class="text-xs xs:text-sm flex items-center"><div style="overflow-wrap: anywhere;" class="max-h-8 xs:max-h-10">{$t("doc.header.buttons.all_docs")}</div></div> <Square3Stack3d tabindex="-1" class="mt-1 flex-none w-7 h-7 mr-1 focus:outline-none "/><div class="text-xs xs:text-sm flex items-center"><div style="overflow-wrap: anywhere;" class="max-h-8 xs:max-h-10">{$t("doc.header.buttons.all_docs")}</div></div>
</div> </div>
</div> </div>
<Document {nuri}/> <Document {nuri}/>
</FullLayout> </FullLayout>
<svelte:window bind:innerWidth={width} /> <svelte:window bind:innerWidth={width} />

@ -32,9 +32,9 @@
} from "svelte-heros-v2"; } from "svelte-heros-v2";
//console.log(params); //console.log(params);
let nuri = ""; let nuri = "";
$: if ($active_session && params[1]) { if (params[1].startsWith("o:"+$active_session.private_store_id)) push("#/"); $: if ($active_session && params[1]) { if (params[1].startsWith($active_session.private_store_id)) push("#/");
else if (params[1].startsWith("o:"+$active_session.protected_store_id)) push("#/shared"); else if (params[1].startsWith($active_session.protected_store_id)) push("#/shared");
else if (params[1].startsWith("o:"+$active_session.public_store_id)) push("#/site"); else nuri = params[1]; } else if (params[1].startsWith($active_session.public_store_id)) push("#/site"); else nuri = params[1]; }
onMount(() => { onMount(() => {
if ($cur_tab.store.store_type) if ($cur_tab.store.store_type)
change_nav_bar(`nav:${$cur_tab.store.store_type}`,$t(`doc.${$cur_tab.store.store_type}_store`), true); change_nav_bar(`nav:${$cur_tab.store.store_type}`,$t(`doc.${$cur_tab.store.store_type}_store`), true);

@ -30,7 +30,7 @@
change_nav_bar("nav:protected",$t("doc.protected_store"), false); change_nav_bar("nav:protected",$t("doc.protected_store"), false);
reset_in_memory(); reset_in_memory();
}); });
let nuri = $active_session && ("o:"+$active_session.protected_store_id); let nuri = $active_session && $active_session.protected_store_id;
</script> </script>
<FullLayout> <FullLayout>

@ -28,7 +28,7 @@
change_nav_bar("nav:public",$t("doc.public_store"), false); change_nav_bar("nav:public",$t("doc.public_store"), false);
reset_in_memory(); reset_in_memory();
}); });
let nuri = $active_session && ("o:"+$active_session.public_store_id); let nuri = $active_session && $active_session.public_store_id;
</script> </script>
<FullLayout> <FullLayout>

@ -433,8 +433,10 @@ export const sparql_query = async function(sparql:string, union:boolean) {
}); });
throw new Error("no session"); throw new Error("no session");
} }
let nuri = union ? undefined : "did:ng:"+get(cur_tab).branch.nuri; let base = "did:ng:"+get(cur_tab).branch.nuri;
return await ng.sparql_query(session.session_id, sparql, nuri); console.log(base)
let nuri = union ? undefined : base;
return await ng.sparql_query(session.session_id, sparql, base, nuri);
} }
export const sparql_update = async function(sparql:string) { export const sparql_update = async function(sparql:string) {

@ -245,7 +245,7 @@ export const all_tabs = writable({
can_edit: false, can_edit: false,
}, },
branch: { branch: {
nuri: "", // :o or :o:b nuri: "", // :o:v or :o:v:b
readcap: "", // "r:" readcap: "", // "r:"
comment_branch: "", // nuri comment_branch: "", // nuri
class: "", class: "",

@ -1434,25 +1434,25 @@ export async function load_svg() {
/************** SPORT *********************/ /************** SPORT *********************/
// sport[0].svg = await import("./assets/pazzle/emoji_u1f93a.svg?component"); sport[0].svg = await import("./assets/pazzle/emoji_u1f93a.svg?component");
// sport[1].svg = await import("./assets/pazzle/emoji_u1f3c7.svg?component"); sport[1].svg = await import("./assets/pazzle/emoji_u1f3c7.svg?component");
// sport[2].svg = await import("./assets/pazzle/emoji_u26f7.svg?component"); sport[2].svg = await import("./assets/pazzle/emoji_u26f7.svg?component");
// sport[3].svg = await import("./assets/pazzle/emoji_u1f6a3.svg?component"); sport[3].svg = await import("./assets/pazzle/emoji_u1f6a3.svg?component");
// sport[4].svg = await import("./assets/pazzle/emoji_u1f3ca.svg?component"); sport[4].svg = await import("./assets/pazzle/emoji_u1f3ca.svg?component");
// sport[5].svg = await import("./assets/pazzle/emoji_u1f3c4.svg?component"); sport[5].svg = await import("./assets/pazzle/emoji_u1f3c4.svg?component");
// sport[6].svg = await import("./assets/pazzle/emoji_u1f3cb.svg?component"); sport[6].svg = await import("./assets/pazzle/emoji_u1f3cb.svg?component");
// sport[7].svg = await import("./assets/pazzle/emoji_u1f93c.svg?component"); sport[7].svg = await import("./assets/pazzle/emoji_u1f93c.svg?component");
// sport[8].svg = await import("./assets/pazzle/emoji_u1f6b4.svg?component"); sport[8].svg = await import("./assets/pazzle/emoji_u1f6b4.svg?component");
// sport[9].svg = await import("./assets/pazzle/emoji_u1fa82.svg?component"); sport[9].svg = await import("./assets/pazzle/emoji_u1fa82.svg?component");
// sport[10].svg = await import("./assets/pazzle/emoji_u26bd.svg?component"); sport[10].svg = await import("./assets/pazzle/emoji_u26bd.svg?component");
// sport[11].svg = await import("./assets/pazzle/emoji_u1f3c0.svg?component"); sport[11].svg = await import("./assets/pazzle/emoji_u1f3c0.svg?component");
// sport[12].svg = await import("./assets/pazzle/emoji_u1f3be.svg?component"); sport[12].svg = await import("./assets/pazzle/emoji_u1f3be.svg?component");
// sport[13].svg = await import("./assets/pazzle/emoji_u1f3d3.svg?component"); sport[13].svg = await import("./assets/pazzle/emoji_u1f3d3.svg?component");
// sport[14].svg = await import("./assets/pazzle/emoji_u1f94b.svg?component"); sport[14].svg = await import("./assets/pazzle/emoji_u1f94b.svg?component");
/************** BIGGER ANIMAL *********************/ /************** BIGGER ANIMAL *********************/

@ -238,10 +238,27 @@ impl NuriV0 {
} }
} }
pub fn to_store_nuri_string(store_id: &RepoId) -> String {
let overlay_id = OverlayId::outer(store_id);
format!("o:{store_id}:v:{overlay_id}")
}
pub fn repo_graph_name(repo_id: &RepoId, overlay_id: &OverlayId) -> String { pub fn repo_graph_name(repo_id: &RepoId, overlay_id: &OverlayId) -> String {
format!("{DID_PREFIX}:o:{repo_id}:v:{overlay_id}") format!("{DID_PREFIX}:o:{repo_id}:v:{overlay_id}")
} }
pub fn repo_skolem(
prefix: &String,
peer_id: &Vec<u8>,
random: u128,
) -> Result<String, NgError> {
let mut arr = Vec::with_capacity(32);
arr.extend_from_slice(peer_id);
arr.extend_from_slice(&random.to_be_bytes());
let sko: SymKey = arr.as_slice().try_into()?;
Ok(format!("{prefix}:u:{sko}"))
}
pub fn overlay_id(overlay_id: &OverlayId) -> String { pub fn overlay_id(overlay_id: &OverlayId) -> String {
format!("{DID_PREFIX}:v:{overlay_id}") format!("{DID_PREFIX}:v:{overlay_id}")
} }
@ -612,7 +629,10 @@ impl AppSessionStart {
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub enum DocQuery { pub enum DocQuery {
V0(String), // Sparql V0 {
sparql: String,
base: Option<String>,
},
} }
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
@ -718,8 +738,8 @@ pub enum AppRequestPayload {
} }
impl AppRequestPayload { impl AppRequestPayload {
pub fn new_sparql_query(query: String) -> Self { pub fn new_sparql_query(sparql: String, base: Option<String>) -> Self {
AppRequestPayload::V0(AppRequestPayloadV0::Query(DocQuery::V0(query))) AppRequestPayload::V0(AppRequestPayloadV0::Query(DocQuery::V0 { sparql, base }))
} }
pub fn new_discrete_update( pub fn new_discrete_update(
head_strings: Vec<String>, head_strings: Vec<String>,

@ -192,8 +192,7 @@ fn read_blank_node(s: &str) -> Result<(BlankNode, &str), TermParseError> {
if let Some(remain) = s.strip_prefix("_:") { if let Some(remain) = s.strip_prefix("_:") {
let end = remain let end = remain
.find(|v: char| { .find(|v: char| {
v.is_whitespace() v.is_whitespace() || matches!(v, '<' | '?' | '$' | '"' | '\'' | '>' | '@' | '^')
|| matches!(v, '<' | '_' | '?' | '$' | '"' | '\'' | '>' | '@' | '^')
}) })
.unwrap_or(remain.len()); .unwrap_or(remain.len());
let (value, remain) = remain.split_at(end); let (value, remain) = remain.split_at(end);

@ -76,7 +76,7 @@ entrust the credentials of user to an ngd server. coming soon
- `ng.init_headless(config)` must be called before any other call. - `ng.init_headless(config)` must be called before any other call.
- `ng.admin_create_user(config)` creates a new user on the server, and populates their 3P stores. returns the user_id - `ng.admin_create_user(config)` creates a new user on the server, and populates their 3P stores. returns the user_id
- `ng.session_headless_start(user_id)` starts a new session for the user. returns the session info, including the session_id - `ng.session_headless_start(user_id)` starts a new session for the user. returns the session info, including the session_id
- `ng.sparql_query(session_id, "[SPARQL query]")` returns or: - `ng.sparql_query(session_id, "[SPARQL query]", base, nuri)` returns or:
- for SELECT queries: a JSON Sparql Query Result as a Javascript object. [SPARQL Query Results JSON Format](https://www.w3.org/TR/sparql11-results-json/) - for SELECT queries: a JSON Sparql Query Result as a Javascript object. [SPARQL Query Results JSON Format](https://www.w3.org/TR/sparql11-results-json/)
- for CONSTRUCT queries: a list of quads in the format [RDF-JS data model](http://rdf.js.org/data-model-spec/) that can be used as ingress to RDFjs lib. - for CONSTRUCT queries: a list of quads in the format [RDF-JS data model](http://rdf.js.org/data-model-spec/) that can be used as ingress to RDFjs lib.
- for ASK queries: a boolean - for ASK queries: a boolean

@ -15,7 +15,8 @@ global.WebSocket = WebSocket;
let config = { let config = {
server_peer_id: "FtdzuDYGewfXWdoPuXIPb0wnd0SAg1WoA2B14S7jW3MA", server_peer_id: "FtdzuDYGewfXWdoPuXIPb0wnd0SAg1WoA2B14S7jW3MA",
admin_user_key: "pye0YFzk1ix1amKEwd6AeqaUAN_PNpH5zGLomh0M1PAA", admin_user_key: "pye0YFzk1ix1amKEwd6AeqaUAN_PNpH5zGLomh0M1PAA",
client_peer_key: "GRP0QnlzaB8o2vdiBaNoOYDNOFX-uehLZMxeCaG3JA0A" client_peer_key: "GRP0QnlzaB8o2vdiBaNoOYDNOFX-uehLZMxeCaG3JA0A",
server_addr: "127.0.0.1:14400"
}; };
ng.init_headless(config).then( async() => { ng.init_headless(config).then( async() => {
@ -24,7 +25,9 @@ ng.init_headless(config).then( async() => {
//let user_id = await ng.admin_create_user(config); //let user_id = await ng.admin_create_user(config);
//console.log("user created: ",user_id); //console.log("user created: ",user_id);
let user_id = "tJVG293o6xirl3Ys5rzxMgdnPE_1d3IPAdrlR5qGRAIA"; let user_id = "NnAJWxO-KapuWyCm7RGwO5VszZwaJARGit-i3i1mXbkA";
let base = "did:ng:o:8mqfhoSprneBjkAASinRk0OYvFpbiyhjMBVHKQIarDEA:v:dmn9xLARD-LrCz1tdmRiTKelikOCadGEvsLklUrwee4A";
let session = await ng.session_headless_start(user_id); let session = await ng.session_headless_start(user_id);
session_id = session.session_id; session_id = session.session_id;
@ -33,38 +36,55 @@ ng.init_headless(config).then( async() => {
let dump = await ng.rdf_dump(session.session_id); let dump = await ng.rdf_dump(session.session_id);
console.log(dump); console.log(dump);
let sparql_result = await ng.sparql_query(session.session_id, "SELECT ?s ?p ?o WHERE { ?s ?p ?o }"); console.log("******** SELECT")
let sparql_result = await ng.sparql_query(session.session_id, "SELECT ?s ?p ?o WHERE { ?s ?p ?o }", base);
console.log(sparql_result); console.log(sparql_result);
for (const q of sparql_result.results.bindings) { for (const q of sparql_result.results.bindings) {
console.log(q); console.log(q);
} }
let history = await ng.branch_history(session.session_id); // let history = await ng.branch_history(session.session_id);
for (const h of history.history) { // for (const h of history.history) {
console.log(h[0], h[1]); // console.log(h[0], h[1]);
} // }
console.log(history.swimlane_state); // console.log(history.swimlane_state);
//await ng.sparql_update(session.session_id, "INSERT DATA { <did:ng:o:8mqfhoSprneBjkAASinRk0OYvFpbiyhjMBVHKQIarDEA> <did:ng:i> <did:ng:j> }");
// await ng.sparql_update(session.session_id, "DELETE DATA { <did:ng:t:AJQ5gCLoXXjalC9diTDCvxxWu5ZQUcYWEE821nhVRMcE> <did:ng:i> <did:ng:j> }"); // await ng.sparql_update(session.session_id, "DELETE DATA { <did:ng:t:AJQ5gCLoXXjalC9diTDCvxxWu5ZQUcYWEE821nhVRMcE> <did:ng:i> <did:ng:j> }");
// await ng.sparql_update(session.session_id, "INSERT DATA { <did:ng:t:AJQ5gCLoXXjalC9diTDCvxxWu5ZQUcYWEE821nhVRMcE> <did:ng:i> <did:ng:j> }"); // await ng.sparql_update(session.session_id, "INSERT DATA { <did:ng:t:AJQ5gCLoXXjalC9diTDCvxxWu5ZQUcYWEE821nhVRMcE> <did:ng:i> <did:ng:j> }");
// await ng.sparql_update(session.session_id, "INSERT { ?s <did:ng:i> <did:ng:k> } WHERE { ?s <did:ng:i> <did:ng:j> } "); // await ng.sparql_update(session.session_id, "INSERT { ?s <did:ng:i> <did:ng:k> } WHERE { ?s <did:ng:i> <did:ng:j> } ");
// await ng.sparql_update(session.session_id, "INSERT DATA { <did:ng:z> <did:ng:j> <did:ng:t:BJQ5gCLoXXjalC9diTDCvxxWu5ZQUcYWEE821nhVRMcE>. <did:ng:t:BJQ5gCLoXXjalC9diTDCvxxWu5ZQUcYWEE821nhVRMcE> <did:ng:m> <did:ng:n> }"); // await ng.sparql_update(session.session_id, "INSERT DATA { <did:ng:z> <did:ng:j> <did:ng:t:BJQ5gCLoXXjalC9diTDCvxxWu5ZQUcYWEE821nhVRMcE>. <did:ng:t:BJQ5gCLoXXjalC9diTDCvxxWu5ZQUcYWEE821nhVRMcE> <did:ng:m> <did:ng:n> }");
//await ng.sparql_update(session.session_id, "INSERT DATA { <did:ng:z> <did:ng:j> [ <did:ng:m> <did:ng:n> ]. }");
//await ng.sparql_update(session.session_id, "INSERT DATA { [ <did:ng:m> <did:ng:n> ] <did:ng:ok> <did:ng:v> . }");
//await ng.sparql_update(session.session_id, "INSERT { ?a <did:ng:ok> <did:ng:v> . } WHERE { ?a <did:ng:m> <did:ng:n> } ");
//await ng.sparql_update(session.session_id, "INSERT DATA { <did:ng:z> <did:ng:j> _:1 . _:1 <did:ng:m> <did:ng:n>. }");
//await ng.sparql_update(session.session_id, "INSERT DATA { _:f766ca988268ddc60315ddd5bd621387 <did:ng:o> <did:ng:>. }");
//await ng.sparql_update(session.session_id, "INSERT { _:_ <did:ng:ok> <did:ng:v> . } WHERE { _:_ <did:ng:m> <did:ng:n> } ");
//await ng.sparql_update(session.session_id, "INSERT DATA { _:_ <abc:a> <d:a> . _:_a <abceee:a> <d:a> . }");
sparql_result = await ng.sparql_query(session.session_id, "SELECT ?a WHERE { ?a <did:ng:j> _:abc. _:abc <did:ng:m> <did:ng:n> }"); //await ng.sparql_update(session.session_id, "INSERT DATA { <> <a:self> <a:self> . }",base);
await ng.sparql_update(session.session_id, "INSERT DATA { <did:ng:TEST3> <did:ng:j> _:_ . _:_ <did:ng:m> <did:ng:n> . }", base);
await ng.sparql_update(session.session_id, "INSERT DATA { <did:ng:TEST4> <did:ng:j> [ <did:ng:m> <did:ng:n> ]. }", base);
sparql_result = await ng.sparql_query(session.session_id, "SELECT ?a WHERE { ?a <did:ng:j> _:abc. _:abc <did:ng:m> <did:ng:n> }", base);
console.log(sparql_result); console.log(sparql_result);
for (const q of sparql_result.results.bindings) { for (const q of sparql_result.results.bindings) {
console.log(q); console.log(q);
} }
sparql_result = await ng.sparql_query(session.session_id, "SELECT ?s ?a WHERE { ?s <did:ng:i> ?a }"); sparql_result = await ng.sparql_query(session.session_id, "SELECT ?s ?a WHERE { ?s <did:ng:j> ?a }", base);
console.log(sparql_result); console.log(sparql_result);
for (const q of sparql_result.results.bindings) { for (const q of sparql_result.results.bindings) {
console.log(q); console.log(q);
} }
let quads = await ng.sparql_query(session.session_id, "CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }"); console.log("******** CONSTRUCT")
let quads = await ng.sparql_query(session.session_id, "CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }",base);
for (const q of quads) { for (const q of quads) {
console.log(q.subject.toString(), q.predicate.toString(), q.object.toString(), q.graph.toString()) console.log(q.subject.toString(), q.predicate.toString(), q.object.toString(), q.graph.toString())
} }

@ -268,6 +268,7 @@ pub async fn session_headless_stop(session_id: JsValue, force_close: bool) -> Re
pub async fn sparql_query( pub async fn sparql_query(
session_id: JsValue, session_id: JsValue,
sparql: String, sparql: String,
base: JsValue,
nuri: JsValue, nuri: JsValue,
) -> Result<JsValue, JsValue> { ) -> Result<JsValue, JsValue> {
let session_id: u64 = serde_wasm_bindgen::from_value::<u64>(session_id) let session_id: u64 = serde_wasm_bindgen::from_value::<u64>(session_id)
@ -278,10 +279,16 @@ pub async fn sparql_query(
NuriV0::new_entire_user_site() NuriV0::new_entire_user_site()
}; };
let base_opt = if base.is_string() {
Some(base.as_string().unwrap())
} else {
None
};
let request = AppRequest::V0(AppRequestV0 { let request = AppRequest::V0(AppRequestV0 {
command: AppRequestCommandV0::new_read_query(), command: AppRequestCommandV0::new_read_query(),
nuri, nuri,
payload: Some(AppRequestPayload::new_sparql_query(sparql)), payload: Some(AppRequestPayload::new_sparql_query(sparql, base_opt)),
session_id, session_id,
}); });
@ -361,16 +368,22 @@ pub async fn discrete_update(
pub async fn sparql_update( pub async fn sparql_update(
session_id: JsValue, session_id: JsValue,
sparql: String, sparql: String,
nuri: String, nuri: JsValue,
) -> Result<(), String> { ) -> Result<(), String> {
let session_id: u64 = serde_wasm_bindgen::from_value::<u64>(session_id) let session_id: u64 = serde_wasm_bindgen::from_value::<u64>(session_id)
.map_err(|_| "Invalid session_id".to_string())?; .map_err(|_| "Invalid session_id".to_string())?;
let nuri = NuriV0::new_from(&nuri).map_err(|e| e.to_string())?;
let (nuri, base) = if nuri.is_string() {
let n = nuri.as_string().unwrap();
(NuriV0::new_from(&n).map_err(|e| e.to_string())?, Some(n))
} else {
(NuriV0::new_private_store_target(), None)
};
let request = AppRequest::V0(AppRequestV0 { let request = AppRequest::V0(AppRequestV0 {
command: AppRequestCommandV0::new_write_query(), command: AppRequestCommandV0::new_write_query(),
nuri, nuri,
payload: Some(AppRequestPayload::new_sparql_query(sparql)), payload: Some(AppRequestPayload::new_sparql_query(sparql, base)),
session_id, session_id,
}); });
@ -389,6 +402,7 @@ pub async fn sparql_update(
pub async fn sparql_query( pub async fn sparql_query(
session_id: JsValue, session_id: JsValue,
sparql: String, sparql: String,
base: JsValue,
nuri: JsValue, nuri: JsValue,
) -> Result<JsValue, JsValue> { ) -> Result<JsValue, JsValue> {
let session_id: u64 = serde_wasm_bindgen::from_value::<u64>(session_id) let session_id: u64 = serde_wasm_bindgen::from_value::<u64>(session_id)
@ -399,10 +413,16 @@ pub async fn sparql_query(
} else { } else {
NuriV0::new_entire_user_site() NuriV0::new_entire_user_site()
}; };
let base_opt = if base.is_string() {
Some(base.as_string().unwrap())
} else {
None
};
let request = AppRequest::V0(AppRequestV0 { let request = AppRequest::V0(AppRequestV0 {
command: AppRequestCommandV0::new_read_query(), command: AppRequestCommandV0::new_read_query(),
nuri, nuri,
payload: Some(AppRequestPayload::new_sparql_query(sparql)), payload: Some(AppRequestPayload::new_sparql_query(sparql, base_opt)),
session_id, session_id,
}); });
@ -460,13 +480,19 @@ pub async fn rdf_dump(session_id: JsValue) -> Result<String, String> {
} }
#[wasm_bindgen] #[wasm_bindgen]
pub async fn branch_history(session_id: JsValue, nuri: String) -> Result<JsValue, String> { pub async fn branch_history(session_id: JsValue, nuri: JsValue) -> Result<JsValue, String> {
let session_id: u64 = serde_wasm_bindgen::from_value::<u64>(session_id) let session_id: u64 = serde_wasm_bindgen::from_value::<u64>(session_id)
.map_err(|_| "Invalid session_id".to_string())?; .map_err(|_| "Invalid session_id".to_string())?;
let nuri = if nuri.is_string() {
NuriV0::new_from(&nuri.as_string().unwrap()).map_err(|e| e.to_string())?
} else {
NuriV0::new_private_store_target()
};
let request = AppRequest::V0(AppRequestV0 { let request = AppRequest::V0(AppRequestV0 {
command: AppRequestCommandV0::new_history(), command: AppRequestCommandV0::new_history(),
nuri: NuriV0::new_from(&nuri).map_err(|e| e.to_string())?, nuri,
payload: None, payload: None,
session_id, session_id,
}); });

@ -20,7 +20,9 @@ use yrs::updates::decoder::Decode;
use yrs::{ReadTxn, StateVector, Transact, Update}; use yrs::{ReadTxn, StateVector, Transact, Update};
use ng_net::app_protocol::*; use ng_net::app_protocol::*;
use ng_oxigraph::oxrdf::{GraphName, GraphNameRef, NamedNode, Quad, Triple, TripleRef}; use ng_oxigraph::oxrdf::{
BlankNode, GraphName, GraphNameRef, NamedNode, Quad, Subject, Term, Triple, TripleRef,
};
use ng_repo::errors::VerifierError; use ng_repo::errors::VerifierError;
use ng_repo::log::*; use ng_repo::log::*;
use ng_repo::store::Store; use ng_repo::store::Store;
@ -668,21 +670,63 @@ impl Verifier {
&mut self, &mut self,
nuri: &NuriV0, nuri: &NuriV0,
query: &String, query: &String,
base: &Option<String>,
peer_id: Vec<u8>,
) -> Result<(), String> { ) -> Result<(), String> {
let store = self.graph_dataset.as_ref().unwrap(); let store = self.graph_dataset.as_ref().unwrap();
let update = ng_oxigraph::oxigraph::sparql::Update::parse(query, base.as_deref())
.map_err(|e| e.to_string())?;
let res = store.ng_update( let res = store.ng_update(
query, update,
self.resolve_target_for_sparql(&nuri.target, true) self.resolve_target_for_sparql(&nuri.target, true)
.map_err(|e| e.to_string())?, .map_err(|e| e.to_string())?,
); );
match res { match res {
Err(e) => Err(e.to_string()), Err(e) => Err(e.to_string()),
Ok((inserts, removes)) => { Ok((mut inserts, removes)) => {
if inserts.is_empty() && removes.is_empty() { if inserts.is_empty() && removes.is_empty() {
Ok(()) Ok(())
} else { } else {
self.prepare_sparql_update(Vec::from_iter(inserts), Vec::from_iter(removes)) let mut new_inserts = Vec::with_capacity(inserts.len());
for mut quad in inserts.drain() {
//log_debug!("INSERTING BN {}", quad);
if quad.subject.is_blank_node() {
if base.is_none() {
return Err("Cannot insert blank nodes without a base".to_string());
}
//log_debug!("INSERTING SUBJECT BN {}", quad.subject);
if let Subject::BlankNode(b) = &quad.subject {
let iri = NuriV0::repo_skolem(
base.as_ref().unwrap(),
&peer_id,
b.as_ref().unique_id().unwrap(),
)
.map_err(|e| e.to_string())?;
quad.subject = Subject::NamedNode(NamedNode::new_unchecked(iri));
}
}
if quad.object.is_blank_node() {
if base.is_none() {
return Err("Cannot insert blank nodes without a base".to_string());
}
//log_debug!("INSERTING OBJECT BN {}", quad.object);
if let Term::BlankNode(b) = &quad.object {
let iri = NuriV0::repo_skolem(
base.as_ref().unwrap(),
&peer_id,
b.as_ref().unique_id().unwrap(),
)
.map_err(|e| e.to_string())?;
quad.object = Term::NamedNode(NamedNode::new_unchecked(iri));
}
}
// TODO deal with triples in subject and object (RDF-STAR)
new_inserts.push(quad);
}
self.prepare_sparql_update(new_inserts, Vec::from_iter(removes))
.await .await
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())
} }

@ -273,7 +273,9 @@ impl Verifier {
NuriV0::repo_graph_name(doc_create.store.repo_id(), &overlay_id); NuriV0::repo_graph_name(doc_create.store.repo_id(), &overlay_id);
let query = format!("INSERT DATA {{ <{store_nuri_string}> <http://www.w3.org/ns/ldp#contains> <{nuri}>. }}"); let query = format!("INSERT DATA {{ <{store_nuri_string}> <http://www.w3.org/ns/ldp#contains> <{nuri}>. }}");
let ret = self.process_sparql_update(&store_nuri, &query).await; let ret = self
.process_sparql_update(&store_nuri, &query, &None, vec![])
.await;
if let Err(e) = ret { if let Err(e) = ret {
return Ok(AppResponse::error(e)); return Ok(AppResponse::error(e));
} }
@ -287,19 +289,22 @@ impl Verifier {
} }
AppRequestCommandV0::Fetch(fetch) => match fetch { AppRequestCommandV0::Fetch(fetch) => match fetch {
AppFetchContentV0::ReadQuery => { AppFetchContentV0::ReadQuery => {
if let Some(AppRequestPayload::V0(AppRequestPayloadV0::Query(DocQuery::V0( if let Some(AppRequestPayload::V0(AppRequestPayloadV0::Query(DocQuery::V0 {
query, sparql,
)))) = payload base,
}))) = payload
{ {
//log_debug!("query={}", query); //log_debug!("query={}", query);
let store = self.graph_dataset.as_ref().unwrap(); let store = self.graph_dataset.as_ref().unwrap();
let parsed = Query::parse(&query, None); let parsed = Query::parse(&sparql, base.as_deref());
if parsed.is_err() { if parsed.is_err() {
return Ok(AppResponse::error(parsed.unwrap_err().to_string())); return Ok(AppResponse::error(parsed.unwrap_err().to_string()));
} }
let mut parsed = parsed.unwrap(); let mut parsed = parsed.unwrap();
let dataset = parsed.dataset_mut(); let dataset = parsed.dataset_mut();
//log_debug!("DEFAULTS {:?}", dataset.default_graph_graphs());
if dataset.has_no_default_dataset() { if dataset.has_no_default_dataset() {
//log_info!("DEFAULT GRAPH AS UNION");
dataset.set_default_graph_as_union(); dataset.set_default_graph_as_union();
} }
let results = store let results = store
@ -323,13 +328,23 @@ impl Verifier {
return Err(NgError::InvalidNuri); return Err(NgError::InvalidNuri);
} }
return if let Some(AppRequestPayload::V0(AppRequestPayloadV0::Query( return if let Some(AppRequestPayload::V0(AppRequestPayloadV0::Query(
DocQuery::V0(query), DocQuery::V0 { sparql, base },
))) = payload ))) = payload
{ {
Ok(match self.process_sparql_update(&nuri, &query).await { Ok(
match self
.process_sparql_update(
&nuri,
&sparql,
&base,
self.get_peer_id_for_skolem(),
)
.await
{
Err(e) => AppResponse::error(e), Err(e) => AppResponse::error(e),
Ok(_) => AppResponse::ok(), Ok(_) => AppResponse::ok(),
}) },
)
} else { } else {
Err(NgError::InvalidPayload) Err(NgError::InvalidPayload)
}; };

@ -122,6 +122,10 @@ struct EventOutboxStorage {
} }
impl Verifier { impl Verifier {
pub(crate) fn get_peer_id_for_skolem(&self) -> Vec<u8> {
self.peer_id.to_dh_slice()[0..16].to_vec()
}
pub fn complement_credentials(&self, creds: &mut Credentials) { pub fn complement_credentials(&self, creds: &mut Credentials) {
creds.private_store = self.private_store_id().clone(); creds.private_store = self.private_store_id().clone();
creds.protected_store = self.protected_store_id().clone(); creds.protected_store = self.protected_store_id().clone();
@ -2416,6 +2420,7 @@ impl Verifier {
fn add_repo_(&mut self, repo: Repo) -> &Repo { fn add_repo_(&mut self, repo: Repo) -> &Repo {
//self.populate_topics(&repo); //self.populate_topics(&repo);
let _ = self.add_doc(&repo.id, &repo.store.overlay_id);
let repo_ref = self.repos.entry(repo.id).or_insert(repo); let repo_ref = self.repos.entry(repo.id).or_insert(repo);
repo_ref repo_ref
} }
@ -2547,7 +2552,15 @@ impl Verifier {
mod test { mod test {
use crate::verifier::*; use crate::verifier::*;
use ng_oxigraph::oxrdf::BlankNode;
use ng_repo::store::Store; use ng_repo::store::Store;
use std::str::FromStr;
#[test]
pub fn test_blank_node() {
let bn = BlankNode::from_str("_:____").expect("parse");
log_debug!("{:?}", bn);
}
#[async_std::test] #[async_std::test]
pub async fn test_new_repo_default() { pub async fn test_new_repo_default() {

Loading…
Cancel
Save