📄 parsing.java
字号:
package it.itc.ectrl.normkit.common;
import java.util.*;
import edu.unika.aifb.kaon.api.oimodel.*;
/**
* Class that provides some usefull static functions to read SBO instance files (MAFRA mappings).
*
* @author Nuno Silva (Nuno.Silva@dei.isep.ipp.pt)
*/
public class Parsing
{
// utils and ever used functions (?)
static public Instance readUniqueConceptInstance(OIModel oimodel, String strURIConcept, String strError) throws Exception
{
Concept concept = oimodel.getConcept(strURIConcept);
Set setConceptInstances = concept.getAllInstances();
//System.out.println(setConceptInstances.size());
if(setConceptInstances.size()!=1)
throw new Exception(strError + ": " + strURIConcept);
// return first (and unique)
Iterator i = setConceptInstances.iterator();
return (Instance) i.next();
}
static public Map readAllConceptInstances(OIModel oimodel, String strURIConcept, String strError) throws Exception
{
Map mapURI_Instances = new HashMap();
// get all instances if this concept
Concept concept = oimodel.getConcept(strURIConcept);
Set setConceptInstances = concept.getAllInstances();
// put them in a map in the form of <uri, Instance>
Iterator i = setConceptInstances.iterator();
for(; i.hasNext(); )
{
Instance instance = (Instance) i.next();
//System.out.println("putting: " + instance.getURI());
mapURI_Instances.put(instance.getURI(), instance);
}
return mapURI_Instances;
}
static public String readUniquePropertyValue(Instance instanceConcept, String strURIProperty, String strError) throws Exception
{
Property property = instanceConcept.getOIModel().getProperty(strURIProperty);
Set setPropertyInstances = instanceConcept.getAllFromPropertyValues(property);
if(setPropertyInstances.size()!=1)
throw new Exception(strError + ": " + instanceConcept.getURI());
// return first (and unique)
Iterator i = setPropertyInstances.iterator();
try
{
Instance instance = (Instance) i.next();
return (String) instance.getURI();
}
catch(ClassCastException e)
{
throw new Exception(CNOErrors.ERROR_RESOURCE_EXPECTED + " in property: " + strURIProperty + " in instance: " + instanceConcept.getURI());
}
}
static public String readEventualPropertyValue(Instance instanceConcept, String strURIProperty, String strError) throws Exception
{
Property property = instanceConcept.getOIModel().getProperty(strURIProperty);
Set setPropertyInstances = instanceConcept.getAllFromPropertyValues(property);
if(setPropertyInstances.size()>1)
throw new Exception(strError + ": " + instanceConcept.getURI());
// return eventual, but unique value
Iterator i = setPropertyInstances.iterator();
if(i.hasNext())
{
Instance instance = (Instance) i.next();
return (String) instance.getURI();
}
return null;
}
static public Set readAtLeastOnePropertyValue(Instance instanceConcept, String strURIProperty, String strError) throws Exception
{
Set setPropertyValues = new HashSet();
Property property = instanceConcept.getOIModel().getProperty(strURIProperty);
Set setPropertyInstances = instanceConcept.getAllFromPropertyValues(property);
if(setPropertyInstances.size()<1)
throw new Exception(strError + ": " + instanceConcept.getURI());
// return eventual, but unique value
Iterator i = setPropertyInstances.iterator();
for(; i.hasNext(); )
{
Instance instance = (Instance) i.next();
String strURIPropertyValue = (String) instance.getURI();
setPropertyValues.add(strURIPropertyValue);
}
return setPropertyValues;
}
static public Set readPropertyValues(Instance instanceConcept, String strURIProperty, String strError) throws Exception
{
Set setPropertyValues = new HashSet();
Property property = instanceConcept.getOIModel().getProperty(strURIProperty);
Set setPropertyInstances = instanceConcept.getAllFromPropertyValues(property);
Iterator i = setPropertyInstances.iterator();
for(; i.hasNext(); )
{
Instance instance = (Instance) i.next();
String strURIPropertyValue = (String) instance.getURI();
setPropertyValues.add(strURIPropertyValue);
}
return setPropertyValues;
}
static public boolean readEventualBooleanPropertyValue(Instance instanceConcept, String strURI, String strError) throws Exception
{
String strValue = readEventualPropertyValue(instanceConcept, strURI, strError);
if( strValue!=null )
{
if ( !strValue.equals(CNOVocabulary.URI_CNO_TRUE) || !strValue.equals(CNOVocabulary.URI_CNO_FALSE) )
throw new Exception(CNOErrors.ERROR_INVALID_BOOLEAN + ": " + instanceConcept.getURI());
else
{
if( strValue.equals(CNOVocabulary.URI_CNO_TRUE) )
return true;
else
return false;
}
}
else
return false;
}
static public String readEventualPropertyLiteral(Instance instanceConcept, String strURIProperty, String strError) throws Exception
{
Property property = instanceConcept.getOIModel().getProperty(strURIProperty);
Set setPropertyInstances = instanceConcept.getAllFromPropertyValues(property);
if(setPropertyInstances.size()>1)
throw new Exception(strError + ": " + instanceConcept.getURI());
// return eventual, but unique value
Iterator i = setPropertyInstances.iterator();
if( i.hasNext() )
{
Object obj = i.next();
if( obj instanceof String )
return (String) obj;
else
throw new Exception( CNOErrors.ERROR_LITERAL_EXPECTED + " in: " + instanceConcept.getURI() );
}
else
return null;
}
static public String readUniquePropertyLiteral(Instance instanceConcept, String strURIProperty, String strError) throws Exception
{
Property property = instanceConcept.getOIModel().getProperty(strURIProperty);
Set setPropertyInstances = instanceConcept.getAllFromPropertyValues(property);
if(setPropertyInstances.size()!=1)
throw new Exception(strError + ": " + instanceConcept.getURI());
// return first (and unique)
Iterator i = setPropertyInstances.iterator();
return (String) i.next();
}
static public Vector readPropertyValuesArray( Instance instanceConcept, String strError ) throws Exception
{
Vector vValues = new Vector();
String strURI;
String strX = CNOVocabulary.URI_RDF_X;
int iX = 1;
do
{
strURI = readEventualPropertyValue( instanceConcept, strX + iX, strError );
if( strURI != null )
vValues.add( iX-1, strURI );
iX++;
}
while( strURI != null );
return vValues;
}
static public Vector readPropertyLiteralArray( Instance instanceConcept, String strError ) throws Exception
{
Vector vValues = new Vector();
String strLiteral;
String strX = CNOVocabulary.URI_RDF_X;
int iX = 1;
do
{
strLiteral = readEventualPropertyLiteral( instanceConcept, strX + iX, strError );
if( strLiteral != null )
vValues.add( iX-1, strLiteral );
iX++;
}
while( strLiteral != null );
return vValues;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -