Esempio n. 1
0
    def __ground__(self, programs, additionalProgramText):
        try:
            additionalPrograms = []
            if additionalProgramText != None:
                (fd, fn) = tempfile.mkstemp('.lp')
                file = os.fdopen(fd, 'w')
                file.write(str(additionalProgramText))
                file.close()
                additionalPrograms.append(fn)

            addoptions = []
            if self.gringo_options:
                addoptions = re.findall(r'(?:[^\s,"]|"(?:\\.|[^"])*")+', self.gringo_options)

            commandline = filter_empty_str([self.gringo_bin] + addoptions + programs + additionalPrograms)
            self._gringo = subprocess.Popen(commandline, stderr=subprocess.PIPE, stdout=subprocess.PIPE)

        except OSError as e:
            if e.errno == 2:
                raise OSError('Grounder \'%s\' not found' % self.gringo_bin)
            else:
                raise e

        grounding, self.gringo_stderr = self._gringo.communicate()
        if self._gringo.returncode not in self.gringo_noerror_retval:
            raise EnvironmentError("got error %d from gringo:\n%s" % \
                (self._gringo.returncode, self.gringo_stderr.decode()))

        return grounding
Esempio n. 2
0
    def version_text(gringo_bin=BIN_GRINGO3,
                     clasp_bin=BIN_CLASP):
        """Return the version text"""
        if gringo_bin:
            try:
                commandline = filter_empty_str([gringo_bin] + ['--version'])
                gringo = subprocess.Popen(commandline, stderr=subprocess.PIPE,
                                          stdout=subprocess.PIPE)
            except OSError as e:
                if e.errno == 2:
                    raise OSError('Grounder \'%s\' not found' % gringo_bin)
                else:
                    raise e
            gringo_version, _ = gringo.communicate()
            gringo_version = gringo_version.decode('utf-8')
        else:
            gringo_version = None

        if clasp_bin:
            try:
                clasp = subprocess.Popen(
                    filter_empty_str([clasp_bin] + ['--version']),
                    stdin = subprocess.PIPE,
                    stderr = subprocess.PIPE,
                    stdout = subprocess.PIPE
                )

            except OSError as e:
                if e.errno == 2:
                    raise Exception('Solver \'%s\' not found' % clasp_bin)
                else:
                    raise e
            clasp_version, _ = clasp.communicate()
            clasp_version = clasp_version.decode('utf-8')
        else:
            clasp_version = None
        return gringo_version, clasp_version
Esempio n. 3
0
    def __solve__(self, grounding, opts=[], json=True):
        try:
            #opts = opts + ['--stats']
            addoptions = []
            if self.clasp_options:
                addoptions = self.clasp_options.split()

            if json:
                opts = opts + ['--outf=2']
            self._clasp = subprocess.Popen(
                filter_empty_str([self.clasp_bin] + addoptions  + opts),
                stdin = subprocess.PIPE,
                stderr = subprocess.PIPE,
                stdout = subprocess.PIPE)

        except OSError as e:
            if e.errno == 2:
                raise Exception('Solver \'%s\' not found' % self.clasp_bin)
            else:
                raise e

        solving, self.clasp_stderr = self._clasp.communicate(grounding)
        self.clasp_stderr = solving + self.clasp_stderr

        if self._clasp.returncode not in self.clasp_noerror_retval:
            error = "got error %d from clasp:\n%s" % (self._clasp.returncode,
                                                      self.clasp_stderr.decode())
            try:
                if self.gringo_stderr:
                    error += "\n\nFrom gringo:\n%s" % self.gringo_stderr
            except AttributeError:  # no gringo error
                pass
            raise EnvironmentError(error)

        self._clasp = None
        self._gringo = None

        return solving