4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include <types_internal.h>
25 #include "clips_handler.h"
26 #include "rule_manager.h"
30 #include <clips/clips.h>
33 static void* env = NULL;
34 static ctx::rule_manager* rule_mgr = NULL;
35 static int string_to_int(std::string str);
37 ctx::clips_handler::clips_handler()
41 ctx::clips_handler::~clips_handler()
44 DestroyEnvironment(env);
48 bool ctx::clips_handler::init(ctx::rule_manager* rm)
52 int error = init_environment(env);
53 IF_FAIL_RETURN(error == ERR_NONE, false);
55 bool ret = define_global_variable_string("zone", "");
56 IF_FAIL_RETURN(ret, false);
61 int ctx::clips_handler::init_environment(void* &environment)
63 environment = CreateEnvironment();
65 _E("Create environment failed");
66 return ERR_OPERATION_FAILED;
69 char* func_name = strdup("execute_action");
70 char* restrictions = strdup("1s");
72 if (func_name == NULL || restrictions == NULL) {
73 _E("Memory allocation failed");
76 return ERR_OUT_OF_MEMORY;
79 EnvDefineFunction2(environment, func_name, 'i', PTIEF execute_action, func_name, restrictions);
87 int ctx::clips_handler::define_template(std::string& script)
89 IF_FAIL_RETURN_TAG(env_build(script) == ERR_NONE, ERR_OPERATION_FAILED, _E, "Deftemplate failed");
93 int ctx::clips_handler::define_class(std::string& script)
95 IF_FAIL_RETURN_TAG(env_build(script) == ERR_NONE, ERR_OPERATION_FAILED, _E, "Defclass failed");
99 int ctx::clips_handler::define_rule(std::string& script)
101 IF_FAIL_RETURN_TAG(env_build(script) == ERR_NONE, ERR_OPERATION_FAILED, _E, "Defrule failed");
105 int ctx::clips_handler::env_build(std::string& script)
107 ASSERT_NOT_NULL(env);
108 if (script.length() == 0)
109 return ERR_INVALID_PARAMETER;
111 _I("EnvBuild script (%s)", script.c_str());
112 int ret = EnvBuild(env, script.c_str());
114 return (ret == 1)? ERR_NONE : ERR_OPERATION_FAILED;
117 int ctx::clips_handler::run_environment()
119 ASSERT_NOT_NULL(env);
121 int fired_rule_num = EnvRun(env, -1);
122 IF_FAIL_RETURN(fired_rule_num >= 0, ERR_OPERATION_FAILED);
127 int ctx::clips_handler::add_fact(std::string& fact)
129 ASSERT_NOT_NULL(env);
130 if (fact.length() == 0)
131 return ERR_INVALID_PARAMETER;
133 void* assert_fact = EnvAssertString(env, fact.c_str());
134 IF_FAIL_RETURN_TAG(assert_fact, ERR_OPERATION_FAILED, _E, "Fact assertion failed");
139 int ctx::clips_handler::route_string_command(std::string& script)
141 ASSERT_NOT_NULL(env);
142 if (script.length() == 0)
143 return ERR_INVALID_PARAMETER;
146 if (RouteCommand(env, script.c_str(), TRUE)){
147 _D("Route command succeeded(%s).", script.c_str());
150 _E("Route command failed");
151 error = ERR_OPERATION_FAILED;
157 int ctx::clips_handler::make_instance(std::string& script)
159 ASSERT_NOT_NULL(env);
160 if (script.length() == 0)
161 return ERR_INVALID_PARAMETER;
164 if (EnvMakeInstance(env, script.c_str())){
165 _D("Make instance succeeded - %s", script.c_str());
168 _E("Make instance failed");
169 error = ERR_OPERATION_FAILED;
175 int ctx::clips_handler::unmake_instance(std::string& instance_name)
177 ASSERT_NOT_NULL(env);
178 if (instance_name.length() == 0)
179 return ERR_INVALID_PARAMETER;
181 void* instance = find_instance_internal(instance_name);
183 _E("Cannot find instance(%s).", instance_name.c_str());
184 return ERR_INVALID_PARAMETER;
187 if (!EnvUnmakeInstance(env, instance)){
188 _E("Unmake instance failed");
189 return ERR_OPERATION_FAILED;
192 _D("Unmake instance succeeded(%s).", instance_name.c_str());
196 int ctx::clips_handler::execute_action()
199 _E("Environment not created");
200 return ERR_OPERATION_FAILED;
203 const char* result = EnvRtnLexeme(env, 1);
205 _E("Failed to get returned rule id");
206 return ERR_OPERATION_FAILED;
208 std::string rule_id = result;
209 std::string id_str = rule_id.substr(4);
211 int id = string_to_int(id_str);
212 rule_mgr->on_rule_triggered(id);
217 bool ctx::clips_handler::find_instance(std::string& instance_name)
219 ASSERT_NOT_NULL(env);
220 if (find_instance_internal(instance_name)) {
221 _D("[%s] already exists", instance_name.c_str());
228 void* ctx::clips_handler::find_instance_internal(std::string& instance_name)
230 void* instance = EnvFindInstance(env, NULL, instance_name.c_str(), TRUE);
234 int string_to_int(std::string str)
237 std::istringstream convert(str);
245 bool ctx::clips_handler::define_global_variable_string(std::string variable_name, std::string value)
247 std::string script = "(defglobal ?*";
248 script += variable_name;
253 IF_FAIL_RETURN_TAG(env_build(script) == ERR_NONE, false, _E, "Defglobal failed");
257 bool ctx::clips_handler::set_global_variable_string(std::string variable_name, std::string value)
259 ASSERT_NOT_NULL(env);
260 if (variable_name.length() == 0)
264 SetType(data, STRING);
265 SetValue(data, EnvAddSymbol(env, value.c_str())) ;
267 int ret = EnvSetDefglobalValue(env, variable_name.c_str(), &data);
269 IF_FAIL_RETURN_TAG(ret == 1, false, _E, "Set global variable(%s) failed", variable_name.c_str());