src.clapi package
Submodules
src.clapi.clapi module
Clingo API module for organizing and processing course data.
Converts a JSON file data to a Clingo. |
|
Converts a range of numbers in a string to a Clingo-compatible format. |
|
Appends the contents of multiple files into a single output file. |
|
Queries a Clingo knowledge base/graph file using a given query. |
|
Processes honors courses from a JSON file and writes them to a list in the form: |
|
Processes repeatable courses from a JSON file and writes them to a list in the form: |
- exception src.clapi.clapi.ClingoSatistfiablityError[source]
Bases:
ExceptionException raised when Clingo returns
UNSATISFIABLE.
- exception src.clapi.clapi.ClingoSyntaxError[source]
Bases:
ExceptionException raised when there is a parsing/syntax error in the Clingo file.
- src.clapi.clapi.append_rules(file_list, output_file)[source]
Appends the contents of multiple files into a single output file. Intended for use with Clingo
.lpfiles.
- src.clapi.clapi.process_course_data_clingo(json_file, output_file=None, repeatable_courses=None)[source]
Converts a JSON file data to a Clingo. This function processes course data from a JSON file and writes it to a Clingo file in the form:
course(course_id, credits, career, spring1, fall1, spring2, fall2).- The corresponding rules for antirequisites, prerequisites, and corequisites are also generated if present, and are of the form:
- Antirequisites:
:- course(course_id, _, career, _, _, _, _), course(antireq_id, _, career, _, _, _, _).
- Prerequisites:
:- course(course_id, _, career, _, _, _, _), not course(prereq_id, _, career, _, _, _, _).
- Corequisites:
:- course(course_id, _, career, _, _, _, _), not course(coreq_id, _, career, _, _, _, _).
Note
If a
KnowledgeBaseorKnowledgeGraphobject is passed, then the Clingo output filepath is updated in the object.
- Usage example:
>>> clingo_file = process_course_data_clingo(json_file="course_data.json")
- Parameters:
json_file (
Union[KnowledgeBase,KnowledgeGraph,str]) – Input JSON file (orKnowledgeBaseorKnowledgeGraphobject) to be converted to Clingo.output_file (
Optional[str]) – Output filename. If not specified, then a new file of the same name is created, with an ‘.lp’ file extension. Defaults to None.repeatable_courses (
Optional[List[Tuple[str,str,str]]]) – List of tuples of other courses that are repeatable. Each tuple should have exactly 3 elements: course_id, times_repeatable, max_credits. Defaults to None.
- Return type:
- Returns:
Output Clingo knowledge base file path.
- src.clapi.clapi.process_honors_courses(file_path)[source]
Processes honors courses from a JSON file and writes them to a list in the form:
honors(course_id).Note
Mainly intended for use with SBU CSE courses.
- Usage example:
>>> process_honors_courses(file_path="cse_courses.json")
- src.clapi.clapi.process_repeatable_courses(json_file, other_courses=None)[source]
Processes repeatable courses from a JSON file and writes them to a list in the form:
repeatable(course_id, times_repeatable, max_credits).Note
The input JSON file should contain course data.
The repeatable courses are identified based on the course description.
The function also accepts a list of other courses that are repeatable.
- Usage example:
>>> process_repeatable_courses(json_file="course_data.json", other_courses=[("cse593", "_", "_")] ['repeatable(cse390, 2, _).', 'repeatable(cse391, 2, _).', 'repeatable(cse392, 2, _).', 'repeatable(cse393, 2, _).', 'repeatable(cse394, 2, _).', 'repeatable(cse475, 2, _).', 'repeatable(cse488, _, 12).', . . . 'repeatable(cse593, _, _).', . . . 'repeatable(cse693, 2, _).']
- Parameters:
- Raises:
ValueError – If each course tuple does not have exactly 3 elements, or if any of the 2nd or 3rd elements of the tuple do not contain an integer or the string “_”.
- Return type:
- Returns:
List of repeatable courses.
- src.clapi.clapi.query_clingo(knowledge, verbose=False, num_models=None, configuration='handy', parallel_mode=None, query=None)[source]
Queries a Clingo knowledge base/graph file using a given query.
Warning
This function assumes that
clingois installed and is accessible via the system path variable.
Note
The query must be included in the Clingo file.
- Parameters:
knowledge (
Union[KnowledgeBase,KnowledgeGraph,str]) – Input Clingo knowledge base/graph file (orKnowledgeBaseorKnowledgeGraphobject) to be queried.verbose (
bool) – Prints verbose output if set toTrue. Defaults toFalse.num_models (
Optional[int]) – Number of models to generate. Defaults to None.configuration (
str) – Clingo configuration. Defaults to"handy".parallel_mode (
Optional[int]) – Parallel mode, maximum number of threads. Defaults to None.query (
Union[str,Tuple[str],None]) – Filepaths to query files (.lp files), to be passed to Clingo. Defaults to None.
- Return type:
- Returns:
Query results.
- Raises:
ValueError – If the query is not a string or a tuple of strings.
ClingoSatistfiablityError – If the query returns
UNSATISFIABLE.ClingoSyntaxError – If there is a parsing/syntax error in the Clingo file.
DependencyError – If Clingo is not installed or added to the system path variable.