src/HOL/Tools/Sledgehammer/MaSh/src/dictionaries.py
author blanchet
Thu, 14 Nov 2013 15:57:48 +0100
changeset 54432 68f8bd1641da
parent 54056 8298976acb54
child 54692 5ce1b9613705
permissions -rw-r--r--
have MaSh support nameless facts (i.e. proofs) and use that support
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50222
40e3c3be6bca added file headers
blanchet
parents: 50220
diff changeset
     1
#     Title:      HOL/Tools/Sledgehammer/MaSh/src/dictionaries.py
40e3c3be6bca added file headers
blanchet
parents: 50220
diff changeset
     2
#     Author:     Daniel Kuehlwein, ICIS, Radboud University Nijmegen
53555
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
     3
#     Copyright   2012-2013
50222
40e3c3be6bca added file headers
blanchet
parents: 50220
diff changeset
     4
#
40e3c3be6bca added file headers
blanchet
parents: 50220
diff changeset
     5
# Persistent dictionaries: accessibility, dependencies, and features.
40e3c3be6bca added file headers
blanchet
parents: 50220
diff changeset
     6
54056
8298976acb54 more robustness in MaSh
blanchet
parents: 53555
diff changeset
     7
import sys
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
     8
from os.path import join
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
     9
from Queue import Queue
53555
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    10
from readData import create_accessible_dict,create_dependencies_dict
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    11
from cPickle import load,dump
54056
8298976acb54 more robustness in MaSh
blanchet
parents: 53555
diff changeset
    12
from exceptions import LookupError
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    13
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    14
class Dictionaries(object):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    15
    '''
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    16
    This class contains all info about name-> id mapping, etc.
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    17
    '''
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    18
    def __init__(self):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    19
        '''
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    20
        Constructor
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    21
        '''
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    22
        self.nameIdDict = {}
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    23
        self.idNameDict = {}
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    24
        self.featureIdDict={}
54432
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
    25
        self.maxNameId = 1
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    26
        self.maxFeatureId = 0
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    27
        self.featureDict = {}
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    28
        self.dependenciesDict = {}
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    29
        self.accessibleDict = {}
50840
a5cc092156da new version of MaSh Python component
blanchet
parents: 50827
diff changeset
    30
        self.expandedAccessibles = {}
53555
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    31
        self.accFile =  ''
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    32
        self.changed = True
54432
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
    33
        # Unnamed facts
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
    34
        self.nameIdDict[''] = 0
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
    35
        self.idNameDict[0] = 'Unnamed Fact'
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
    36
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    37
    """
50951
e1cbaa7d5536 updated MaSh
blanchet
parents: 50840
diff changeset
    38
    Init functions. nameIdDict, idNameDict, featureIdDict, articleDict get filled!
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
    39
    """
53555
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    40
    def init_featureDict(self,featureFile):
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    41
        self.create_feature_dict(featureFile)
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    42
        #self.featureDict,self.maxNameId,self.maxFeatureId,self.featureCountDict,self.triggerFeaturesDict,self.featureTriggeredFormulasDict =\
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    43
        # create_feature_dict(self.nameIdDict,self.idNameDict,self.maxNameId,self.featureIdDict,self.maxFeatureId,self.featureCountDict,\
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    44
        #                     self.triggerFeaturesDict,self.featureTriggeredFormulasDict,sineFeatures,featureFile)
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    45
    def init_dependenciesDict(self,depFile):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    46
        self.dependenciesDict = create_dependencies_dict(self.nameIdDict,depFile)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    47
    def init_accessibleDict(self,accFile):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    48
        self.accessibleDict,self.maxNameId = create_accessible_dict(self.nameIdDict,self.idNameDict,self.maxNameId,accFile)
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
    49
50951
e1cbaa7d5536 updated MaSh
blanchet
parents: 50840
diff changeset
    50
    def init_all(self,args):
e1cbaa7d5536 updated MaSh
blanchet
parents: 50840
diff changeset
    51
        self.featureFileName = 'mash_features'
e1cbaa7d5536 updated MaSh
blanchet
parents: 50840
diff changeset
    52
        self.accFileName = 'mash_accessibility'
e1cbaa7d5536 updated MaSh
blanchet
parents: 50840
diff changeset
    53
        featureFile = join(args.inputDir,self.featureFileName)
e1cbaa7d5536 updated MaSh
blanchet
parents: 50840
diff changeset
    54
        depFile = join(args.inputDir,args.depFile)
53555
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    55
        self.accFile = join(args.inputDir,self.accFileName)
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    56
        self.init_featureDict(featureFile)
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    57
        self.init_accessibleDict(self.accFile)
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    58
        self.init_dependenciesDict(depFile)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    59
        self.expandedAccessibles = {}
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    60
        self.changed = True
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
    61
53555
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    62
    def create_feature_dict(self,inputFile):
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    63
        self.featureDict = {}
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    64
        IS = open(inputFile,'r')
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    65
        for line in IS:
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    66
            line = line.split(':')
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    67
            name = line[0]
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    68
            # Name Id
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    69
            if self.nameIdDict.has_key(name):
54056
8298976acb54 more robustness in MaSh
blanchet
parents: 53555
diff changeset
    70
                raise LookupError('%s appears twice in the feature file. Aborting.'% name)
53555
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    71
                sys.exit(-1)
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    72
            else:
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    73
                self.nameIdDict[name] = self.maxNameId
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    74
                self.idNameDict[self.maxNameId] = name
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    75
                nameId = self.maxNameId
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    76
                self.maxNameId += 1
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    77
            features = self.get_features(line)
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    78
            # Store results
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    79
            self.featureDict[nameId] = features
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    80
        IS.close()
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    81
        return
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
    82
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    83
    def get_name_id(self,name):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    84
        """
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    85
        Return the Id for a name.
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    86
        If it doesn't exist yet, a new entry is created.
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    87
        """
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    88
        if self.nameIdDict.has_key(name):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    89
            nameId = self.nameIdDict[name]
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    90
        else:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    91
            self.nameIdDict[name] = self.maxNameId
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    92
            self.idNameDict[self.maxNameId] = name
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    93
            nameId = self.maxNameId
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
    94
            self.maxNameId += 1
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    95
            self.changed = True
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    96
        return nameId
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    97
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    98
    def add_feature(self,featureName):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
    99
        if not self.featureIdDict.has_key(featureName):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   100
            self.featureIdDict[featureName] = self.maxFeatureId
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   101
            self.maxFeatureId += 1
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   102
            self.changed = True
50827
aba769dc82e9 updated MaSh Python component
blanchet
parents: 50619
diff changeset
   103
        fId = self.featureIdDict[featureName]
aba769dc82e9 updated MaSh Python component
blanchet
parents: 50619
diff changeset
   104
        return fId
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   105
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   106
    def get_features(self,line):
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   107
        featureNames = [f.strip() for f in line[1].split()]
53555
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
   108
        features = {}
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   109
        for fn in featureNames:
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   110
            tmp = fn.split('=')
53555
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
   111
            weight = 1.0 
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   112
            if len(tmp) == 2:
50827
aba769dc82e9 updated MaSh Python component
blanchet
parents: 50619
diff changeset
   113
                fn = tmp[0]
aba769dc82e9 updated MaSh Python component
blanchet
parents: 50619
diff changeset
   114
                weight = float(tmp[1])
aba769dc82e9 updated MaSh Python component
blanchet
parents: 50619
diff changeset
   115
            fId = self.add_feature(tmp[0])
53555
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
   116
            features[fId] = weight
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
   117
            #features[fId] = 1.0 ###
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   118
        return features
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   119
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   120
    def expand_accessibles(self,acc):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   121
        accessibles = set(acc)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   122
        unexpandedQueue = Queue()
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   123
        for a in acc:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   124
            if self.expandedAccessibles.has_key(a):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   125
                accessibles = accessibles.union(self.expandedAccessibles[a])
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   126
            else:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   127
                unexpandedQueue.put(a)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   128
        while not unexpandedQueue.empty():
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   129
            nextUnExp = unexpandedQueue.get()
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   130
            nextUnExpAcc = self.accessibleDict[nextUnExp]
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   131
            for a in nextUnExpAcc:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   132
                if not a in accessibles:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   133
                    accessibles = accessibles.union([a])
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   134
                    if self.expandedAccessibles.has_key(a):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   135
                        accessibles = accessibles.union(self.expandedAccessibles[a])
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   136
                    else:
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   137
                        unexpandedQueue.put(a)
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   138
        return list(accessibles)
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   139
54056
8298976acb54 more robustness in MaSh
blanchet
parents: 53555
diff changeset
   140
    def parse_unExpAcc(self,line):
8298976acb54 more robustness in MaSh
blanchet
parents: 53555
diff changeset
   141
        try:
8298976acb54 more robustness in MaSh
blanchet
parents: 53555
diff changeset
   142
            unExpAcc = [self.nameIdDict[a.strip()] for a in line.split()]            
8298976acb54 more robustness in MaSh
blanchet
parents: 53555
diff changeset
   143
        except:
8298976acb54 more robustness in MaSh
blanchet
parents: 53555
diff changeset
   144
            raise LookupError('Cannot find the accessibles:%s. Accessibles need to be introduced before referring to them.' % line)
8298976acb54 more robustness in MaSh
blanchet
parents: 53555
diff changeset
   145
        return unExpAcc
8298976acb54 more robustness in MaSh
blanchet
parents: 53555
diff changeset
   146
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   147
    def parse_fact(self,line):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   148
        """
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   149
        Parses a single line, extracting accessibles, features, and dependencies.
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   150
        """
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   151
        assert line.startswith('! ')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   152
        line = line[2:]
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   153
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   154
        # line = name:accessibles;features;dependencies
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   155
        line = line.split(':')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   156
        name = line[0].strip()
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   157
        nameId = self.get_name_id(name)
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   158
        line = line[1].split(';')
50840
a5cc092156da new version of MaSh Python component
blanchet
parents: 50827
diff changeset
   159
        features = self.get_features(line)
a5cc092156da new version of MaSh Python component
blanchet
parents: 50827
diff changeset
   160
        self.featureDict[nameId] = features
54432
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   161
        try:
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   162
            self.dependenciesDict[nameId] = [self.nameIdDict[d.strip()] for d in line[2].split()]        
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   163
        except:
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   164
            unknownDeps = []
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   165
            for d in line[2].split():
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   166
                if not self.nameIdDict.has_key(d):
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   167
                    unknownDeps.append(d)
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   168
            raise LookupError('Unknown fact used as dependency: %s. Facts need to be introduced before being used as depedency.' % ','.join(unknownDeps))
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   169
        self.accessibleDict[nameId] = self.parse_unExpAcc(line[0])
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   170
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   171
        self.changed = True
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   172
        return nameId
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   173
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   174
    def parse_overwrite(self,line):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   175
        """
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   176
        Parses a single line, extracts the problemId and the Ids of the dependencies.
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   177
        """
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   178
        assert line.startswith('p ')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   179
        line = line[2:]
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   180
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   181
        # line = name:dependencies
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   182
        line = line.split(':')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   183
        name = line[0].strip()
54432
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   184
        try:
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   185
            nameId = self.nameIdDict[name]
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   186
        except:
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   187
            raise LookupError('Trying to overwrite dependencies for unknown fact: %s. Facts need to be introduced before overwriting them.' % name)
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   188
        try:
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   189
            dependencies = [self.nameIdDict[d.strip()] for d in line[1].split()]
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   190
        except:
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   191
            unknownDeps = []
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   192
            for d in line[1].split():
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   193
                if not self.nameIdDict.has_key(d):
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   194
                    unknownDeps.append(d)
68f8bd1641da have MaSh support nameless facts (i.e. proofs) and use that support
blanchet
parents: 54056
diff changeset
   195
            raise LookupError('Unknown fact used as dependency: %s. Facts need to be introduced before being used as depedency.' % ','.join(unknownDeps))
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   196
        self.changed = True
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   197
        return nameId,dependencies
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   198
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   199
    def parse_problem(self,line):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   200
        """
50827
aba769dc82e9 updated MaSh Python component
blanchet
parents: 50619
diff changeset
   201
        Parses a problem and returns the features, the accessibles, and any hints.
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   202
        """
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   203
        assert line.startswith('? ')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   204
        line = line[2:]
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   205
        name = None
53100
1133b9e83f09 new version of MaSh tool -- experimental server
blanchet
parents: 50951
diff changeset
   206
        numberOfPredictions = None
1133b9e83f09 new version of MaSh tool -- experimental server
blanchet
parents: 50951
diff changeset
   207
54056
8298976acb54 more robustness in MaSh
blanchet
parents: 53555
diff changeset
   208
        # How many predictions should be returned:
