movieListFromTurtle static method

Map<String, dynamic>? movieListFromTurtle(
  1. String ttlContent
)

Parses a MovieList from TTL content and extracts movies. Returns a map containing movieList metadata and movies list.

Implementation

static Map<String, dynamic>? movieListFromTurtle(String ttlContent) {
  try {
    // Parse using proper RDF.

    final triples = turtleToTripleMap(ttlContent);

    String? listId;
    String? listName;
    String? description;
    String? filePath;
    final Set<String> movieResourceIds = {};

    // Find MovieList resource and extract movie references.

    for (final subject in triples.keys) {
      final predicates = triples[subject]!;
      final typeValues =
          predicates['http://www.w3.org/1999/02/22-rdf-syntax-ns#type'] ?? [];

      // Check for MovieList.

      final isMovieList = typeValues.any((type) =>
          type.toString().contains('MovieList') ||
          type == 'https://sii.anu.edu.au/onto/moviestar#MovieList');

      if (isMovieList) {
        // Extract MovieList metadata.

        for (final predicate in predicates.keys) {
          final values = predicates[predicate]!;
          if (values.isNotEmpty) {
            final value = values.first.toString();

            if (predicate.contains('identifier')) {
              listId = value;
            } else if (predicate.contains('name')) {
              listName = value;
            } else if (predicate.contains('description')) {
              description = value;
            } else if (predicate.contains('filePath')) {
              filePath = value;
            } else if (predicate.contains('hasMovie')) {
              // Extract movie resource references.

              for (final movieRef in values) {
                final movieRefStr = movieRef.toString();
                // Extract movie ID from resource URI like "movie-12345" (lowercase).

                final movieIdMatch =
                    RegExp(r'movie-(\d+)').firstMatch(movieRefStr);
                if (movieIdMatch != null) {
                  movieResourceIds.add(movieIdMatch.group(1)!);
                }
              }
            }
          }
        }
      }
    }

    // Create placeholder Movie objects from the movie references.
    // In the new ontology structure, MovieLists only contain references.
    // The actual movie data is in separate files.

    final List<Movie> movies = [];
    for (final movieId in movieResourceIds) {
      try {
        // Create a minimal Movie object with just the ID.
        // The full movie data should be loaded separately when needed.

        final movie = Movie(
          id: int.parse(movieId),
          title: 'Movie $movieId',
          overview: '',
          posterUrl: '',
          backdropUrl: '',
          releaseDate: DateTime.now(),
          voteAverage: 0.0,
          genreIds: [],
        );
        movies.add(movie);
      } catch (e) {
        debugPrint('❌ Failed to parse movie ID $movieId: $e');
      }
    }

    return {
      'id': listId,
      'name': listName ?? 'Unknown List',
      'description': description,
      'filePath': filePath,
      'movies': movies,
    };
  } catch (e) {
    debugPrint('❌ Error parsing MovieList from TTL: $e');
    return null;
  }
}