Пример #1
0
    def do_query(self, arg):
        """Manipulate the query.

        query [show]                   Show current query.
        query reset                    Reset query to be empty.
        query set <attribute> <value>  Set query attribute <attribute> to <value>.
        query unset <attribute>        Unset query attribute <attribute>.
        query names [attribute]        Show possible attribute names.
        query run                      Run the current query.

        By default, the query is automatically run when changed, and
        the result is automatically displayed when run. This behaviour
        can be changed by setting respectively the 'auto_run' and
        'auto_display' config parameters."""
        if arg in ('', 'show'):
            if self.query:
                print_table([self.query], ["Attribute", "Value"])
            else:
                print "No current query."
        elif arg == "reset":
            self.query = Case()
        elif arg.startswith('set'):
            parts = arg.split(None, 2)
            if len(parts) < 3:
                print "Usage: query set <attribute> <value>."
                return
            arg,key,val = parts
            try:
                self.query[key_name(key, possible_attributes)] = val
                if self.config['auto_run']:
                    self.do_query("run")
            except KeyError:
                print "Invalid attribute name '%s'." % key
                print "Possible attribute names:"
                print "\n".join(["  "+i for i in sorted(possible_attributes.keys())])
            except ValueError, e:
                print str(e)
Пример #2
0
 def _set_value(self, value):
     try:
         self._value = key_name(value, self._match_table)
     except KeyError:
         raise ValueError("Unrecognised value for %s: '%s'." %
                          (self.name, value))
Пример #3
0
 def _set_value(self, value):
     try:
         self._value = key_name(value, self._match_table)
     except KeyError:
         raise ValueError("Unrecognised value for %s: '%s'." % (self.name, value))
    def do_query(self, arg):
        """Manipulate the query.

        query [show]                   Show current query.
        query reset                    Reset query to be empty.
        query set <attribute> <value>  Set query attribute <attribute> to <value>.
        query unset <attribute>        Unset query attribute <attribute>.
        query names [attribute]        Show possible attribute names.
        query run                      Run the current query.

        By default, the query is automatically run when changed, and
        the result is automatically displayed when run. This behaviour
        can be changed by setting respectively the 'auto_run' and
        'auto_display' config parameters."""
        if arg in ('', 'show'):
            if self.query:
                print_table([self.query], ["Attribute", "Value"])
            else:
                print ("No current query.")
        elif arg == "reset":
            self.query = Case()
        elif arg.startswith('set'):
            parts = arg.split(None, 2)
            if len(parts) < 3:
                print ("Usage: query set <attribute> <value>.")
                return
            arg,key,val = parts
            try:
                self.query[key_name(key, possible_attributes)] = val
                if self.config['auto_run']:
                    self.do_query("run")
            except KeyError:
                print ("Invalid attribute name '%s'.") % key
                print ("Possible attribute names:")
                print ("\n".join(["  "+i for i in sorted(possible_attributes.keys())]))
            except ValueError as e:
                print (str(e))
        elif arg.startswith('unset'):
            parts = arg.split()
            if len(parts) < 2:
                print ("Usage: query unset <attribute>.")
                return
            arg,key = parts[:2]
            try:
                key = key_name(key, possible_attributes)
                del self.query[key]
                if self.config['auto_run']:
                    self.do_query("run")
            except KeyError:
                print ("Attribute '%s' not found.") % key
                return
        elif arg.startswith('names'):
            parts = arg.split()
            if len(parts) < 2:
                print ("Possible attributes:")
                print_table([dict([(k,v._weight) for (k,v) in possible_attributes.items()]),
                             dict([(k,v._adaptable) for (k,v) in possible_attributes.items()]),
                             dict([(k,v._adjustable) for (k,v) in possible_attributes.items()]),],
                            ["Attribute name", "Weight", "Adaptable", "Adjusted"])
                print ("\n".join(("Weight is the weight of the attribute for case similarity.",
                                 "",
                                 "Adaptable specifies whether the attribute can be adapted to",
                                 "the query value.",
                                 "",
                                 "Adjustable specifies whether the attribute is adjusted based",
                                 "on the adaptable ones.",
                                 "",
                                 "Run 'query names <attribute>' for help on an attribute.")))

            else:
                try:
                    key = key_name(parts[1], possible_attributes)
                    attr = possible_attributes[key]
                    print ("\n".join(("Attribute :  %s" % key,
                                     "Weight    :  %s" % attr._weight,
                                     "Adaptable :  %s" % attr._adaptable,
                                     "Adjusted  :  %s" % attr._adjustable,
                                     "")))
                    print (self.gen_help(attr))
                except KeyError:
                    print ("Unrecognised attribute name: %s" % parts[1])
        elif arg.startswith('run'):
            if not self.query:
                print ("No query to run.")
                return
            result = self.matcher.match(self.query, self.config['retrieve'])
            if result:
                if self.config['adapt']:
                    try:
                        result.insert(0, self.matcher.adapt(self.query, result))
                    except AdaptationError:
                        pass
                self.result = (Case(self.query), result)
                if self.config['auto_display']:
                    self.do_result("")
                elif self.interactive:
                    print ("Query run successfully. Use the 'result' command to view the result.")
            else:
                print ("no result.")
        else:
            print ("Unrecognised argument. Type 'help query' for help.")
