use of org.semanticweb.owlapi.model.OWLIndividual in project stanbol by apache.
the class OntologyNetworkConfigurationUtils method getScopeObjectPropertyValues.
/**
* Utility method to get all the values of an object property of a Scope
*
* @param ontology
* @param individualIRI
* @param op
* @return
*/
private static String[] getScopeObjectPropertyValues(OWLOntology ontology, String individualIRI, OWLObjectProperty op) {
Set<OWLIndividual> scopes = cScope.getIndividuals(ontology);
List<String> result = new ArrayList<String>();
OWLIndividual iiScope = null;
// Optimised loop.
for (OWLIndividual ind : scopes) {
if (ind.isAnonymous())
continue;
if (((OWLNamedIndividual) ind).getIRI().toString().equals(individualIRI)) {
iiScope = ind;
break;
}
}
if (iiScope != null) {
}
for (OWLIndividual iScope : scopes) {
if (iScope.isNamed()) {
if (((OWLNamedIndividual) iScope).getIRI().toString().equals(individualIRI)) {
Set<OWLIndividual> values = iScope.getObjectPropertyValues(op, ontology);
Iterator<OWLIndividual> it = values.iterator();
while (it.hasNext()) {
OWLIndividual i = it.next();
if (i.isNamed())
result.add(((OWLNamedIndividual) i).getIRI().toString());
}
}
}
}
return result.toArray(EMPTY_IRI_ARRAY);
}
use of org.semanticweb.owlapi.model.OWLIndividual in project stanbol by apache.
the class OntologyNetworkConfigurationUtils method getLibraryObjectPropertyValues.
/**
* Utility method to get all the values of a property from a Library subject
*
* @param ontology
* @param individualIRI
* @param op
* @return
*/
private static String[] getLibraryObjectPropertyValues(OWLOntology ontology, String individualIRI, OWLObjectProperty op) {
Set<OWLIndividual> scopes = cLibrary.getIndividuals(ontology);
List<String> result = new ArrayList<String>();
for (OWLIndividual iLibrary : scopes) {
if (iLibrary.isNamed()) {
if (((OWLNamedIndividual) iLibrary).getIRI().toString().equals(individualIRI)) {
Set<OWLIndividual> values = iLibrary.getObjectPropertyValues(op, ontology);
Iterator<OWLIndividual> it = values.iterator();
while (it.hasNext()) {
OWLIndividual i = it.next();
if (i.isNamed())
result.add(((OWLNamedIndividual) iLibrary).getIRI().toString());
}
}
}
}
return result.toArray(EMPTY_IRI_ARRAY);
}
use of org.semanticweb.owlapi.model.OWLIndividual in project stanbol by apache.
the class RuleListWriter method writeTo.
@Override
public void writeTo(RuleList ruleList, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType mediaType, MultivaluedMap<String, Object> arg5, OutputStream out) throws IOException, WebApplicationException {
Logger log = LoggerFactory.getLogger(getClass());
log.debug("Rendering the list of recipes.");
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLDataFactory factory = OWLManager.getOWLDataFactory();
OWLOntology ontology;
try {
ontology = manager.createOntology();
String recipeClassURI = Symbols.Recipe.toString().replace("<", "").replace(">", "");
IRI recipeClassIRI = IRI.create(recipeClassURI);
OWLClass owlRecipeClass = factory.getOWLClass(recipeClassIRI);
String ruleClassURI = Symbols.Rule.toString().replace("<", "").replace(">", "");
IRI ruleClassIRI = IRI.create(ruleClassURI);
OWLClass owlRuleClass = factory.getOWLClass(ruleClassIRI);
String descriptionURI = Symbols.description.toString().replace("<", "").replace(">", "");
IRI descriptionIRI = IRI.create(descriptionURI);
OWLDataProperty descriptionProperty = factory.getOWLDataProperty(descriptionIRI);
String hasRuleURI = Symbols.hasRule.toString().replace("<", "").replace(">", "");
IRI hasRuleIRI = IRI.create(hasRuleURI);
OWLObjectProperty hasRule = factory.getOWLObjectProperty(hasRuleIRI);
String ruleBodyURI = Symbols.ruleBody.toString().replace("<", "").replace(">", "");
IRI ruleBodyIRI = IRI.create(ruleBodyURI);
OWLDataProperty ruleBody = factory.getOWLDataProperty(ruleBodyIRI);
String ruleHeadURI = Symbols.ruleHead.toString().replace("<", "").replace(">", "");
IRI ruleHeadIRI = IRI.create(ruleHeadURI);
OWLDataProperty ruleHead = factory.getOWLDataProperty(ruleHeadIRI);
if (ruleList != null) {
for (Rule rule : ruleList) {
String recipeId = rule.getRecipe().getRecipeID().toString().replace("<", "").replace(">", "");
IRI reicpeIRI = IRI.create(recipeId);
OWLIndividual owlRecipe = factory.getOWLNamedIndividual(reicpeIRI);
String ruleId = rule.getRuleID().toString().replace("<", "").replace(">", "");
IRI ruleIRI = IRI.create(ruleId);
OWLIndividual owlRule = factory.getOWLNamedIndividual(ruleIRI);
OWLAxiom axiom = factory.getOWLClassAssertionAxiom(owlRecipeClass, owlRecipe);
manager.addAxiom(ontology, axiom);
axiom = factory.getOWLClassAssertionAxiom(owlRuleClass, owlRule);
manager.addAxiom(ontology, axiom);
axiom = factory.getOWLObjectPropertyAssertionAxiom(hasRule, owlRecipe, owlRule);
manager.addAxiom(ontology, axiom);
String recipeDescription = rule.getRecipe().getRecipeDescription();
String ruleDescription = rule.getDescription();
if (recipeDescription != null) {
axiom = factory.getOWLDataPropertyAssertionAxiom(descriptionProperty, owlRecipe, recipeDescription);
manager.addAxiom(ontology, axiom);
}
if (ruleDescription != null) {
axiom = factory.getOWLDataPropertyAssertionAxiom(descriptionProperty, owlRule, ruleDescription);
manager.addAxiom(ontology, axiom);
}
String ruleContent = rule.toString();
String[] parts = ruleContent.split("\\->");
axiom = factory.getOWLDataPropertyAssertionAxiom(ruleBody, owlRule, parts[0]);
manager.addAxiom(ontology, axiom);
axiom = factory.getOWLDataPropertyAssertionAxiom(ruleHead, owlRule, parts[1]);
manager.addAxiom(ontology, axiom);
}
}
if (mediaType.toString().equals(KRFormat.RDF_XML)) {
try {
manager.saveOntology(ontology, new RDFXMLOntologyFormat(), out);
} catch (OWLOntologyStorageException e) {
log.error("Failed to store ontology for rendering.", e);
}
} else if (mediaType.toString().equals(KRFormat.OWL_XML)) {
try {
manager.saveOntology(ontology, new OWLXMLOntologyFormat(), out);
} catch (OWLOntologyStorageException e) {
log.error("Failed to store ontology for rendering.", e);
}
} else if (mediaType.toString().equals(KRFormat.MANCHESTER_OWL)) {
try {
manager.saveOntology(ontology, new ManchesterOWLSyntaxOntologyFormat(), out);
} catch (OWLOntologyStorageException e) {
log.error("Failed to store ontology for rendering.", e);
}
} else if (mediaType.toString().equals(KRFormat.FUNCTIONAL_OWL)) {
try {
manager.saveOntology(ontology, new OWLFunctionalSyntaxOntologyFormat(), out);
} catch (OWLOntologyStorageException e) {
log.error("Failed to store ontology for rendering.", e);
}
} else if (mediaType.toString().equals(KRFormat.TURTLE)) {
try {
manager.saveOntology(ontology, new TurtleOntologyFormat(), out);
} catch (OWLOntologyStorageException e) {
log.error("Failed to store ontology for rendering.", e);
}
} else if (mediaType.toString().equals(KRFormat.RDF_JSON)) {
Graph mGraph = OWLAPIToClerezzaConverter.owlOntologyToClerezzaGraph(ontology);
RdfJsonSerializingProvider provider = new RdfJsonSerializingProvider();
provider.serialize(out, mGraph, SupportedFormat.RDF_JSON);
}
} catch (OWLOntologyCreationException e1) {
log.error("An error occurred.", e1);
}
out.flush();
}
use of org.semanticweb.owlapi.model.OWLIndividual in project stanbol by apache.
the class ResourceAtom method adapt.
@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
org.apache.stanbol.rules.manager.atoms.ResourceAtom tmp = (org.apache.stanbol.rules.manager.atoms.ResourceAtom) ruleAtom;
URI uri = tmp.getURI();
OWLDataFactory factory = OWLManager.getOWLDataFactory();
OWLIndividual owlIndividual = factory.getOWLNamedIndividual(IRI.create(uri.toString()));
SWRLIArgument swrliArgument = factory.getSWRLIndividualArgument(owlIndividual);
return (T) new ArgumentSWRLAtom(swrliArgument, uri.toString());
}
use of org.semanticweb.owlapi.model.OWLIndividual in project graal by graphik-team.
the class OWLAPIUtils method classExpressionDisjunctiveNormalForm.
/**
* disjunctive normal form
*
* @param classExpression
* @return a {@link OWLClassExpression} in disjunctive normal form equivalents to the specified one
*/
public static OWLClassExpression classExpressionDisjunctiveNormalForm(OWLClassExpression classExpression) {
if (classExpression instanceof OWLObjectUnionOf) {
Set<OWLClassExpression> union = new TreeSet<>();
for (OWLClassExpression element : OWLAPIUtils.getObjectUnionOperands(classExpression)) {
element = classExpressionDisjunctiveNormalForm(element);
for (OWLClassExpression e : OWLAPIUtils.getObjectUnionOperands(element)) {
union.add(e);
}
}
return new OWLObjectUnionOfImpl(union);
} else if (classExpression instanceof OWLObjectIntersectionOf) {
List<Set<OWLClassExpression>> conjunctions = new LinkedList<>();
conjunctions.add(new TreeSet<OWLClassExpression>());
for (OWLClassExpression element : OWLAPIUtils.getObjectIntersectionOperands(classExpression)) {
element = classExpressionDisjunctiveNormalForm(element);
if (element instanceof OWLObjectUnionOf) {
List<Set<OWLClassExpression>> tmp = new LinkedList<>();
for (Set<OWLClassExpression> conj : conjunctions) {
for (OWLClassExpression e : OWLAPIUtils.getObjectUnionOperands(element)) {
Set<OWLClassExpression> newConj = new TreeSet<>(conj);
newConj.add(classExpressionDisjunctiveNormalForm(e));
tmp.add(newConj);
}
}
conjunctions = tmp;
} else {
for (Set<OWLClassExpression> conj : conjunctions) {
for (OWLClassExpression e : OWLAPIUtils.getObjectIntersectionOperands(element)) {
conj.add(e);
}
}
}
}
Set<OWLClassExpression> union = new TreeSet<>();
if (conjunctions.size() > 1) {
for (Set<OWLClassExpression> conj : conjunctions) {
union.add(new OWLObjectIntersectionOfImpl(conj));
}
return new OWLObjectUnionOfImpl(union);
} else {
return new OWLObjectIntersectionOfImpl(conjunctions.get(0));
}
} else if (classExpression instanceof OWLObjectSomeValuesFrom) {
OWLObjectSomeValuesFrom expr = (OWLObjectSomeValuesFrom) classExpression;
OWLObjectPropertyExpression prop = expr.getProperty();
OWLClassExpression filler = classExpressionDisjunctiveNormalForm(expr.getFiller());
if (filler instanceof OWLObjectUnionOf) {
Set<OWLClassExpression> union = new TreeSet<>();
for (OWLClassExpression e : OWLAPIUtils.getObjectUnionOperands(filler)) {
e = classExpressionDisjunctiveNormalForm(e);
union.add(new OWLObjectSomeValuesFromImpl(prop, e));
}
return new OWLObjectUnionOfImpl(union);
}
return new OWLObjectSomeValuesFromImpl(prop, filler);
} else if (classExpression instanceof OWLDataSomeValuesFrom) {
OWLDataSomeValuesFrom expr = (OWLDataSomeValuesFrom) classExpression;
OWLDataPropertyExpression prop = expr.getProperty();
OWLDataRange filler = dataRangeDisjunctiveNormalForm(expr.getFiller());
if (filler instanceof OWLDataUnionOf) {
Set<OWLClassExpression> union = new TreeSet<>();
for (OWLDataRange e : OWLAPIUtils.getDataUnionOperands(filler)) {
e = dataRangeDisjunctiveNormalForm(e);
union.add(new OWLDataSomeValuesFromImpl(prop, e));
}
return new OWLObjectUnionOfImpl(union);
}
return new OWLDataSomeValuesFromImpl(prop, filler);
} else if (classExpression instanceof OWLObjectOneOf) {
OWLObjectOneOf expr = (OWLObjectOneOf) classExpression;
if (expr.getIndividuals().size() <= 1) {
return expr;
}
Set<OWLClassExpression> union = new TreeSet<>();
for (OWLIndividual i : expr.getIndividuals()) {
Set<OWLIndividual> individuals = Collections.singleton(i);
union.add(new OWLObjectOneOfImpl(individuals));
}
return new OWLObjectUnionOfImpl(union);
} else if (classExpression instanceof OWLObjectAllValuesFrom) {
OWLObjectAllValuesFrom expr = (OWLObjectAllValuesFrom) classExpression;
return new OWLObjectAllValuesFromImpl(expr.getProperty(), classExpressionDisjunctiveNormalForm(expr.getFiller()));
} else if (classExpression instanceof OWLDataAllValuesFrom) {
OWLDataAllValuesFrom expr = (OWLDataAllValuesFrom) classExpression;
return new OWLDataAllValuesFromImpl(expr.getProperty(), dataRangeDisjunctiveNormalForm(expr.getFiller()));
} else if (classExpression instanceof OWLObjectComplementOf) {
OWLObjectComplementOf expr = (OWLObjectComplementOf) classExpression;
return new OWLObjectComplementOfImpl(classExpressionDisjunctiveNormalForm(expr.getOperand()));
} else if (classExpression instanceof OWLObjectMinCardinality) {
OWLObjectMinCardinality c = (OWLObjectMinCardinality) classExpression;
return new OWLObjectMinCardinalityImpl(c.getProperty(), c.getCardinality(), classExpressionDisjunctiveNormalForm(c.getFiller()));
} else if (classExpression instanceof OWLDataMinCardinality) {
OWLDataMinCardinality c = (OWLDataMinCardinality) classExpression;
return new OWLDataMinCardinalityImpl(c.getProperty(), c.getCardinality(), dataRangeDisjunctiveNormalForm(c.getFiller()));
} else if (classExpression instanceof OWLObjectMaxCardinality) {
OWLObjectMaxCardinality c = (OWLObjectMaxCardinality) classExpression;
return new OWLObjectMaxCardinalityImpl(c.getProperty(), c.getCardinality(), classExpressionDisjunctiveNormalForm(c.getFiller()));
} else if (classExpression instanceof OWLDataMaxCardinality) {
OWLDataMaxCardinality c = (OWLDataMaxCardinality) classExpression;
return new OWLDataMaxCardinalityImpl(c.getProperty(), c.getCardinality(), dataRangeDisjunctiveNormalForm(c.getFiller()));
} else if (classExpression instanceof OWLObjectExactCardinality) {
OWLObjectExactCardinality c = (OWLObjectExactCardinality) classExpression;
return new OWLObjectExactCardinalityImpl(c.getProperty(), c.getCardinality(), classExpressionDisjunctiveNormalForm(c.getFiller()));
} else if (classExpression instanceof OWLDataExactCardinality) {
OWLDataExactCardinality c = (OWLDataExactCardinality) classExpression;
return new OWLDataExactCardinalityImpl(c.getProperty(), c.getCardinality(), dataRangeDisjunctiveNormalForm(c.getFiller()));
}
return classExpression;
}
Aggregations