I have a given ontology, on which I like to reason. I use the ROWLEX API for .NET.
How can I find all owlSubClasses of a class?
ROWLEX is not a reasoner. It has some functionality of a reasoner implemented - it is very necessary for the C# class generation - but its API is not designed for that. However, there is still hope, just not the most elegant solution.
I assume, that you have an ontology and you generated .NET classes from that using OwlGrinder.exe. Therefore by default, you have two associated .NET classes generated for each corresponding OWL class in the ontology: one light class and one full class. We are going to use the light classes only. We simply iterate through ALL .NET classes and filter out if is a subclass. That is it.
string baseClassUri = "http://myontology/2012/10#mybaseclass";
Assembly asm = GetMyAssemblyGeneratedByOwlGrinder();
Type[] subClasses = (from type in asm.GetTypes()
where type.IsSubclassOf(typeof(NC3A.SI.Rowlex.OwlThing))
// selecting subclasses only
let attributes = type.GetCustomAttributes(typeof(NC3A.SI.Rowlex.SubClassOfAttribute), false)
from attr in attributes
let subClassAttr = attr as NC3A.SI.Rowlex.SubClassOfAttribute
where subClassAttr.TypeUri == baseClassUri
// selecting light classes only
let lightAttributes = type.GetCustomAttributes(typeof(NC3A.SI.Rowlex.LightVersionAttribute), false)
from lightAttr in lightAttributes
let lightAttr_ = lightAttr as NC3A.SI.Rowlex.LightVersionAttribute
where lightAttr_.LightVersion == true
select type).ToArray();
I did not try the code, it may be buggy. But it does show the idea. Each generated class have a bunch of attributes added. These include their base classes in the SubClassOfAttribute and whether they are light classes or full classes using the LightVersionAttribute. You can filter out the classes you are interested in based on these attributes.
Related
I'm using flink 1.11 with Scala and i have a question regarding the schema evolution using a POJO.
In the documentation is written, that POJOs are supported for state schema evolution (with some limitations).
Are Scala case clases also considered as POJO and therefore supported?
case class WordCount(word: String, count: Int)
Or have i to write something like this:
class WordCount(var word: String, var count: Int) {
def this() {
this(null, -1)
}
}
Case classes are not POJOs. In particular, they do not satisfy:
The class has a public no-argument constructor
All non-static, non-transient fields in the class (and all superclasses) are either public (and non-final) or have a public getter- and a setter- method that follows the Java beans naming conventions for getters and setters. (afaik case classes have final fields with getters in the generated JVM class)
You can implement all required things in a normal scala class but your IDE might not support you well. An option is to create your class in Java, let your IDE beanify it and convert it to scala (or use it directly).
There is also the option to create evolution support for case classes with a custom serializer. That will eventually be available by Flink. (You could also go ahead and contribute it).
Suppose the range of an object property is defined through a class expression. I want to know whether a particular instance of the class can be used in the range of the object property. Is it possible to use the OWL API and check if a particular class is subsumed by this class expression ?
OWLAPI cannot provide a complete answer for this - an OWLReasoner implementation is necessary for complete results.
Given an implementation of OWLReasoner, to check entailment you can either list all the subclasses of a class expression and check if the class you're interested in appears in the response, or ask the reasoner if your class of interest is a subclass, i.e.,
OWLDataFactory df = ...
OWLClassExpression ce = ...
OWLClass c = ...
OWLReasoner r = ...
OWLAxiom ax = df.getOWLObjectSubClassOf(c, ce);
boolean cIsSubclassOfCE = r.isEntailed(ax);
I recently encountered a situation where one might want to run a datastore query which includes a kind, but the class of the corresponding model is not available (e.g. if it's defined in a module that hasn't been imported yet).
I couldn't find any out-of-the-box way to do this using the google.appengine.ext.db package, so I ended up using the google.appengine.api.datastore.Query class from the low-level datastore API.
This worked fine for my needs (my query only needed to count the number of results, without returning any model instances), but I was wondering if anyone knows of a better solution.
Another approach I've tried (which also worked) was subclassing db.GqlQuery to bypass its constructor. This might not be the cleanest solution, but if anyone is interested, here is the code:
import logging
from google.appengine.ext import db, gql
class ClasslessGqlQuery(db.GqlQuery):
"""
This subclass of :class:`db.GqlQuery` uses a modified version of ``db.GqlQuery``'s constructor to suppress any
:class:`db.KindError` that might be raised by ``db.class_for_kind(kindName)``.
This allows using the functionality :class:`db.GqlQuery` without requiring that a Model class for the query's kind
be available in the local environment, which could happen if a module defining that class hasn't been imported yet.
In that case, no validation of the Model's properties will be performed (will not check whether they're not indexed),
but otherwise, this class should work the same as :class:`db.GqlQuery`.
"""
def __init__(self, query_string, *args, **kwds):
"""
**NOTE**: this is a modified version of :class:`db.GqlQuery`'s constructor, suppressing any :class:`db.KindError`s
that might be raised by ``db.class_for_kind(kindName)``.
In that case, no validation of the Model's properties will be performed (will not check whether they're not indexed),
but otherwise, this class should work the same as :class:`db.GqlQuery`.
Args:
query_string: Properly formatted GQL query string.
*args: Positional arguments used to bind numeric references in the query.
**kwds: Dictionary-based arguments for named references.
Raises:
PropertyError if the query filters or sorts on a property that's not indexed.
"""
from google.appengine.ext import gql
app = kwds.pop('_app', None)
namespace = None
if isinstance(app, tuple):
if len(app) != 2:
raise db.BadArgumentError('_app must have 2 values if type is tuple.')
app, namespace = app
self._proto_query = gql.GQL(query_string, _app=app, namespace=namespace)
kind = self._proto_query._kind
model_class = None
try:
if kind is not None:
model_class = db.class_for_kind(kind)
except db.KindError, e:
logging.warning("%s on %s without a model class", self.__class__.__name__, kind, exc_info=True)
super(db.GqlQuery, self).__init__(model_class)
if model_class is not None:
for property, unused in (self._proto_query.filters().keys() +
self._proto_query.orderings()):
if property in model_class._unindexed_properties:
raise db.PropertyError('Property \'%s\' is not indexed' % property)
self.bind(*args, **kwds)
(also available as a gist)
You could create a temporary class just to do the query. If you use an Expando model, the properties of the class don't need to match what is actually in the datastore.
class KindName(ndb.Expando):
pass
You could then do:
KindName.query()
If you need to filter on specific properties, then I suspect you'll have to add them to the temporary class.
I used Protege to make an ontology. It consists of two base classes. Each of them has three subsclasses. Subclasses are classes with specific object and data properties. When I make an instance I make it as class individual but with such data properties so that instance also belongs to some subclass. When I run Hermit in Protege, it is well done. It says that my instance belongs to class and subclass. But in Jfact in Java and Owlapi, when I try to print all instances from subclasses, my instance belongs only to superclass, not to subclass. Is it about JFact or something else?
for (OWLClass c : ont.getClassesInSignature()) {
if (c.getIRI().getFragment().equals("Baby")) {
NodeSet<OWLClass> subClasses = reasoner.getSubClasses(c, false);
for (OWLClass subClass : subClasses.getFlattened()) {
System.out.println(subClass.getIRI().getFragment());
NodeSet<OWLNamedIndividual> instances = reasoner.getInstances(subClass, false);
for (OWLNamedIndividual i : instances.getFlattened()) {
System.out.println(i.getIRI().getFragment());
s += i.getIRI().getFragment() + " ";
}
}
}
}
Looks like a bug in JFact. Can you add an issue on its tracker, with version and ontology?
Edit: JFact 1.2.1 is ancient and has known bugs. Update to at least 1.2.4 (and OWLAPI 3.5.5, the most recent release for the 3.x version).
If possible, I recommend updating to OWLAPI 4 (for Java 7) or 5 (for Java 8) and the same main version for JFact.
I'm using XmlSerializer. I've had no problems with it until now. I updated Silverlight from 4 to 5 and at the same time also updated the WCF RIA Services from v1 SP1 to v1 SP2. Now the following line gives me an error.
XmlSerializer s = new XmlSerializer(typeof(MyCustomObject));
The error is:
System.InvalidOperationException: System.ServiceModel.DomainServices.Client.EntityConflict cannot be serialized because it does not have a parameterless constructor.
The object I'm using (MyCustomObject in the sample) has not changed in any way so I'm starting to think it's either SL5 or the new RIA Services that is breaking my code. I didn't find any breaking changes document or mentions that this could happen. I don't know why it has a problem with EntityConflict since I'm not using any entities within my object.
Has anyone seen an error like this and/or know how to solve it?
UPDATE!
The final property that the error message says before EntityConflict is an Entity. I think that makes a difference but it has been working before. I'd also like to know why the serializer already tries to serialize the object in the constructor?
public static XmlSerializer GetEntityXmlSerializer<TEntity>()
where TEntity : Entity
{
XmlAttributes ignoreAttribute = new XmlAttributes()
{
XmlIgnore = true,
};
// use base class of Entity,
// if you use type of implementation
// you will get the error.
Type entityType = typeof(Entity);
var xmlAttributeOverrides = new XmlAttributeOverrides();
xmlAttributeOverrides.Add(entityType, "EntityConflict", ignoreAttribute);
xmlAttributeOverrides.Add(entityType, "EntityState", ignoreAttribute);
return new XmlSerializer(typeof(TEntity), xmlAttributeOverrides);
}
I am not sure why this would be happening, RIA Services entities are not XmlSerializable objects and the entities themselves are not decorated with the [Serializable] attribute. Have you added partial classes on the client side which decorate the entities with [Serializable] or modified the code generation in some way?
I got around this problem by using intermediary serializable POCO objects which were copies of my custom objects (which were inherited from Entity). The POCO objects did not inherit from Entity. I just updated their values from the original Entity objects. They then serialized quite nicely. Of course, when you de-serialize you need to update your Entity objects from the POCO objects.