Contexts#

Evidence class#

Represent one evidence pair of Name and Value

public class Evidence
{
    public string Name { get; set; }

    public string Value { get; set; }

    public override int GetHashCode()
    {
        unchecked
        {
            return (Name != null ? (Name.GetHashCode() * 397) : 0) ^ (Value != null ? Value.GetHashCode() : 0);
        }
    }
}

Context class#

Represent base class for context - Rights or Processing context. Both of them is a list of Evidence.

public class Context
{
    public List<Evidence> Evidences { get; set; }

    public override int GetHashCode()
    {
        unchecked
        {
            // ReSharper disable once NonReadonlyMemberInGetHashCode
            return Evidences.Select(v => v.GetHashCode()).Aggregate((total, next) => (total * 397) ^ next);
        }
    }
}

RightsContext class#

public class RightsContext : Context
{
}

ProcessingContext class#

Adding additional properties to EngineContext in the case of ProcessingContext. AllowCache property - indicate if for such ProcessingContext allowed caching of transformed values. (For example, in case of probabilistic transformation we shouldn't cache values).

public class ProcessingContext : Context
{
}

IRPSEngineContextProvider interface#

Interface for a provider of rights and processing contexts.

public interface IRPSEngineContextProvider
{
    void Initialize();

    bool TryGetRightsContext(string contextKey, out RightsContext rightsContext);

    bool TryGetProcessingContext(string contextKey, out ProcessingContext processingContext);
}

RPSEngineContextProviderBase class#

Abstract base class for ContextProvider. Implement base functionality for retrieve rights and processing contexts from the dictionary by key. Contains two abstract methods: GetRightsContexts and GetProcessingContexts to implement into specific provider (File, DB, etc).

public abstract class RPSEngineContextProviderBase : IRPSEngineContextProvider
{
    private ConcurrentDictionary<string, RightsContext> _rightsContextsByKey;
    private ConcurrentDictionary<string, ProcessingContext> _processingContextsByKey;

    public void Initialize()
    {
        _rightsContextsByKey = new ConcurrentDictionary<string, RightsContext>(GetRightsContexts());
        _processingContextsByKey = new ConcurrentDictionary<string, ProcessingContext>(GetProcessingContexts());
    }

    public bool TryGetRightsContext(string contextKey, out RightsContext rightsContext)
    {
        if (_rightsContextsByKey == null)
            throw new InvalidOperationException("Context provider is not initialized");

        return _rightsContextsByKey.TryGetValue(contextKey, out rightsContext);
    }

    public bool TryGetProcessingContext(string contextKey, out ProcessingContext processingContext)
    {
        if (_processingContextsByKey == null)
            throw new InvalidOperationException("Context provider is not initialized");

        return _processingContextsByKey.TryGetValue(contextKey, out processingContext);
    }

    protected abstract IReadOnlyDictionary<string, RightsContext> GetRightsContexts();

    protected abstract IReadOnlyDictionary<string, ProcessingContext> GetProcessingContexts();
}

RPSEngineContextFileProvider class#

Implementation of RPSEngineContextProviderBase based on separate JSON files for rights context and processing contexts.

public class RPSEngineContextJsonFileProvider : RPSEngineContextProviderBase
{
    private readonly string _rightsContextsFilePath;
    private readonly string _processingContextsFilePath;

    public RPSEngineContextJsonFileProvider(string rightsContextsFilePath, string processingContextsFilePath)
    {
        _rightsContextsFilePath = rightsContextsFilePath;
        _processingContextsFilePath = processingContextsFilePath;
    }

    protected override IReadOnlyDictionary<string, RightsContext> GetRightsContexts()
        => GetDictionaryFromJsonFile<string, RightsContext>(_rightsContextsFilePath);

    protected override IReadOnlyDictionary<string, ProcessingContext> GetProcessingContexts()
        => GetDictionaryFromJsonFile<string, ProcessingContext>(_processingContextsFilePath);

    private static IReadOnlyDictionary<TKey, TValue> GetDictionaryFromJsonFile<TKey, TValue>(string filePath)
        => JsonConvert.DeserializeObject<Dictionary<TKey, TValue>>(File.ReadAllText(filePath));
}