Reasoning inbetween dates

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Reasoning inbetween dates

Lukas
I try to create some semantic rules and want to save them using owlready2 for python. I want to reason over timerange related errors, so i created a error class and a TimeRangeWithError class. Now i want to check if the incoming error is a TimeRangeRelatedError, so i try to use the equivalent_to-method to set up some rule, that the hermit reasoner can distinguish between an Error and a TimeRelatedError. Nevertheless, the compiler raise the an error saying i can't store a function:

File "C:\Users\-name-\AppData\Local\Programs\Python\Python36\lib\site-packages\owlready2\base.py", line 49, in to_literal
    if datatype is None: raise ValueError("Cannot store literal '%s' of type '%s'!" % (o, type(o))) ValueError: Cannot store literal '<function TimeRangeWithError.is_in_timerange at 0x000002797417B6A8>' of type '<class 'function'>'!

I don't know any other way to check if the timespan is in a specific range. I think i modelled something wrong within my ontology description. Could you help me to build an ontology that is capable of checking if the value is inbetween dates?

from owlready2 import *

onto = get_ontology("http://test.org/rules.owl")



with onto:
    class Error(Thing):
        pass
    class appeared_at(Error >> datetime.datetime): pass
    class TimeRangeWithError(Thing):
        def is_in_timerange(self):
            return self.timerange_start_at <= self.error_at and self.timerange_end_at >= self.error_at

    class timerange_start_at(TimeRangeWithError >> datetime.datetime): pass
    class timerange_end_at(TimeRangeWithError >> datetime.datetime): pass
    class error_at(TimeRangeWithError >> datetime.datetime): pass
    class TimeRangeRelatedError(Error):
        equivalent_to = [Error & TimeRangeWithError.is_in_timerange]


onto.save()
Reply | Threaded
Open this post in threaded view
|

Re: Reasoning inbetween dates

Lukas
Next try with a swrl rule:

with onto:
    class Error(Thing): pass
    class appeared_at(Error >> datetime.datetime): pass
    class TimeRangeWithError(Thing): pass

    class timerange_start_at(TimeRangeWithError >> datetime.datetime, FunctionalProperty): pass
    class timerange_end_at(TimeRangeWithError >> datetime.datetime, FunctionalProperty): pass
    class error_at(TimeRangeWithError >> datetime.datetime, FunctionalProperty): pass
    class is_during_timerange(TimeRangeWithError >> bool, FunctionalProperty) : pass
    class TimeRangeRelatedError(Error):
        equivalent_to = [Error & is_during_timerange]

    rule = Imp()
    rule.set_as_rule("""TimeRangeWithError(?d), timerange_start_at(?d, ?s), timerange_end_at(?d, ?e), error_at(?d, ?b), lessThanOrEqual(?b, ?s, ?r1), greaterThanOrEqual(?b, ?e, ?r2), equal(?r1, ?r2, ?r4) -> is_during_timerange(?d, ?r4)""")

But i get this error:
  File "C:\Users\----\AppData\Local\Programs\Python\Python36\lib\site-packages\owlready2\namespace.py", line 1058, in _open_onto_file
    if (mode.startswith("w")): return open(os.path.join(onto_path[0], "%s.owl" % name), mode)
IndexError: list index out of range
Reply | Threaded
Open this post in threaded view
|

Re: Reasoning inbetween dates

Lukas
Got it working, here is my code for others:
from datetime import timedelta

from owlready2 import *

onto = get_ontology("http://test.org/onto.owl")


with onto:
    class Error(Thing):
        def who(self):
            print("Error")
    class timerange_start_at(Error >> datetime.datetime, FunctionalProperty):
        python_name = "start"
    class timerange_end_at(Error >> datetime.datetime, FunctionalProperty):
        python_name = "end"
    class error_at(Error >> datetime.datetime, FunctionalProperty):
        python_name = "appeared"
    class is_after_start(Error >> bool, FunctionalProperty) : pass
    class is_before_end(Error >> bool, FunctionalProperty): pass

    class TimeRangeRelatedError(Error):
        equivalent_to = [Error & is_after_start.value(True) & is_before_end.value(True)]
        def who(self):
            print("TimeRange")

    rule1 = Imp()
    rule1.set_as_rule("""Error(?d), timerange_start_at(?d, ?s), error_at(?d, ?b), greaterThan(?b, ?s) -> is_after_start(?d, true)""")
    rule2 = Imp()
    rule2.set_as_rule("""Error(?d), timerange_end_at(?d, ?e), error_at(?d, ?b), lessThan(?b, ?e) -> is_before_end(?d, true)""")

onto.save(file="C:/Users/----/PycharmProjects/owlreasoner/rules.owl")

current = datetime.datetime.now()
start = current - timedelta(days=1)
end = current + timedelta(days=1)


error = Error(start = start, end = end, appeared = current)


error.who()



sync_reasoner_pellet(infer_property_values = True, infer_data_property_values = True)


error.who()