Skip to content

Commit

Permalink
Added Python version
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanogermano committed Jul 14, 2017
1 parent 3c80882 commit d29c72d
Show file tree
Hide file tree
Showing 184 changed files with 5,444 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Python version/base/Callback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from abc import ABCMeta, abstractmethod

class Callback(object):
"""This interface is used to communicate results of solver invocation to the user"""

__metaclass__ = ABCMeta

@abstractmethod
def callback(self, o):
pass
132 changes: 132 additions & 0 deletions Python version/base/Handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
from abc import ABCMeta

class Handler(object):
"""A collection of InputProgram and OptionDescriptor.
The subclasses have to implement startAsync(Callback, List, List) and startSync(List, List) methods.
Each instance inside _programs and _options are represented by an integer (id) , respectively.
"""
__metaclass__ = ABCMeta

def __init__(self):
self._programs = dict() # Is where InputProgram elements are stored.
self._options = dict() # Is where OptionDescriptor elements are stored

def addOption(self, o):
"""Add a new element inside _options dict.
The o parameter is the new OptionDescriptor instance.
The method return the id associate to the new added OptionDescriptor instance.
"""
last_index = len(self._options)
current_value = last_index
self._options[last_index]=o
return current_value

def addProgram(self, program):
"""Add a new element inside _programs dict.
The program param is the InputProgram instance added to the collection.
The method return the id associate to the new added InputProgram instance.
"""
last_index = len(self._programs)
current_value = last_index
self._programs[last_index]=program
return current_value

def _collect_options(self, option_index):
"""Return a list of options in _options dict, according to set of indexes given.
If option_index is empty, the method return a list of all options.
"""
input_option = list()
if (not option_index):
for k in self._options.keys():
input_option.append(self._options.get(k))
else:
for index in option_index:
input_option.append(self._options.get(index))
return input_option

def _collect_programs(self, program_index):
"""Return a list of programs in _programs dict, according to set of indexes given.
If program_index is empty, the method return a list of all program.
"""
input_programs = list()
if (not program_index):
for k in self._programs.keys():
input_programs.append(self._programs.get(k))
else:
for index in program_index:
input_programs.append(self._programs.get(index))
return input_programs

def getInputProgram(self, key):
"""Returns the specified InputProgram element
The parameter key represents the id
The method return the InputProgram element associate with the given key
"""
return self._programs.get(key)

def getOptionDescriptor(self, key):
"""Returns the specified OptionDescriptor element
The parameter key represents the id
The method return the OptionDescriptor element associate with the given key
"""
return self._options.get(key)

def removeAll(self):
"""Removes all of the elements from _programs and _options.
Both of the collections will be empty after this method returns.
"""
self._options.clear()
self._programs.clear()

def removeOptionFromId(self, option_id):
"""Removes the element associate within the given id from _options dict.
option_id represents the id associate within an element.
"""
self._options.pop(option_id)

def removeOptionFromValue(self, o):
"""Removes every occurrence of a specified OptionDescriptor element from _options dict.
the parameter o represents the element to be removed
The method return true if one or more elements are removed, false otherwise
"""
result=False
for k in self._options:
if(self._options.get(k) == o):
self._options.pop(k)
result=True
return result

def removeProgramFromValue(self, p):
"""Removes every occurrence of a specified InputProgram element from _programs dict.
The parameter p represents the element to be removed
The method return true if one or more elements are removed, false otherwise
"""
result=False
for k in self._programs:
if(self._programs.get(k) == p):
self._programs.pop(k)
result=True
return result

def removeProgramFromId(self, program_id):
"""Removes the element associate within the given id from _programs} dict.
The parameter program_id represents the id associate within an element
"""
self._programs.pop(program_id)


def startAsync(self, c, program_index=None, option_index=None):
"""This method have to be implemented by subclasses to execute solver in a asynchronous way,
if no parameters are given, the entire sets of programs and option are used
"""
pass

def startSync(self, program_index=None, option_index=None):
"""This method have to be implemented by subclasses to execute solver in a synchronous way,
if no parameters are given, the entire sets of programs and option are used
"""
return None




79 changes: 79 additions & 0 deletions Python version/base/InputProgram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
class InputProgram(object):
"""Represents a generic option"""

