Mentions légales du service

Skip to content
Snippets Groups Projects
Commit a0b6cb0c authored by Maria-Elena Rosoiu's avatar Maria-Elena Rosoiu
Browse files

Initial version

parent 1e1b9d5f
No related branches found
No related tags found
No related merge requests found
package fr.inrialpes.exmo.align.gen;
import java.util.Enumeration;
import java.util.Vector;
import fr.inrialpes.exmo.align.service.jade.messageontology.Parameter;
import org.semanticweb.owl.align.Parameters;
/*
* Retains the list of parameters that the test generator must receive
*/
public class GeneratorParameters implements Parameters {
Vector<Parameter> parameters;
public GeneratorParameters ( ) { //the constructor
this.parameters = new Vector<Parameter>();
}
//get the names of parameters
@Override
public Enumeration<String> getNames() {
if ( this.parameters.isEmpty() ) //no parameter
return null;
Vector<String> names = new Vector<String>();//return the list of names if parameter is no empty
for ( Parameter p : this.parameters )
names.add( p.getName() );
return names.elements();
}
//get the value of the parameter name
//return null if the parameter is unset or it's not on the vector
public String getValue(String name){
for ( Parameter p : this.parameters )
if ( p.getName().equals( name ) )
return p.getValue();
return null;
}
//get the parameter
//return (null) if no corresponding parameter exist
@Override
public String getParameter(String name) {
if ( this.parameters.isEmpty() ) //empty vector
return null;
for ( Parameter p : this.parameters ) //check if the parameter exists
if ( p.getName().equals( name ) )
return p.getName();
return null; //the parameter does not exists
}
//set the value of a parameter
@Override
public void setParameter(String name, String value) {
for ( Parameter p : this.parameters ) //check if we have already the entry
if ( p.getName().equals( name ) ) {
p.setValue( value );
return;
}
Parameter p = new Parameter(); //add a new one if the parameter is not on the
p.setName( name );
p.setValue( value );
this.parameters.add( p ); //else add a new entry in parameters
}
//unset the value of a parameter
@Override
public void unsetParameter(String name) {
for ( Parameter p : this.parameters ) //check if the parameter is in the list
if ( p.getName().equals( name ) )
p.setValue( null ); //unset the value of the parameter
}
//returns the parameter from the position index
public Parameter getParameter (int index) {
return this.parameters.get( index );
}
//print the parameters
@Override
public void write() {
System.out.println( "Parameters:" );
for ( Parameter p : this.parameters )
System.out.println( "Name [" + p.getName() + "] Value [" + p.getValue() + "]" );
}
public int size() {
return this.parameters.size(); //returns the list of all parameters
}
}
This diff is collapsed.
package fr.inrialpes.exmo.align.gen;
/*
* The list of all the modifications that can be applied to the test generator
*/
public class ParametersIds {
public static String ADD_SUBCLASS = "addSubClass";
public static String REMOVE_SUBCLASS = "removeSubClass";
public static String REMOVE_PROPERTY = "removeProperty";
public static String REMOVE_COMMENT = "removeComment";
public static String LEVEL_FLATTENED = "levelFlattened";
public static String ADD_PROPERTY = "addProperty";
public static String REMOVE_CLASSES = "removeClasses"; //remove classes from level
//add c classes beginning from level l -> the value of this parameters should be:
//beginning_level.number_of_classes_to_add
public static String ADD_CLASSES = "addClasses";
public static String RENAME_PROPERTIES="renameProperties"; //rename properties
public static String RENAME_CLASSES ="renameClasses"; //rename classes
public static String RENAME_RESOURCES = "renameResources"; //value is null for this parameter
public static String REMOVE_RESTRICTION= "removeRestriction"; //remove restrictions
}
package fr.inrialpes.exmo.align.gen;
//Java classes
import java.io.FileOutputStream;
import java.io.InputStream;
//Jena API classes
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFWriter;
import com.hp.hpl.jena.util.FileManager;
//Alignment API classes
import org.semanticweb.owl.align.Alignment;
// Alignment API implementation classes
import fr.inrialpes.exmo.align.impl.URIAlignment;
import fr.inrialpes.exmo.align.service.jade.messageontology.Parameter;
import java.io.File;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
public class TestGenerator {
//load ontology
public OntModel loadOntology ( String fileName ) {
InputStream in = FileManager.get().open( fileName );
OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );
model.read(in, null);
return model;
}
//write ontology
public void writeOntology(OntModel model, String dest) {
try {
File f = new File(dest);
FileOutputStream fout = new FileOutputStream(f);
Charset defaultCharset = Charset.forName("UTF8");
RDFWriter writer = model.getWriter("RDF/XML-ABBREV");
writer.setProperty("showXmlDeclaration","true");
writer.write(model.getBaseModel(), new OutputStreamWriter(fout, defaultCharset), "");
fout.close();
} catch (Exception ex) {
System.out.println("Exception " + ex.getMessage());
}
}
public static void printUsage() {
System.out.println( "inputOntology outputOntology parameters" );
System.out.println( "[--------------------------------------------------------------------------]" );
System.out.println( "[------------- The list of all modification is the following --------------]" );
System.out.println( "[1. Remove percentage subclasses \"removeSubClass\" --------------]" );
System.out.println( "[2. Remove percentage properties \"removeProperty\" --------------]" );
System.out.println( "[3. Remove percentage comments \"removeComment\" --------------]" );
System.out.println( "[4. Remove percentage restrictions \"removeRestriction\" --------------]" );
System.out.println( "[5. Add percentage subclasses \"addSubClass\" --------------]" );
System.out.println( "[6. Add percentage properties \"addProperty\" --------------]" );
System.out.println( "[7. Rename percentage classes \"renameClasses\" --------------]" );
System.out.println( "[8. Rename percentage properties \"renameProperties\" --------------]" );
System.out.println( "[9. Remove all the classes from a level\"removeClasses\" ---------------]" );
System.out.println( "[10. Add nbClasses to a specific level \"addClasses\" ---------------]" );
System.out.println( "[11. Level flattened \"levelFlattened\" ---------------]" );
System.out.println( "[--------------------------------------------------------------------------]" );
System.exit(-1);
}
/*
* FileName -> the name of the Ontology
* GeneratorParameters -> the list of parameters
*/
public static void main (String [] args) {
TestGenerator t = new TestGenerator();
String fileName = "", destFile = "";
//String fileName = "onto.rdf";
//String destFile = "onto1.rdf";
GeneratorParameters parameters = new GeneratorParameters();
if ( args.length < 2 ) {
System.out.println("Usage");
printUsage();
}
else {
System.out.println( args[0] );
System.out.println( args[1] );
fileName = args[0];
destFile = args[1];
parameters = new GeneratorParameters(); //initialize the parameters
for ( int i=2; i<args.length; i+=2 ) {
if ( args[i].equals("addSubClass") ) /* add percentage classes */
parameters.setParameter(ParametersIds.ADD_SUBCLASS, args[i+1]);
//add c classes beginning from level l -> the value of this parameters should be:
//beginning_level.number_of_classes_to_add
if ( args[i].equals("addClasses") ) /* add c classes beginning from level l */
parameters.setParameter(ParametersIds.ADD_CLASSES, args[i+1]);
if ( args[i].equals("removeSubClass") ) /* remove percentage subclasses */
parameters.setParameter(ParametersIds.REMOVE_SUBCLASS, args[i+1]);
if ( args[i].equals("removeClasses") ) /* remove classes from level */
parameters.setParameter(ParametersIds.REMOVE_CLASSES, args[i+1]);
if ( args[i].equals("addProperty") ) /* add percentage properties */
parameters.setParameter(ParametersIds.ADD_PROPERTY, args[i+1]);
if ( args[i].equals("removeProperty") ) /* remove percentage properties */
parameters.setParameter(ParametersIds.REMOVE_PROPERTY, args[i+1]);
if ( args[i].equals("renameProperties") )/* rename percentage properties */
parameters.setParameter(ParametersIds.RENAME_PROPERTIES, args[i+1]);
if ( args[i].equals("removeComment") ) /* remove percentage comments */
parameters.setParameter(ParametersIds.REMOVE_COMMENT, args[i+1]);
if ( args[i].equals("levelFlattened") ) /* flattened level */
parameters.setParameter(ParametersIds.LEVEL_FLATTENED, args[i+1]);
if ( args[i].equals("renameClasses") ) /* rename percentage classes */
parameters.setParameter(ParametersIds.RENAME_CLASSES, args[i+1]);
if ( args[i].equals("renameResources") )/* rename percentage resources */
parameters.setParameter(ParametersIds.RENAME_RESOURCES, args[i+1]);
if ( args[i].equals("removeRestriction") ) /* remove percentage restrictions */
parameters.setParameter(ParametersIds.REMOVE_RESTRICTION, args[i+1]);
}
//load the model
OntModel model = t.loadOntology( fileName ); //the initial Ontology
OntModel modifiedModel = t.loadOntology( fileName ); //the modified Ontology
Alignment alignment = new URIAlignment(); //the initial Alignment
//build the ontology modifier for the first time
OntologyModifier modifier = new OntologyModifier( model, modifiedModel, alignment);
//get the max level of the class hierarchy of the ontology
int level = modifier.getMaxLevel();
System.out.println( "[-------------------------------------------------]" );
for ( int i=0; i<parameters.size(); i++ ) {
Parameter p = parameters.getParameter(i); //the parameter at position index
modifier.modifyOntology( p ); //modify the ontology according to parameter p
System.out.println( "[We-modified-the-ontology-for-parameter " + p.getName() + "]");
}
System.out.println( "[-------------------------------------------------]" );
modifier.computeAlignment( "referenceAlignment.rdf" );
alignment = modifier.getAlignment(); //get the reference alignment
modifiedModel = modifier.getModifiedOntology(); //get the modified ontology
t.writeOntology( modifiedModel, destFile ); //write the model
System.out.println( "***" );
System.out.println( "END" );
System.out.println( "***" );
}
}
}
\ No newline at end of file
package fr.inrialpes.exmo.align.gen;
import java.util.ArrayList;
import java.util.List;
/*
* This class represents the class hierarchy.
* It retains only the URI of the classes .
*/
public class URITree {
private String URI; //the URI of the node
private ArrayList<URITree> children; //the list of children
private URITree parent; //the parent of the node
int depth; //the depth of the node
int maxDepth; //the max depth of the node
public URITree(String URI) { //the constructor
this.URI = URI;
this.children = new ArrayList<URITree>();
this.parent = null;
this.depth = 0;
this.maxDepth = 0;
}
//get the URI of the node
public String getURI () {
return this.URI;
}
//set the URI of the node
public void setURI(String URI) {
this.URI = URI;
}
//set the depth of the node
public void setDepth (int depth) {
this.depth = depth;
}
//get the depth of the node
public int getDepth() {
return this.depth;
}
//return the max depth
public int getMaxDepth() {
return this.maxDepth;
}
//set the parent of the node
public void setParent ( URITree parent ) {
this.parent = parent;
}
//get the parent of the node
public URITree getParent () {
return this.parent;
}
//returns a child from a specific position
public URITree getChildAt ( int index ) {
return this.children.get( index );
}
//return the list of children nodes
public ArrayList<URITree> getChildrenList() {
return this.children;
}
//returns the size of the children
public int getChildrenSize() {
return this.children.size();
}
//add the node with the childURI to the parent with the URI parentURI
public void add(URITree root, String childURI, String parentURI) {
URITree parent;
parent = searchURITree(root, parentURI); //we search for the parent URITree
addChild(root, parent, childURI); //we add the new URITree
}
//add a child
public void addChild (URITree root, URITree node, String URI) {
int index = 0;
URITree n = null;
while ( index < node.getChildrenSize() ) { //if the child is already in the list
if ( node.getChildAt( index ).getURI().equals( URI ) )
return;
index++;
}
index = 0;
while ( index < root.getChildrenSize() ) { //we search among root children to see if the node is already there
n = root.getChildrenList().get( index );
if ( n.getURI().equals( URI ) ) { //the node is already there
root.getChildrenList().remove( n ); //we remove the node
break;
}
index++;
}
addChildToNode( node, URI );
}
//add child to a specific node
public void addChildToNode(URITree node, String URI) {
URITree child = new URITree( URI ); //creates a new node
child.setDepth( node.getDepth() + 1 ); //set the depth of the node
if ( this.maxDepth < node.getDepth()+1 ) //keep track of the max depth of the hierarchy
this.maxDepth = node.getDepth() + 1;
child.setParent( node ); //set the parent of the node
node.getChildrenList().add( child ); //add the node to the parent children list
}
//returns the URITree with the given URI
@SuppressWarnings("unchecked")
public URITree searchURITree(URITree root, String URI) {
int index = 0;
URITree node = root;
URITree ans = null;
if ( root.getURI().equals( URI ) ) //if the root has the URI as the URI searched
return root;
while ( index < root.getChildrenSize() ) { //we start to search recursively
if ( root.getChildAt( index ).getURI().equals( URI ) )
return root.getChildAt( index ); //we found the node with the given URI
ans = search(root.getChildAt( index ), new ArrayList(), 0, URI);
if ( ans != null )
return ans;
index++;
}
return node;
}
@SuppressWarnings("unchecked")
public URITree search(URITree node, List occurs, int depth, String URI) {
int index = 0;
URITree ans = null;
//verify if the label of the URITree is the one with the label of the URITree we search for
if ( node.getURI().equals( URI ) ) {
ans = node;
return ans; //has found the parent
}
while( index < node.getChildrenSize()) {
URITree n = node.getChildAt( index );
occurs.add( node );
ans = search( n, occurs, depth+1, URI );
if ( ans != null )
return ans;
occurs.remove( node );
index++;
}
return null; //has not found the parent
}
//remove a child from the tree
@SuppressWarnings("unchecked")
public void removeFromURITree(URITree root, String URI) {
int index = 0;
int found = 0;
while ( index < root.getChildrenSize() ) {
if ( root.getChildAt( index ).getURI().equals( URI ) ) {
root.getChildrenList().remove( index ); //found the node to delete
return;
}
found = remove(root.getChildAt( index ), new ArrayList(), 0, URI);
if ( found == 1 )
return;
index++;
}
}
@SuppressWarnings("unchecked")
public int remove(URITree node, List occurs, int depth, String URI) {
int index = 0;
int found = 0;
if ( node.getURI().equals( URI ) ) {
URITree parent = node.getParent();
//add the node children to the parent of the node
int cnt = 0; //reestablish the connection between nodes
while ( cnt < node.getChildrenSize() ) {
URITree child = node.getChildrenList().get( cnt );
child.setDepth( node.getDepth() ); //modify the depth
child.setParent( parent ); //modify the parent
parent.getChildrenList().add( child ); //add the child to the parent of node
cnt++;
}
parent.getChildrenList().remove( node ); //remove the node from the children list
found = 1;
return found; //has found the parent
}
while( index < node.getChildrenSize()) {
URITree n = node.getChildAt( index );
occurs.add( node );
found = remove( n, occurs, depth+1, URI );
if ( found == 1 )
return found;
occurs.remove( node );
index++;
}
return found;
}
//get all the node from a specific level
@SuppressWarnings("unchecked")
public List<URITree> getNodesFromLevel (URITree root, int level) {
List<URITree> nodes = new ArrayList<URITree>(); //to store the nodes from a specific level
int index = 0;
if ( root.getDepth() == level )
nodes.add( root );
while ( index < root.getChildrenList().size() ) {
getNodes ( root.getChildAt(index), new ArrayList(), 0, nodes, level ); //recursively print all the children URITrees
index++;
}
return nodes; //return the list of nodes
}
@SuppressWarnings("unchecked")
public void getNodes (URITree node, List occurs, int depth, List<URITree> nodes, int level) {
int index = 0;
if ( node.getDepth() == level ) //if it's on the level that we want, we add it to the hierarchy
nodes.add( node );
while( index < node.getChildrenList().size() ) {
URITree n = node.getChildrenList().get(index);
occurs.add( node );
getNodes( n, occurs, depth+1, nodes, level );
occurs.remove( node );
index++;
}
}
//change the depth if the nodes lower the level to node.getDepth()-1
@SuppressWarnings("unchecked")
public void changeDepth (URITree root, int level) {
int index = 0;
this.maxDepth = this.maxDepth-1;
while ( index < root.getChildrenList().size() ) {
change ( root.getChildAt(index), new ArrayList(), 0, level );
index++;
}
}
@SuppressWarnings("unchecked")
public void change (URITree node, List occurs, int depth, int level) {
int index = 0;
if ( node.getDepth() > level ) { //if it's on the level that we want, we add it to the hierarchy
int dept = node.getDepth();
node.setDepth( dept-1 );
}
while( index < node.getChildrenList().size() ) {
URITree n = node.getChildrenList().get(index);
occurs.add( node );
change( n, occurs, depth+1, level );
occurs.remove( node );
index++;
}
}
//print the tree
@SuppressWarnings("unchecked")
public void printURITree( URITree root ) {
int index = 0;
//System.out.println( "[" + root.getURI() + "]" + "->" + root.getDepth() );
while ( index < root.getChildrenList().size() ) {
//recursively print all the children URITrees
print(root.getChildAt(index), new ArrayList(), 0);
index++;
}
}
@SuppressWarnings("unchecked")
public void print (URITree node, List occurs, int depth) {
int index = 0;
indent( node.getDepth() );
System.out.println( "[" + node.getURI() + "]" + "->" + node.getDepth() );
while( index < node.getChildrenList().size() ) {
URITree n = node.getChildrenList().get( index );
occurs.add( node );
print( n, occurs, depth+1 );
occurs.remove( node );
index++;
}
}
protected void indent( int depth ) {
for (int i = 0; i < depth; i++) {
System.out.print( " " );
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment