package com.taxonic.rml.util;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.rdf4j.model.Model;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taxonic.rml.model.TriplesMap;
import com.taxonic.rml.model.impl.TriplesMapImpl;
import com.taxonic.rml.rdf_mapper.Mapper;
import com.taxonic.rml.rdf_mapper.impl.MapperImpl;
import com.taxonic.rml.vocab.Rdf;

public class RmlMappingLoader {
	private static final Logger logger = LoggerFactory.getLogger(RmlMappingLoader.class);
	
	private RmlConstantShorthandExpander shorthandExpander;
	
	public RmlMappingLoader(
		RmlConstantShorthandExpander shorthandExpander
	) {
		this.shorthandExpander = shorthandExpander;
	}

	public List<TriplesMap> load(String resource) {

		Model originalModel = IoUtils.parse(resource);
		
		Model model = shorthandExpander.apply(originalModel);
		
		Mapper mapper = new MapperImpl();
		
		return
		Collections.unmodifiableList(
			model
				.filter(null, Rdf.Rml.logicalSource, null)
				.subjects()
				.stream()
				.<TriplesMap>map(r -> mapper.map(model, r, TriplesMapImpl.class))
				.collect(Collectors.toList())
		);
	}
	
	public static RmlMappingLoader build() {
		return new RmlMappingLoader(
			new RmlConstantShorthandExpander()
		);
	}
}

@@ -41,27 +41,20 @@
 va.util.
-Collections
+List
 ;%0Aimport
@@ -56,35 +56,34 @@
 mport java.util.
-Lis
+Se
 t;%0Aimport java.u
@@ -90,25 +90,25 @@
 til.
-stream.Collectors
+function.Function
 ;%0A%0Ai
@@ -159,54 +159,132 @@
 org.
-slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
+eclipse.rdf4j.model.Resource;%0Aimport com.google.common.collect.ImmutableList;%0Aimport com.google.common.collect.ImmutableSet;
 %0Aimp
@@ -409,65 +409,28 @@
 per.
-Mapper;%0Aimport com.taxonic.rml.rdf_mapper.impl.MapperImpl
+util.RdfObjectLoader
 ;%0Aim
@@ -498,95 +498,8 @@
 r %7B%0A
-%09private static final Logger logger = LoggerFactory.getLogger(RmlMappingLoader.class);%0A
 %09%0A%09p
@@ -685,16 +685,64 @@
 er;%0A%09%7D%0A%0A
+%09// TODO: PM: shouldn't the return type be Set?%0A
 %09public 
@@ -841,278 +841,97 @@
 %09%0A%09%09
-Model model = shorthandExpander.apply(originalModel);%0A%09%09%0A%09%09Mapper mapper = new MapperImpl();%0A%09%09%0A%09%09return%0A%09%09Collections.unmodifiableList(%0A%09%09%09model%0A%09%09%09%09.filter(null, Rdf.Rml.logicalSource, null)%0A%09%09%09%09.subjects()%0A%09%09%09%09.stream()%0A%09%09%09%09.%3CTriplesMap%3Emap(r -%3E mapper.map(model, r, 
+return %0A%09%09%09ImmutableList.copyOf(%0A%09%09%09%09RdfObjectLoader.load(%0A%09%09%09%09%09selectTriplesMaps, %0A%09%09%09%09%09
 Trip
@@ -950,50 +950,70 @@
 lass
-))
+, 
 %0A%09%09%09%09
-.collect(Collectors.toList()
+%09originalModel, %0A%09%09%09%09%09shorthandExpander%0A%09%09%09%09
 )%0A%09%09
+%09
 );%0A
+%09%09%0A
 %09%7D%0A%09
@@ -1132,10 +1132,194 @@
 %09%09);%0A%09%7D%0A
+%09%0A%09private static Function%3CModel, Set%3CResource%3E%3E selectTriplesMaps = %0A%09%09model -%3E%0A%09%09%09ImmutableSet.copyOf(%0A%09%09%09%09model%0A%09%09%09%09.filter(null, Rdf.Rml.logicalSource, null)%0A%09%09%09%09.subjects()%0A%09%09%09);%0A
 %7D%0A

integrate RdfObjectLoader in RmlMappingLoader