Jena Rule: How to use now(?x) Builtin primitive - jena

Can please someone explain about now(?x) Builtin primitive in jena rule. I need to understand about this but not found resource on internet. Like example etc.
Only found information about this from Apache jean website which is that:
now(?x):Binds ?x to an xsd:dateTime value corresponding to the current time.
Any Example how to use in rules.
Thanks!

call this function in rule body, here is an example :
[r1: (?s :startTime ?x) <- (?s a :Match), now(?x)]

Related

Does the Feel language builtin string function 'replace' affect the first match or all occurrances of the search pattern?

The Decision Model and Notation Feel Language has many builtin functions.
For strings, one function is replace. It accepts a search string, a regex pattern, a replacement string, and optional flags.
Does replace act only on the first regex match or does it replace all matches? The DMN version 1.3 specification, page 138, does not seem to address this.
In your question, it replaces all matches.
Some other valid examples:
replace("banana","a","o") = "bonono"
taken as one of the agreed behaviour test cases, from the DMN TCK project.
I agree in the DMN Specification document from OMG, it could list some more down-to-Earth examples :)

How can I determine if two mathematical expressions are 'sort of' the same?

Sorry about the vagueness of this question, and maybe I want a different exchange? Not sure. Anyway, here goes:
I'd like to be able to determine if two mathematical expressions are 'the same'. I don't need full equivalence testing, which I understand is impossible anyway. Basically, I'd like to make a function that looks like this:
areTheSame(expression1, expression2, [testing methods])
where [testing methods] might include: 'exact', 'allow commutativity', 'allow distribution', ...
'exact' would be easy: expression1 == expression2 if their strings
are exactly equal
'allow commutativity' would be more difficult. For instance, if
expression1 is y=3*x and expression2 is y=x*3, then under 'allow
commutativity', they would be the same. Ditto here for 'y=x' and 'x=y'.
'allow distribution' would allow 'y=2*(x-3)' to be equal to
'y=2*x-6'.
others?
Ideally, I'd like to be lazy! I'd love to find some library that
supports parsing expressions from latex representations (or MathML, which is xml already, maybe it's easier to parse)
supports equivalence testing with some flags or something that
govern how exact the comparison should be, as above.
is written in c or c++ (or Objective-C - this is for an iOS project)
is not GPL.
4 rules out SymbolicC++ and GiNaC afaik. Mathomatic is LGPL, which I'm not sure about in the context of apple's app-Store (and I would really not like to have to give out object files)
Any ideas? Thanks!
This may not be an answer, but it's too long to be a comment. :)
My first thought: this is not an easy or common problem, so you probably won't have much luck finding a library to do it for you. Finding a library that does some of the non-critical parts, on the other hand, shouldn't be difficult.
What you probably want to do is parse the expressions to create an abstract syntax tree (you'll probably find lots of libraries for that), then recursively analyze the AST yourself to test whatever definition of sameness you're after.
In iOS, a decent place to start might be (horribly ab)using NSExpression and NSPredicate. These have constructor methods that parse a string and return a structure of expression and predicate objects.
Recursively walk that structure. For each predicate, check to see if the predicateOperatorType matches... if it doesn't, the predicates aren't the same. If it does, look at the left predicate's leftExpression and the right predicate's leftExpression. Each expression has a function that tells you what its operator is (add, subtract, etc). If they don't match, the expressions aren't the same. (Do the same check for the other side.) If they do, recurse: look at each expression's sub-expressions and do a similar check, and so on until you get to expressions that are constant values or variables.
That's a rough sketch of how to see if two predicates (and the expressions they contain) "match". For "sort of the same", just relax each check you perform while recursively walking the tree and/or add more checks; e.g. if you get to an expression whose function is add, check for commutativity by comparing its sub-expressions to the corresponding ones in the other predicate in either order. (Also, there are probably other libraries that'll parse basic math expressions and get you an AST you can walk however you like.)
That still won't get you everything you're after — "allow distribution" gets you into the realm of full-fledged CAS software. Maybe look into whether the likes of Wolfram Alpha have web service APIs?

First class patterns in Erlang? (Alternatives)

Is there a way to create first-class-like patterns in Erlang? I need to be able to create and pass patterns as args to other functions but I know patterns are not first class in Erlang. I also looked at Elixir but it doesn't seem to offer anything more as far as patterns go.
I was wondering if anyone has come up with a simple solution to this problem. I was thinking of trying to implement something like this:
% Instead of using variables, we would just use uppercase atoms which would serve as vars
% A passable pattern
Pattern = {ok, 'Result'}.
% Custom function to check for matches
match(pattern, {ok, [1,2,3]}). % => true
I am new to Erlang so perhaps this is completely unnecessary. Perhaps there is a library that does this sort of thing?
Any advice is greatly appreciated. Thanks in advance!
I don't know if something exists already that does what you want, but you can easily implement it like this:
-module (match).
-compile([export_all]).
-define(MF(S), fun(S) -> true; (_)->false end).
match(F,V) -> F(V).
test() ->
Pattern = ?MF({ok,_}),
false = match(Pattern,{error,reason}),
true = match(Pattern,{ok,[1,2,3]}).
You might want to look at Erlang match specifications, which I believe are the sorts of patterns you're asking about. They're used for matching values in Erlang's tables and databases as well as in Erlang tracing. You might find some inspiration there.
I'm not sure I see your entire problem but it seems that a predicate function would suite you well. It's pretty common way to parameterize generic functions with them in a functional language. Take a look at lists functions such as map, foldl, filter.
I ended up using Elixir's macro functionality to implement something similar to Erlang's match specs. The code looks much cleaner(since I am simply defining functions with patterns) and they work very similar to Erlang's match specs.

About the | operator in erlang.

We can make a nested list in erlang by writing something like this:
NL = [[2,3], [1]].
[[2,3],[1]]
But assume we wrote it like this instead:
OL = [[2,3]|1].
[[2,3]|1]
Is OL still a list? Can someone please elaborate more what OL is?
This is called an improper list and should typically not be used. I think most library functions expects proper lists (e.g. length([1|2]) throws bad argument exception). Pattern matching with improper lists works though.
For some use cases, see Practical use of improper lists in Erlang (perhaps all functional languages)
More information about | and building list is given in Functional Programming: what is an "improper list"? .

Is the "expression problem" solvable in F#?

I've been watching an interesting video in which type classes in Haskell are used to solve the so-called "expression problem". About 15 minutes in, it shows how type classes can be used to "open up" a datatype based on a discriminated union for extension -- additional discriminators can be added separately without modifying / rebuilding the original definition.
I know type classes aren't available in F#, but is there a way using other language features to achieve this kind of extensibility? If not, how close can we come to solving the expression problem in F#?
Clarification: I'm assuming the problem is defined as described in the previous video
in the series -- extensibility of the datatype and operations on the datatype with the features of code-level modularization and separate compilation (extensions can be deployed as separate modules without needing to modify or recompile the original code) as well as static type safety.
As Jörg pointed out in a comment, it depends on what you mean by solve. If you mean solve including some form of type-checking that the you're not missing an implementation of some function for some case, then F# doesn't give you any elegant way (and I'm not sure if the Haskell solution is elegant). You may be able to encode it using the SML solution mentioned by kvb or maybe using one of the OO based solutions.
In reality, if I was developing a real-world system that needs to solve the problem, I would choose a solution that doesn't give you full checking, but is much easier to use.
A sketch would be to use obj as the representation of a type and use reflection to locate functions that provide implementation for individual cases. I would probably mark all parts using some attribute to make checking easier. A module adding application to an expression might look like this:
[<Extends("Expr")>] // Specifies that this type should be treated as a case of 'Expr'
type App = App of obj * obj
module AppModule =
[<Implements("format")>] // Specifies that this extends function 'format'
let format (App(e1, e2)) =
// We don't make recursive calls directly, but instead use `invoke` function
// and some representation of the function named `formatFunc`. Alternatively
// you could support 'e1?format' using dynamic invoke.
sprintfn "(%s %s)" (invoke formatFunc e1) (invoke formatFunc e2)
This does not give you any type-checking, but it gives you a fairly elegant solution that is easy to use and not that difficult to implement (using reflection). Checking that you're not missing a case is not done at compile-time, but you can easily write unit tests for that.
See Vesa Karvonen's comment here for one SML solution (albeit cumbersome), which can easily be translated to F#.
I know type classes aren't available in F#, but is there a way using other language features to achieve this kind of extensibility?
I do not believe so, no.
If not, how close can we come to solving the expression problem in F#?
The expression problem is about allowing the user to augment your library code with both new functions and new types without having to recompile your library. In F#, union types make it easy to add new functions (but impossible to add new union cases to an existing union type) and class types make it easy to derive new class types (but impossible to add new methods to an existing class hierarchy). These are the two forms of extensibility required in practice. The ability to extend in both directions simultaneously without sacrificing static type safety is just an academic curiosity, IME.
Incidentally, the most elegant way to provide this kind of extensibility that I have seen is to sacrifice type safety and use so-called "rule-based programming". Mathematica does this. For example, a function to compute the symbolic derivative of an expression that is an integer literal, variable or addition may be written in Mathematica like this:
D[_Integer, _] := 0
D[x_Symbol, x_] := 1
D[_Symbol, _] := 0
D[f_ + g_, x_] := D[f, x] + D[g, x]
We can retrofit support for multiplication like this:
D[f_ g_, x_] := f D[g, x] + g D[f, x]
and we can add a new function to evaluate an expression like this:
E[n_Integer] := n
E[f_ + g_] = E[f] + E[g]
To me, this is far more elegant than any of the solutions written in languages like OCaml, Haskell and Scala but, of course, it is not type safe.

Resources