view src/main/java/org/mpi/openmind/scripts/WitnessOwnValueGenerator.java @ 112:933d17f95016

new script MigratePrimeAliases to migrate is_prime_alias_X_of.
author Robert Casties <casties@mpiwg-berlin.mpg.de>
date Wed, 14 Aug 2019 20:48:02 +0200
parents 615d27dce9b3
children
line wrap: on
line source

package org.mpi.openmind.scripts;

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

import org.apache.commons.lang.StringUtils;
import org.mpi.openmind.cache.WrapperService;
import org.mpi.openmind.repository.bo.Entity;
import org.mpi.openmind.repository.bo.Node;
import org.mpi.openmind.repository.services.PersistenceService;
import org.mpi.openmind.repository.services.ServiceRegistry;

public class WitnessOwnValueGenerator {

	
	private WrapperService ot;
	private PersistenceService ss;
	
	public WitnessOwnValueGenerator(WrapperService ot){
		this.ot = ot;
		this.ss = ot.getPS();
	}
	
	
	public void execute(){
		
		try {
			List<Entity> witnessList = ot.getLightweightAssertions("WITNESS", null, -1);
			
			List<Node> dirtyEntities = new ArrayList<Node>();
			int count = 0;
			for(Entity witness : witnessList){

				String ownValue = new String();
				Entity text = null;
				
				List<Entity> list0 = this.ss.getTargetsForSourceRelation(witness, "is_exemplar_of", "TEXT", 1);
				if(list0.size() > 0){
					text = list0.get(0);
					ownValue += text.getOwnValue();
				}
				
				
				List<Entity> list = this.ss.getTargetsForSourceRelation(witness, "is_part_of", "CODEX", 1);
				if(list.size() > 0){
					Entity codex = list.get(0);
					//Attribute identifier = ot.getPSice().getAttributeByName(codex, "identifier");
					//String codexValue = (identifier == null) ? "" : "_" + identifier.getValue();
					list = this.ss.getTargetsForSourceRelation(codex, "is_part_of", "COLLECTION", 1);
					if(list.size() > 0){
						Entity collection = list.get(0);
						list = this.ss.getTargetsForSourceRelation(collection, "is_part_of", "REPOSITORY", 1);
						if(list.size() > 0){
							Entity repository = list.get(0);
							list = this.ss.getTargetsForSourceRelation(repository, "is_in", "PLACE", 1);
							if(list.size() > 0){
								Entity city = list.get(0);
								if(StringUtils.isNotEmpty(ownValue))
									ownValue += "_";	
								ownValue += city.getOwnValue() + "_" + repository.getOwnValue() + "_" + /*collection.getOwnValue() + */codex.getOwnValue();
								System.out.println(ownValue);
								witness.setOwnValue(ownValue);
								dirtyEntities.add(witness);
							}
						}
					}
				}
				
				
				count++;
				if((count % 50) == 0){
					System.out.println("*");
				}
				if((count % 200) == 0){
					System.out.println("\n["+ count + "/" + witnessList.size() + "]" + (double)(count/witnessList.size()) * 100 + "%");
				}
			}
			ot.saveNodeListOnlyForScripts(dirtyEntities);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	
	public static void main(String[] args) {
		ServiceRegistry services = new ServiceRegistry();
		WitnessOwnValueGenerator script = new WitnessOwnValueGenerator(services.getWrapper());
		script.execute();
		System.exit(0);	
	}
}