public CspTest(ITestOutputHelper testOutputHelper) { _testOutputHelper = testOutputHelper; _mapColoredCsp = CspFactory.Create( new Dictionary <string, IEnumerable <ColorWrapper> > { ["SA"] = _colorsDomain.ToList(), ["WA"] = _colorsDomain.ToList(), ["NT"] = _colorsDomain.ToList(), ["Q"] = _colorsDomain.ToList(), ["NSW"] = _colorsDomain.ToList(), ["V"] = _colorsDomain.ToList(), ["T"] = _colorsDomain.ToList() }, new Dictionary <string, IEnumerable <string> > { ["SA"] = new [] { "WA", "NT", "Q", "NSW", "V" }, ["WA"] = new [] { "SA", "NT" }, ["NT"] = new [] { "WA", "Q", "SA" }, ["Q"] = new [] { "NSW", "NT", "SA" }, ["NSW"] = new [] { "Q", "V", "SA" }, ["V"] = new [] { "NSW", "SA" } }, new Func <string, ColorWrapper, string, ColorWrapper, bool>[] { DiffValuesConstraint.Eval } ); }
public CspTest(ITestOutputHelper testOutputHelper) { _testOutputHelper = testOutputHelper; var testData = SudokuTestFactory.CreateTestData(); var domains = testData.Select(v => new KeyValuePair <string, IEnumerable <Number> >(v.Key, v.Value.domains)); var relations = testData.Select(v => new KeyValuePair <string, IEnumerable <string> >(v.Key, v.Value.relations)); _sudokuCsp = CspFactory.Create( new Dictionary <string, IEnumerable <Number> >(domains), new Dictionary <string, IEnumerable <string> >(relations), new Func <string, Number, string, Number, bool>[] { DiffValuesConstraint.Eval } ); var initialConfig = SudokuTestFactory.CreateStartConfig(); // Apply initial config foreach (var varValue in initialConfig) { _sudokuCsp.AddAssignment(varValue.Key, varValue.Value).ShrinkDomainToAssignment(varValue.Key); } }
public bool Inference(Csp <T> csp, string varKey, T value, out IEnumerable <string> prunedDomains) { var prunedSet = new HashSet <string>(); foreach (var unassignedNeighbor in csp.Model.VariableRelations(varKey).Values.Where(v => !v.Assigned)) { foreach (var val in csp.Model.GetDomain(unassignedNeighbor.Key).Values.ToList()) { if (csp.Model.GetConstraints().Any(c => !c.Rule.Invoke(varKey, value, unassignedNeighbor.Key, val))) { csp.Model.Prune(unassignedNeighbor.Key, val); prunedSet.Add(unassignedNeighbor.Key); } } if (!csp.Model.GetDomain(unassignedNeighbor.Key).Values.Any()) { prunedDomains = prunedSet.ToList(); return(false); } } prunedDomains = prunedSet.ToList(); return(true); }
public IEnumerable <T> GetDomainValues(Csp <T> csp, string key) { var values = csp.Model.GetDomain(key).Values; values.Sort(); return(values); }
public bool Resolve(Csp <T> csp) { if (csp.Model.IsAllAssigned) { return(true); } var variable = _selUnVarStrategy.Next(csp); foreach (var domainValue in _domValOrdStrategy.GetDomainValues(csp, variable.Key).ToList()) { if (csp.Conflicts(variable.Key, domainValue) == 0) { csp.AddAssignment(variable.Key, domainValue); csp.Model.Suppose(variable.Key, domainValue); if (_infStrategy.Inference(csp, variable.Key, domainValue, out var pruned)) { if (Resolve(csp)) { return(true); } } foreach (var prunedDomain in pruned) { csp.Model.RestorePruned(prunedDomain); } csp.Model.RestoreGuess(variable.Key); } } csp.RemoveAssignment(variable.Key); return(false); }
public IEnumerable <T> GetDomainValues(Csp <T> csp, string key) { return(csp.Model.GetDomain(key).Values .Select(v => (v, nConflicts: csp.Conflicts(key, v))) .OrderByDescending(v => v.nConflicts) .Select(v => v.v)); }
public void PoliciesCanBeMerged() { var octopusServerPolicy = new Csp( new CspDirective("default-src", "'self'"), new CspDirective("script-src", "'self'"), new CspDirective("connect-src", "'self'"), new CspDirective("font-src", "'none'") ); var octopusDotComPolicy = new Csp( new CspDirective("script-src", "octopus.com"), new CspDirective("connect-src", "octopus.com"), new CspDirective("img-src", "octopus.com") ); var otherServicePolicy = new Csp( new CspDirective("script-src", "otherservice.com"), new CspDirective("connect-src", "otherservice.com"), new CspDirective("font-src", "otherservice.com") ); var actual = octopusServerPolicy .Union(octopusDotComPolicy) .Union(otherServicePolicy); var expected = new Csp( new CspDirective("default-src", "'self'"), new CspDirective("script-src", "'self'", "octopus.com", "otherservice.com"), new CspDirective("connect-src", "'self'", "octopus.com", "otherservice.com"), new CspDirective("font-src", "otherservice.com"), new CspDirective("img-src", "octopus.com") ); actual.Should().Be(expected); }
public Variable <T> Next(Csp <T> csp) { var vars = csp.Model.UnassignedVariables.Select(key => { var domain = csp.Model.GetDomain(key); var legalValues = domain.Pruned.Any() ? domain.Values.Count : domain.Values.Count(c => csp.Model.Conflicts(key, c) == 0); return(new { Key = key, LegalValuesCount = legalValues }); }) .GroupBy(i => i.LegalValuesCount) .OrderBy(i => i.Key) .First() .ToList(); return(csp.Model.GetVariable( vars.Count > 1 ? vars[new Random().Next(vars.Count)].Key : vars.Single().Key )); }
public void CanParseASimplePolicy() { var actual = Parser.Parse("script-src octopus.com"); var expected = new Csp( new CspDirective("script-src", "octopus.com") ); actual.Should().Be(expected); }
public SudokuResult PostSudoku(int index, [FromBody] SudokuSettings settings) { var csp = Csp.FromFile(settings); var sudokuSolver = new SudokuSolver(csp.Sudokus[index], settings); var result = sudokuSolver.SolveSudoku(); return(result); }
public void CanParseAComplexPolicy() { var actual = Parser.Parse("default-src 'self'; script-src 'self' octopus.com; connect-src octopus.com"); var expected = new Csp( new CspDirective("default-src", "'self'"), new CspDirective("script-src", "'self'", "octopus.com"), new CspDirective("connect-src", "octopus.com") ); actual.Should().Be(expected); }
private T MinConflictsValue(Csp <T> csp, string varKey) { var min = csp.Model.GetDomain(varKey) .Values .Select(v => (value: v, nConflicts: csp.Conflicts(varKey, v))) .GroupBy(i => i.nConflicts) .OrderBy(i => i.Key) .First() .ToList(); return(min.Count > 1 ? min[new Random().Next(min.Count)].value : min.Single().value); }
public Sensitive GetSensitive() { TpmPublic fromCspPublic; TpmPrivate fromCspPrivate = Csp.CspToTpm(ExportCspBlob(), out fromCspPublic); var m = new Marshaller(fromCspPrivate.buffer); ushort privSize = m.Get <UInt16>(); if (fromCspPrivate.buffer.Length != privSize + 2) { Globs.Throw("Invalid key blob"); } return(m.Get <Sensitive>()); }
bool IsValidSignatureValue(string signatureValue) { byte[] sigVal = Convert.FromBase64String(signatureValue); XmlNode signedInfo = Doc.GetElementsByTagName("ds:SignedInfo")[0]; Hashtable ns = RetrieveNameSpaces((XmlElement)signedInfo); InsertNamespacesIntoElement(ns, (XmlElement)signedInfo); Stream signedInfoStream = CanonicalizeNode(signedInfo); SHA256 sha256 = SHA256.Create(); byte[] hashedSignedInfo = sha256.ComputeHash(signedInfoStream); string oid = CryptoConfig.MapNameToOID("SHA256"); return(Csp.VerifyHash(hashedSignedInfo, oid, sigVal)); }
public CspTest(ITestOutputHelper testOutputHelper) { _testOutputHelper = testOutputHelper; var testData = SchoolCalendarTestFactory.CreateTestData(); var domains = testData.Select(v => new KeyValuePair <string, IEnumerable <Teacher> >(v.Key, v.Value.domains)); var relations = testData.Select(v => new KeyValuePair <string, IEnumerable <string> >(v.Key, v.Value.relations)); _schoolCalendarCsp = CspFactory.Create( new Dictionary <string, IEnumerable <Teacher> >(domains), new Dictionary <string, IEnumerable <string> >(relations), new Func <string, Teacher, string, Teacher, bool>[] { NextPreviousHoursConstraint.Eval, SimultaneouslyHoursConstraint.Eval } ); }
private void PrintPlan(Csp <TruckCspValue> model) { using var ms = new MemoryStream(); using var writer = new StreamWriter(ms); var table = new ConsoleTable("Week Day", "Travel 1", "Travel 2", "Travel 3") { Options = { EnableCount = false, OutputTo = writer } }; var travelsByDay = model.Status.GroupBy(k => DemoFleetDataFactory.DecodeDay(k.Key).ToUpper()).ToList(); foreach (var day in travelsByDay) { var cells = day.Select(i => $"{i.Key}: {i.Value.ModelCode}").Cast <object>().ToList(); cells.Insert(0, day.Key); table.AddRow(cells.ToArray()); } table.Write(); var trucks = model.Status.Values .Select(m => m.ModelCode) .Distinct() .Select(code => (Code: code, Assignement: model.Status.Values.First(v => v.ModelCode.Equals(code)).Assigned)); foreach (var truck in trucks) { writer.WriteLine($"Truck: {truck.Code}, Total travels: {truck.Assignement}"); } writer.Flush(); ms.Seek(0, SeekOrigin.Begin); DemoLogger.InfLog(Environment.NewLine + Encoding.UTF8.GetString(ms.ToArray())); }
public bool Resolve(Csp <T> csp) { foreach (var varKey in csp.Model.VariablesKeys) { var v = MinConflictsValue(csp, varKey); csp.AddAssignment(varKey, v); } for (var i = 0; i < MaxLoop; i++) { var conflicted = csp.Model.ConflictedVariables.Select(v => v.Key).ToList(); if (!conflicted.Any()) { return(true); } var nextVarKey = conflicted[new Random().Next(conflicted.Count)]; var nextVal = MinConflictsValue(csp, nextVarKey); csp.AddAssignment(nextVarKey, nextVal); } return(false); }
public IEnumerable <string> GetFiles() { return(Csp.GetSudokuNames()); }
public void StyleSheetsRuleTest() { Assert.Equal("style-src 'self' 'unsafe-inline'", Csp.GetCspStyleSheetRule()); }
public void FontRuleTest() { Assert.Equal("font-src 'self'", Csp.GetCspFontsRule()); }
public void FrameRuleTest() { Assert.Equal("frame-src 'self'", Csp.GetCspFrameRule()); }
public IEnumerable <T> GetDomainValues(Csp <T> csp, string key) { return(csp.Model.GetDomain(key).Values); }
public bool Inference(Csp <T> csp, string varKey, T value, out IEnumerable <string> prunedDomains) { prunedDomains = new List <string>(); return(true); }
private static byte[] GenerateRandomBytes(int bytesNumber) { byte[] buffer = new byte[bytesNumber]; Csp.GetBytes(buffer); return(buffer); }
public Variable <T> Next(Csp <T> csp) { return(csp.Model.GetFirstVariable(v => !v.Assigned)); }
public void ScriptsRuleTest() { Assert.Equal("script-src 'self' 'sha256-NIDT1bUKf5Ez3feQSP65cgv5YGrWo7EEQjUGoP7TnLs='", Csp.GetCspScriptRule()); }
public void ImgRuleTest() { Assert.Equal("img-src 'self'", Csp.GetCspImagesRule()); }
public void CspRuleTest() { Assert.Equal("default-src 'self'; style-src 'self' 'unsafe-inline'; frame-src 'self'; script-src 'self' 'sha256-NIDT1bUKf5Ez3feQSP65cgv5YGrWo7EEQjUGoP7TnLs='; img-src 'self'; font-src 'self'", Csp.GetCspString()); }