def __init__(self):
"""Creates a new programs , setting space as default separator"""
self._programs = "" # Where programs data is stored
self.__files_paths = list() # Where associated files are stored
self._separator = " " # Used as separator for programs


def addObjectInput(self,inputObj):
raise "Functionality not implemented"

def addObjectsInput(self, inputObjs):
for inputObj in inputObjs:
self.addObjectInput(inputObj)

def addFilesPath(self, file_path):
"""Add a new file path into __files_paths
The parameter file_path represents a new file path
"""
self.__files_paths.append(file_path)


def addProgram(self, new_instruction):
"""Adds a new instruction directly into _programs
The parameter new_instruction rapresents a new programs instruction
"""
if(self._programs == None):
self._programs = new_instruction
else:
self._programs += self._separator + new_instruction

def clearFilesPaths(self):
"""After this method, __files_paths will be empty"""
del self.__files_paths[:]

def clearPrograms(self):
"""After this method, _programs will be empty"""
self._programs = None

def clearAll(self):
"""After this method, both __files_paths and _programs will be empty"""
self.clearFilesPaths()
self.clearPrograms()

def getFilesPaths(self):
"""Returns the __files_paths list"""
return self.__files_paths

def getPrograms(self):
"""Returns data stored in _programs"""
return self._programs

def getSeparator(self):
"""Separator the _separator character"""
return self._separator

def getStringOfFilesPaths(self):
"""Returns string concatenating files paths"""
to_return=""
for paths in self.__files_paths:
if(len(paths) != 0):
to_return += paths + " "
return to_return

def setPrograms(self, programs):
"""Sets _programs value to the new given one
The parameter programs, rapresents new value
"""
self._programs = programs

def setSeparator(self, separator):
"""Set programs separator to current value
The parameter separator is used as new separator
"""
self._separator = separator


36 changes: 36 additions & 0 deletions Python version/base/OptionDescriptor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class OptionDescriptor(object):
"""Represents options for a generic ASP programs"""

def __init__(self, initial_option=None):
self._options = initial_option # Where options are stored
self._separator = "" # Used as option separator

def addOption(self, option):
"""Concatenate a new option in a string format to the current _options
The parameter option is the string to be concatenated
"""
if (self._options == None or self._options == ""):
self.setOptions(option)
else:
self._options += self._separator + option

def clear(self):
"""After using this method the _options variable will be empty"""
self._options = ""

def getOptions(self):
"""Returns values stored in _options, in a string format"""
return self._options

def getSeparator(self):
"""Get separator character"""
return self._separator

def setOptions(self, option):
"""Set _option string with new string"""
self._options = option

def setSeparator(self, separator):
"""Set _separator character with new separator"""
self._separator = separator

26 changes: 26 additions & 0 deletions Python version/base/Output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Output(object):
"""Represents a generic output for a solver"""

def __init__(self, output=None, errors=None):
self._output = output # Variable in which results are stored
self._errors = errors # The errors thrown by the solver

def getErrors(self):
"""Get error string"""
return self._errors

def getOutput(self):
"""Get output string"""
return self._output

def setErrors(self, errors):
"""Set error string"""
self._errors=errors

def setOutput(self, output):
"""Set output string"""
self._output=output

def _parse(self):
"""This method have to be implemented by subclasses to parse a solver output"""
pass
23 changes: 23 additions & 0 deletions Python version/base/Service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from abc import ABCMeta, abstractmethod

class Service(object):
"""Contains generic methods for ASP Solver execution."""
__metaclass__ = ABCMeta

@abstractmethod
def startAsync(self, callback, programs, options):
"""Starts ASP solving Asyncronously on a subset of data and options.
The parameter callback is an interface used to interact with user
The parameter programs rapresents a list of InputProgram used as data.
The parameter options is a list of OptionDescriptor used as options.
"""
pass

@abstractmethod
def startSync(self, programs, options):
"""Starts ASP solving Syncronously on a subset of data and options.
The parameter programs is a list of InputProgram used as data.
The parameter options is a list of OptionDescriptor used as options.
The method return an Output element filled with results
"""
pass
Empty file added Python version/base/__init__.py
Empty file.
Loading

0 comments on commit d29c72d

Please sign in to comment.