Пример #5
0
                             dict([(k,v._adaptable) for (k,v) in possible_attributes.items()]),
                             dict([(k,v._adjustable) for (k,v) in possible_attributes.items()]),],
                            ["Attribute name", "Weight", "Adaptable", "Adjusted"])
                print "\n".join(("Weight is the weight of the attribute for case similarity.",
                                 "",
                                 "Adaptable specifies whether the attribute can be adapted to",
                                 "the query value.",
                                 "",
                                 "Adjustable specifies whether the attribute is adjusted based",
                                 "on the adaptable ones.",
                                 "",
                                 "Run 'query names <attribute>' for help on an attribute."))

            else:
                try:
                    key = key_name(parts[1], possible_attributes)
                    attr = possible_attributes[key]
                    print "\n".join(("Attribute :  %s" % key,
                                     "Weight    :  %s" % attr._weight,
                                     "Adaptable :  %s" % attr._adaptable,
                                     "Adjusted  :  %s" % attr._adjustable,
                                     ""))
                    print self.gen_help(attr)
                except KeyError:
                    print "Unrecognised attribute name: %s" % parts[1]
        elif arg.startswith('run'):
            if not self.query:
                print "No query to run."
                return
            result = self.matcher.match(self.query, self.config['retrieve'])
            if result:
Пример #6
0
    def do_query(self, arg):
        """Manipulate the query.

        query [show]                   Show current query.
        query reset                    Reset query to be empty.
        query set <attribute> <value>  Set query attribute <attribute> to <value>.
        query unset <attribute>        Unset query attribute <attribute>.
        query names [attribute]        Show possible attribute names.
        query run                      Run the current query.

        By default, the query is automatically run when changed, and
        the result is automatically displayed when run. This behaviour
        can be changed by setting respectively the 'auto_run' and
        'auto_display' config parameters."""
        if arg in ('', 'show'):
            if self.query:
                print_table([self.query], ["Attribute", "Value"])
            else:
                print("No current query.")
        elif arg == "reset":
            self.query = Case()
        elif arg.startswith('set'):
            parts = arg.split(None, 2)
            if len(parts) < 3:
                print("Usage: query set <attribute> <value>.")
                return
            arg,key,val = parts
            try:
                self.query[key_name(key, possible_attributes)] = val
                if self.config['auto_run']:
                    self.do_query("run")
            except KeyError:
                print("Invalid attribute name '%s'." % key)
                print("Possible attribute names:")
                print("\n".join(["  "+i for i in sorted(possible_attributes.keys())]))
            except ValueError as e:
                print(str(e))
        elif arg.startswith('unset'):
            parts = arg.split()
            if len(parts) < 2:
                print("Usage: query unset <attribute>.")
                return
            arg,key = parts[:2]
            try:
                key = key_name(key, possible_attributes)
                del self.query[key]
                if self.config['auto_run']:
                    self.do_query("run")
            except KeyError:
                print("Attribute '%s' not found." % key)
                return
        elif arg.startswith('names'):
            parts = arg.split()
            if len(parts) < 2:
                print("Possible attributes:")
                print_table([dict([(k,v._weight) for (k,v) in list(possible_attributes.items())]),
                             dict([(k,v._adaptable) for (k,v) in list(possible_attributes.items())]),
                             dict([(k,v._adjustable) for (k,v) in list(possible_attributes.items())]),],
                            ["Attribute name", "Weight", "Adaptable", "Adjusted"])
                print("\n".join(("Weight is the weight of the attribute for case similarity.",
                                 "",
                                 "Adaptable specifies whether the attribute can be adapted to",
                                 "the query value.",
                                 "",
                                 "Adjustable specifies whether the attribute is adjusted based",
                                 "on the adaptable ones.",
                                 "",
                                 "Run 'query names <attribute>' for help on an attribute.")))

            else:
                try:
                    key = key_name(parts[1], possible_attributes)
                    attr = possible_attributes[key]
                    print("\n".join(("Attribute :  %s" % key,
                                     "Weight    :  %s" % attr._weight,
                                     "Adaptable :  %s" % attr._adaptable,
                                     "Adjusted  :  %s" % attr._adjustable,
                                     "")))
                    print(self.gen_help(attr))
                except KeyError:
                    print("Unrecognised attribute name: %s" % parts[1])
        elif arg.startswith('run'):
            if not self.query:
                print("No query to run.")
                return
            result = self.matcher.match(self.query, self.config['retrieve'])
            if result:
                if self.config['adapt']:
                    try:
                        result.insert(0, self.matcher.adapt(self.query, result))
                    except AdaptationError:
                        pass
                self.result = (Case(self.query), result)
                if self.config['auto_display']:
                    self.do_result("")
                elif self.interactive:
                    print("Query run successfully. Use the 'result' command to view the result.")
            else:
                print("no result.")
        else:
            print("Unrecognised argument. Type 'help query' for help.")