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

it.uniroma2.art.sheet2rdf.pearl.NodePearlElement Maven / Gradle / Ivy

There is a newer version: 6.0.7
Show newest version
package it.uniroma2.art.sheet2rdf.pearl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import it.uniroma2.art.sheet2rdf.coda.CODAConverter;

public class NodePearlElement implements PearlElement {
	
	private List triplesList; 
	
	public NodePearlElement(){
		triplesList = new ArrayList();
	}
	
	/**
	 * Append a node triple to the NodePearlElement
	 * @param placeholder
	 * @param converter
	 * @param fsValue
	 */
	public void add(String placeholder, CODAConverter converter, String fsValue){
		this.add(placeholder, converter, fsValue, false);
	}
	
	/**
	 * Append a node definition to the NodePearlElement
	 * @param node
	 */
	public void add(NodeDefinition node) {
		this.add(node, false);
	}
	
	/**
	 * Add a node triple to the NodePearlElement. If addBefore is true, the new triple is added at the beginning and
	 * the other elements are unshifted
	 * @param placeholder
	 * @param converter
	 * @param fsValue
	 * @param addBefore
	 */
	public void add(String placeholder, CODAConverter converter, String fsValue, boolean addBefore){
		if (addBefore) {
			triplesList.add(0, new NodeDefinition(placeholder, converter, fsValue));
		} else {
			triplesList.add(new NodeDefinition(placeholder, converter, fsValue));
		}
	}
	
	/**
	 * Add a node definition to the NodePearlElement. If addBefore is true, the new node is added at the beginning and
	 * the other elements are unshifted
	 * @param node
	 * @param addBefore
	 */
	public void add(NodeDefinition node, boolean addBefore) {
		if (addBefore) {
			triplesList.add(0, node);
		} else {
			triplesList.add(node);
		}
	}
	
	/**
	 * Returns the NodeTriple that defines the given placeholder
	 * @param placeholder
	 * @return
	 */
	public NodeDefinition getNodeDefinition(String placeholder) {
		for (NodeDefinition t : triplesList) {
			if (t.getPlaceholder().equals(placeholder)) {
				return t;
			}
		}
		return null;
	}
	
	@Override
	public String serialize(String tabs, Map prefixMapping){
		String output = "";
		for (NodeDefinition triple : triplesList) {
			String placeholder = triple.getPlaceholder();

			CODAConverter converter = triple.getConverter();
			String converterSerialization = converter != null ? converter.serialize(prefixMapping) : "%pls_provide_a_converter%";
			if (triple.isMemoize()) {
				output += tabs + "@Memoized\n";
			}

			String fsValue = triple.getFsValue();
			if (fsValue == null) {
				fsValue = "%pls_provide_a_fs%";
			} else {
				fsValue += "/value";
			}

			output += tabs + placeholder + " " + converterSerialization + " " + fsValue + " .\n";
		}
		return output;
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy