Skip to content

Commit

Permalink
#175 Expose Model->Semantics extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco authored and Marco committed Mar 3, 2021
1 parent 9d140ac commit 2c883df
Show file tree
Hide file tree
Showing 2 changed files with 311 additions and 325 deletions.
311 changes: 311 additions & 0 deletions RDFSharp/Semantics/OWL/Ontology/RDFOntologyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1899,6 +1899,317 @@ public static void ClearInferences(this RDFOntologyData ontologyData)

#endregion

#region Extensions

#region Model Extensions
/// <summary>
/// Gets an ontology class of the given nature from the given RDF resource
/// </summary>
public static RDFOntologyClass ToRDFOntologyClass(this RDFResource ontResource,
RDFSemanticsEnums.RDFOntologyClassNature nature = RDFSemanticsEnums.RDFOntologyClassNature.OWL)
=> new RDFOntologyClass(ontResource, nature);

/// <summary>
/// Gets an ontology property from the given RDF resource
/// </summary>
public static RDFOntologyProperty ToRDFOntologyProperty(this RDFResource ontResource)
=> new RDFOntologyProperty(ontResource);

/// <summary>
/// Gets an ontology object property from the given RDF resource
/// </summary>
public static RDFOntologyObjectProperty ToRDFOntologyObjectProperty(this RDFResource ontResource)
=> new RDFOntologyObjectProperty(ontResource);

/// <summary>
/// Gets an ontology datatype property from the given RDF resource
/// </summary>
public static RDFOntologyDatatypeProperty ToRDFOntologyDatatypeProperty(this RDFResource ontResource)
=> new RDFOntologyDatatypeProperty(ontResource);

/// <summary>
/// Gets an ontology annotation property from the given RDF resource
/// </summary>
public static RDFOntologyAnnotationProperty ToRDFOntologyAnnotationProperty(this RDFResource ontResource)
=> new RDFOntologyAnnotationProperty(ontResource);

/// <summary>
/// Gets an ontology fact from the given RDF resource
/// </summary>
public static RDFOntologyFact ToRDFOntologyFact(this RDFResource ontResource)
=> new RDFOntologyFact(ontResource);

/// <summary>
/// Gets an ontology literal from the given RDF literal
/// </summary>
public static RDFOntologyLiteral ToRDFOntologyLiteral(this RDFLiteral ontLiteral)
=> new RDFOntologyLiteral(ontLiteral);
#endregion

#region Query Extensions
/// <summary>
/// Applies the given SPARQL SELECT query to the given ontology (which is converted into
/// a RDF graph including semantic inferences in respect of the given export behavior)
/// </summary>
public static RDFSelectQueryResult ApplyToOntology(this RDFSelectQuery selectQuery,
RDFOntology ontology,
RDFSemanticsEnums.RDFOntologyInferenceExportBehavior ontologyInferenceExportBehavior = RDFSemanticsEnums.RDFOntologyInferenceExportBehavior.ModelAndData)
{
if (selectQuery != null && ontology != null)
return selectQuery.ApplyToGraph(ontology.ToRDFGraph(ontologyInferenceExportBehavior));

return new RDFSelectQueryResult();
}

/// <summary>
/// Applies the given SPARQL ASK query to the given ontology (which is converted into
/// a RDF graph including semantic inferences in respect of the given export behavior)
/// </summary>
public static RDFAskQueryResult ApplyToOntology(this RDFAskQuery askQuery,
RDFOntology ontology,
RDFSemanticsEnums.RDFOntologyInferenceExportBehavior ontologyInferenceExportBehavior = RDFSemanticsEnums.RDFOntologyInferenceExportBehavior.ModelAndData)
{
if (askQuery != null && ontology != null)
return askQuery.ApplyToGraph(ontology.ToRDFGraph(ontologyInferenceExportBehavior));

return new RDFAskQueryResult();
}

/// <summary>
/// Applies the given SPARQL CONSTRUCT query to the given ontology (which is converted into
/// a RDF graph including semantic inferences in respect of the given export behavior)
/// </summary>
public static RDFConstructQueryResult ApplyToOntology(this RDFConstructQuery constructQuery,
RDFOntology ontology,
RDFSemanticsEnums.RDFOntologyInferenceExportBehavior ontologyInferenceExportBehavior = RDFSemanticsEnums.RDFOntologyInferenceExportBehavior.ModelAndData)
{
if (constructQuery != null && ontology != null)
return constructQuery.ApplyToGraph(ontology.ToRDFGraph(ontologyInferenceExportBehavior));

return new RDFConstructQueryResult(RDFNamespaceRegister.DefaultNamespace.ToString());
}

