| 1 |
package rdfwriter |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"github.com/spdx/gordf/rdfloader/parser" |
| 6 |
"github.com/spdx/gordf/uri" |
| 7 |
"strings" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
func GetAdjacencyList(triples []*parser.Triple) (adjList map[*parser.Node][]*parser.Node) { |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
adjList = make(map[*parser.Node][]*parser.Node) |
| 25 |
for _, triple := range triples { |
| 26 |
|
| 27 |
if adjList[triple.Subject] == nil { |
| 28 |
adjList[triple.Subject] = []*parser.Node{} |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
adjList[triple.Subject] = append(adjList[triple.Subject], triple.Object) |
| 33 |
|
| 34 |
|
| 35 |
if adjList[triple.Object] == nil { |
| 36 |
adjList[triple.Object] = []*parser.Node{} |
| 37 |
} |
| 38 |
} |
| 39 |
return adjList |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
func GetNodeToTriples(triples []*parser.Triple) (recoveryDS map[string][]*parser.Triple) { |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
recoveryDS = make(map[string][]*parser.Triple) |
| 57 |
for _, triple := range triples { |
| 58 |
|
| 59 |
if recoveryDS[triple.Subject.String()] == nil { |
| 60 |
recoveryDS[triple.Subject.String()] = []*parser.Triple{} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
recoveryDS[triple.Subject.String()] = append(recoveryDS[triple.Subject.String()], triple) |
| 65 |
|
| 66 |
|
| 67 |
if recoveryDS[triple.Object.String()] == nil { |
| 68 |
recoveryDS[triple.Object.String()] = []*parser.Triple{} |
| 69 |
} |
| 70 |
} |
| 71 |
return removeDuplicateTriples(recoveryDS) |
| 72 |
} |
| 73 |
|
| 74 |
func getUniqueTriples(triples []*parser.Triple) []*parser.Triple { |
| 75 |
set := map[string]*parser.Triple{} |
| 76 |
for _, triple := range triples { |
| 77 |
set[triple.Hash()] = triple |
| 78 |
} |
| 79 |
var retList []*parser.Triple |
| 80 |
for key := range set { |
| 81 |
retList = append(retList, set[key]) |
| 82 |
} |
| 83 |
return retList |
| 84 |
} |
| 85 |
|
| 86 |
func removeDuplicateTriples(nodeToTriples map[string][]*parser.Triple) map[string][]*parser.Triple { |
| 87 |
retMap := map[string][]*parser.Triple{} |
| 88 |
for key := range nodeToTriples { |
| 89 |
retMap[key] = getUniqueTriples(nodeToTriples[key]) |
| 90 |
} |
| 91 |
return retMap |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
func topologicalSortHelper(node *parser.Node, lastIndex *int, adjList map[*parser.Node][]*parser.Node, visited *map[*parser.Node]bool, resultList *[]*parser.Node) (err error) { |
| 101 |
if node == nil { |
| 102 |
return |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
_, exists := adjList[node] |
| 107 |
if !exists { |
| 108 |
return fmt.Errorf("node%v doesn't exist in the graph", *node) |
| 109 |
} |
| 110 |
if (*visited)[node] { |
| 111 |
|
| 112 |
|
| 113 |
return |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
(*visited)[node] = true |
| 118 |
|
| 119 |
|
| 120 |
for _, neighbor := range adjList[node] { |
| 121 |
|
| 122 |
if !(*visited)[neighbor] { |
| 123 |
err = topologicalSortHelper(neighbor, lastIndex, adjList, visited, resultList) |
| 124 |
if err != nil { |
| 125 |
return err |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
if *lastIndex >= len(adjList) { |
| 131 |
|
| 132 |
|
| 133 |
return fmt.Errorf("found more nodes than the number of keys in the adjacency list") |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
(*resultList)[*lastIndex] = node |
| 138 |
*lastIndex++ |
| 139 |
return nil |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
func topologicalSort(adjList map[*parser.Node][]*parser.Node) ([]*parser.Node, error) { |
| 151 |
|
| 152 |
numberNodes := len(adjList) |
| 153 |
resultList := make([]*parser.Node, numberNodes) |
| 154 |
visited := make(map[*parser.Node]bool, numberNodes) |
| 155 |
lastIndex := 0 |
| 156 |
|
| 157 |
|
| 158 |
for node := range adjList { |
| 159 |
if !visited[node] { |
| 160 |
err := topologicalSortHelper(node, &lastIndex, adjList, &visited, &resultList) |
| 161 |
if err != nil { |
| 162 |
return resultList, err |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
return resultList, nil |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
func TopologicalSortTriples(triples []*parser.Triple) (sortedTriples []*parser.Triple, err error) { |
| 172 |
adjList := GetAdjacencyList(triples) |
| 173 |
recoveryDS := GetNodeToTriples(triples) |
| 174 |
sortedNodes, err := topologicalSort(adjList) |
| 175 |
if err != nil { |
| 176 |
return sortedTriples, fmt.Errorf("error sorting the triples: %v", err) |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
sortedTriples = []*parser.Triple{} |
| 181 |
|
| 182 |
for _, subjectNode := range sortedNodes { |
| 183 |
|
| 184 |
for _, triple := range recoveryDS[subjectNode.String()] { |
| 185 |
sortedTriples = append(sortedTriples, triple) |
| 186 |
} |
| 187 |
} |
| 188 |
return sortedTriples, nil |
| 189 |
} |
| 190 |
|
| 191 |
func DisjointSet(triples []*parser.Triple) map[*parser.Node]*parser.Node { |
| 192 |
nodeStringMap := map[string]*parser.Node{} |
| 193 |
parentString := map[string]*parser.Node{} |
| 194 |
for _, triple := range triples { |
| 195 |
parentString[triple.Object.String()] = triple.Subject |
| 196 |
nodeStringMap[triple.Object.String()] = triple.Object |
| 197 |
if _, exists := parentString[triple.Subject.String()]; !exists { |
| 198 |
parentString[triple.Subject.String()] = nil |
| 199 |
nodeStringMap[triple.Subject.String()] = triple.Subject |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
parent := make(map[*parser.Node]*parser.Node) |
| 204 |
for keyString := range parentString { |
| 205 |
node := nodeStringMap[keyString] |
| 206 |
parent[node] = parentString[keyString] |
| 207 |
} |
| 208 |
return parent |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
func invertSchemaDefinition(schemaDefinition map[string]uri.URIRef) map[string]string { |
| 220 |
invertedMap := make(map[string]string) |
| 221 |
for abbreviation := range schemaDefinition { |
| 222 |
_uri := schemaDefinition[abbreviation] |
| 223 |
invertedMap[strings.Trim(_uri.String(), "#")] = abbreviation |
| 224 |
} |
| 225 |
return invertedMap |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
func any(target string, list []string) bool { |
| 230 |
for _, s := range list { |
| 231 |
if s == target { |
| 232 |
return true |
| 233 |
} |
| 234 |
} |
| 235 |
return false |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
func getRDFNSAbbreviation(invSchemaDefinition map[string]string) string { |
| 241 |
rdfNSAbbrev := "rdf" |
| 242 |
if abbrev, exists := invSchemaDefinition[parser.RDFNS]; exists { |
| 243 |
rdfNSAbbrev = abbrev |
| 244 |
} |
| 245 |
return rdfNSAbbrev |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
func shortenURI(uri string, invSchemaDefinition map[string]string) (string, error) { |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
splitIndex := strings.LastIndex(uri, "#") |
| 257 |
if splitIndex == -1 { |
| 258 |
return "", fmt.Errorf("uri doesn't have two parts of type schemaName:tagName. URI: %s", uri) |
| 259 |
} |
| 260 |
|
| 261 |
baseURI := strings.Trim(uri[:splitIndex], "#") |
| 262 |
fragment := strings.TrimSuffix(uri[splitIndex+1:], "#") |
| 263 |
fragment = strings.TrimSpace(fragment) |
| 264 |
if len(fragment) == 0 { |
| 265 |
return "", fmt.Errorf(`fragment "%v" doesn't exist`, fragment) |
| 266 |
} |
| 267 |
if abbrev, exists := invSchemaDefinition[baseURI]; exists { |
| 268 |
if abbrev == "" { |
| 269 |
return fragment, nil |
| 270 |
} |
| 271 |
return fmt.Sprintf("%s:%s", abbrev, fragment), nil |
| 272 |
} |
| 273 |
return "", fmt.Errorf("declaration of URI(%s) not found in the schemaDefinition", baseURI) |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
func GetRootNodes(triples []*parser.Triple) (rootNodes []*parser.Node) { |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
var parent map[*parser.Node]*parser.Node |
| 284 |
parent = DisjointSet(triples) |
| 285 |
|
| 286 |
for node := range parent { |
| 287 |
if parent[node] == nil { |
| 288 |
rootNodes = append(rootNodes, node) |
| 289 |
} |
| 290 |
} |
| 291 |
return rootNodes |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
func getRestTriples(triples []*parser.Triple) (restTriples []*parser.Triple) { |
| 296 |
rdfTypeURI := parser.RDFNS + "type" |
| 297 |
rdfNodeIDURI := parser.RDFNS + "nodeID" |
| 298 |
for _, triple := range triples { |
| 299 |
if !any(triple.Predicate.ID, []string{rdfNodeIDURI, rdfTypeURI}) { |
| 300 |
restTriples = append(restTriples, triple) |
| 301 |
} |
| 302 |
} |
| 303 |
return restTriples |
| 304 |
} |