Skip to content

Commit

Permalink
#149 Savepoint
Browse files Browse the repository at this point in the history
  • Loading branch information
docktermj committed Dec 17, 2024
1 parent 1701cb5 commit 1b7083e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 155 deletions.
105 changes: 29 additions & 76 deletions rootfs/examples/go/senzing_load_truthsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,60 +29,20 @@ type Record struct {

var (
ctx = context.TODO()
err error
grpcAddress = "localhost:8261"
jsonDataSource DataSourceKey
homePath = "./"
jsonRecord Record
szConfig senzing.SzConfig
truthSetURLPrefix = "https://raw.githubusercontent.com/Senzing/truth-sets/refs/heads/main/truthsets/demo/"
truthSetFileNames = []string{"customers.json", "reference.json", "watchlist.json"}
)

func getSzAbstractFactory() senzing.SzAbstractFactory {
grpcConnection, err := grpc.NewClient(grpcAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
testErr(err)
return &szabstractfactory.Szabstractfactory{
GrpcConnection: grpcConnection,
}
}

func testErr(err error) {
if err != nil {
panic(err)
}
}

func getSzConfig(ctx context.Context, szAbstractFactory senzing.SzAbstractFactory) senzing.SzConfig {
result, err := szAbstractFactory.CreateConfig(ctx)
testErr(err)
return result
}

func getSzConfigManager(ctx context.Context, szAbstractFactory senzing.SzAbstractFactory) senzing.SzConfigManager {
result, err := szAbstractFactory.CreateConfigManager(ctx)
testErr(err)
return result
}

func getSzDiagnostic(ctx context.Context, szAbstractFactory senzing.SzAbstractFactory) senzing.SzDiagnostic {
result, err := szAbstractFactory.CreateDiagnostic(ctx)
testErr(err)
return result
}

func getSzEngine(ctx context.Context, szAbstractFactory senzing.SzAbstractFactory) senzing.SzEngine {
result, err := szAbstractFactory.CreateEngine(ctx)
testErr(err)
return result
}

func getSzProduct(ctx context.Context, szAbstractFactory senzing.SzAbstractFactory) senzing.SzProduct {
result, err := szAbstractFactory.CreateProduct(ctx)
testErr(err)
return result
}

func downloadFile(url string, filepath string) error {
outputFile, err := os.Create(filepath)
if err != nil {
Expand Down Expand Up @@ -131,28 +91,44 @@ func getDataSources() []string {
return result
}

func prettyJSON(str string) (string, error) {
func asPrettyJSON(str string) string {
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, []byte(str), "", " "); err != nil {
return "", err
return str
}
return prettyJSON.String(), nil
return prettyJSON.String()
}

func main() {

// Download truth-sets.

for i := 0; i < len(truthSetFileNames); i++ {
url := fmt.Sprintf("%s/%s", truthSetURLPrefix, truthSetFileNames[i])
filepath := fmt.Sprintf("%s%s", homePath, truthSetFileNames[i])
err := downloadFile(url, filepath)
testErr(err)
}

// Identify datasources.

var dataSources = getDataSources()
fmt.Printf("Found the following DATA_SOURCE values in the data: %v\n", dataSources)
var szAbstractFactory = getSzAbstractFactory()
var szConfig = getSzConfig(ctx, szAbstractFactory)
var szConfigManager = getSzConfigManager(ctx, szAbstractFactory)
// var szDiagnostic = getSzDiagnostic(ctx, szAbstractFactory)

grpcConnection, err := grpc.NewClient(grpcAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
testErr(err)
szAbstractFactory := &szabstractfactory.Szabstractfactory{
GrpcConnection: grpcConnection,
}

szConfig, err := szAbstractFactory.CreateConfig(ctx)
testErr(err)

szConfigManager, err := szAbstractFactory.CreateConfigManager(ctx)
testErr(err)

szEngine, err := szAbstractFactory.CreateEngine(ctx)
testErr(err)

oldConfigID, err := szConfigManager.GetDefaultConfigID(ctx)
testErr(err)
Expand All @@ -179,27 +155,10 @@ func main() {
err = szConfigManager.ReplaceDefaultConfigID(ctx, oldConfigID, newConfigID)
testErr(err)

fmt.Printf(">>>>>3 szAbstractFactory.Reinitialize(ctx, %d)\n", newConfigID)
szAbstractFactory.Reinitialize(ctx, newConfigID)

testErr(err)

var szEngine = getSzEngine(ctx, szAbstractFactory)

defaultConfigID, err := szConfigManager.GetDefaultConfigID(ctx)
testErr(err)
fmt.Printf("default: %d\n", defaultConfigID)

configDefinition, err := szConfigManager.GetConfig(ctx, defaultConfigID)
testErr(err)

configHandle, err = szConfig.ImportConfig(ctx, configDefinition)
testErr(err)

dataXX, err := szConfig.GetDataSources(ctx, configHandle)
err = szAbstractFactory.Reinitialize(ctx, newConfigID)
testErr(err)

fmt.Println(dataXX)
// Add records.

for _, value := range truthSetFileNames {
filepath := fmt.Sprintf("%s%s", homePath, value)
Expand All @@ -218,24 +177,18 @@ func main() {
}
}

customer1070Entity, err := szEngine.GetEntityByRecordID(ctx, "CUSTOMERS", "1070", senzing.SzEntityIncludeRecordSummary)
testErr(err)
// View results.

customer1070EntityPretty, err := prettyJSON(customer1070Entity)
customer1070Entity, err := szEngine.GetEntityByRecordID(ctx, "CUSTOMERS", "1070", senzing.SzEntityIncludeRecordSummary)
testErr(err)
fmt.Println(asPrettyJSON(customer1070Entity))

fmt.Println(customer1070EntityPretty)
searchProfile := ""
searchQuery := `{
"name_full": "robert smith",
"date_of_birth": "11/12/1978"
}`

searchResult, err := szEngine.SearchByAttributes(ctx, searchQuery, searchProfile, senzing.SzSearchByAttributesDefaultFlags)
testErr(err)

searchResultPretty, err := prettyJSON(searchResult)
testErr(err)

fmt.Println(searchResultPretty)
fmt.Println(asPrettyJSON(searchResult))
}
99 changes: 20 additions & 79 deletions rootfs/examples/notebooks/go/senzing_load_truth_sets.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,29 @@
"\t\"google.golang.org/grpc\"\n",
"\t\"google.golang.org/grpc/credentials/insecure\"\n",
" \"github.com/senzing-garage/sz-sdk-go-grpc/szabstractfactory\"\n",
" \"github.com/senzing-garage/sz-sdk-go/senzing\" \n",
" \"github.com/senzing-garage/sz-sdk-go/senzing\"\n",
")\n",
"\n",
"type DataSourceKey struct {\n",
" Data_Source string\n",
" Data_Source string\n",
"}\n",
"\n",
"type Record struct {\n",
" Data_Source string\n",
" Record_ID string\n",
" Data_Source string\n",
" Record_ID string\n",
"}\n",
"\n",
"var (\n",
" ctx = context.TODO()\n",
" err error\n",
"\tgrpcAddress = \"localhost:8261\"\n",
"\tgrpcAddress = \"localhost:8261\"\n",
" jsonDataSource DataSourceKey\n",
" jsonRecord Record\n",
" homePath = \"./\"\n",
" jsonRecord Record \n",
" szConfig senzing.SzConfig\n",
" truthSetURLPrefix = \"https://raw.githubusercontent.com/Senzing/truth-sets/refs/heads/main/truthsets/demo/\"\n",
" truthSetFileNames = []string{\"customers.json\", \"reference.json\", \"watchlist.json\"}\n",
") "
")"
]
},
{
Expand Down Expand Up @@ -149,22 +149,10 @@
" return result\n",
"}\n",
"\n",
"func getSzDiagnostic(ctx context.Context, szAbstractFactory senzing.SzAbstractFactory) senzing.SzDiagnostic {\n",
" result, err := szAbstractFactory.CreateDiagnostic(ctx)\n",
" testErr(err)\n",
" return result\n",
"}\n",
"\n",
"func getSzEngine(ctx context.Context, szAbstractFactory senzing.SzAbstractFactory) senzing.SzEngine {\n",
" result, err := szAbstractFactory.CreateEngine(ctx)\n",
" testErr(err)\n",
" return result\n",
"}\n",
"\n",
"func getSzProduct(ctx context.Context, szAbstractFactory senzing.SzAbstractFactory) senzing.SzProduct {\n",
" result, err := szAbstractFactory.CreateProduct(ctx)\n",
" testErr(err)\n",
" return result\n",
"}"
]
},
Expand Down Expand Up @@ -263,12 +251,12 @@
"metadata": {},
"outputs": [],
"source": [
"func prettyJSON(str string) (string, error) {\n",
" var prettyJSON bytes.Buffer\n",
" if err := json.Indent(&prettyJSON, []byte(str), \"\", \" \"); err != nil {\n",
" return \"\", err\n",
" }\n",
" return prettyJSON.String(), nil\n",
"func asPrettyJSON(str string) string {\n",
"\tvar prettyJSON bytes.Buffer\n",
"\tif err := json.Indent(&prettyJSON, []byte(str), \"\", \" \"); err != nil {\n",
"\t\treturn str\n",
"\t}\n",
"\treturn prettyJSON.String()\n",
"}"
]
},
Expand Down Expand Up @@ -377,8 +365,7 @@
"outputs": [],
"source": [
"var szConfig = getSzConfig(ctx, szAbstractFactory)\n",
"var szConfigManager = getSzConfigManager(ctx, szAbstractFactory)\n",
"var szDiagnostic = getSzDiagnostic(ctx, szAbstractFactory)"
"var szConfigManager = getSzConfigManager(ctx, szAbstractFactory)"
]
},
{
Expand Down Expand Up @@ -422,40 +409,8 @@
"err = szConfigManager.ReplaceDefaultConfigID(ctx, oldConfigID, newConfigID)\n",
"testErr(err)\n",
"\n",
"szAbstractFactory.Reinitialize(ctx, newConfigID)"
]
},
{
"cell_type": "markdown",
"id": "2ffc09e3-dc87-4fc0-9b01-63f3d8885661",
"metadata": {},
"source": [
"TODO: Remove after debugging."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9c843e84-cc89-460c-9036-641d78218c6a",
"metadata": {},
"outputs": [],
"source": [
"var szEngine = getSzEngine(ctx, szAbstractFactory)\n",
"\n",
"%%\n",
"defaultConfigID, err := szConfigManager.GetDefaultConfigID(ctx)\n",
"fmt.Printf(\"default: %d\\n\", defaultConfigID)\n",
"\n",
"configDefinition, err := szConfigManager.GetConfig(ctx, defaultConfigID)\n",
"testErr(err)\n",
"\n",
"configHandle, err := szConfig.ImportConfig(ctx, configDefinition)\n",
"testErr(err)\n",
"\n",
"dataXX, err := szConfig.GetDataSources(ctx, configHandle)\n",
"testErr(err)\n",
"\n",
"fmt.Println(dataXX)"
"err = szAbstractFactory.Reinitialize(ctx, newConfigID)\n",
"testErr(err)"
]
},
{
Expand All @@ -481,6 +436,8 @@
"metadata": {},
"outputs": [],
"source": [
"var szEngine = getSzEngine(ctx, szAbstractFactory)\n",
"\n",
"%%\n",
"for _, value := range truthSetFileNames {\n",
" filepath := fmt.Sprintf(\"%s%s\", homePath, value)\n",
Expand Down Expand Up @@ -528,11 +485,7 @@
"%%\n",
"customer1070Entity, err := szEngine.GetEntityByRecordID(ctx, \"CUSTOMERS\", \"1070\", senzing.SzEntityIncludeRecordSummary)\n",
"testErr(err)\n",
"\n",
"customer1070EntityPretty, err := prettyJSON(customer1070Entity)\n",
"testErr(err)\n",
"\n",
"fmt.Println(customer1070EntityPretty)"
"fmt.Println(asPrettyJSON(customer1070Entity))"
]
},
{
Expand All @@ -559,20 +512,8 @@
"\n",
"searchResult, err := szEngine.SearchByAttributes(ctx, searchQuery, searchProfile, senzing.SzSearchByAttributesDefaultFlags)\n",
"testErr(err)\n",
"\n",
"searchResultPretty, err := prettyJSON(searchResult)\n",
"testErr(err)\n",
"\n",
"fmt.Println(searchResultPretty)"
"fmt.Println(asPrettyJSON(searchResult))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f9de192d-6a15-4398-a877-c62868f587cc",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit 1b7083e

Please sign in to comment.