Back to index

texmacs  1.0.7.15
tmfs_properties.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : tmfs_properties.cpp
00004 * DESCRIPTION: property database
00005 * COPYRIGHT  : (C) 2007  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "tmfs.hpp"
00013 
00014 /******************************************************************************
00015 * Adding and removing properties
00016 ******************************************************************************/
00017 
00018 static void
00019 property_treat (transaction& t, property p, int i, property val, int eps) {
00020   //cout << "Property treat " << p << ", " << i << ", "
00021   //<< val << ", " << eps << "\n";
00022   int j, n= N(p);
00023   for (j=1; j<n; j++)
00024     if (p[j] != "*") {
00025       add (t, property_append (p), property_append (val), eps);
00026       break;
00027     }
00028   for (j=i; j<n; j++) {
00029     property p2  = copy (p);
00030     property val2= copy (val);
00031     p2[j]= "*";
00032     val2 << p[j];
00033     property_treat (t, p2, j+1, val2, eps);
00034   }
00035 }
00036 
00037 transaction
00038 as_transaction (property p, int eps) {
00039   p= property_quote (p);
00040   transaction t;
00041   property_treat (t, p, 0, property (), eps);
00042   return t;
00043 }
00044 
00045 void
00046 tmfs_set_property (property p) {
00047   tmfs_write (as_transaction (p, 1));
00048 }
00049 
00050 void
00051 tmfs_set_properties (properties ps) {
00052   for (int i=0; i<N(ps); i++)
00053     tmfs_set_property (ps[i]);
00054 }
00055 
00056 void
00057 tmfs_reset_property (property p) {
00058   for (int i=0; i<N(p); i++)
00059     if (is_unknown (p[i])) {
00060       properties ps= substitute (p, tmfs_get_solutions (p));
00061       for (int j=0; j<N(ps); j++)
00062        tmfs_write (as_transaction (ps[j], -1));
00063       return;
00064     }
00065   tmfs_write (as_transaction (p, -1));
00066 }
00067 
00068 void
00069 tmfs_reset_properties (properties ps) {
00070   for (int i=0; i<N(ps); i++)
00071     tmfs_reset_property (ps[i]);
00072 }
00073 
00074 /******************************************************************************
00075 * Querying for properties
00076 ******************************************************************************/
00077 
00078 void
00079 property_wildcards (property query, property& p, property &v) {
00080   p= property_quote (query);
00081   v= property ();
00082   int i, n= N(query);
00083   for (i=0; i<n; i++)
00084     if (is_unknown (query[i])) {
00085       p[i]= "*";
00086       v << query[i];
00087     }
00088 }
00089 
00090 solutions
00091 decode_solutions (collection t, property v) {
00092   solutions sols;
00093   iterator<string> it= iterate (t);
00094   while (it->busy ()) {
00095     solution sol;
00096     string s= it->next ();
00097     property d= property_unquote (property_unappend (s));
00098     if (t[s] > 0 && N(v) <= N(d))
00099       for (int i=0; i<N(v); i++)
00100        sol(v[i])= d[i];
00101     sols << sol;
00102   }
00103   return sols;
00104 }
00105 
00106 solutions
00107 tmfs_get_solutions (property query) {
00108   // cout << "  Query: " << query << "\n";
00109   property p;
00110   property v;
00111   property_wildcards (query, p, v);
00112   collection c= tmfs_get (property_append (p));
00113   return decode_solutions (c, v);
00114 }
00115 
00116 collection
00117 tmfs_get_values (property query) {
00118   return as_collection (tmfs_get_solutions (query), query);
00119 }
00120 
00121 /******************************************************************************
00122 * Combining queries
00123 ******************************************************************************/
00124 
00125 solutions
00126 tmfs_get_solutions (solutions sols1, property query) {
00127   //cout << "Get property " << sols1 << ", " << query << "\n";
00128   solutions sols2;
00129   hashmap<string,solutions> cache;
00130   for (int i=0; i<N(sols1); i++) {
00131     property p= substitute (query, sols1[i]);
00132     string s= property_append (p);
00133     if (!cache->contains (s))
00134       cache(s)= tmfs_get_solutions (p);
00135     solutions sols3; sols3 << sols1[i];
00136     sols2 << combine (sols3, cache[s]);
00137   }
00138   return sols2;
00139 }
00140 
00141 solutions
00142 tmfs_get_solutions (properties queries) {
00143   solutions sols; sols << solution ();
00144   for (int i=0; i<N(queries); i++)
00145     sols= tmfs_get_solutions (sols, queries[i]);
00146   return sols;
00147 }
00148 
00149 solutions
00150 tmfs_var_get_solutions (solutions sols1, property p) {
00151   property query= simplify (p, sols1);
00152   solutions sols2= tmfs_get_solutions (query);
00153   return combine (sols1, sols2);
00154 }