/////////////////////////////////////////////////////////////////////// #region Private Members private void Vote( PolicyDecision decision ) { switch (decision) { case PolicyDecision.Undecided: { Interlocked.Increment(ref undecidedCount); break; } case PolicyDecision.Denied: { Interlocked.Increment(ref deniedCount); break; } case PolicyDecision.Approved: { Interlocked.Increment(ref approvedCount); break; } } }
protected override PolicyDecision <ActionProbability[]> MapContext(VowpalWabbit vw, string context) { using (var vwJson = new VowpalWabbitJsonSerializer(vw)) using (VowpalWabbitExampleCollection vwExample = vwJson.ParseAndCreate(context)) { if (this.developmentMode) { Trace.TraceInformation("Example Context: '{0}'", vwExample.VowpalWabbitString); } var vwPredictions = vwExample.Predict(VowpalWabbitPredictionType.ActionProbabilities); // VW multi-label predictions are 0-based var ap = vwPredictions .Select(a => new ActionProbability { Action = (int)(a.Action + 1), Probability = a.Score }) .ToArray(); var state = new VWState { ModelId = vw.Native.ID }; return(PolicyDecision.Create(ap, state)); } }
protected override PolicyDecision <ActionProbability[]> MapContext(VowpalWabbit <TContext> vw, TContext context) { if (this.developmentMode) { using (var serializer = vw.Serializer.Create(vw.Native)) { Trace.TraceInformation("Example Context: {0}", serializer.SerializeToString(context)); } } var vwPredictions = vw.Predict(context, VowpalWabbitPredictionType.ActionProbabilities); // VW multi-label predictions are 0-based var ap = vwPredictions .Select(a => new ActionProbability { Action = (int)(a.Action + 1), Probability = a.Score }) .ToArray(); var state = new VWState { ModelId = vw.Native.ID }; return(PolicyDecision.Create(ap, state)); }
public PolicyDecision <ActionProbability[]> MapContext(T context) { return(PolicyDecision.Create(Enumerable.Range(1, this.numActionsFunc(context)) .Select(a => new ActionProbability { Action = a, Probability = a == 1 ? 1f : 0 }) .ToArray())); }
public PolicyDecision GetPolicyDecision(IPrincipal principal, object securable) { var retVal = new PolicyDecision(securable, new System.Collections.Generic.List <PolicyDecisionDetail>() { new PolicyDecisionDetail(PermissionPolicyIdentifiers.AccessAuditLog, PolicyGrantType.Grant) }); return(retVal); }
/// <summary> /// Add policy authorization to the audit /// </summary> public static AuditData WithPolicyAuthorization(this AuditData me, PolicyDecision policy) { me.AuditableObjects.AddRange(policy.Details.Select(o => new AuditableObject() { IDTypeCode = AuditableObjectIdType.Uri, Role = AuditableObjectRole.SecurityGranularityDefinition, Type = AuditableObjectType.SystemObject, LifecycleType = AuditableObjectLifecycle.Verification, ObjectId = o.PolicyId })); return(me); }
/// <summary> /// Determines the action to take for a given context. /// This implementation should be thread-safe if multithreading is needed. /// </summary> /// <param name="vw">The Vowpal Wabbit instance to use.</param> /// <param name="context">A user-defined context for the decision.</param> /// <returns>A decision tuple containing the index of the action to take (1-based), and the Id of the model or policy used to make the decision. /// Can be null if the Policy is not ready yet (e.g. model not loaded).</returns> protected override PolicyDecision <int> MapContext(VowpalWabbit vw, string context) { using (var vwJson = new VowpalWabbitJsonSerializer(vw)) using (VowpalWabbitExampleCollection vwExample = vwJson.ParseAndCreate(context)) { var action = (int)vwExample.Predict(VowpalWabbitPredictionType.CostSensitive); var state = new VWState { ModelId = vw.ID }; return(PolicyDecision.Create(action, state)); } }
/// <summary> /// Determines the action to take for a given context. /// This implementation should be thread-safe if multithreading is needed. /// </summary> /// <param name="vw">The Vowpal Wabbit instance to use.</param> /// <param name="context">A user-defined context for the decision.</param> /// <returns>A decision tuple containing the index of the action to take (1-based), and the Id of the model or policy used to make the decision. /// Can be null if the Policy is not ready yet (e.g. model not loaded).</returns> protected override PolicyDecision <int[]> MapContext(VowpalWabbit vw, string context) { using (var vwJson = new VowpalWabbitJsonSerializer(vw)) using (VowpalWabbitExampleCollection vwExample = vwJson.ParseAndCreate(context)) { ActionScore[] vwMultilabelPredictions = vwExample.Predict(VowpalWabbitPredictionType.ActionProbabilities); // VW multi-label predictions are 0-based var actions = vwMultilabelPredictions.Select(a => (int)a.Action + 1).ToArray(); var state = new VWState { ModelId = vw.ID }; return(PolicyDecision.Create(actions, state)); } }
protected override PolicyDecision <int> MapContext(VowpalWabbit <TContext> vw, TContext context) { if (this.developmentMode) { using (var serializer = vw.Serializer.Create(vw.Native)) { Trace.TraceInformation("Example Context: {0}", serializer.SerializeToString(context)); } } var action = (int)vw.Predict(context, VowpalWabbitPredictionType.CostSensitive); var state = new VWState { ModelId = vw.Native.ID }; return(PolicyDecision.Create(action, state)); }
/////////////////////////////////////////////////////////////////////// private PolicyContext( PolicyFlags flags, AssemblyName assemblyName, string typeName, IExecute execute, ArgumentList arguments, IScript script, string fileName, byte[] bytes, string text, Encoding encoding, byte[] hashValue, string hashAlgorithmName, IClientData clientData, IPlugin plugin, PolicyDecision originalDecision ) : this() { this.flags = flags; this.assemblyName = assemblyName; this.typeName = typeName; this.execute = execute; this.arguments = arguments; this.script = script; this.fileName = fileName; this.bytes = bytes; this.text = text; this.encoding = encoding; this.hashValue = hashValue; this.hashAlgorithmName = hashAlgorithmName; this.clientData = clientData; this.plugin = plugin; this.originalDecision = originalDecision; // // NOTE: *WARNING* Take the original decision into account. With // the current logic, if the original decision is "denied" // then any later votes do not matter. // Vote(this.originalDecision); }
protected override PolicyDecision <int[]> MapContext(VowpalWabbit <TContext> vw, TContext context) { if (this.developmentMode) { using (var serializer = vw.Serializer.Create(vw.Native)) { Trace.TraceInformation("Example Context: {0}", serializer.SerializeToString(context)); } } ActionScore[] vwMultilabelPredictions = vw.Predict(context, VowpalWabbitPredictionType.ActionScore); // VW multi-label predictions are 0-based var actions = vwMultilabelPredictions.Select(a => (int)a.Action + 1).ToArray(); var state = new VWState { ModelId = vw.Native.ID }; return(PolicyDecision.Create(actions, state)); }
protected override bool OnDecidePolicy(PolicyDecision decision, PolicyDecisionType decision_type) { if (decision_type != PolicyDecisionType.NavigationAction) { decision.Use(); return(false); } string uri = (decision as NavigationPolicyDecision).Request.Uri; if (uri.Equals("file:///")) { decision.Use(); return(false); } LinkClicked(uri); decision.Ignore(); return(true); }
/////////////////////////////////////////////////////////////////////// #region Static "Factory" Members public static PolicyContext Create( PolicyFlags flags, AssemblyName assemblyName, string typeName, IExecute execute, ArgumentList arguments, IScript script, string fileName, byte[] bytes, string text, Encoding encoding, byte[] hashValue, string hashAlgorithmName, IClientData clientData, IPlugin plugin, PolicyDecision originalDecision ) { return(new PolicyContext( flags, assemblyName, typeName, execute, arguments, script, fileName, bytes, text, encoding, hashValue, hashAlgorithmName, clientData, plugin, originalDecision)); }
protected override bool OnDecidePolicy (PolicyDecision decision, PolicyDecisionType decision_type) { if (decision_type != PolicyDecisionType.NavigationAction) { decision.Use (); return false; } string uri = (decision as NavigationPolicyDecision).Request.Uri; if (uri.Equals ("file:///")) { decision.Use (); return false; } LinkClicked (uri); decision.Ignore (); return true; }
/////////////////////////////////////////////////////////////////////// public static bool IsDenied( PolicyDecision decision ) { return(decision == PolicyDecision.Denied); }
PolicyDecision <int> IContextMapper <UserContext, int> .MapContext(UserContext context) { int chosenAction = (int)Math.Round(context.Features.Sum(f => f.Value) / context.Features.Count + 1); return(PolicyDecision.Create(chosenAction)); }
/////////////////////////////////////////////////////////////////////// public static bool IsApproved( PolicyDecision decision ) { return(decision == PolicyDecision.Approved); }
/////////////////////////////////////////////////////////////////////// public static bool IsUndecided( PolicyDecision decision ) { return(decision == PolicyDecision.Undecided); }
/// <summary> /// Initializes a new instance of the <see cref="PolicyViolationException"/> class. /// </summary> /// <param name="principal">The principal which attempted the action</param> /// <param name="decision">The decision of the policy which caused the exception</param> public PolicyViolationException(IPrincipal principal, PolicyDecision decision) { this.PolicyId = decision.Details.First(p => p.Outcome == decision.Outcome).PolicyId; this.Detail = decision; this.PolicyDecision = principal.Identity.Name == "ANONYMOUS" ? PolicyGrantType.Elevate : decision.Outcome; }
public Task <PolicyDecision <int[]> > MapContextAsync(int[] context) { return(Task.FromResult(PolicyDecision.Create(context))); }
/////////////////////////////////////////////////////////////////////// public static bool IsNone( PolicyDecision decision ) { return(decision == PolicyDecision.None); }