Skip to content

Commit

Permalink
Expressions are evaluated over bindings and validated on contexts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerilk committed Mar 21, 2024
1 parent bc7bdd4 commit 40ad86c
Show file tree
Hide file tree
Showing 43 changed files with 609 additions and 579 deletions.
6 changes: 3 additions & 3 deletions bindings/python/cconfigspace/binding.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import ctypes as ct
from .base import Object, Error, Result, _ccs_get_function, ccs_context, ccs_binding, ccs_parameter, Datum, DatumFix, ccs_hash, ccs_int
from .base import Object, Error, Result, _ccs_get_function, ccs_context, ccs_binding, ccs_parameter, Datum, DatumFix, ccs_hash, ccs_int, ccs_bool
from .parameter import Parameter

ccs_binding_get_context = _ccs_get_function("ccs_binding_get_context", [ccs_binding, ct.POINTER(ccs_context)])
ccs_binding_get_value = _ccs_get_function("ccs_binding_get_value", [ccs_binding, ct.c_size_t, ct.POINTER(Datum)])
ccs_binding_get_values = _ccs_get_function("ccs_binding_get_values", [ccs_binding, ct.c_size_t, ct.POINTER(Datum), ct.POINTER(ct.c_size_t)])
ccs_binding_get_value_by_name = _ccs_get_function("ccs_binding_get_value_by_name", [ccs_binding, ct.c_char_p, ct.POINTER(Datum)])
ccs_binding_get_value_by_parameter = _ccs_get_function("ccs_binding_get_value_by_parameter", [ccs_binding, ccs_parameter, ct.POINTER(Datum)])
ccs_binding_get_value_by_parameter = _ccs_get_function("ccs_binding_get_value_by_parameter", [ccs_binding, ccs_parameter, ct.POINTER(ccs_bool), ct.POINTER(Datum)])
ccs_binding_hash = _ccs_get_function("ccs_binding_hash", [ccs_binding, ct.POINTER(ccs_hash)])
ccs_binding_cmp = _ccs_get_function("ccs_binding_cmp", [ccs_binding, ccs_binding, ct.POINTER(ct.c_int)])

Expand Down Expand Up @@ -35,7 +35,7 @@ def num_values(self):
def value(self, parameter):
v = Datum()
if isinstance(parameter, Parameter):
res = ccs_binding_get_value_by_parameter(self.handle, parameter.handle, ct.byref(v))
res = ccs_binding_get_value_by_parameter(self.handle, parameter.handle, None, ct.byref(v))
elif isinstance(parameter, str):
res = ccs_binding_get_value_by_name(self.handle, str.encode(parameter), ct.byref(v))
else:
Expand Down
14 changes: 0 additions & 14 deletions bindings/python/cconfigspace/configuration_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
ccs_configuration_space_get_forbidden_clause = _ccs_get_function("ccs_configuration_space_get_forbidden_clause", [ccs_configuration_space, ct.c_size_t, ct.POINTER(ccs_expression)])
ccs_configuration_space_get_forbidden_clauses = _ccs_get_function("ccs_configuration_space_get_forbidden_clauses", [ccs_configuration_space, ct.c_size_t, ct.POINTER(ccs_expression), ct.POINTER(ct.c_size_t)])
ccs_configuration_space_check_configuration = _ccs_get_function("ccs_configuration_space_check_configuration", [ccs_configuration_space, ccs_configuration, ct.POINTER(ccs_bool)])
ccs_configuration_space_check_configuration_values = _ccs_get_function("ccs_configuration_space_check_configuration_values", [ccs_configuration_space, ct.c_size_t, ct.POINTER(Datum), ct.POINTER(ccs_bool)])
ccs_configuration_space_get_default_configuration = _ccs_get_function("ccs_configuration_space_get_default_configuration", [ccs_configuration_space, ct.POINTER(ccs_configuration)])
ccs_configuration_space_sample = _ccs_get_function("ccs_configuration_space_sample", [ccs_configuration_space, ccs_distribution_space, ccs_rng, ct.POINTER(ccs_configuration)])
ccs_configuration_space_samples = _ccs_get_function("ccs_configuration_space_samples", [ccs_configuration_space, ccs_distribution_space, ccs_rng, ct.c_size_t, ct.POINTER(ccs_configuration)])
Expand Down Expand Up @@ -147,19 +146,6 @@ def check(self, configuration):
Error.check(res)
return False if valid.value == 0 else True