53100
1133b9e83f09 new version of MaSh tool -- experimental server
blanchet
parents: 50951
diff changeset
   209
        tmp = line.split('#')
1133b9e83f09 new version of MaSh tool -- experimental server
blanchet
parents: 50951
diff changeset
   210
        if len(tmp) == 2:
1133b9e83f09 new version of MaSh tool -- experimental server
blanchet
parents: 50951
diff changeset
   211
            numberOfPredictions = int(tmp[0].strip())
1133b9e83f09 new version of MaSh tool -- experimental server
blanchet
parents: 50951
diff changeset
   212
            line = tmp[1]
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   213
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   214
        # Check whether there is a problem name:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   215
        tmp = line.split(':')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   216
        if len(tmp) == 2:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   217
            name = tmp[0].strip()
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   218
            line = tmp[1]
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   219
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   220
        # line = accessibles;features
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   221
        line = line.split(';')
54056
8298976acb54 more robustness in MaSh
blanchet
parents: 53555
diff changeset
   222
        features = self.get_features(line)
8298976acb54 more robustness in MaSh
blanchet
parents: 53555
diff changeset
   223
        
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   224
        # Accessible Ids, expand and store the accessibles.
54056
8298976acb54 more robustness in MaSh
blanchet
parents: 53555
diff changeset
   225
        #unExpAcc = [self.nameIdDict[a.strip()] for a in line[0].split()]
8298976acb54 more robustness in MaSh
blanchet
parents: 53555
diff changeset
   226
        unExpAcc = self.parse_unExpAcc(line[0])        
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   227
        if len(self.expandedAccessibles.keys())>=100:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   228
            self.expandedAccessibles = {}
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   229
            self.changed = True
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   230
        for accId in unExpAcc:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   231
            if not self.expandedAccessibles.has_key(accId):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   232
                accIdAcc = self.accessibleDict[accId]
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   233
                self.expandedAccessibles[accId] = self.expand_accessibles(accIdAcc)
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   234
                self.changed = True
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   235
        accessibles = self.expand_accessibles(unExpAcc)
54056
8298976acb54 more robustness in MaSh
blanchet
parents: 53555
diff changeset
   236
        
50827
aba769dc82e9 updated MaSh Python component
blanchet
parents: 50619
diff changeset
   237
        # Get hints:
aba769dc82e9 updated MaSh Python component
blanchet
parents: 50619
diff changeset
   238
        if len(line) == 3:
aba769dc82e9 updated MaSh Python component
blanchet
parents: 50619
diff changeset
   239
            hints = [self.nameIdDict[d.strip()] for d in line[2].split()]
aba769dc82e9 updated MaSh Python component
blanchet
parents: 50619
diff changeset
   240
        else:
aba769dc82e9 updated MaSh Python component
blanchet
parents: 50619
diff changeset
   241
            hints = []
50619
b958a94cf811 new version of MaSh, with theory-level reasoning
blanchet
parents: 50388
diff changeset
   242
53100
1133b9e83f09 new version of MaSh tool -- experimental server
blanchet
parents: 50951
diff changeset
   243
        return name,features,accessibles,hints,numberOfPredictions
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   244
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   245
    def save(self,fileName):
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   246
        if self.changed:
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   247
            dictsStream = open(fileName, 'wb')
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   248
            dump((self.accessibleDict,self.dependenciesDict,self.expandedAccessibles,self.featureDict,\
53555
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
   249
                self.featureIdDict,self.idNameDict,self.maxFeatureId,self.maxNameId,self.nameIdDict),dictsStream)
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   250
            self.changed = False
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   251
            dictsStream.close()
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   252
    def load(self,fileName):
50388
a5b666e0c3c2 added weights to MaSh (by Daniel Kuehlwein)
blanchet
parents: 50222
diff changeset
   253
        dictsStream = open(fileName, 'rb')
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   254
        self.accessibleDict,self.dependenciesDict,self.expandedAccessibles,self.featureDict,\
53555
12251bc889f1 new version of MaSh
blanchet
parents: 53100
diff changeset
   255
              self.featureIdDict,self.idNameDict,self.maxFeatureId,self.maxNameId,self.nameIdDict = load(dictsStream)
50220
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   256
        self.changed = False
90280d85cd03 moved MaSh's Python code into Isabelle
blanchet
parents:
diff changeset
   257
        dictsStream.close()