def Tree_ctor(tree_type): st = new(tree_type) st.nodes = List.empty_list(TreeNodeType) # st.u_ys = np.zeros(0,dtype=np.int32) st.context_cache = new_akd(u8,SplitterContextType)#Dict.empty(i8_arr, SplitterContextType) st.data_stats = DataStats_ctor() return st
def init_kb_data(context_data): kb_data = new(KnowledgeBaseDataType) kb_data.facts = new_vector( BASE_T_ID_STACK_SIZE) #List.empty_list(basefact_list) kb_data.retracted_f_ids = new_vector(BASE_T_ID_STACK_SIZE) # kb_data.empty_f_id_stacks = List.empty_list(i8_arr) # kb_data.empty_f_id_heads = np.zeros((BASE_T_ID_STACK_SIZE,),dtype=np.int64) kb_data.n_types = 0 kb_data.names_to_idrecs = Dict.empty(unicode_type, u8) kb_data.enum_data = Dict.empty(unicode_type, i8_arr) # kb_data.consistency_listeners = Dict.empty(i8, two_str_set) kb_data.enum_consistency = Dict.empty(two_str, u1) # consistency_listener_counter = np.zeros(1,dtype=np.int64) # consistency_listeners[0] = enum_consistency # consistency_listener_counter += 1 kb_data.subscribers = List.empty_list( BaseSubscriberType) #FUTURE: Replace w/ resolved type kb_data.change_queue = new_vector(BASE_CHANGE_QUEUE_SIZE) kb_data.grow_queue = new_vector(BASE_CHANGE_QUEUE_SIZE) # kb_data.unnamed_counter = np.zeros(1,dtype=np.int64) kb_data.NULL_FACT = BaseFact() # kb_data = KnowledgeBaseData(facts, empty_f_id_stacks, empty_f_id_heads, names_to_idrecs, # enum_data, enum_consistency, subscribers, # unnamed_counter, BaseFact() #Empty BaseFact is NULL_FACT # ) L = max(len(context_data.attr_inds_by_type), 1) expand_kb_data_types(kb_data, L) return kb_data
def new_vector(size): ''' Makes a new vector of size. ''' st = new(VectorType) st.head = 0 st.data = np.empty(size, dtype=np.int64) return st
def var_ctor(var_struct_type, fact_type_name, alias): st = new(var_struct_type) st.fact_type_name = fact_type_name st.base_ptr = _pointer_from_struct(st) st.alias = "boop" if(alias is None) else alias st.deref_attrs = List.empty_list(unicode_type) st.deref_offsets = List.empty_list(i8) return st
def impl(_vars, dnf=None): st = new(ConditionsType) st.vars = build_var_list(_vars) st.var_map = _vars.copy() # is shallow copy # print("B",st.var_map) st.dnf = dnf if (dnf) else new_dnf(len(_vars)) st.is_initialized = False return st
def TreeNode_ctor(ttype, index, counts): st = new(TreeNodeType) st.ttype = ttype st.index = index st.op_enum = OP_NOP st.split_data = List.empty_list(SplitDataType) st.counts = counts return st
def beta_pterm_ctor(pn, left_var, op_str, right_var): st = new(PTermType) st.pred_node = pn st.str_val = str(left_var) + " " + op_str + " " + str(right_var) st.var_base_ptrs = (left_var.base_ptr, right_var.base_ptr) st.negated = False st.is_alpha = False st.is_linked = False return st
def SplitterContext_ctor(split_chain): st = new(SplitterContextType) st.split_chain = split_chain st.n_last_update = 0 st.nominal_split_cache_ptrs = np.zeros((32,),dtype=np.int64) st.continous_split_cache_ptrs = np.zeros((32,),dtype=np.int64) return st
def impl(_vars, dnf=None): st = new(ConditionsType) st.vars = List.empty_list(GenericVarType) st.vars.append(_struct_from_pointer(GenericVarType, _vars.base_ptr)) st.var_map = build_var_map(st.vars) # print("A",st.var_map) st.dnf = dnf if (dnf) else new_dnf(1) st.is_initialized = False return st
def NominalSplitCache_ctor(n_vals, n_classes): st = new(NominalSplitCacheType) # instantiate as one so it is gaurenteed to be contiguous # data = np.zeros((n_vals*(n_classes+1))) # st.v_counts = data[:n_vals] # st.y_counts_per_v = data[n_vals:n_vals*(n_classes+1)].reshape((n_vals,n_classes)) st.best_v = -1 st.v_counts = np.zeros((n_vals, ), dtype=np.uint32) st.y_counts_per_v = np.zeros((n_vals, n_classes), dtype=np.uint32) return st
def alpha_pterm_ctor(pn, left_var, op_str, right_var): st = new(PTermType) st.pred_node = pn st.str_val = str( left_var ) + " " + op_str + " " + "?" #base_str + "?"#TODO str->float needs to work st.var_base_ptrs = (left_var.base_ptr, 0) st.negated = False st.is_alpha = True st.is_linked = False return st
def pterm_copy(self): st = new(PTermType) st.str_val = self.str_val st.pred_node = self.pred_node st.var_base_ptrs = self.var_base_ptrs st.negated = self.negated st.is_alpha = self.is_alpha st.is_linked = self.is_linked if (self.is_linked): st.link_data = self.link_data return st
def generate_link_data(pn, kb): '''Takes a prototype predicate node and a knowledge base and returns a linked instance of that predicate node, which is either a copy or an equivalent instance already linked to the knowledge base''' link_data = new(PredicateNodeLinkDataType) # print(pn.left_fact_type_name) # print(kb.context_data.fact_to_t_id) # print(pn.left_fact_type_name) # print("Q") link_data.left_t_id = kb.context_data.fact_to_t_id[pn.left_fact_type_name] # print("Q2", link_data.left_t_id) link_data.left_facts = facts_for_t_id(kb.kb_data,i8(link_data.left_t_id)) # print("Z") if(not pn.is_alpha): link_data.right_t_id = kb.context_data.fact_to_t_id[pn.right_fact_type_name] link_data.right_facts = facts_for_t_id(kb.kb_data,i8(link_data.right_t_id)) link_data.left_consistency = np.empty((0,),dtype=np.uint8) link_data.right_consistency = np.empty((0,),dtype=np.uint8) else: link_data.right_t_id = -1 # print("S") link_data.change_head = 0 link_data.grow_head = 0 link_data.change_queue = new_vector(8) link_data.grow_queue = new_vector(8) link_data.kb_grow_queue = kb.kb_data.grow_queue link_data.kb_change_queue = kb.kb_data.change_queue link_data.truth_values = np.empty((0,0),dtype=np.uint8) # print("DONE") # print(pn.is_alpha) # if(pn.is_alpha): # a = _cast_structref(GenericAlphaPredicateNodeType, pn) # new_a = new(GenericAlphaPredicateNodeType) # new_a.filter_func = a.filter_func # new_a.right_val = a.right_val # new_pn = _cast_structref(BasePredicateNodeType, new_a) # else: # b = _cast_structref(GenericBetaPredicateNodeType, pn) # new_b = new(GenericBetaPredicateNodeType) # new_b.filter_func = b.filter_func # new_b.right_t_id = b.right_t_id # new_b.right_facts = b.right_facts # new_pn = _cast_structref(BasePredicateNodeType, new_b) return link_data
def my_struct(values, counter): st = structref.new(my_struct_ty) my_struct_init(st, values, counter) return st
def new_config(typ): return new(typ)
def dummy_subscriber_ctor(): st = new(BaseSubscriberType) init_base_subscriber(st) return st
def new_var_and_append(self): st = new(typ) copy_and_append(self,st,attr,offset) return st