Beispiel #1
0
func Test_CLG_Round(t *testing.T) {
	testCases := []struct {
		Float     float64
		Precision int
		Expected  float64
	}{
		{
			Float:     3.5,
			Precision: 0,
			Expected:  4,
		},
		{
			Float:     3.4,
			Precision: 0,
			Expected:  3,
		},
		{
			Float:     3.4,
			Precision: 1,
			Expected:  3.4,
		},
		{
			Float:     3.4,
			Precision: 2,
			Expected:  3.4,
		},
		{
			Float:     3.476,
			Precision: 2,
			Expected:  3.48,
		},
		{
			Float:     -3.476,
			Precision: 2,
			Expected:  -3.48,
		},
		{
			Float:     3,
			Precision: 0,
			Expected:  3,
		},
		{
			Float:     3,
			Precision: 2,
			Expected:  3,
		},
	}

	newCLG := MustNew()

	for i, testCase := range testCases {
		f, err := newCLG.(*clg).calculate(context.MustNew(), testCase.Float, testCase.Precision)
		if err != nil {
			t.Fatal("case", i+1, "expected", nil, "got", err)
		}
		if f != testCase.Expected {
			t.Fatal("case", i+1, "expected", testCase.Expected, "got", f)
		}
	}
}
Beispiel #2
0
func Test_CLG_Input_UnknownInputSequence(t *testing.T) {
	newCLG := MustNew()
	newCtx := context.MustNew()
	newServiceCollection := testMustNewServiceCollection(t)
	newStorageCollection := testMustNewStorageCollection(t)

	// Note we do not create a record for the test input. This test is about an
	// unknown input sequence.
	newInput := "test input"

	// Set prepared storage to CLG we want to test.
	newCLG.(*clg).ServiceCollection = newServiceCollection
	newCLG.(*clg).StorageCollection = newStorageCollection

	// Execute CLG.
	err := newCLG.(*clg).calculate(newCtx, newInput)
	if err != nil {
		t.Fatal("expected", nil, "got", err)
	}

	// Check if the information ID was set to the context.
	injectedInformationID, _ := newCtx.GetInformationID()
	if injectedInformationID != "new-ID" {
		t.Fatal("expected", true, "got", false)
	}
}
Beispiel #3
0
func Test_CLG_Round_Error_NegativePrecision(t *testing.T) {
	newCLG := MustNew()
	_, err := newCLG.(*clg).calculate(context.MustNew(), 3.4465, -3)
	if !IsParseFloatSyntax(err) {
		t.Fatal("case", "expected", true, "got", false)
	}
}
Beispiel #4
0
func Test_CLG_Input_SetInformationSequenceError(t *testing.T) {
	newCLG := MustNew()
	newCtx := context.MustNew()
	newServiceCollection := testMustNewServiceCollection(t)

	// Prepare the storage connection to fake a returned error.
	newInput := "test input"
	informationIDKey := key.NewNetworkKey("information-sequence:%s:information-id", newInput)
	// Our test ID factory always returns the same ID. That way we are able to
	// check for the ID being used during the test.
	newID, err := newServiceCollection.ID().New()
	if err != nil {
		t.Fatal("expected", nil, "got", err)
	}
	informationSequenceKey := key.NewNetworkKey("information-id:%s:information-sequence", newID)

	c := redigomock.NewConn()
	c.Command("GET", "prefix:"+informationIDKey).ExpectError(redigo.ErrNil)
	c.Command("SET", "prefix:"+informationIDKey, string(newID)).Expect("OK")
	c.Command("SET", "prefix:"+informationSequenceKey, newInput).ExpectError(invalidConfigError)
	newStorageCollection := testMustNewStorageCollectionWithConn(t, c)

	// Set prepared storage to CLG we want to test.
	newCLG.(*clg).StorageCollection = newStorageCollection
	newCLG.(*clg).ServiceCollection = newServiceCollection

	// Execute CLG.
	err = newCLG.(*clg).calculate(newCtx, newInput)
	if !IsInvalidConfig(err) {
		t.Fatal("expected", true, "got", false)
	}
}
Beispiel #5
0
func Test_CLG_Input_KnownInputSequence(t *testing.T) {
	newCLG := MustNew()
	newCtx := context.MustNew()
	newStorageCollection := testMustNewStorageCollection(t)

	// Create record for the test input.
	informationID := "123"
	newInput := "test input"
	informationIDKey := key.NewNetworkKey("information-sequence:%s:information-id", newInput)
	err := newStorageCollection.General().Set(informationIDKey, informationID)
	if err != nil {
		t.Fatal("expected", nil, "got", err)
	}

	// Set prepared storage to CLG we want to test.
	newCLG.(*clg).StorageCollection = newStorageCollection

	// Execute CLG.
	err = newCLG.(*clg).calculate(newCtx, newInput)
	if err != nil {
		t.Fatal("expected", nil, "got", err)
	}

	// Check if the information ID was set to the context.
	injectedInformationID, _ := newCtx.GetInformationID()
	if informationID != injectedInformationID {
		t.Fatal("expected", informationID, "got", injectedInformationID)
	}
}
Beispiel #6
0
func Test_CLG_Sum(t *testing.T) {
	testCases := []struct {
		A        float64
		B        float64
		Expected float64
	}{
		{
			A:        3.5,
			B:        12.5,
			Expected: 16,
		},
		{
			A:        35.5,
			B:        14.5,
			Expected: 50,
		},
		{
			A:        -3.5,
			B:        7.5,
			Expected: 4,
		},
		{
			A:        12.5,
			B:        4.5,
			Expected: 17,
		},
		{
			A:        36.5,
			B:        6.5,
			Expected: 43,
		},
		{
			A:        36,
			B:        6.5,
			Expected: 42.5,
		},
		{
			A:        99.99,
			B:        12.15,
			Expected: 112.14,
		},
		{
			A:        17,
			B:        65,
			Expected: 82,
		},
	}

	newCLG := MustNew()

	for i, testCase := range testCases {
		f := newCLG.(*clg).calculate(context.MustNew(), testCase.A, testCase.B)
		if f != testCase.Expected {
			t.Fatal("case", i+1, "expected", testCase.Expected, "got", f)
		}
	}
}
Beispiel #7
0
// DefaultConfig provides a default configuration to create a new
// network payload object by best effort.
func DefaultConfig() Config {
	newConfig := Config{
		Args:        nil,
		Context:     context.MustNew(),
		Destination: "",
		Sources:     nil,
	}

	return newConfig
}
Beispiel #8
0
func Test_CLG_IsLesser(t *testing.T) {
	testCases := []struct {
		A        float64
		B        float64
		Expected bool
	}{
		{
			A:        3.5,
			B:        3.5,
			Expected: false,
		},
		{
			A:        12.5,
			B:        3.5,
			Expected: false,
		},
		{
			A:        14.5,
			B:        35.5,
			Expected: true,
		},
		{
			A:        7.5,
			B:        -3.5,
			Expected: false,
		},
		{
			A:        4.5,
			B:        12.5,
			Expected: true,
		},
		{
			A:        65,
			B:        17,
			Expected: false,
		},
		{
			A:        17,
			B:        65,
			Expected: true,
		},
	}

	newCLG := MustNew()

	for i, testCase := range testCases {
		f := newCLG.(*clg).calculate(context.MustNew(), testCase.A, testCase.B)
		if f != testCase.Expected {
			t.Fatal("case", i+1, "expected", testCase.Expected, "got", f)
		}
	}
}
Beispiel #9
0
func Test_CLG_Subtract(t *testing.T) {
	testCases := []struct {
		A        float64
		B        float64
		Expected float64
	}{
		{
			A:        3.5,
			B:        12.5,
			Expected: -9,
		},
		{
			A:        35.5,
			B:        14.5,
			Expected: 21,
		},
		{
			A:        -3.5,
			B:        -7.5,
			Expected: 4,
		},
		{
			A:        12.5,
			B:        4.5,
			Expected: 8,
		},
		{
			A:        36.5,
			B:        6.5,
			Expected: 30,
		},
		{
			A:        11.11,
			B:        10.10,
			Expected: 1.0099999999999998,
		},
	}

	newCLG := MustNew()

	for i, testCase := range testCases {
		f := newCLG.(*clg).calculate(context.MustNew(), testCase.A, testCase.B)
		if f != testCase.Expected {
			t.Fatal("case", i+1, "expected", testCase.Expected, "got", f)
		}
	}
}
Beispiel #10
0
func Test_CLG_Multiply(t *testing.T) {
	testCases := []struct {
		A        float64
		B        float64
		Expected float64
	}{
		{
			A:        3.5,
			B:        12.5,
			Expected: 43.75,
		},
		{
			A:        35.5,
			B:        14.5,
			Expected: 514.75,
		},
		{
			A:        -3.5,
			B:        7.5,
			Expected: -26.25,
		},
		{
			A:        12.5,
			B:        4.5,
			Expected: 56.25,
		},
		{
			A:        36.5,
			B:        6.5,
			Expected: 237.25,
		},
		{
			A:        17,
			B:        65,
			Expected: 1105,
		},
	}

	newCLG := MustNew()

	for i, testCase := range testCases {
		f := newCLG.(*clg).calculate(context.MustNew(), testCase.A, testCase.B)
		if f != testCase.Expected {
			t.Fatal("case", i+1, "expected", testCase.Expected, "got", f)
		}
	}
}
Beispiel #11
0
func Test_CLG_Input_DataProperlyStored(t *testing.T) {
	newCLG := MustNew()
	newCtx := context.MustNew()
	newStorageCollection := testMustNewStorageCollection(t)
	newServiceCollection := testMustNewServiceCollection(t)

	// Note we do not create a record for the test input. This test is about an
	// unknown input sequence.
	newInput := "test input"
	// Our test ID factory always returns the same ID. That way we are able to
	// check for the ID being used during the test.
	newID, err := newServiceCollection.ID().New()
	if err != nil {
		t.Fatal("expected", nil, "got", err)
	}

	// Set prepared storage to CLG we want to test.
	newCLG.(*clg).ServiceCollection = newServiceCollection
	newCLG.(*clg).StorageCollection = newStorageCollection

	// Execute CLG.
	err = newCLG.(*clg).calculate(newCtx, newInput)
	if err != nil {
		t.Fatal("expected", nil, "got", err)
	}

	informationIDKey := key.NewNetworkKey("information-sequence:%s:information-id", newInput)
	storedID, err := newStorageCollection.General().Get(informationIDKey)
	if err != nil {
		t.Fatal("expected", nil, "got", err)
	}
	if storedID != string(newID) {
		t.Fatal("expected", newID, "got", storedID)
	}

	informationSequenceKey := key.NewNetworkKey("information-id:%s:information-sequence", newID)
	storedInput, err := newStorageCollection.General().Get(informationSequenceKey)
	if err != nil {
		t.Fatal("expected", nil, "got", err)
	}
	if storedInput != newInput {
		t.Fatal("expected", newInput, "got", storedInput)
	}
}
Beispiel #12
0
func Test_CLG_Divide(t *testing.T) {
	testCases := []struct {
		A        float64
		B        float64
		Expected float64
	}{
		{
			A:        3.5,
			B:        12.5,
			Expected: 0.28,
		},
		{
			A:        35.5,
			B:        14.5,
			Expected: 2.4482758620689653,
		},
		{
			A:        -3.5,
			B:        7.5,
			Expected: -0.4666666666666667,
		},
		{
			A:        12.5,
			B:        4.5,
			Expected: 2.7777777777777777,
		},
		{
			A:        36.5,
			B:        6.5,
			Expected: 5.615384615384615,
		},
	}

	newCLG := MustNew()

	for i, testCase := range testCases {
		f := newCLG.(*clg).calculate(context.MustNew(), testCase.A, testCase.B)
		if f != testCase.Expected {
			t.Fatal("case", i+1, "expected", testCase.Expected, "got", f)
		}
	}
}
Beispiel #13
0
func Test_CLG_Input_IDServiceError(t *testing.T) {
	newCLG := MustNew()
	newCtx := context.MustNew()
	newStorageCollection := testMustNewStorageCollection(t)
	newServiceCollection := testMustNewErrorServiceCollection(t)

	// Note we do not create a record for the test input. This test is about an
	// unknown input sequence.
	newInput := "test input"

	// Set prepared storage to CLG we want to test.
	newCLG.(*clg).ServiceCollection = newServiceCollection
	newCLG.(*clg).StorageCollection = newStorageCollection

	// Execute CLG.
	err := newCLG.(*clg).calculate(newCtx, newInput)
	if !IsInvalidConfig(err) {
		t.Fatal("expected", true, "got", false)
	}
}
Beispiel #14
0
func Test_CLG_Input_GetInformationIDError(t *testing.T) {
	newCLG := MustNew()
	newCtx := context.MustNew()

	newInput := "test input"
	informationIDKey := key.NewNetworkKey("information-sequence:%s:information-id", newInput)

	// Prepare the storage connection to fake a returned error.
	c := redigomock.NewConn()
	c.Command("GET", "prefix:"+informationIDKey).ExpectError(invalidConfigError)
	newStorageCollection := testMustNewStorageCollectionWithConn(t, c)

	// Set prepared storage to CLG we want to test.
	newCLG.SetStorageCollection(newStorageCollection)

	// Execute CLG.
	err := newCLG.(*clg).calculate(newCtx, newInput)
	if !IsInvalidConfig(err) {
		t.Fatal("expected", true, "got", false)
	}
}
Beispiel #15
0
func Test_CLG_IsBetween(t *testing.T) {
	testCases := []struct {
		N        float64
		Min      float64
		Max      float64
		Expected bool
	}{
		{
			N:        1,
			Min:      2,
			Max:      4,
			Expected: false,
		},
		{
			N:        2,
			Min:      2,
			Max:      4,
			Expected: true,
		},
		{
			N:        3,
			Min:      2,
			Max:      4,
			Expected: true,
		},
		{
			N:        4,
			Min:      2,
			Max:      4,
			Expected: true,
		},
		{
			N:        5,
			Min:      2,
			Max:      4,
			Expected: false,
		},
		{
			N:        35,
			Min:      -13,
			Max:      518,
			Expected: true,
		},
		{
			N:        -87,
			Min:      -413,
			Max:      -18,
			Expected: true,
		},
		{
			N:        -7,
			Min:      -413,
			Max:      -18,
			Expected: false,
		},
		{
			N:        -987,
			Min:      -413,
			Max:      -18,
			Expected: false,
		},
		{
			N:        1.8,
			Min:      2.34,
			Max:      4.944,
			Expected: false,
		},
		{
			N:        2.334,
			Min:      2.2,
			Max:      4.1,
			Expected: true,
		},
		{
			N:        3.9,
			Min:      2.003,
			Max:      4,
			Expected: true,
		},
		{
			N:        4,
			Min:      2.22,
			Max:      4.83,
			Expected: true,
		},
	}

	newCLG := MustNew()

	for i, testCase := range testCases {
		b := newCLG.(*clg).calculate(context.MustNew(), testCase.N, testCase.Min, testCase.Max)
		if b != testCase.Expected {
			t.Fatal("case", i+1, "expected", testCase.Expected, "got", b)
		}
	}
}
Beispiel #16
0
func (n *network) InputHandler(CLG systemspec.CLG, textInput objectspec.TextInput) error {
	// In case the text request defines the echo flag, we overwrite the given CLG
	// directly to the output CLG. This will cause the created network payload to
	// be forwarded to the output CLG without indirection. Note that this should
	// only be used for testing purposes to bypass more complex neural network
	// activities to directly respond with the received input.
	if textInput.GetEcho() {
		var ok bool
		CLG, ok = n.CLGs["output"]
		if !ok {
			return maskAnyf(clgNotFoundError, "name: %s", "output")
		}
	}

	// Create new IDs for the new CLG tree and the input CLG.
	clgTreeID, err := n.Service().ID().New()
	if err != nil {
		return maskAny(err)
	}
	behaviourID, err := n.Service().ID().New()
	if err != nil {
		return maskAny(err)
	}

	// Create a new context and adapt it using the information of the current scope.
	ctx := context.MustNew()
	ctx.SetBehaviourID(string(behaviourID))
	ctx.SetCLGName(CLG.GetName())
	ctx.SetCLGTreeID(string(clgTreeID))
	ctx.SetExpectation(textInput.GetExpectation())
	ctx.SetSessionID(textInput.GetSessionID())

	// We transform the received text request to a network payload to have a
	// conventional data structure within the neural network.
	newNetworkPayloadConfig := networkpayload.DefaultConfig()
	newNetworkPayloadConfig.Args = []reflect.Value{reflect.ValueOf(textInput.GetInput())}
	newNetworkPayloadConfig.Context = ctx
	newNetworkPayloadConfig.Destination = behaviourID
	newNetworkPayloadConfig.Sources = []string{n.GetID()}
	newNetworkPayload, err := networkpayload.New(newNetworkPayloadConfig)
	if err != nil {
		return maskAny(err)
	}

	// Write the new CLG tree ID to reference the input CLG ID and add the CLG
	// tree ID to the new context.
	firstBehaviourIDKey := key.NewNetworkKey("clg-tree-id:%s:first-behaviour-id", clgTreeID)
	err = n.Storage().General().Set(firstBehaviourIDKey, string(behaviourID))
	if err != nil {
		return maskAny(err)
	}

	// Write the transformed network payload to the queue.
	eventKey := key.NewNetworkKey("event:network-payload")
	b, err := json.Marshal(newNetworkPayload)
	if err != nil {
		return maskAny(err)
	}
	err = n.Storage().General().PushToList(eventKey, string(b))
	if err != nil {
		return maskAny(err)
	}

	return nil
}