Advertisement
NeoSniperkiller

OWL class mapped in Java

Oct 13th, 2013
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.07 KB | None | 0 0
  1. /**
  2.  * Person - Abstract class for creating a person
  3.  *
  4.  * @author NeoSniperkiller
  5.  */
  6. public class Person extends AmigoPhysicalEntity {
  7.     private final String personURI = ThesisOntologyTools.PERSON_URI + "Person";
  8.     private final String isLocationConfirmedURI = ThesisOntologyTools.PERSON_URI + "isLocationConfirmed";
  9.     private final String hasCurrentRoleURI = ThesisOntologyTools.PERSON_URI + "hasCurrentRole";
  10.     private final String hasRoleURI = ThesisOntologyTools.PERSON_URI + "hasRole";
  11.     private final String hasCurrentTaskURI = ThesisOntologyTools.TASK_URI + "hasCurrentTask";
  12.     private final String hasToDoTaskURI = ThesisOntologyTools.TASK_URI + "hasToDoTask";
  13.     private final String hasLocationURI = ThesisOntologyTools.CONTEXT_URI + "hasLocation";
  14.    
  15.     // local variables
  16.     protected Role currentRole;
  17.     protected Role role;
  18.     protected Task currentTask;
  19.     protected List<Task> todoTasks;
  20.     protected boolean locationConfirmed;
  21.     protected AmigoRoom currentLocation;
  22.    
  23.    
  24.     /**
  25.      * Empty constructor - DO NOT USE this constructor for anything else than instantiating a dummy object of this class
  26.      *
  27.      * @deprecated      Only use this constructor in the process of converting a OWLIndividual
  28.      */
  29.     public Person() {
  30.         super();
  31.     }
  32.    
  33.     /**
  34.      * Default constructor
  35.      *
  36.      * @param personID                              String representation of a person id
  37.      * @param personStatus                          Status of the person
  38.      * @param currentRole                           Role of the person
  39.      */
  40.     public Person(ID personID, PersonStatus personStatus, Role currentRole) {
  41.         super(personID, personStatus);
  42.        
  43.         this.currentRole = currentRole;
  44.     }
  45.  
  46.     /**
  47.      * @return the currentRole
  48.      */
  49.     public Role getCurrentRole() {
  50.         return currentRole;
  51.     }
  52.  
  53.     /**
  54.      * @param currentRole the currentRole to set
  55.      */
  56.     public void setCurrentRole(Role currentRole) {
  57.         this.currentRole = currentRole;
  58.     }
  59.  
  60.     /**
  61.      * @return the role
  62.      */
  63.     public Role getRole() {
  64.         return role;
  65.     }
  66.  
  67.     /**
  68.      * @param role the role to set
  69.      */
  70.     public void setRole(Role role) {
  71.         this.role = role;
  72.     }
  73.  
  74.     /**
  75.      * @return the currentTask
  76.      */
  77.     public Task getCurrentTask() {
  78.         return currentTask;
  79.     }
  80.  
  81.     /**
  82.      * @param currentTask the currentTask to set
  83.      */
  84.     public void setCurrentTask(Task currentTask) {
  85.         this.currentTask = currentTask;
  86.     }
  87.  
  88.     /**
  89.      * @return the todoTasks
  90.      */
  91.     public List<Task> getTodoTasks() {
  92.         return todoTasks;
  93.     }
  94.  
  95.     /**
  96.      * @param todoTasks the todoTasks to set
  97.      */
  98.     public void setTodoTasks(List<Task> todoTasks) {
  99.         this.todoTasks = todoTasks;
  100.     }
  101.  
  102.     /**
  103.      * @return the locationConfirmed
  104.      */
  105.     public boolean isLocationConfirmed() {
  106.         return locationConfirmed;
  107.     }
  108.  
  109.     /**
  110.      * @param locationConfirmed the locationConfirmed to set
  111.      */
  112.     public void setLocationConfirmed(boolean locationConfirmed) {
  113.         this.locationConfirmed = locationConfirmed;
  114.     }
  115.  
  116.     /**
  117.      * @return the currentLocation
  118.      */
  119.     public AmigoRoom getCurrentLocation() {
  120.         return currentLocation;
  121.     }
  122.  
  123.     /**
  124.      * @param currentLocation the currentLocation to set
  125.      */
  126.     public void setCurrentLocation(AmigoRoom currentLocation) {
  127.         this.currentLocation = currentLocation;
  128.     }
  129.  
  130.     @Override
  131.     public OWLIndividual convertToReasoningIndividual(OWLOntologyManager ontologyManager, OWLDataFactory dataFactory, OWLOntology ontology, OWLIndividual parentIndividual) {
  132.         OWLIndividual person = parentIndividual;
  133.        
  134.         if(parentIndividual == null) {
  135.             OWLClass personClass = dataFactory.getOWLClass(IRI.create(personURI));
  136.             person = dataFactory.getOWLNamedIndividual(IRI.create(ThesisOntologyTools.PERSON_URI + "Person_" + super.id));
  137.             OWLAxiom axiom = dataFactory.getOWLClassAssertionAxiom(personClass, person);
  138.             ontologyManager.addAxiom(ontology, axiom);
  139.         }
  140.        
  141.         // data properties
  142.         // ===============
  143.         // - locationConfirmed
  144.         OWLDataProperty isLocationConfirmed = dataFactory.getOWLDataProperty(IRI.create(isLocationConfirmedURI));
  145.         OWLDataPropertyAssertionAxiom pidAssertion = dataFactory.getOWLDataPropertyAssertionAxiom(isLocationConfirmed, person, locationConfirmed);
  146.         ontologyManager.addAxiom(ontology, pidAssertion);
  147.        
  148.         // object properties
  149.         // =================
  150.         // - hasCurrentRole
  151.         if(currentRole != null) {
  152.             OWLIndividual currentRoleIndividual = currentRole.convertToReasoningIndividual(ontologyManager, dataFactory, ontology, person);
  153.             OWLObjectProperty hasCurrentRole = dataFactory.getOWLObjectProperty(IRI.create(hasCurrentRoleURI));
  154.             OWLObjectPropertyAssertionAxiom assertion = dataFactory.getOWLObjectPropertyAssertionAxiom(hasCurrentRole, person, currentRoleIndividual);
  155.             ontologyManager.addAxiom(ontology, assertion);
  156.         }
  157.        
  158.         // - hasRole
  159.         if(role != null) {
  160.             OWLIndividual roleIndividual = role.convertToReasoningIndividual(ontologyManager, dataFactory, ontology, person);
  161.             OWLObjectProperty hasRole = dataFactory.getOWLObjectProperty(IRI.create(hasRoleURI));
  162.             OWLObjectPropertyAssertionAxiom assertion = dataFactory.getOWLObjectPropertyAssertionAxiom(hasRole, person, roleIndividual);
  163.             ontologyManager.addAxiom(ontology, assertion);
  164.         }
  165.        
  166.         // - hasCurrentTask
  167.         if(currentTask != null) {
  168.             OWLIndividual currentTaskIndividual = currentTask.convertToReasoningIndividual(ontologyManager, dataFactory, ontology, person);
  169.             OWLObjectProperty hasCurrentTask = dataFactory.getOWLObjectProperty(IRI.create(hasCurrentTaskURI));
  170.             OWLObjectPropertyAssertionAxiom assertion = dataFactory.getOWLObjectPropertyAssertionAxiom(hasCurrentTask, person, currentTaskIndividual);
  171.             ontologyManager.addAxiom(ontology, assertion);
  172.         }
  173.        
  174.         // - hasToDoTask
  175.         if(todoTasks != null) {
  176.             for(Task todoTask : todoTasks) {
  177.                 OWLIndividual todoTaskIndividual = todoTask.convertToReasoningIndividual(ontologyManager, dataFactory, ontology, person);
  178.                 OWLObjectProperty hasToDoTask = dataFactory.getOWLObjectProperty(IRI.create(hasToDoTaskURI));
  179.                 OWLObjectPropertyAssertionAxiom assertion = dataFactory.getOWLObjectPropertyAssertionAxiom(hasToDoTask, person, todoTaskIndividual);
  180.                 ontologyManager.addAxiom(ontology, assertion);
  181.             }
  182.         }
  183.        
  184.         // - hasLocation
  185.         if(currentLocation != null) {
  186.             OWLIndividual locationIndividual = currentLocation.convertToReasoningIndividual(ontologyManager, dataFactory, ontology, person);
  187.             OWLObjectProperty hasLocation = dataFactory.getOWLObjectProperty(IRI.create(hasLocationURI));
  188.             OWLObjectPropertyAssertionAxiom assertion = dataFactory.getOWLObjectPropertyAssertionAxiom(hasLocation, person, locationIndividual);
  189.             ontologyManager.addAxiom(ontology, assertion);
  190.         }
  191.        
  192.         return super.convertToReasoningIndividual(ontologyManager, dataFactory, ontology, person);
  193.     }
  194.  
  195.     @Override
  196.     public void convertFromReasoningIndividual(OWLNamedIndividual reasoningIndividual, OWLDataFactory dataFactory, OWLReasoner reasoner) {
  197.        
  198.         // extract properties from this class
  199.         // - data properties
  200.         // -- locationConfirmed
  201.         OWLDataProperty dataProperty = dataFactory.getOWLDataProperty(IRI.create(isLocationConfirmedURI));
  202.         Set<OWLLiteral> returnLiterals = reasoner.getDataPropertyValues(reasoningIndividual, dataProperty);
  203.         this.locationConfirmed = returnLiterals.iterator().next().parseBoolean();
  204.        
  205.         // - object properties
  206.         // -- currentRole
  207.         OWLObjectProperty objectProperty = dataFactory.getOWLObjectProperty(IRI.create(hasCurrentRoleURI));
  208.         Set<OWLNamedIndividual> returnObjects = reasoner.getObjectPropertyValues(reasoningIndividual, objectProperty).getFlattened();
  209.         if(returnObjects.size() > 0) {
  210.             this.currentRole = Role.retrieveCorrespondingLocalObject(returnObjects.iterator().next(), dataFactory, reasoner);
  211.         }
  212.        
  213.         // -- role
  214.         objectProperty = dataFactory.getOWLObjectProperty(IRI.create(hasRoleURI));
  215.         returnObjects = reasoner.getObjectPropertyValues(reasoningIndividual, objectProperty).getFlattened();
  216.         if(returnObjects.size() > 0) {
  217.             this.role = Role.retrieveCorrespondingLocalObject(returnObjects.iterator().next(), dataFactory, reasoner);
  218.         }
  219.        
  220.         // -- currentTask
  221.         objectProperty = dataFactory.getOWLObjectProperty(IRI.create(hasCurrentTaskURI));
  222.         returnObjects = reasoner.getObjectPropertyValues(reasoningIndividual, objectProperty).getFlattened();
  223.         if(returnObjects.size() > 0) {
  224.             this.currentTask = Task.retrieveCorrespondingLocalObject(returnObjects.iterator().next(), dataFactory, reasoner);
  225.         }
  226.        
  227.         // -- todoTasks
  228.         objectProperty = dataFactory.getOWLObjectProperty(IRI.create(hasToDoTaskURI));
  229.         returnObjects = reasoner.getObjectPropertyValues(reasoningIndividual, objectProperty).getFlattened();
  230.         if(returnObjects.size() > 0) {
  231.             this.todoTasks = new ArrayList<Task>();
  232.            
  233.             for(OWLNamedIndividual oi : returnObjects) {
  234.                 this.todoTasks.add(Task.retrieveCorrespondingLocalObject(oi, dataFactory, reasoner));
  235.             }
  236.         }
  237.        
  238.         // -- hasLocation
  239.         objectProperty = dataFactory.getOWLObjectProperty(IRI.create(hasCurrentTaskURI));
  240.         returnObjects = reasoner.getObjectPropertyValues(reasoningIndividual, objectProperty).getFlattened();
  241.         if(returnObjects.size() > 0) {
  242.             this.currentTask = Task.retrieveCorrespondingLocalObject(returnObjects.iterator().next(), dataFactory, reasoner);
  243.         }
  244.        
  245.         // continue extracting in the superclass
  246.         super.convertFromReasoningIndividual(reasoningIndividual, dataFactory, reasoner);
  247.     }
  248.    
  249.     public static Person retrieveCorrespondingLocalObject(OWLNamedIndividual reasoningIndividual, OWLDataFactory dataFactory, OWLReasoner reasoner) {
  250.         String oiURI = reasoningIndividual.getIRI().getFragment();
  251.         Person e = null;
  252.  
  253.         if(oiURI.contains("Man")) {
  254.             e = new Man();
  255.         }
  256.         if(oiURI.contains("Woman")) {
  257.             e = new Woman();
  258.         }
  259.         if(oiURI.contains("StressedPerson")) {
  260.             e = new StressedPerson();
  261.         }
  262.         if(oiURI.contains("Person")) {
  263.             e = new Person();
  264.         }
  265.  
  266.         if(e != null) {
  267.             e.convertFromReasoningIndividual(reasoningIndividual, dataFactory, reasoner);
  268.         }
  269.         return e;
  270.     }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement