Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mustExist flag to patch object #299

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 59 additions & 60 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ class IndexedFormula extends Formula { // IN future - allow pass array of statem
applyPatch (patch, target, patchCallback) { // patchCallback(err)
const Query = require('./query').Query
var targetKB = this
var ds
var binding = null

// /////////// Debug strings
/*
Expand All @@ -134,74 +132,75 @@ class IndexedFormula extends Formula { // IN future - allow pass array of statem
return str
}
*/
var doPatch = function (onDonePatch) {
if (patch['delete']) {
ds = patch['delete']
// console.log(bindingDebug(binding))
// console.log('ds before substitute: ' + ds)
if (binding) ds = ds.substitute(binding)
// console.log('applyPatch: delete: ' + ds)
ds = ds.statements
var bad = []
var ds2 = ds.map(function (st) { // Find the actual statemnts in the store
var sts = targetKB.statementsMatching(st.subject, st.predicate, st.object, target)
const { delete: deletions, insert: insertions, where } = patch
const mustExist = 'mustExist' in patch ? !!patch.mustExist : true

const toBeAdded = []
const toBeRemoved = []

if (!where) {
createChanges([null])
} else {
const query = new Query('patch')
query.pat.statements = where.statements.map(function (st) {
const { subject, predicate, object } = st
return { subject, predicate, object, why: target }
})

const bindingsFound = []
targetKB.query(query, b => bindingsFound.push(b),
targetKB.fetcher,
() => {
if (bindingsFound.length === 0) {
if (mustExist || insertions)
return patchCallback('No match found to be patched:' + patch.where)
bindingsFound.push(null);
}
createChanges(bindingsFound);
})
}

function createChanges(bindings) {
try {
for (const binding of bindings) {
createChange(binding)
}
}
catch (error) {
return patchCallback(error)
}
applyChanges()
}

function createChange(binding) {
if (deletions) {
const graph = !binding ? deletions : deletions.substitute(binding)
const notFound = []
graph.statements.forEach(function (st) { // Find the actual statemnts in the store
const sts = targetKB.statementsMatching(st.subject, st.predicate, st.object, target)
if (sts.length === 0) {
// log.info("NOT FOUND deletable " + st)
bad.push(st)
return null
notFound.push(st)
} else {
// log.info("Found deletable " + st)
return sts[0]
toBeRemoved.push(sts[0])
}
})
if (bad.length) {
// console.log('Could not find to delete ' + bad.length + 'statements')
// console.log('despite ' + targetKB.statementsMatching(bad[0].subject, bad[0].predicate)[0])
return patchCallback('Could not find to delete: ' + bad.join('\n or '))
if (mustExist && notFound.length !== 0) {
throw new Error('Could not find to delete: ' + notFound.join('\n or '))
}
ds2.map(function (st) {
targetKB.remove(st)
})
}
if (patch['insert']) {
// log.info("doPatch insert "+patch['insert'])
ds = patch['insert']
if (binding) ds = ds.substitute(binding)
ds = ds.statements
ds.map(function (st) {
if (insertions) {
const graph = !binding ? insertions : insertions.substitute(binding)
graph.statements.forEach(function (st) {
st.why = target
targetKB.add(st.subject, st.predicate, st.object, st.why)
toBeAdded.push(st)
})
}
onDonePatch()
}
if (patch.where) {
// log.info("Processing WHERE: " + patch.where + '\n')
var query = new Query('patch')
query.pat = patch.where
query.pat.statements.map(function (st) {
st.why = target
})

var bindingsFound = []
}

targetKB.query(query, function onBinding (binding) {
bindingsFound.push(binding)
// console.log(' got a binding: ' + bindingDebug(binding))
},
targetKB.fetcher,
function onDone () {
if (bindingsFound.length === 0) {
return patchCallback('No match found to be patched:' + patch.where)
}
if (bindingsFound.length > 1) {
return patchCallback('Patch ambiguous. No patch done.')
}
binding = bindingsFound[0]
doPatch(patchCallback)
})
} else {
doPatch(patchCallback)
function applyChanges() {
toBeRemoved.forEach(st => targetKB.remove(st))
toBeAdded.forEach(st => targetKB.add(st))
patchCallback()
}
}

Expand Down