/// <summary>
/// Applies the given SPARQL DESCRIBE query to the given ontology (which is converted into
/// a RDF graph including semantic inferences in respect of the given export behavior)
/// </summary>
public static RDFDescribeQueryResult ApplyToOntology(this RDFDescribeQuery describeQuery,
RDFOntology ontology,
RDFSemanticsEnums.RDFOntologyInferenceExportBehavior ontologyInferenceExportBehavior = RDFSemanticsEnums.RDFOntologyInferenceExportBehavior.ModelAndData)
{
if (describeQuery != null && ontology != null)
return describeQuery.ApplyToGraph(ontology.ToRDFGraph(ontologyInferenceExportBehavior));

return new RDFDescribeQueryResult(RDFNamespaceRegister.DefaultNamespace.ToString());
}
#endregion

#region SemanticsExtensions
/// <summary>
/// Gets a graph representation of the given taxonomy, exporting inferences according to the selected behavior [OWL2]
/// </summary>
internal static RDFGraph ReifyToRDFGraph(this RDFOntologyTaxonomy taxonomy, RDFSemanticsEnums.RDFOntologyInferenceExportBehavior infexpBehavior, string taxonomyName)
{
RDFGraph result = new RDFGraph();
switch (taxonomyName)
{
//Semantic-based reification
case nameof(RDFOntologyDataMetadata.NegativeAssertions):
result = ReifyNegativeAssertionsTaxonomyToGraph(taxonomy, infexpBehavior);
break;

//List-based reification
case nameof(RDFOntologyClassModelMetadata.HasKey):
case nameof(RDFOntologyPropertyModelMetadata.PropertyChainAxiom):
result = ReifyListTaxonomyToGraph(taxonomy, taxonomyName, infexpBehavior);
break;

//Triple-based reification
default:
result = ReifyTaxonomyToGraph(taxonomy, infexpBehavior);
break;
}
return result;
}
private static RDFGraph ReifyNegativeAssertionsTaxonomyToGraph(RDFOntologyTaxonomy taxonomy, RDFSemanticsEnums.RDFOntologyInferenceExportBehavior infexpBehavior)
{
RDFGraph result = new RDFGraph();
foreach (RDFOntologyTaxonomyEntry te in taxonomy)
{

//Build reification triples of taxonomy entry
RDFTriple teTriple = te.ToRDFTriple();

//Do not export semantic inferences
if (infexpBehavior == RDFSemanticsEnums.RDFOntologyInferenceExportBehavior.None)
{
if (te.InferenceType == RDFSemanticsEnums.RDFOntologyInferenceType.None)
{
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.NEGATIVE_PROPERTY_ASSERTION));
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.SOURCE_INDIVIDUAL, (RDFResource)teTriple.Subject));
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.ASSERTION_PROPERTY, (RDFResource)teTriple.Predicate));
if (teTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL)
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.TARGET_VALUE, (RDFLiteral)teTriple.Object));
else
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.TARGET_INDIVIDUAL, (RDFResource)teTriple.Object));
}
}

//Export semantic inferences related only to ontology model
else if (infexpBehavior == RDFSemanticsEnums.RDFOntologyInferenceExportBehavior.OnlyModel)
{
if (taxonomy.Category == RDFSemanticsEnums.RDFOntologyTaxonomyCategory.Model ||
taxonomy.Category == RDFSemanticsEnums.RDFOntologyTaxonomyCategory.Annotation)
{
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.NEGATIVE_PROPERTY_ASSERTION));
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.SOURCE_INDIVIDUAL, (RDFResource)teTriple.Subject));
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.ASSERTION_PROPERTY, (RDFResource)teTriple.Predicate));
if (teTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL)
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.TARGET_VALUE, (RDFLiteral)teTriple.Object));
else
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.TARGET_INDIVIDUAL, (RDFResource)teTriple.Object));
}
else
{
if (te.InferenceType == RDFSemanticsEnums.RDFOntologyInferenceType.None)
{
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.NEGATIVE_PROPERTY_ASSERTION));
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.SOURCE_INDIVIDUAL, (RDFResource)teTriple.Subject));
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.ASSERTION_PROPERTY, (RDFResource)teTriple.Predicate));
if (teTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL)
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.TARGET_VALUE, (RDFLiteral)teTriple.Object));
else
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.TARGET_INDIVIDUAL, (RDFResource)teTriple.Object));
}
}
}

//Export semantic inferences related only to ontology data
else if (infexpBehavior == RDFSemanticsEnums.RDFOntologyInferenceExportBehavior.OnlyData)
{
if (taxonomy.Category == RDFSemanticsEnums.RDFOntologyTaxonomyCategory.Data ||
taxonomy.Category == RDFSemanticsEnums.RDFOntologyTaxonomyCategory.Annotation)
{
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.NEGATIVE_PROPERTY_ASSERTION));
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.SOURCE_INDIVIDUAL, (RDFResource)teTriple.Subject));
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.ASSERTION_PROPERTY, (RDFResource)teTriple.Predicate));
if (teTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL)
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.TARGET_VALUE, (RDFLiteral)teTriple.Object));
else
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.TARGET_INDIVIDUAL, (RDFResource)teTriple.Object));
}
else
{
if (te.InferenceType == RDFSemanticsEnums.RDFOntologyInferenceType.None)
{
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.NEGATIVE_PROPERTY_ASSERTION));
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.SOURCE_INDIVIDUAL, (RDFResource)teTriple.Subject));
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.ASSERTION_PROPERTY, (RDFResource)teTriple.Predicate));
if (teTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL)
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.TARGET_VALUE, (RDFLiteral)teTriple.Object));
else
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.TARGET_INDIVIDUAL, (RDFResource)teTriple.Object));
}
}
}

