00001 /*!@file Learn/SWIProlog.C interface to SWI-Prolog */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Lior Elazary <elazary@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Learn/SWIProlog.C $ 00035 // $Id: SWIProlog.C 9308 2008-02-22 19:04:41Z rjpeters $ 00036 // 00037 00038 #ifndef LEARN_SWIPROLOG_C_DEFINED 00039 #define LEARN_SWIPROLOG_C_DEFINED 00040 00041 #include "Learn/SWIProlog.H" 00042 #include "Util/Assert.H" 00043 #include "Util/log.H" 00044 #include <math.h> 00045 #include <fcntl.h> 00046 #include <limits> 00047 #include <string> 00048 00049 // ###################################################################### 00050 SWIProlog::SWIProlog(int argc, char **argv) 00051 { 00052 const char *av[10]; 00053 int ac = 0; 00054 00055 // av[ac++] = argv[0]; 00056 av[ac++] = "DefaultProg"; 00057 av[ac++] = "-q"; 00058 av[ac++] = "-nosignals"; 00059 av[ac] = NULL; 00060 00061 #ifdef HAVE_SWI_PROLOG_H 00062 if (!PL_initialise(ac, av)) 00063 { 00064 PL_halt(1); 00065 LFATAL("Failed to init prolog"); 00066 } 00067 #else 00068 LINFO("SWI prolog not found"); 00069 #endif 00070 00071 } 00072 00073 // ###################################################################### 00074 SWIProlog::~SWIProlog() 00075 { 00076 00077 } 00078 00079 // ###################################################################### 00080 bool SWIProlog::consult(const char *filename) 00081 { 00082 00083 bool ret = false; 00084 #ifdef HAVE_SWI_PROLOG_H 00085 term_t a0 = PL_new_term_refs(1); 00086 predicate_t p = NULL; 00087 00088 p = PL_predicate("consult", 1, NULL); 00089 00090 PL_put_atom_chars(a0, filename); 00091 00092 qid_t query_id= PL_open_query(NULL, (PL_Q_NORMAL|PL_Q_CATCH_EXCEPTION), p, a0); 00093 00094 ret = PL_next_solution(query_id); 00095 PL_close_query(query_id); 00096 #else 00097 LINFO("SWI prolog not found"); 00098 #endif 00099 00100 return ret; 00101 } 00102 00103 // ###################################################################### 00104 bool SWIProlog::query(const char *predicate, std::vector<std::string> &args) 00105 { 00106 bool ret=false; 00107 #ifdef HAVE_SWI_PROLOG_H 00108 term_t a0 = PL_new_term_refs(args.size()); 00109 predicate_t p = NULL; 00110 00111 p = PL_predicate(predicate, args.size(), NULL); 00112 00113 for(uint i=0; i<args.size(); i++) 00114 { 00115 if (args[i].size() != 0) 00116 PL_put_atom_chars(a0+i, args[i].c_str()); 00117 } 00118 00119 qid_t query_id= PL_open_query(NULL, (PL_Q_NORMAL|PL_Q_CATCH_EXCEPTION), p, a0); 00120 00121 ret = PL_next_solution(query_id); 00122 00123 if (ret) 00124 { 00125 //fill in the results in the place holdes 00126 for(uint i=0; i<args.size(); i++) 00127 { 00128 if (args[i].size() == 0) 00129 { 00130 char *data; 00131 PL_get_atom_chars(a0+i, &data); 00132 args[i] = std::string(data); 00133 } 00134 } 00135 } 00136 00137 PL_close_query(query_id); 00138 #else 00139 LINFO("SWI prolog not found"); 00140 #endif 00141 00142 return ret; 00143 } 00144 00145 // ###################################################################### 00146 /* So things look consistent in everyone's emacs... */ 00147 /* Local Variables: */ 00148 /* indent-tabs-mode: nil */ 00149 /* End: */ 00150 00151 #endif // LEARN_SWIPROLOG_C_DEFINED