def check_values(self, values):
count = len(values)
if count != self.num_parameters:
raise Error(Result(Result.ERROR_INVALID_VALUE))
v = (Datum * count)()
ss = []
for i in range(count):
v[i].set_value(values[i], string_store = ss)
valid = ccs_bool()
res = ccs_configuration_space_check_configuration_values(self.handle, count, v, ct.byref(valid))
Error.check(res)
return False if valid.value == 0 else True

@property
def default_configuration(self):
v = ccs_configuration()
Expand Down
8 changes: 4 additions & 4 deletions bindings/python/cconfigspace/context.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import ctypes as ct
from .base import Object, Error, Result, _ccs_get_function, ccs_context, ccs_parameter, Datum, DatumFix
from .base import Object, Error, Result, _ccs_get_function, ccs_context, ccs_parameter, Datum, DatumFix, ccs_bool
from .parameter import Parameter

ccs_context_get_name = _ccs_get_function("ccs_context_get_name", [ccs_context, ct.POINTER(ct.c_char_p)])
ccs_context_get_num_parameters = _ccs_get_function("ccs_context_get_num_parameters", [ccs_context, ct.POINTER(ct.c_size_t)])
ccs_context_get_parameter = _ccs_get_function("ccs_context_get_parameter", [ccs_context, ct.c_size_t, ct.POINTER(ccs_parameter)])
ccs_context_get_parameter_by_name = _ccs_get_function("ccs_context_get_parameter_by_name", [ccs_context, ct.c_char_p, ct.POINTER(ccs_parameter)])
ccs_context_get_parameter_index_by_name = _ccs_get_function("ccs_context_get_parameter_index_by_name", [ccs_context, ct.c_char_p, ct.POINTER(ct.c_size_t)])
ccs_context_get_parameter_index = _ccs_get_function("ccs_context_get_parameter_index", [ccs_context, ccs_parameter, ct.POINTER(ct.c_size_t)])
ccs_context_get_parameter_indexes = _ccs_get_function("ccs_context_get_parameter_indexes", [ccs_context, ct.c_size_t, ct.POINTER(ccs_parameter), ct.POINTER(ct.c_size_t)])
ccs_context_get_parameter_index = _ccs_get_function("ccs_context_get_parameter_index", [ccs_context, ccs_parameter, ct.POINTER(ccs_bool), ct.POINTER(ct.c_size_t)])
ccs_context_get_parameter_indexes = _ccs_get_function("ccs_context_get_parameter_indexes", [ccs_context, ct.c_size_t, ct.POINTER(ccs_parameter), ct.POINTER(ccs_bool), ct.POINTER(ct.c_size_t)])
ccs_context_get_parameters = _ccs_get_function("ccs_context_get_parameters", [ccs_context, ct.c_size_t, ct.POINTER(ccs_parameter), ct.POINTER(ct.c_size_t)])
ccs_context_validate_value = _ccs_get_function("ccs_context_validate_value", [ccs_context, ct.c_size_t, DatumFix, ct.POINTER(Datum)])

Expand Down Expand Up @@ -38,7 +38,7 @@ def parameter_by_name(self, name):

def parameter_index(self, parameter):
v = ct.c_size_t()
res = ccs_context_get_parameter_index(self.handle, parameter.handle, ct.byref(v))
res = ccs_context_get_parameter_index(self.handle, parameter.handle, None, ct.byref(v))
Error.check(res)
return v.value

