view src/main/java/org/mpi/openmind/scripts/RoleToRelation.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.Attribute;
import org.mpi.openmind.repository.bo.Entity;
import org.mpi.openmind.repository.bo.Node;
import org.mpi.openmind.repository.bo.Relation;
import org.mpi.openmind.repository.services.ServiceRegistry;
import org.mpi.openmind.repository.utils.ImportOM3Util;

public class RoleToRelation {
	
	private static List<String> roles = new ArrayList<String>();
	private static String USER = "script-role";
	private WrapperService ontology;
	private List<Entity> roleList = new ArrayList<Entity>();
	private String filePath = new String();
	
	
	static{
		roles.add("Annotator");
		roles.add("Author");
		roles.add("Copyist");
		roles.add("Illuminator");
		roles.add("Illustrator");
		roles.add("Owner");
		roles.add("Patron");
		roles.add("Student");
		roles.add("Teacher");
		roles.add("Translator");
	}
	
	public RoleToRelation(WrapperService ot, String filePath){
		this.ontology = ot;
		this.filePath = filePath;
	}
	
	public void execute(){
	
		if(StringUtils.isNotEmpty(filePath)){
			ImportOM3Util.importConcepts(ontology, filePath, true);	
		}
		
		
		this.createRoles();
		
		List<Entity> personList = this.ontology.getLightweightAssertions("PERSON", null,-1);
		
		for(Entity person : personList){
			Attribute att = this.ontology.getPS().getAttributeByName(person, "role");
			if(att != null){
				Entity role = getRole(att.getValue());
				if(role != null){
					Relation hasRole = new Relation(person, role, "has_role");
					try {
						ontology.saveNodeOnlyForScripts(hasRole, USER);	
					} catch (Exception e) {
						e.printStackTrace();
					}
					ontology.removeNode(att);
				}else{
					System.err.println("This role was not found: " + att.getOwnValue());	
				}
			}
		}
	}
	
	public Entity getRole(String name){
		if(StringUtils.isNotEmpty(name)){
			for(Entity role : roleList){
				if(role.getOwnValue().toLowerCase().equals(name.toLowerCase())){
					return role;
				}
			}	
		}
		return null;
	}
	
	public void createRoles(){
		for(String s : roles){
			try {
				Entity role = new Entity(Node.TYPE_ABOX, "ROLE", false);
				role.setOwnValue(s);
				role.addAttribute(new Attribute("name", "text", s));
				ontology.saveAssertion(role, USER);
				this.roleList.add(role);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	
	public static void main(String[] args) {
		
		//path of the file that constains the definitions.
		String filePath = (args.length > 0) ? args[0] : null;
		
		ServiceRegistry services = new ServiceRegistry();
		RoleToRelation script = new RoleToRelation(services.getWrapper(), filePath);
		script.execute();
		System.exit(0);

	}

}