radial sugiyama positioning integration

This commit is contained in:
Oxy8
2026-03-23 11:13:27 -03:00
parent 6b9115e43b
commit 696844f341
51 changed files with 10089 additions and 364 deletions

View File

@@ -3,6 +3,7 @@ package selection_queries
import (
"encoding/json"
"fmt"
"log"
"sort"
"strings"
)
@@ -66,30 +67,83 @@ func selectedNodesFromIDs(idx Index, selectedIDs []uint32, includeBNodes bool) (
return out, set
}
func idsFromBindings(raw []byte, varName string, idx Index, selectedSet map[uint32]struct{}, includeBNodes bool) ([]uint32, error) {
func idFromSparqlTerm(term sparqlTerm, idx Index, includeBNodes bool) (uint32, bool) {
key, ok := termKeyFromSparqlTerm(term, includeBNodes)
if !ok {
return 0, false
}
nid, ok := idx.KeyToID[key]
return nid, ok
}
func tripleTermFromSparqlTerm(term sparqlTerm) TripleTerm {
return TripleTerm{
Type: term.Type,
Value: term.Value,
Lang: term.Lang,
}
}
func logQueryExecutionFailure(queryName string, selectedIDs []uint32, includeBNodes bool, sparql string, err error) {
log.Printf(
"%s: SPARQL execution failed selected_ids=%v include_bnodes=%t err=%v\nSPARQL:\n%s",
queryName,
selectedIDs,
includeBNodes,
err,
strings.TrimSpace(sparql),
)
}
func resultFromTripleBindings(raw []byte, idx Index, selectedSet map[uint32]struct{}, includeBNodes bool) (Result, error) {
var res sparqlResponse
if err := json.Unmarshal(raw, &res); err != nil {
return nil, fmt.Errorf("failed to parse SPARQL JSON: %w", err)
return Result{}, fmt.Errorf("failed to parse SPARQL JSON: %w", err)
}
neighborSet := make(map[uint32]struct{})
triples := make([]Triple, 0, len(res.Results.Bindings))
for _, b := range res.Results.Bindings {
term, ok := b[varName]
if !ok {
sTerm, okS := b["s"]
pTerm, okP := b["p"]
oTerm, okO := b["o"]
if !okS || !okP || !okO {
continue
}
key, ok := termKeyFromSparqlTerm(term, includeBNodes)
if !ok {
continue
triple := Triple{
S: tripleTermFromSparqlTerm(sTerm),
P: tripleTermFromSparqlTerm(pTerm),
O: tripleTermFromSparqlTerm(oTerm),
}
nid, ok := idx.KeyToID[key]
if !ok {
continue
subjID, subjOK := idFromSparqlTerm(sTerm, idx, includeBNodes)
if subjOK {
id := subjID
triple.SubjectID = &id
}
if _, sel := selectedSet[nid]; sel {
continue
objID, objOK := idFromSparqlTerm(oTerm, idx, includeBNodes)
if objOK {
id := objID
triple.ObjectID = &id
}
neighborSet[nid] = struct{}{}
if pTerm.Type == "uri" {
if predID, ok := idx.PredicateIDByIRI[pTerm.Value]; ok {
id := predID
triple.PredicateID = &id
}
}
_, subjSelected := selectedSet[subjID]
_, objSelected := selectedSet[objID]
if subjOK && subjSelected && objOK && !objSelected {
neighborSet[objID] = struct{}{}
}
if objOK && objSelected && subjOK && !subjSelected {
neighborSet[subjID] = struct{}{}
}
triples = append(triples, triple)
}
ids := make([]uint32, 0, len(neighborSet))
@@ -97,5 +151,5 @@ func idsFromBindings(raw []byte, varName string, idx Index, selectedSet map[uint
ids = append(ids, nid)
}
sort.Slice(ids, func(i, j int) bool { return ids[i] < ids[j] })
return ids, nil
return Result{NeighborIDs: ids, Triples: triples}, nil
}