All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.uniroma2.art.coda.structures.table.TablePlaceholdersBindingsVariables Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package it.uniroma2.art.coda.structures.table;

import it.uniroma2.art.coda.pearl.model.BindingStruct;
import it.uniroma2.art.coda.pearl.model.PlaceholderStruct;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.rdf4j.model.Value;

public class TablePlaceholdersBindingsVariables {

	private List> placeholderBindingList;
	private List> placeholderBindingVariablesList;

	public TablePlaceholdersBindingsVariables() {
		placeholderBindingList = new ArrayList>();
		placeholderBindingVariablesList = new ArrayList>();
	}

	public void constructPlaceholderBindingMap(List> valuesFromAnnotationListList) {
		
		
		
		List>> tablesList = 
				new ArrayList>>();
		// first iterate until ValuesFromAnAnnotation are reached to create several tables, each one of them 
		// dealing with a particular rule (and maybe more instances of annotation, but using just one rule)
		for(int i=0; i annlist = valuesFromAnnotationListList.get(i);
			List> vfaaConcatList = new ArrayList>();
			for(int k=0; k> tableFromVFAA = constructTableFromVFAA(valuesFromAnAnnotation);
					
				vfaaConcatList.addAll(tableFromVFAA);
			}
			//now add the table just created to a table list
			tablesList.add(vfaaConcatList);
		}
		
		//now iterate on every table ( List> ) stored in tablesList to do 
		// a JOIN between them
		
		for(int i=0; i> singleTable = tablesList.get(i);
			if(placeholderBindingList.isEmpty()){
				placeholderBindingList.addAll(singleTable);
			} else {
				placeholderBindingList = doJoinBetweenTable(placeholderBindingList, singleTable);
			}
		}
		
	}


	/**
	 * Given a ValuesFromAnAnnotation it return a List of Table row (Map)
	 */
	private List> constructTableFromVFAA( ValuesFromAnAnnotation vfaa) {
		//construct the empty (for the moment) result table, each row is 
		List> idToSingleTableValueMapList = 
				new ArrayList>();
		//get the List of List of value associated to each placehodler/binding
		List> singleTableValueListList = vfaa.getSingleTableValueListList();
		
		Map idToSingleTableValueMap = new HashMap();
		recursiveMapConstruct(singleTableValueListList, 0, idToSingleTableValueMap, idToSingleTableValueMapList);
		
		return idToSingleTableValueMapList;
	}
	
	
	private void recursiveMapConstruct(List> valueForTableListList, int index,
			Map idToSingleTableValueMap,
			List> idToSingleTableValueMapList) {

		
		if(valueForTableListList.size() <= index){ 
			return;
		}
		
		List valueForTableList = valueForTableListList.get(index);

		for (ValueForTable valueForTable : valueForTableList) {
			// First of all clone the input map and work on this clone
			Map idToSingleTableValueClonedMap = new HashMap();
			for (String key : idToSingleTableValueMap.keySet()){
				idToSingleTableValueClonedMap.put(key, idToSingleTableValueMap.get(key));
			}
			
			// Depenending of the type of currentElement perform a different action
			addVFT(valueForTable, idToSingleTableValueClonedMap);

			if (valueForTableListList.size() == index + 1) {
				// this is the last element of the valueForTableList, so store the
				// idToSingleTableValueClonedMap
				idToSingleTableValueMapList.add(idToSingleTableValueClonedMap);
			} else {
				recursiveMapConstruct(valueForTableListList, index + 1, idToSingleTableValueClonedMap,
						idToSingleTableValueMapList);
			}
		}
	}
	
	private void addVFT(ValueForTable valueForTable, Map idToSingleTableValueMap){
		if (valueForTable instanceof ValueForTablePlaceholder) {
			ValueForTablePlaceholder currentElementPlaceholder = (ValueForTablePlaceholder) valueForTable;
			String id = currentElementPlaceholder.getId();
			Value value = currentElementPlaceholder.getArtNode();
			PlaceholderStruct placeholderStruct = currentElementPlaceholder.getPlaceholderStruct();
			SingleTableValuePlaceholder singleTableValuePlaceholder = new SingleTableValuePlaceholder(
					id, value, placeholderStruct);
			idToSingleTableValueMap.put(id, singleTableValuePlaceholder);
		} else if (valueForTable instanceof ValueForTableBinding) {
			ValueForTableBinding currentElementBinding = (ValueForTableBinding) valueForTable;
			BindingStruct bindingStruct = currentElementBinding.getBindingStruct();
			Map idToARTNodeMap = currentElementBinding.getIdToValueMap();
			for (String bindId : idToARTNodeMap.keySet()) {
				idToARTNodeMap.get(bindId);
				Value artNode = idToARTNodeMap.get(bindId);
				SingleTableValueBinding singleTableValueBinding = new SingleTableValueBinding(bindId,
						artNode, bindingStruct);
				idToSingleTableValueMap.put(bindId, singleTableValueBinding);
			}
		}
	}
	