Expand Down
58 changes: 24 additions & 34 deletions bindings/python/cconfigspace/expression.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ctypes as ct
from . import libcconfigspace
from .base import Object, Error, Result, CEnumeration, _ccs_get_function, ccs_expression, Datum, DatumFix, ccs_parameter, ccs_context
from .base import Object, Error, Result, CEnumeration, _ccs_get_function, ccs_expression, Datum, DatumFix, ccs_parameter, ccs_context, ccs_binding
from .parameter import Parameter

class ExpressionType(CEnumeration):
Expand Down Expand Up @@ -63,10 +63,10 @@ class TerminalType(CEnumeration):
ccs_expression_get_nodes = _ccs_get_function("ccs_expression_get_nodes", [ccs_expression, ct.c_size_t, ct.POINTER(ccs_expression), ct.POINTER(ct.c_size_t)])
ccs_literal_get_value = _ccs_get_function("ccs_literal_get_value", [ccs_expression, ct.POINTER(Datum)])
ccs_variable_get_parameter = _ccs_get_function("ccs_variable_get_parameter", [ccs_expression, ct.POINTER(ccs_parameter)])
ccs_expression_eval = _ccs_get_function("ccs_expression_eval", [ccs_expression, ccs_context, ct.POINTER(Datum), ct.POINTER(Datum)])
ccs_expression_list_eval_node = _ccs_get_function("ccs_expression_list_eval_node", [ccs_expression, ccs_context, ct.POINTER(Datum), ct.c_size_t, ct.POINTER(Datum)])
ccs_expression_eval = _ccs_get_function("ccs_expression_eval", [ccs_expression, ct.c_size_t, ct.POINTER(ccs_binding), ct.POINTER(Datum)])
ccs_expression_list_eval_node = _ccs_get_function("ccs_expression_list_eval_node", [ccs_expression, ct.c_size_t, ct.POINTER(ccs_binding), ct.c_size_t, ct.POINTER(Datum)])
ccs_expression_get_parameters = _ccs_get_function("ccs_expression_get_parameters", [ccs_expression, ct.c_size_t, ct.POINTER(ccs_parameter), ct.POINTER(ct.c_size_t)])
ccs_expression_check_context = _ccs_get_function("ccs_expression_check_context", [ccs_expression, ccs_context])
ccs_expression_check_context = _ccs_get_function("ccs_expression_check_contexts", [ccs_expression, ct.c_size_t, ct.POINTER(ccs_context)])

class Expression(Object):

Expand Down Expand Up @@ -144,26 +144,22 @@ def parameters(self):
self._parameters = tuple(Parameter.from_handle(ccs_parameter(x)) for x in v)
return self._parameters

def eval(self, context = None, values = None):
if context and values:
count = context.num_parameters
if count != len(values):
raise Error(Result(Result.ERROR_INVALID_VALUE))
v = (Datum * count)()
ss = []
for i in range(count):
v[i].set_value(values[i], string_store = ss)
values = v
context = context.handle
elif context or values:
raise Error(Result(Result.ERROR_INVALID_VALUE))
def eval(self, bindings = None):
if bindings is not None:
num_bindings = len(bindings)
bindingsv = (ccs_binding * num_bindings)(*[x.handle.value for x in bindings])
else:
num_bindings = 0
bindingsv = None
v = Datum()
res = ccs_expression_eval(self.handle, context, values, ct.byref(v))
res = ccs_expression_eval(self.handle, num_bindings, bindingsv, ct.byref(v))
Error.check(res)
return v.value

def check_context(self, context):
res = ccs_expression_check_context(self.handle, context.handle)
def check_context(self, contexts):
num_contexts = len(contexts)
contextsv = (ccs_context * num_context)(*[x.handle.value for x in contexts])
res = ccs_expression_check_context(self.handle, num_contexts, contextsv)
Error.check(res)

def __str__(self):
Expand Down Expand Up @@ -464,21 +460,15 @@ def __init__(self, handle = None, retain = False, auto_release = True,
else:
super().__init__(handle = handle, retain = retain, auto_release = auto_release)

def eval(self, index, context = None, values = None):
if context and values:
count = context.num_parameters
if count != len(values):
raise Error(Result(Result.ERROR_INVALID_VALUE))
v = (Datum * count)()
ss = []
for i in range(count):
v[i].set_value(values[i], string_store = ss)
values = v
context = context.handle
elif context or values:
raise Error(Result(Result.ERROR_INVALID_VALUE))
def eval(self, index, bindings = None):
if bindings is not None:
num_bindings = len(bindings)
bindingsv = (ccs_binding * num_bindings)(*[x.handle.value for x in bindings])
else:
num_bindings = 0
bindingsv = None
v = Datum()
res = ccs_expression_list_eval_node(self.handle, context, values, index, ct.byref(v))
res = ccs_expression_list_eval_node(self.handle, num_bindings, bindingsv, index, ct.byref(v))
Error.check(res)
return v.value

Expand Down
13 changes: 0 additions & 13 deletions bindings/python/cconfigspace/features_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

ccs_create_features_space = _ccs_get_function("ccs_create_features_space", [ct.c_char_p, ct.c_size_t, ct.POINTER(ccs_parameter), ct.POINTER(ccs_features_space)])
ccs_features_space_check_features = _ccs_get_function("ccs_features_space_check_features", [ccs_features_space, ccs_features, ct.POINTER(ccs_bool)])
ccs_features_space_check_features_values = _ccs_get_function("ccs_features_space_check_features_values", [ccs_features_space, ct.c_size_t, ct.POINTER(Datum), ct.POINTER(ccs_bool)])

class FeaturesSpace(Context):
def __init__(self, handle = None, retain = False, auto_release = True,
Expand All @@ -30,15 +29,3 @@ def check(self, features):
Error.check(res)
return False if valid.value == 0 else True

def check_values(self, values):
count = len(values)
if count != self.num_parameters:
raise Error(Result(Result.ERROR_INVALID_VALUE))
v = (Datum * count)()
ss = []
for i in range(count):
v[i].set_value(values[i], string_store = ss)
valid = ccs_bool()
res = ccs_features_space_check_features_values(self.handle, count, v, ct.byref(valid))
Error.check(res)
return False if valid.value == 0 else True
13 changes: 3 additions & 10 deletions bindings/python/cconfigspace/objective_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ObjectiveType(CEnumeration):
ccs_create_objective_space = _ccs_get_function("ccs_create_objective_space", [ct.c_char_p, ct.c_size_t, ct.POINTER(ccs_parameter), ct.c_size_t, ct.POINTER(ccs_expression), ct.POINTER(ObjectiveType), ct.POINTER(ccs_objective_space)])
ccs_objective_space_get_objective = _ccs_get_function("ccs_objective_space_get_objective", [ccs_objective_space, ct.c_size_t, ct.POINTER(ccs_expression), ct.POINTER(ObjectiveType)])
ccs_objective_space_get_objectives = _ccs_get_function("ccs_objective_space_get_objectives", [ccs_objective_space, ct.c_size_t, ct.POINTER(ccs_expression), ct.POINTER(ObjectiveType), ct.POINTER(ct.c_size_t)])
ccs_objective_space_check_evaluation_values = _ccs_get_function("ccs_objective_space_check_evaluation_values", [ccs_objective_space, ct.c_size_t, ct.POINTER(Datum), ct.POINTER(ccs_bool)])
ccs_objective_space_check_evaluation = _ccs_get_function("ccs_objective_space_check_evaluation", [ccs_objective_space, ccs_evaluation, ct.POINTER(ccs_bool)])

class ObjectiveSpace(Context):
def __init__(self, handle = None, retain = False, auto_release = True,
Expand Down Expand Up @@ -76,16 +76,9 @@ def objectives(self):
self._objectives = tuple((Expression.from_handle(ccs_expression(v[x])), t[x].value) for x in range(sz))
return self._objectives

def check_values(self, values):
count = len(values)
if count != self.num_parameters:
raise Error(Result(Result.ERROR_INVALID_VALUE))
v = (Datum * count)()
ss = []
for i in range(count):
v[i].set_value(values[i], string_store = ss)
def check(self, evaluation):
valid = ccs_bool()
res = ccs_objective_space_check_evaluation_values(self.handle, count, v, ct.byref(valid))
res = ccs_objective_space_check_evaluation(self.handle, evaluation.handle, ct.byref(valid))
Error.check(res)
return False if valid.value == 0 else True

1 change: 0 additions & 1 deletion bindings/python/test/test_configuration_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def test_create(self):
c = cs.sample()
self.assertTrue( cs.check(c) )
self.assertEqual( cs.handle.value, c.configuration_space.handle.value )
self.assertTrue( cs.check_values(cs.sample().values) )
for c in cs.samples(100):
self.assertTrue( cs.check(c) )

Expand Down
2 changes: 1 addition & 1 deletion bindings/python/test/test_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_create(self):
self.assertEqual( (0.5, 0.6), ev1.values )
self.assertEqual( (0.5, 0.6), ev1.objective_values )
self.assertTrue( ev1.check )
self.assertTrue( os.check_values(ev1.values) )
self.assertTrue( os.check(ev1) )
ev2 = ccs.Evaluation(objective_space = os, configuration = cs.sample(), values = [0.5, 0.6])
self.assertEqual( (0.5, 0.6), ev2.values )
self.assertEqual( (0.5, 0.6), ev2.objective_values )
Expand Down
2 changes: 1 addition & 1 deletion bindings/python/test/test_tree_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_create(self):
self.assertEqual( (0.5, 0.6), ev1.values )
self.assertEqual( (0.5, 0.6), ev1.objective_values )
self.assertTrue( ev1.check )
self.assertTrue( os.check_values(ev1.values) )
self.assertTrue( os.check(ev1) )
ev2 = ccs.TreeEvaluation(objective_space = os, configuration = ts.sample(), values = [0.5, 0.6])
self.assertEqual( (0.5, 0.6), ev2.values )
self.assertEqual( (0.5, 0.6), ev2.objective_values )
Expand Down
4 changes: 2 additions & 2 deletions bindings/ruby/lib/cconfigspace/binding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module CCS
attach_function :ccs_binding_get_value, [:ccs_binding_t, :size_t, :pointer], :ccs_result_t
attach_function :ccs_binding_get_values, [:ccs_binding_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_binding_get_value_by_name, [:ccs_binding_t, :string, :pointer], :ccs_result_t
attach_function :ccs_binding_get_value_by_parameter, [:ccs_binding_t, :ccs_parameter_t, :pointer], :ccs_result_t
attach_function :ccs_binding_get_value_by_parameter, [:ccs_binding_t, :ccs_parameter_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_binding_hash, [:ccs_binding_t, :pointer], :ccs_result_t
attach_function :ccs_binding_cmp, [:ccs_binding_t, :ccs_binding_t, :pointer], :ccs_result_t

Expand All @@ -21,7 +21,7 @@ def value(parameter)
name = parameter.inspect
CCS.error_check CCS.ccs_binding_get_value_by_name(@handle, name, ptr)
when Parameter
CCS.error_check CCS.ccs_binding_get_by_parameter(@handle, parameter.handle, ptr)
CCS.error_check CCS.ccs_binding_get_value_by_parameter(@handle, parameter.handle, nil, ptr)
when Integer
CCS.error_check CCS.ccs_binding_get_value(@handle, parameter, ptr)
else
Expand Down
14 changes: 1 addition & 13 deletions bindings/ruby/lib/cconfigspace/configuration_space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module CCS
attach_function :ccs_configuration_space_get_forbidden_clause, [:ccs_configuration_space_t, :size_t, :pointer], :ccs_result_t
attach_function :ccs_configuration_space_get_forbidden_clauses, [:ccs_configuration_space_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_configuration_space_check_configuration, [:ccs_configuration_space_t, :ccs_configuration_t, :pointer], :ccs_result_t
attach_function :ccs_configuration_space_check_configuration_values, [:ccs_configuration_space_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_configuration_space_get_default_configuration, [:ccs_configuration_space_t, :pointer], :ccs_result_t
attach_function :ccs_configuration_space_sample, [:ccs_configuration_space_t, :ccs_distribution_space_t, :ccs_rng_t, :pointer], :ccs_result_t
attach_function :ccs_configuration_space_samples, [:ccs_configuration_space_t, :ccs_distribution_space_t, :ccs_rng_t, :size_t, :pointer], :ccs_result_t
Expand Down Expand Up @@ -138,21 +137,10 @@ def forbidden_clauses

def check(configuration)
ptr = MemoryPointer::new(:ccs_bool_t)
CCS.error_check CCS.ccs_configuration_space_check_configuration(@handle, configuration, ptr)
CCS.error_check CCS.ccs_configuration_space_check_configuration(@handle, configuration.handle, ptr)
return ptr.read_ccs_bool_t == CCS::FALSE ? false : true
end

def check_values(values)
count = values.size
raise CCSError, :CCS_RESULT_ERROR_INVALID_VALUE if count != num_parameters
ss = []
ptr = MemoryPointer::new(:ccs_datum_t, count)
values.each_with_index { |v, i| Datum::new(ptr[i]).set_value(v, string_store: ss) }
ptr2 = MemoryPointer::new(:ccs_bool_t)
CCS.error_check CCS.ccs_configuration_space_check_configuration_values(@handle, count, ptr, ptr2)
return ptr2.read_ccs_bool_t == CCS::FALSE ? false : true
end

def default_configuration
ptr = MemoryPointer::new(:ccs_configuration_t)
CCS.error_check CCS.ccs_configuration_space_get_default_configuration(@handle, ptr)
Expand Down
6 changes: 3 additions & 3 deletions bindings/ruby/lib/cconfigspace/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module CCS
attach_function :ccs_context_get_parameter, [:ccs_context_t, :size_t, :pointer], :ccs_result_t
attach_function :ccs_context_get_parameter_by_name, [:ccs_context_t, :string, :pointer], :ccs_result_t
attach_function :ccs_context_get_parameter_index_by_name, [:ccs_context_t, :string, :pointer], :ccs_result_t
attach_function :ccs_context_get_parameter_index, [:ccs_context_t, :ccs_parameter_t, :pointer], :ccs_result_t
attach_function :ccs_context_get_parameter_indexes, [:ccs_context_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_context_get_parameter_index, [:ccs_context_t, :ccs_parameter_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_context_get_parameter_indexes, [:ccs_context_t, :size_t, :pointer, :pointer, :pointer], :ccs_result_t
attach_function :ccs_context_get_parameters, [:ccs_context_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_context_validate_value, [:ccs_context_t, :size_t, :ccs_datum_t, :pointer], :ccs_result_t

Expand Down Expand Up @@ -42,7 +42,7 @@ def parameter_index_by_name(name)

def parameter_index(parameter)
ptr = MemoryPointer::new(:size_t)
CCS.error_check CCS.ccs_context_get_parameter_index(@handle, parameter, ptr)
CCS.error_check CCS.ccs_context_get_parameter_index(@handle, parameter, nil, ptr)
ptr.read_size_t
end

Expand Down
Loading

0 comments on commit 40ad86c

Please sign in to comment.