//Export semantic inferences related both to ontology model and data
else
{
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.RDF.TYPE, RDFVocabulary.OWL.NEGATIVE_PROPERTY_ASSERTION));
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.SOURCE_INDIVIDUAL, (RDFResource)teTriple.Subject));
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.ASSERTION_PROPERTY, (RDFResource)teTriple.Predicate));
if (teTriple.TripleFlavor == RDFModelEnums.RDFTripleFlavors.SPL)
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.TARGET_VALUE, (RDFLiteral)teTriple.Object));
else
result.AddTriple(new RDFTriple(teTriple.ReificationSubject, RDFVocabulary.OWL.TARGET_INDIVIDUAL, (RDFResource)teTriple.Object));
}

}
return result;
}
private static RDFGraph ReifyListTaxonomyToGraph(RDFOntologyTaxonomy taxonomy, string taxonomyName, RDFSemanticsEnums.RDFOntologyInferenceExportBehavior infexpBehavior)
{
RDFGraph result = new RDFGraph();

RDFResource taxonomyPredicate = taxonomyName.Equals(nameof(RDFOntologyClassModelMetadata.HasKey))
? RDFVocabulary.OWL.HAS_KEY : RDFVocabulary.OWL.PROPERTY_CHAIN_AXIOM;
foreach (IGrouping<RDFOntologyResource, RDFOntologyTaxonomyEntry> tgroup in taxonomy.GroupBy(t => t.TaxonomySubject))
{
//Build collection corresponding to the current subject of the given taxonomy
RDFCollection tgroupColl = new RDFCollection(RDFModelEnums.RDFItemTypes.Resource, taxonomy.AcceptDuplicates);
foreach (RDFOntologyTaxonomyEntry tgroupEntry in tgroup.ToList())
tgroupColl.AddItem((RDFResource)tgroupEntry.TaxonomyObject.Value);
result.AddCollection(tgroupColl);

//Attach collection with taxonomy-specific predicate
result.AddTriple(new RDFTriple((RDFResource)tgroup.Key.Value, taxonomyPredicate, tgroupColl.ReificationSubject));
}

return result;
}
private static RDFGraph ReifyTaxonomyToGraph(RDFOntologyTaxonomy taxonomy, RDFSemanticsEnums.RDFOntologyInferenceExportBehavior infexpBehavior)
{
RDFGraph result = new RDFGraph();
foreach (RDFOntologyTaxonomyEntry te in taxonomy)
{

//Do not export semantic inferences
if (infexpBehavior == RDFSemanticsEnums.RDFOntologyInferenceExportBehavior.None)
{
if (te.InferenceType == RDFSemanticsEnums.RDFOntologyInferenceType.None)
{
result.AddTriple(te.ToRDFTriple());
}
}

//Export semantic inferences related only to ontology model
else if (infexpBehavior == RDFSemanticsEnums.RDFOntologyInferenceExportBehavior.OnlyModel)
{
if (taxonomy.Category == RDFSemanticsEnums.RDFOntologyTaxonomyCategory.Model
|| taxonomy.Category == RDFSemanticsEnums.RDFOntologyTaxonomyCategory.Annotation)
{
result.AddTriple(te.ToRDFTriple());
}
else
{
if (te.InferenceType == RDFSemanticsEnums.RDFOntologyInferenceType.None)
{
result.AddTriple(te.ToRDFTriple());
}
}
}

//Export semantic inferences related only to ontology data
else if (infexpBehavior == RDFSemanticsEnums.RDFOntologyInferenceExportBehavior.OnlyData)
{
if (taxonomy.Category == RDFSemanticsEnums.RDFOntologyTaxonomyCategory.Data
|| taxonomy.Category == RDFSemanticsEnums.RDFOntologyTaxonomyCategory.Annotation)
{
result.AddTriple(te.ToRDFTriple());
}
else
{
if (te.InferenceType == RDFSemanticsEnums.RDFOntologyInferenceType.None)
{
result.AddTriple(te.ToRDFTriple());
}
}
}

//Export semantic inferences related both to ontology model and data
else
{
result.AddTriple(te.ToRDFTriple());
}

}
return result;
}
#endregion

#endregion

}

}
Loading

0 comments on commit 2c883df

Please sign in to comment.