	private List> doJoinBetweenTable(
			List> firstTable,
			List> secondTable) {
		List> resultTable = new ArrayList>();
		for(int firstTableIndex=0; firstTableIndex firstTableRow = firstTable.get(firstTableIndex);
				Map secondTableRow = secondTable.get(secondTableIndex);
				//create the Row for the new Table
				Map newTableRow = new HashMap();
				//clone the firstTableRow in the newTableRow
				newTableRow.putAll(firstTableRow);
				// add the secondTableRow to newTableRow
				newTableRow.putAll(secondTableRow);
				// now newTableRow = firstTableRow + secondTableRow, so added this new row to the new table
				resultTable.add(newTableRow);
			}
		}
		
		
		return resultTable;
	}


	
	
	
	
	public void addVariableValueToPlaceholderBindingVariableMap(
			Collection singleTableValuePlaceholderBindingList,
			List> valueVariableList) {

		Map idToSingleTableValueMap = new HashMap();
		// first of all add to the map the value corresponding to the placeholder and the binding
		for (SingleTableValue singleTableValue : singleTableValuePlaceholderBindingList) {
			idToSingleTableValueMap.put(singleTableValue.getId(), singleTableValue);
		}

		if (valueVariableList == null || valueVariableList.isEmpty()) {
			placeholderBindingVariablesList.add(idToSingleTableValueMap);
		} else {
			// now add the variable associated values
			recursiveSecondMapConstruct(valueVariableList, 0, idToSingleTableValueMap);
		}
	}

	private void recursiveSecondMapConstruct(List> valueVariableList,
			int index, Map idToSingleTableValueMap) {

		List currentElementList = valueVariableList.get(index);

		for (SingleTableValueVariable singleTableVariable : currentElementList) {
			// First of all clone the input map and word on this clone
			Map idToSingleTableValueClonedMap = new HashMap();
			for (String key : idToSingleTableValueMap.keySet())
				idToSingleTableValueClonedMap.put(key, idToSingleTableValueMap.get(key));

			idToSingleTableValueClonedMap.put(singleTableVariable.getId(), singleTableVariable);

			if (valueVariableList.size() == index + 1) {
				// this is the last element of the valueForTableList, so store the
				// idToSingleTableValueClonedMap
				placeholderBindingVariablesList.add(idToSingleTableValueClonedMap);
			} else {
				recursiveSecondMapConstruct(valueVariableList, index + 1, idToSingleTableValueClonedMap);
			}
		}
	}

	public Iterator> getIteratorForPlaceholderBindingMap() {
		return placeholderBindingList.iterator();
	}

	public Iterator> getIteratorForPlaceholderBindingVariableMap() {
		return placeholderBindingVariablesList.iterator();
	}

	public void printPlaceholderBindingTable() {
		System.out.println("PRINTING TABLE");
		boolean firstRow = true;
		for (Map row : placeholderBindingList) {
			if (firstRow) {
				firstRow = false;
				for (String id : row.keySet()) {
					System.out.print("\t\t" + id + "\t\t||");
				}
				System.out.println();
			}
			System.out.println();
			for (String id : row.keySet()) {
				System.out.print(row.get(id).toString() + " || ");
			}

		}
	}

	public void printPlaceholderBindingVariableTable() {
		for (Map row : placeholderBindingVariablesList) {
			boolean firstRow = true;
			if (firstRow) {
				firstRow = false;
				for (String id : row.keySet()) {
					System.out.print(id + "\t\t");
				}
				System.out.println();
			}
			System.out.println();
			for (String id : row.keySet()) {
				System.out.print(row.get(id).toString() + " || ");
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy