src.clapi package

Submodules

src.clapi.clapi module

Clingo API module for organizing and processing course data.

process_course_data_clingo

Converts a JSON file data to a Clingo.

translate_range

Converts a range of numbers in a string to a Clingo-compatible format.

append_rules

Appends the contents of multiple files into a single output file.

query_clingo

Queries a Clingo knowledge base/graph file using a given query.

process_honors_courses

Processes honors courses from a JSON file and writes them to a list in the form: honors(course_id).

process_repeatable_courses

Processes repeatable courses from a JSON file and writes them to a list in the form: repeatable(course_id, times_repeatable, max_credits).

exception src.clapi.clapi.ClingoSatistfiablityError[source]

Bases: Exception

Exception raised when Clingo returns UNSATISFIABLE.

exception src.clapi.clapi.ClingoSyntaxError[source]

Bases: Exception

Exception 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 .lp files.

Parameters:
  • file_list (List[str]) – Input list of files to be appended.

  • output_file (str) – New file to be created with the appended contents.

Return type:

str

Returns:

Path to the output file.

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

Usage example:
>>> clingo_file = process_course_data_clingo(json_file="course_data.json")
Parameters:
  • json_file (Union[KnowledgeBase, KnowledgeGraph, str]) – Input JSON file (or KnowledgeBase or KnowledgeGraph object) 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:

str

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")
Parameters:

file_path (str) – Path to the JSON file containing course data.

Return type:

List[str]

Returns:

List of honors courses.

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:
  • json_file (str) – Input JSON file containing course data.

  • other_courses (Optional[Iterable[Tuple[str, str, str]]]) – Iterable of tuples of other courses that are repeatable. Each tuple should have exactly 3 elements: course_id, times_repeatable, max_credits. Defaults to None.

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:

List[str]

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 clingo is 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 (or KnowledgeBase or KnowledgeGraph object) to be queried.

  • verbose (bool) – Prints verbose output if set to True. Defaults to False.

  • 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:

str

Returns:

Query results.

Raises:
src.clapi.clapi.translate_range(input_string)[source]

Converts a range of numbers in a string to a Clingo-compatible format.

Usage example:
>>> translate_range("0 - 9")
'0..9'
Parameters:

input_string (str) – Input string containing a range of numbers (e.g. 0 - 9).

Return type:

str

Returns:

Clingo-compatible range format (e.g. 0..9).

Module contents