Пример #1
0
    def dispatch(self, match, network):

        if not self.test_conditions(match, network):
            return

        if self.condcode:
            if not self.condcode.test(match, network):
                return


        cons = []
        for con in self.consecuences:
            cons.append(con.substitute(match))

        for con in self.vconsecuences:
            new_pred = match[con.name].copy()
            cons.append(new_pred)

        for con in cons:
            factset = network.present
            if isa(con, network.lexicon.exclusive_endure):
                old_pred = Predicate(con.true, con.term_type)
                old_pred.add_object('subj', con.get_object('subj'))
                for label in con.objects:
                    if label.startswith('u-'):
                        old_pred.add_object(label, con.get_object(label))
                network.finish(old_pred)
            elif isa(con, network.lexicon.finish):
                tofinish = con.get_object('what')
                network.finish(tofinish)
            # XXX make contradiction configurabe
            #neg = con.copy()
            #neg.true = not neg.true
            #contradiction = factset.query(neg)
            #if contradiction:
            #    raise exceptions.Contradiction('we already have ' + str(neg))
            if factset.query_facts(con, {}).count() == 0:
                if isa(con, network.lexicon.endure):
                    con.add_object('since_', network.lexicon.now_term)
                fact = factset.add_fact(con)
                if isa(con, network.lexicon.happen):
                    if network.pipe is not None:
                        network.pipe.send_bytes(str(con).encode('utf8'))
                if network.root.child_path:
                    logger.debug('con to add: ' + str(con))
                    m = Match(con)
                    m.paths = network.get_paths(con)
                    logger.debug('con in fact: ' + str(fact.pred))
                    m.fact = fact
                    network.activations.append(m)
Пример #2
0
 def query(self, pred):
     taken_vars = {}
     qfacts = self.query_facts(pred, taken_vars)
     matches = []
     for fact in qfacts:
         match = Match(fact.pred, query=pred)
         match.fact = fact
         for name, path in taken_vars.items():
             cls = self._get_nclass(path[0])
             preds = True
             if 'Verb' in name[1:]:
                 preds = False
             value = cls.resolve(fact.pred, path[0], self, preds=preds)
             match[name] = value
         matches.append(match)
     return matches
Пример #3
0
    def add_fact(self, pred):
        factset = self.present
        if isa(pred, self.lexicon.exclusive_endure):
            old_pred = Predicate(pred.true, pred.term_type)
            old_pred.add_object('subj', pred.get_object('subj'))
            for label in pred.objects:
                if label.startswith('u-'):
                    old_pred.add_object(label, pred.get_object(label))
            self.finish(old_pred)
        elif isa(pred, self.lexicon.finish):
            tofinish = pred.get_object('what')
            self.finish(tofinish)
        #neg = pred.copy()
        #neg.true = not neg.true
        #contradiction = factset.query(neg)
        #if contradiction:
        #    raise exceptions.Contradiction('we already have ' + str(neg))

        facts = factset.query_facts(pred, {})
        if facts.count() == 0:
            if isa(pred, self.lexicon.endure):
                pred.add_object('since_', self.lexicon.now_term)
            fact = factset.add_fact(pred)
            if isa(pred, self.lexicon.happen):
                if self.pipe is not None:
                    self.pipe.send_bytes(str(pred).encode('utf8'))
            if self.root.child_path:
                m = Match(pred)
                m.paths = self.get_paths(pred)
                m.fact = fact
                Node.dispatch(self.root, m, self)
            n = 0
            while self.activations:
                n += 1
                cmc = int(self.config['commit_many_consecuences'])
                if cmc and n % cmc == 0:
                    self.session.commit()
                match = self.activations.pop(0)
                Node.dispatch(self.root, match, self)
            return fact
        else:
            return facts.first()