/// <summary> /// Checks whether the given ProvidedSocket and SocketUse definitions have compatible socket types /// </summary> internal static bool CanTypeConnect(ProvidedSocket providedSocket, Definitions.SocketUse socketUseDef) { if (providedSocket.Types == null || socketUseDef.Types == null) { return(false); } foreach (SocketType t in socketUseDef.Types) { var st = t as SingleSocketType; if (st != null) { if (providedSocket.Types.Any(pst => pst == st.Value)) { return(true); } } var ct = t as CompositeType; if (ct != null) { //Can all the types in the composite type by found in the provided socket? if (ct.Types.All(cst => providedSocket.Types.Any(pst => pst == cst.Value))) { return(true); } } } return(false); }
public static Microsoft.Gadgeteer.Designer.Definitions.SocketUse CreateSocketUseDef(string label, IEnumerable <string> singleValues, IEnumerable <IEnumerable <string> > compositeValues) { var su = new Microsoft.Gadgeteer.Designer.Definitions.SocketUse(); su.TypesLabel = label; su.Types = new Collection <SocketType>(); if (singleValues != null) { foreach (var value in singleValues) { su.Types.Add(new SingleSocketType() { Value = value }); } } if (compositeValues != null) { foreach (var cType in compositeValues) { var c = new CompositeType(); c.Types = new Collection <SingleSocketType>(); foreach (var value in cType) { c.Types.Add(new SingleSocketType() { Value = value }); } } } return(su); }
public void CanConnectTest() { var providedSocket = new ProvidedSocket(); var socketUse = new Microsoft.Gadgeteer.Designer.Definitions.SocketUse(); Assert.IsFalse(Microsoft.Gadgeteer.Designer.SocketUse.CanTypeConnect(providedSocket, socketUse), "null collections"); socketUse.Types = new Collection <SocketType>(); socketUse.Types.Add(new SingleSocketType() { Value = "A" }); socketUse.Types.Add(new SingleSocketType() { Value = "B" }); var composite = new CompositeType(); composite.Types = new Collection <SingleSocketType>(); composite.Types.Add(new SingleSocketType() { Value = "Z" }); composite.Types.Add(new SingleSocketType() { Value = "Y" }); socketUse.Types.Add(composite); Assert.IsFalse(Microsoft.Gadgeteer.Designer.SocketUse.CanTypeConnect(providedSocket, socketUse), "Tried to connect nothing"); providedSocket.Types = new Collection <string>(); Assert.IsFalse(Microsoft.Gadgeteer.Designer.SocketUse.CanTypeConnect(providedSocket, socketUse), "Empty collection"); providedSocket.Types.Add("A"); Assert.IsTrue(Microsoft.Gadgeteer.Designer.SocketUse.CanTypeConnect(providedSocket, socketUse), "Connect to 'A'"); providedSocket.Types.Clear(); providedSocket.Types.Add("a"); Assert.IsFalse(Microsoft.Gadgeteer.Designer.SocketUse.CanTypeConnect(providedSocket, socketUse), "Connect to 'a'"); providedSocket.Types.Clear(); providedSocket.Types.Add("C"); Assert.IsFalse(Microsoft.Gadgeteer.Designer.SocketUse.CanTypeConnect(providedSocket, socketUse), "Tried to connect to 'C'"); providedSocket.Types.Clear(); providedSocket.Types.Add("B"); providedSocket.Types.Add("C"); Assert.IsTrue(Microsoft.Gadgeteer.Designer.SocketUse.CanTypeConnect(providedSocket, socketUse), "Connect to 'B' or 'C'"); providedSocket.Types.Clear(); providedSocket.Types.Add("B"); providedSocket.Types.Add("Z"); Assert.IsTrue(Microsoft.Gadgeteer.Designer.SocketUse.CanTypeConnect(providedSocket, socketUse), "Connect to single with part of composite"); providedSocket.Types.Clear(); providedSocket.Types.Add("Z"); Assert.IsFalse(Microsoft.Gadgeteer.Designer.SocketUse.CanTypeConnect(providedSocket, socketUse), "try to connect to composite"); providedSocket.Types.Clear(); providedSocket.Types.Add("Y"); providedSocket.Types.Add("Z"); Assert.IsTrue(Microsoft.Gadgeteer.Designer.SocketUse.CanTypeConnect(providedSocket, socketUse), "Connect to composite"); }