<commit_before>-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.

local class = require('class')
local check = require('check')

local log = haka.log_section("core")

local module = {}

local Policy = class.class('Policy')

local function criteria_sort(criteria)
	return criteria
end

function Policy.method:__init(name, actions)
	self.name = name
	self.actions = actions
	self.policies = {}
end

function Policy.method:insert(name, criteria, actions)
	if name then
		log.info("register policy '%s' on '%s'", name, self.name)
	else
		log.info("register policy on '%s'", self.name)
	end
	-- JIT Optim
	local linear_criteria = {}
	for index, criterion in pairs(criteria) do
		if not class.isa(criterion, module.Criterion) then
			criterion = module.value(criterion)
		end
		table.insert(linear_criteria, {name = index, value = criterion})
	end
	table.insert(self.policies, {name = name, criteria = linear_criteria, actions = actions})
end

module.Criterion = class.class('Criterion')

function module.Criterion.__class_init(self, cls)
	self.super:__class_init(cls)

	module[cls.name] = {}
	setmetatable(module[cls.name], {
		__class = cls,
		__call = function (table, ...)
			local self = cls:new()
			self:init(...)
			return self
		end
	})
end

function module.Criterion.method:init()
	self._learn = nil
end

function module.Criterion.method:compare()
	error("not implemented")
end

function module.Criterion.method:learn()
	error("not implemented")
end

local ValueCriterion = class.class('value', module.Criterion)

function ValueCriterion.method:init(value)
	self._value = value
end

function ValueCriterion.method:compare(value)
	if value == nil then
		return false
	end
	return self._value == value
end

local SetCriterion = class.class('set', module.Criterion)

function SetCriterion.method:__init(list)
	self._set = {}
end

function SetCriterion.method:init(list)
	for _, m in pairs(list) do
		self._set[m] = true
	end
end

function SetCriterion.method:compare(value)
	if value == nil then
		return false<commit_msg>Initialize criterion in a non learning state<commit_after>-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.

local class = require('class')
local check = require('check')

local log = haka.log_section("core")

local module = {}

local Policy = class.class('Policy')

local function criteria_sort(criteria)
	return criteria
end

function Policy.method:__init(name, actions)
	self.name = name
	self.actions = actions
	self.policies = {}
end

function Policy.method:insert(name, criteria, actions)
	if name then
		log.info("register policy '%s' on '%s'", name, self.name)
	else
		log.info("register policy on '%s'", self.name)
	end
	-- JIT Optim
	local linear_criteria = {}
	for index, criterion in pairs(criteria) do
		if not class.isa(criterion, module.Criterion) then
			criterion = module.value(criterion)
		end
		table.insert(linear_criteria, {name = index, value = criterion})
	end
	table.insert(self.policies, {name = name, criteria = linear_criteria, actions = actions})
end

module.Criterion = class.class('Criterion')

function module.Criterion.__class_init(self, cls)
	self.super:__class_init(cls)

	module[cls.name] = {}
	setmetatable(module[cls.name], {
		__class = cls,
		__call = function (table, ...)
			local self = cls:new()
			self:init(...)
			return self
		end
	})
end

function module.Criterion.method:__init()
	self._learn = false
end

function module.Criterion.method:init()
	-- Nothing to do
end

function module.Criterion.method:compare()
	error("not implemented")
end

function module.Criterion.method:learn()
	error("not implemented")
end

local ValueCriterion = class.class('value', module.Criterion)

function ValueCriterion.method:init(value)
	self._value = value
end

function ValueCriterion.method:compare(value)
	if value == nil then
		return false
	end
	return self._value == value
end

local SetCriterion = class.class('set', module.Criterion)

function SetCriterion.method:__init()
	class.super(SetCriterion).__init(self, parent)
	self._set = {}
end

function SetCriterion.method:init(list)
	for _, m in pairs(list) do
		self._set[m] = true
	end
end

function SetCriterion.method:compare(value)
	if value == nil then
		return false
{'fraction_non_alphanumeric': 0.11932324131789848, 'fraction_numerical': 0.0017809439002671415, 'mean_word_length': 4.553770086526576, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7522586', 'n_tokens_mistral': 1465, 'n_tokens_neox': 1453, 'n_words': 417}