triple.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. Copyright (c) 2012 Kier Davis
  3. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  4. associated documentation files (the "Software"), to deal in the Software without restriction,
  5. including without limitation the rights to use, copy, modify, merge, publish, distribute,
  6. sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  7. furnished to do so, subject to the following conditions:
  8. The above copyright notice and this permission notice shall be included in all copies or substantial
  9. portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  11. NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  12. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  13. OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  14. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  15. */
  16. package gold
  17. import (
  18. "fmt"
  19. )
  20. // Triple contains a subject, a predicate and an object term.
  21. type Triple struct {
  22. Subject Term
  23. Predicate Term
  24. Object Term
  25. }
  26. // NewTriple returns a new triple with the given subject, predicate and object.
  27. func NewTriple(subject Term, predicate Term, object Term) (triple *Triple) {
  28. return &Triple{
  29. Subject: subject,
  30. Predicate: predicate,
  31. Object: object,
  32. }
  33. }
  34. // String returns the NTriples representation of this triple.
  35. func (triple Triple) String() (str string) {
  36. subjStr := "nil"
  37. if triple.Subject != nil {
  38. subjStr = triple.Subject.String()
  39. }
  40. predStr := "nil"
  41. if triple.Predicate != nil {
  42. predStr = triple.Predicate.String()
  43. }
  44. objStr := "nil"
  45. if triple.Object != nil {
  46. objStr = triple.Object.String()
  47. }
  48. return fmt.Sprintf("%s %s %s .", subjStr, predStr, objStr)
  49. }
  50. // Equal returns this triple is equivalent to the argument.
  51. func (triple Triple) Equal(other *Triple) bool {
  52. return triple.Subject.Equal(other.Subject) &&
  53. triple.Predicate.Equal(other.Predicate) &&
  54. triple.Object.Equal(other.Object)
  55. }