diff --git a/CHANGELOG.md b/CHANGELOG.md index 81ef209..ec57c45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [[unpublished]](https://github.com/mlange-42/bbn/compare/v0.6.0...main) + +### Other + +* Removes unused type `ConflictingEvidenceError` (#87) + ## [[v0.6.0]](https://github.com/mlange-42/bbn/compare/v0.5.0...v0.6.0) ### Features diff --git a/network.go b/network.go index fcf65b7..deb65d1 100644 --- a/network.go +++ b/network.go @@ -2,7 +2,10 @@ package bbn import ( "fmt" + "os" + "path/filepath" "slices" + "strings" "github.com/mlange-42/bbn/ve" ) @@ -102,6 +105,32 @@ func New(name string, info string, variables []Variable, factors []Factor) (*Net return net, nil } +// FromFile reads a [Network] from an YAML or XML file. +func FromFile(path string) (*Network, []Variable, error) { + data, err := os.ReadFile(path) + if err != nil { + return nil, nil, err + } + ext := filepath.Ext(path) + + switch strings.ToLower(ext) { + case ".yml": + n, err := FromYAML(data) + if err != nil { + return nil, nil, err + } + return n, n.variables, nil + case ".xml", ".bifxml": + n, err := FromBIFXML(data) + if err != nil { + return nil, nil, err + } + return n, n.variables, nil + default: + return nil, nil, fmt.Errorf("unsupported file format '%s'", ext) + } +} + // prepareVariables, called from the constructor. func (n *Network) prepareVariables() error { varNames := map[string]*Variable{} diff --git a/util.go b/util.go deleted file mode 100644 index e488eb2..0000000 --- a/util.go +++ /dev/null @@ -1,41 +0,0 @@ -package bbn - -import ( - "fmt" - "os" - "path/filepath" - "strings" -) - -// Error type for conflicting evidence. -type ConflictingEvidenceError struct{} - -func (m *ConflictingEvidenceError) Error() string { - return "conflicting evidence / all samples rejected" -} - -// FromFile reads a [Network] from an YAML or XML file. -func FromFile(path string) (*Network, []Variable, error) { - data, err := os.ReadFile(path) - if err != nil { - return nil, nil, err - } - ext := filepath.Ext(path) - - switch strings.ToLower(ext) { - case ".yml": - n, err := FromYAML(data) - if err != nil { - return nil, nil, err - } - return n, n.variables, nil - case ".xml", ".bifxml": - n, err := FromBIFXML(data) - if err != nil { - return nil, nil, err - } - return n, n.variables, nil - default: - return nil, nil, fmt.Errorf("unsupported file format '%s'", ext) - } -}