github.com/gopherd/gonum@v0.0.4/graph/formats/rdf/equi_canonical_example_test.go (about)

     1  // Copyright ©2021 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package rdf_test
     6  
     7  import (
     8  	"fmt"
     9  	"strings"
    10  
    11  	"github.com/gopherd/gonum/graph/formats/rdf"
    12  )
    13  
    14  func ExampleLean() {
    15  	for i, statements := range []string{
    16  		0: `
    17  _:author1 <ex:contributesTo> _:go .
    18  _:author2 <ex:contributesTo> _:go .
    19  _:author2 <ex:contributesTo> _:gonum .
    20  _:author3 <ex:contributesTo> _:gonum .
    21  `,
    22  		1: `
    23  _:author1 <ex:contributesTo> _:go .
    24  _:author2 <ex:contributesTo> _:go .
    25  _:author2 <ex:contributesTo> _:gonum .
    26  _:author3 <ex:contributesTo> _:gonum .
    27  _:gonum <ex:dependsOn> _:go .
    28  `,
    29  		2: `
    30  _:author1 <ex:contributesTo> _:go .
    31  _:author1 <ex:notContributesTo> _:gonum .
    32  _:author2 <ex:contributesTo> _:go .
    33  _:author2 <ex:contributesTo> _:gonum .
    34  _:author3 <ex:contributesTo> _:gonum .
    35  _:gonum <ex:dependsOn> _:go .
    36  `,
    37  		3: `
    38  _:author1 <ex:is> "Alice" .
    39  _:author1 <ex:contributesTo> _:go .
    40  _:author2 <ex:contributesTo> _:go .
    41  _:author2 <ex:contributesTo> _:gonum .
    42  _:author3 <ex:contributesTo> _:gonum .
    43  _:gonum <ex:dependsOn> _:go .
    44  `,
    45  		4: `
    46  _:author1 <ex:contributesTo> _:go .
    47  _:author1 <ex:notContributesTo> _:gonum .
    48  _:author2 <ex:contributesTo> _:go .
    49  _:author2 <ex:contributesTo> _:gonum .
    50  _:author3 <ex:contributesTo> _:gonum .
    51  _:author3 <ex:notContributesTo> _:go .
    52  _:gonum <ex:dependsOn> _:go .
    53  `,
    54  		5: `
    55  _:author1 <ex:is> "Alice" .
    56  _:author2 <ex:is> "Bob" .
    57  _:author1 <ex:contributesTo> _:go .
    58  _:author2 <ex:contributesTo> _:go .
    59  _:author2 <ex:contributesTo> _:gonum .
    60  _:author3 <ex:contributesTo> _:gonum .
    61  _:gonum <ex:dependsOn> _:go .
    62  `,
    63  		6: `
    64  _:author1 <ex:is> "Alice" .
    65  _:author2 <ex:is> "Bob" .
    66  _:author3 <ex:is> "Charlie" .
    67  _:author1 <ex:contributesTo> _:go .
    68  _:author2 <ex:contributesTo> _:go .
    69  _:author2 <ex:contributesTo> _:gonum .
    70  _:author3 <ex:contributesTo> _:gonum .
    71  _:gonum <ex:dependsOn> _:go .
    72  `,
    73  	} {
    74  		// Decode the statement stream.
    75  		dec := rdf.NewDecoder(strings.NewReader(statements))
    76  		var s []*rdf.Statement
    77  		for {
    78  			l, err := dec.Unmarshal()
    79  			if err != nil {
    80  				break
    81  			}
    82  			s = append(s, l)
    83  		}
    84  
    85  		// Lean the graph to remove redundant statements.
    86  		lean, err := rdf.Lean(s)
    87  		if err != nil {
    88  			fmt.Println(err)
    89  		}
    90  
    91  		// Canonicalize the blank nodes in-place.
    92  		_, err = rdf.URDNA2015(lean, lean)
    93  		if err != nil {
    94  			fmt.Println(err)
    95  			continue
    96  		}
    97  
    98  		fmt.Printf("%d:\n", i)
    99  		for _, s := range lean {
   100  			fmt.Println(s)
   101  		}
   102  		fmt.Println()
   103  	}
   104  
   105  	// Output:
   106  	//
   107  	// 0:
   108  	// _:c14n0 <ex:contributesTo> _:c14n1 .
   109  	//
   110  	// 1:
   111  	// _:c14n0 <ex:contributesTo> _:c14n1 .
   112  	// _:c14n0 <ex:contributesTo> _:c14n2 .
   113  	// _:c14n2 <ex:dependsOn> _:c14n1 .
   114  	//
   115  	// 2:
   116  	// _:c14n0 <ex:contributesTo> _:c14n1 .
   117  	// _:c14n0 <ex:contributesTo> _:c14n3 .
   118  	// _:c14n2 <ex:contributesTo> _:c14n1 .
   119  	// _:c14n2 <ex:notContributesTo> _:c14n3 .
   120  	// _:c14n3 <ex:dependsOn> _:c14n1 .
   121  	//
   122  	// 3:
   123  	// _:c14n0 <ex:contributesTo> _:c14n1 .
   124  	// _:c14n0 <ex:contributesTo> _:c14n3 .
   125  	// _:c14n2 <ex:contributesTo> _:c14n1 .
   126  	// _:c14n2 <ex:is> "Alice" .
   127  	// _:c14n3 <ex:dependsOn> _:c14n1 .
   128  	//
   129  	// 4:
   130  	// _:c14n0 <ex:contributesTo> _:c14n1 .
   131  	// _:c14n0 <ex:contributesTo> _:c14n2 .
   132  	// _:c14n2 <ex:dependsOn> _:c14n1 .
   133  	// _:c14n3 <ex:contributesTo> _:c14n1 .
   134  	// _:c14n3 <ex:notContributesTo> _:c14n2 .
   135  	// _:c14n4 <ex:contributesTo> _:c14n2 .
   136  	// _:c14n4 <ex:notContributesTo> _:c14n1 .
   137  	//
   138  	// 5:
   139  	// _:c14n1 <ex:contributesTo> _:c14n0 .
   140  	// _:c14n1 <ex:contributesTo> _:c14n3 .
   141  	// _:c14n1 <ex:is> "Bob" .
   142  	// _:c14n2 <ex:contributesTo> _:c14n0 .
   143  	// _:c14n2 <ex:is> "Alice" .
   144  	// _:c14n3 <ex:dependsOn> _:c14n0 .
   145  	//
   146  	// 6:
   147  	// _:c14n0 <ex:dependsOn> _:c14n1 .
   148  	// _:c14n2 <ex:contributesTo> _:c14n0 .
   149  	// _:c14n2 <ex:contributesTo> _:c14n1 .
   150  	// _:c14n2 <ex:is> "Bob" .
   151  	// _:c14n3 <ex:contributesTo> _:c14n1 .
   152  	// _:c14n3 <ex:is> "Alice" .
   153  	// _:c14n4 <ex:contributesTo> _:c14n0 .
   154  	// _:c14n4 <ex:is> "Charlie" .
   155  }