Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package it.unibz.krdb.obda.sesame.r2rml;
  
  /*
   * #%L
   * ontop-obdalib-sesame
   * %%
   * Copyright (C) 2009 - 2014 Free University of Bozen-Bolzano
   * %%
   * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * #L%
  */

Author(s):
timea bagosi Class responsible of parsing R2RML mappings from file or from an RDF graph
 
 
 
 import java.io.File;
 import java.net.URL;
 import java.util.List;
 import java.util.Set;
 
 
 public class R2RMLManager {
 	
 	private OBDADataFactory fac = OBDADataFactoryImpl.getInstance();
 	
 	
 	private R2RMLParser r2rmlParser;

Constructor to start parsing R2RML mappings from file.

Parameters:
file - the full path of the file
 
 	public R2RMLManager(String file) {
 		this(new File(file));
 	}

Constructor to start parsing R2RML mappings from file.

Parameters:
file - the File object
 
 	public R2RMLManager(File file) {
 		try {
 			 = new R2RMLParser();
 			InputStream in = new FileInputStream(file);
 			URL documentUrl = new URL("file://" + file);
 			parser.setRDFHandler(collector);
 			parser.parse(indocumentUrl.toString());
 		} catch (Exception e) {
 		}
 	}

Constructor to start the parser from an RDF graph

Parameters:
graph - the sesame graph containing mappings
 
 	public R2RMLManager(Graph graph){
 		 = graph;
 		 = new R2RMLParser();
 		
	}

Get the graph of mappings

Returns:
the Graph object containing the mappings
	public Graph getGraph() {
		while (it.hasNext())
		return ;
	}

This method return the list of mappings from the Graph main method to be called, assembles everything

Parameters:
myGraph - the graph structure containing mappings
Returns:
ArrayList<OBDAMappingAxiom> - list of mapping axioms read from the graph
		// retrieve the TriplesMap nodes
		Set<ResourcetripleMaps = .getMappingNodes(myGraph);
		for (Resource tripleMap : tripleMaps) {
			// for each node get a mapping
			try {
				mapping = getMapping(myGraphtripleMap);
				// add it to the list of mappings
				mappings.add(mapping);
				// pass 2 - check for join conditions, add to list
				List<OBDAMappingAxiomjoinMappings = getJoinMappings(myGraphtripleMap);
				if (joinMappings != null) {
					mappings.addAll(joinMappings);
				}
catch (Exception e) {
			}
		}
		return mappings;
	}

Method to get an OBDAMappingAxiom from a Resource node in the given Graph

Parameters:
myGraph - the graph containing mappings
subj - the Resource node of one mapping
Returns:
OBDAMappingAxiom - the mapping axiom read from the graph
	private OBDAMappingAxiom getMapping(Graph myGraphResource subjthrows Exception {
		String sourceQuery = .getSQLQuery(myGraphsubj);
		List<Functionbody = getMappingTripleAtoms(myGraphsubj);
		Function head = getHeadAtom(body);
		CQIE targetQuery = .getCQIE(headbody);
		OBDAMappingAxiom mapping = .getRDBMSMappingAxiom("mapping-"+subj.stringValue(), sourceQuerytargetQuery);
		return mapping;
	}

Method to get the join OBDAMappingAxioms from a Resource node in the given Graph

Parameters:
myGraph - the Graph structure containing mappings
tripleMap - the Resource node of a mapping
Returns:
List<OBDAMappingAxiom> - the list of join mappings read from the given node
	private List<OBDAMappingAxiomgetJoinMappings(Graph myGraphResource tripleMapthrows Exception {
		String sourceQuery = "";
		//get all predicateobject nodes that contain joins for the subj triplemap given
		List<ResourcejoinNodes = .getJoinNodes(myGraphtripleMap);
		if (!joinNodes.isEmpty()) {
			List<OBDAMappingAxiomjoinMappings = new ArrayList<OBDAMappingAxiom>(joinNodes.size());
			//get subject sql string and newliteral of given node
			String sourceQuery1 = .getSQLQuery(myGraphtripleMap);
			Term joinSubject1 = .getSubjectAtom(myGraphtripleMap);
			Term joinSubject1Child = .getSubjectAtom(myGraphtripleMap"CHILD_");
			int idx = 1;
			//for each predicateobject map that contains a join
			for (Resource joinPredObjNode : joinNodes) {
				//get the predicates
				List<PredicatejoinPredicates = .getBodyPredicates(myGraphjoinPredObjNode);
				//get the referenced triple map node
				Resource referencedTripleMap = .getReferencedTripleMap(myGraphjoinPredObjNode);
				//get the referenced triple map sql query and subject atom
				String sourceQuery2 = .getSQLQuery(myGraphreferencedTripleMap);
				Term joinSubject2 = .getSubjectAtom(myGraphreferencedTripleMap);
				Term joinSubject2Parent = .getSubjectAtom(myGraphreferencedTripleMap"PARENT_");
				//get join condition
				String childCol = .getChildColumn(myGraphjoinPredObjNode);
				String parentCol = .getParentColumn(myGraphjoinPredObjNode);
				List<Functionbody = new ArrayList<Function>();
				// construct the atom from subject 1 and 2
				List<Termterms = new ArrayList<Term>();
				//if join condition is empty, the two sql queries are the same
				if (childCol == null || parentCol == null) {
					sourceQuery = sourceQuery1;
					terms.add(joinSubject1);
					terms.add(joinSubject2);
else {
					//TODO replace this workaround
					//have to introduce alias as Child and as Parent 
					sourceQuery = "SELECT * FROM ("+sourceQuery1 + ") as CHILD, (" + sourceQuery2 + ") as PARENT " +
							"WHERE CHILD." + childCol + " = PARENT." + parentCol;
					terms.add(joinSubject1Child);
					terms.add(joinSubject2Parent);
				}
				//for each predicate construct an atom and add to body
				for (Predicate pred : joinPredicates) {
					Function bodyAtom = .getFunction(predterms);
					body.add(bodyAtom);
				}
				//get head and construct cqie
				Function head = getHeadAtom(body);
				CQIE targetQuery = .getCQIE(headbody);
				if (sourceQuery.isEmpty()) {
					throw new Exception("Could not create source query for join in "+tripleMap.stringValue());
				}
				//finally, create mapping and add it to the list
				OBDAMappingAxiom mapping = .getRDBMSMappingAxiom("mapping-join"+idx+"-"+tripleMap.stringValue(), sourceQuerytargetQuery);
				idx++;
				..println("WARNING joinMapping introduced : "+mapping.toString());
				..println("Renaming of variables as CHILD_ and PARENT_ introduced!");
				joinMappings.add(mapping);
			}
			return joinMappings;
		}
		return null;
	}

construct head of mapping q(variables) from the body

Parameters:
body - the body of the mapping
Returns:
Function - the head of the mapping
	private Function getHeadAtom(List<Functionbody) {
		Set<Variablevars = new HashSet<Variable>();
		for (Function bodyAtom : body) {
			 vars.addAll(bodyAtom.getReferencedVariables());
		}
		int arity = vars.size();
		List<Termdvars = new ArrayList<Term>(vars);
		return head;
	}

method to get the body atoms of the mapping from a given Resource node in the Graph

Parameters:
myGraph - the rdf graph containing mappings
subj - the Resource node of the specific mapping
Returns:
List<Function> - the body of the mapping constructed from the graph
	private List<FunctiongetMappingTripleAtoms(Graph myGraphResource subjthrows Exception {
		//the body to return
		List<Functionbody = new ArrayList<Function>();
		//get subject
		Term subjectAtom = .getSubjectAtom(myGraphsubj);		
		//get any class predicates, construct atom Class(subject), add to body
		for (Predicate classPred : classPredicates) {
			body.add(.getFunction(classPredsubjectAtom));
		}		
		//get predicate-object nodes
		Set<ResourcepredicateObjectNodes = .getPredicateObjects(myGraphsubj);	
		for (Resource predobj : predicateObjectNodes) {
			//for each predicate object map
			//get body predicate
			List<PredicatebodyPredicates = .getBodyPredicates(myGraphpredobj);
			//predicates that contain a variable are separately treated
			List<FunctionbodyURIPredicates = .getBodyURIPredicates(myGraphpredobj);
			//get object atom
			Term objectAtom = .getObjectAtom(myGraphpredobj);
			if (objectAtom == null) {
				// skip, object is a join
				continue;
			}
			// construct the atom, add it to the body
			List<Termterms = new ArrayList<Term>();
			terms.add(subjectAtom);
			for (Predicate bodyPred : bodyPredicates) {
				//for each predicate if there are more in the same node
				//check if predicate = rdf:type
					//create term triple(subjAtom, URI("...rdf_type"), objAtom)
						terms.add(rdftype);
						terms.add(objectAtom);
						body.add(.getFunction(newpredterms));
else {
					// create predicate(subject, object) and add it to the body
					terms.add(objectAtom);
					Function bodyAtom = .getFunction(bodyPredterms);
					body.add(bodyAtom);
				}
			}
			//treat predicates that contain a variable (column or template declarations)
			for (Function predFunction : bodyURIPredicates) {
				//create triple(subj, predURIFunction, objAtom) terms
				terms.add(predFunction);
				terms.add(objectAtom);
				body.add(.getFunction(newpredterms));
			}
		}
		return body;
	}
New to GrepCode? Check out our FAQ X