can I do this in AllegroGraph prolog? - prolog

I have an RDF file and I need to extract some information from it in a single line.
Now, I'm using AllegroGraph with Prolog query engin :
(select (?result)
(q ?a !rdfs:label ?alabel)
(q ?b !rdfs:label ?blabel)
(lisp ?result (string+ ?alabel " AND " ?blabel)))
to get the results in a single line:
"{a1} AND {b1}"
"{a1} AND {b2}"
"{a2} AND {b1}"
"{a2} AND {b2}"
Now, I need to group all the rows of ?result in a single line with the string "OR". so i get:
"{a1} AND {b1} OR {a1} AND {b2} OR {a2} AND {b1} OR {a2} AND {b2}"
Is there any function in prolog to do this?

The fact that you've only got a* on the left and b* on the right means that you've got some other selection condition than just having a label. Given data like this:
#prefix : <http://example.org/>.
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
:a1 a :ClassA ; rdfs:label "a1" .
:a2 a :ClassA ; rdfs:label "a2" .
:b1 a :ClassB ; rdfs:label "b1" .
:b2 a :ClassB ; rdfs:label "b2" .
you can select ?a and ?b by their classes (:ClassA and :ClassB), and then extract their labels as well, with a pattern like:
?a a :ClassA ; rdfs:label ?alabel .
?b a :ClassB ; rdfs:label ?blabel .
Then you can get the {alabel} AND {blabel} with a bind and a concat:
bind( concat( "{", ?alabel, "} AND {", ?blabel, "}" ) as ?AandB )
Using these, a query like
prefix : <http://example.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?AandB {
?a a :ClassA ; rdfs:label ?alabel .
?b a :ClassB ; rdfs:label ?blabel .
bind( concat( "{", ?alabel, "} AND {", ?blabel, "}" ) as ?AandB )
}
will get you the kind of results that you can already get:
-------------------
| AandB |
===================
| "{a2} AND {b2}" |
| "{a2} AND {b1}" |
| "{a1} AND {b2}" |
| "{a1} AND {b1}" |
-------------------
The trick now is to use group_concat and an implicit group to combine all these into a string, with a separator of " OR ":
prefix : <http://example.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ( group_concat( ?AandB ; separator=" OR ") as ?string ) where {
?a a :ClassA ; rdfs:label ?alabel .
?b a :ClassB ; rdfs:label ?blabel .
bind( concat( "{", ?alabel, "} AND {", ?blabel, "}" ) as ?AandB )
}
to get a result:
----------------------------------------------------------------------
| string |
======================================================================
| "{a2} AND {b2} OR {a2} AND {b1} OR {a1} AND {b2} OR {a1} AND {b1}" |
----------------------------------------------------------------------
If you like, you can even get rid of the bind, and just put the concat expression right into the group_concat. You might find that easier to read (less jumping around) or harder to read (big one-liner), but at least it's good to have options:
prefix : <http://example.org/>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ( group_concat( concat( "{",?alabel,"} AND {",?blabel,"}" ) ; separator=" OR ") as ?string ) where {
?a a :ClassA ; rdfs:label ?alabel .
?b a :ClassB ; rdfs:label ?blabel .
}
There are some other examples of group_concat floating around on StackOverflow that might be useful to you as well:
Aggregating results from SPARQL query
RDF list subjects with their objects in a single line (this is actually another question that you asked, and that I answered, and in that answer, I also pointed out the previous question)
obtain the matrix in protege (this does some pretty fancy string manipulation)
SPARQL query to get all parent of a node

Related

Stardog raises ConcurrentModificationException upon reasoned query on graph with SWRL Rules

When trying to execute a query with reasoning in a model, I get this error: 000012: com.complexible.stardog.plan.eval.operator.OperatorException: Uncaught error during query evaluation: ConcurrentModificationException
Ontology model has 2 SWRL rules and is consistent, as it runs fine in Protege (with built-in pellet reasoner). Model has been imported into a stardog rdf model and the query
SELECT ?tse
FROM <urn:test_graph>
{
?tse a :TaperedShaftEnd
}
fails with the above error message.
By deleting the SWRL rules the query runs fine, but of course the model does no longer represent the desired domain. By translating the SWRL rules into stardog native rules, the error appears again.
Reasoning level is set to DL, as SL would imply too high processing times for the target application.
Changing the sameAs flag in the reasoner options does not change the behavior.
Setting the reasoning level to SL, as advised generally in Stardog documentation would solve the problem on this simplified model, but as said would result impractical for the use on the final target application.
May you help me in identifying the root cause and the possible solution?
Did anyone experienced the same issue when dealing with SWRL rules?
Here below the concerned model in turtle format, to easily replicate the issue on a test graph.
Heartful thanks to anyone who may help me with this.
#prefix : <http://api.stardog.com/> .
#prefix owl: <http://www.w3.org/2002/07/owl#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
#prefix stardog: <tag:stardog:api:> .
#prefix test: <urn:test_graph#> .
<http://swrl.stanford.edu/ontologies/3.3/swrla.owl#isRuleEnabled> a owl:AnnotationProperty .
<urn:test> a owl:Ontology .
:hasTorqueTransmissionConnectionWith a owl:ObjectProperty , owl:FunctionalProperty , owl:InverseFunctionalProperty , owl:SymmetricProperty , owl:IrreflexiveProperty .
:CouplingConnection a owl:Class ;
owl:disjointWith :ShaftEnd .
:ShaftEnd a owl:Class ;
owl:disjointWith :ShaftEnd .
:LeftCouplingConnection a owl:Class ;
rdfs:subClassOf :CouplingConnection ;
owl:disjointWith :RightCouplingConnection .
:RightCouplingConnection a owl:Class ;
rdfs:subClassOf :CouplingConnection .
:LeftShaftEnd a owl:Class ;
rdfs:subClassOf :ShaftEnd ;
owl:disjointWith :RightShaftEnd .
:RightShaftEnd a owl:Class ;
rdfs:subClassOf :ShaftEnd .
:TaperedCouplingConnection a owl:Class , <tag:stardog:api:rule:SPARQLRule> ;
rdfs:subClassOf :CouplingConnection ;
<tag:stardog:api:rule:content> """IF {
?tse a :TaperedShaftEnd .
?tse :hasTorqueTransmissionConnectionWith ?cc .
?cc a :CouplingConnection .
}
THEN {
?cc a :TaperedCouplingConnection .
}
""" .
:TaperedShaftEnd a owl:Class , <tag:stardog:api:rule:SPARQLRule> ;
rdfs:subClassOf :ShaftEnd ;
<tag:stardog:api:rule:content> """IF {
?tcc a :TaperedCouplingConnection .
?tcc :hasTorqueTransmissionConnectionWith ?se .
?se a :ShaftEnd .
}
THEN {
?se a :TaperedShaftEnd .
}
""" .
:lcc1 a :LeftCouplingConnection , owl:NamedIndividual .
:lse2 a :LeftShaftEnd , owl:NamedIndividual .
:rcc2 a :RightCouplingConnection , :TaperedCouplingConnection , owl:NamedIndividual ;
:hasTorqueTransmissionConnectionWith :lse2 .
:rse1 a :RightShaftEnd , owl:NamedIndividual ;
:hasTorqueTransmissionConnectionWith :lcc1 .

Grammar LaTeX like with mixed whitespace utf and commands

I've tried to implement a LaTeX like grammar that could allow me to parse this kind of sentence :
\title{Un pré é"'§è" \VAR state \draw( 200\if{expression kjlkjé} ) bis tèr }
As you can see, the \title{ } can contain several kind of items :
string in utf8 without quotes and with whitespace which I'd like to
keep in one token
a variable call as : \variable_name
some \keyword following by parentheses or other with braces : for instance \draw( utf8 \var \if{ } ... ) or \if{ idem }.
These items can be nested.
I get inspiration from the XML parser presented in ANTLR 4 book and try to use mode. I meet a problem concerning the recognition of the closing braces of closing parentheses. I also meet a problem with some whitespaces, for instance the one who follows the \variable_name ( I get a : extraneous input ' ').
Here my lexer gramar code :
lexer grammar OEFLexer;
// Default mode rules (the SEA)
SEA_WS : (' '|'\t'|'\r'? '\n')+ ;
TITLE : '\\title';
OB : '{';
OP : '(';
BSLASH : '\\' -> mode(CALLREFERENCE) ;
TEXT : ~[\\({]+; // clump all text together
// ----------------- Everything Callreference ---------------------
mode CALLREFERENCE;
CLOSECALLVAR : ' ' -> mode(DEFAULT_MODE) ; // back to SEA mode
CB : '}' -> mode(DEFAULT_MODE) ; // back to SEA mode
CP : ')' -> mode(DEFAULT_MODE) ; // back to SEA mode
DRAW : 'draw' OP;
IF : 'if' OB;
ID : [a-zA-Z]+ ; // match/send ID in tag to parser
Here my parser grammar
parser grammar OEFParser;
options { tokenVocab=OEFLexer; }
document: TITLE OB ( callreference | string )* CB;
string : TEXT;
var : ID;
commandDraw : DRAW ( callreference | string )* CP ;
commandIf : IF ( callreference | string )* CB ;
callreference : BSLASH ID | BSLASH commandDraw CP | BSLASH commandIf CP;
When I tried to parse the \title code mentionned at the beginning I obtain :
line 1:25 extraneous input ' ' expecting {'\', TEXT, '}'}
line 1:37 extraneous input ' ' expecting {'\', TEXT, ')'}
line 1:45 mismatched input 'expression' expecting {'\', TEXT, '}'}
line 1:75 extraneous input '<EOF>' expecting {'\', TEXT, ')'}
With this generated tree generated by Grun
Thanks for your help to help me tackle this issue.
Chris
The problem is the space after expression:
\title{Un pré é"'§è" \VAR state \draw( 200\if{expression kjlkjé} ) bis tèr }
^
^
^
which causes the mode to go back to the DEFAULT_MODE:
CLOSECALLVAR : ' ' -> mode(DEFAULT_MODE) ;
Something that you don't want because you're (obviously) still in the CALLREFERENCE context.
One way to handle this is to use -> pushMode(...) and -> popMode directives that causes a stack of CALLREFERENCE modes to be created. Whenever you stumble upon a \... ( and \... { you push a new CALLREFERENCE onto this stack, and then pop one off when you see a ) or }.
A quick lexer grammar demo:
lexer grammar OEFLexer;
TITLE : '\\title' S? OB -> pushMode(CALLREFERENCE);
fragment OB : '{';
fragment OP : '(';
fragment S : [ \t\r\n]+;
mode CALLREFERENCE;
CB : '}' -> popMode;
CP : ')' -> popMode;
DRAW : '\\draw' S? OP -> pushMode(CALLREFERENCE);
IF : '\\if' S? OB -> pushMode(CALLREFERENCE);
BSLASH : '\\';
ID : [a-zA-Z]+;
CR_OTHER : .;
and the parser grammar:
parser grammar OEFParser;
options { tokenVocab=OEFLexer; }
document
: TITLE ( callreference | string )* CB EOF
;
string
: CR_OTHER+
| ID
;
commandDraw
: DRAW ( callreference | string )* CP
;
commandIf
: IF ( callreference | string )* CB
;
callreference
: BSLASH ID
| commandDraw
| commandIf
;
Parsing you example input will result in the following parse tree:

preg_match(): No ending delimite

My website shows the following error when host updates the php version
Warning: preg_match(): No ending delimiter '^' found in
in the following code:
<?php
$mid_str = " and mid != '0' and ";
if($_REQUEST['search']){
mysql_select_db($database_myconn, $myconn);
$query_spages = "SELECT id, url, title, description, keywords, active, ip, catID, exp, pdate,
MATCH(title,description,keywords)
AGAINST ('$search_str' IN BOOLEAN MODE) AS score FROM pages
WHERE MATCH(title, description,keywords)
AGAINST ('$search_str' IN BOOLEAN MODE) " . $mid_str . " active = 'Yes' ORDER BY score DESC";
}else// not search fetch rand by catid
$query_spages = "SELECT * FROM pages where " . preg_match("^ and", "", $mid_str) . " active = 'Yes' and catID = '" . $_REQUEST['id'] . "' ORDER BY mid DESC";
mysql_select_db($database_myconn, $myconn);
$spages = mysql_query($query_spages, $myconn) or die(mysql_error());
//$row_spages = mysql_fetch_assoc($spages);
unset($settings);
$settings = mysql_fetch_assoc(mysql_query('select * from settings where id = 1',$myconn));
?>
preg_match expects a start and an end of the regular expression.
For example preg_match("/^ and/", "", $mid_str).
In your case, ^ is taken as start delimiter but the regex does not end with ^ so you get an error. Start and end delimiter can be anything, but most likely /is used to not clash with other specially treated characters.
Also, you probably mixed up preg_matchand preg_replace. I think you want to preg_replacehere, p.e.
$query_spages = "SELECT * FROM pages where " . preg_replace("/^ and/", "", $mid_str) . " active = 'Yes' and catID = '" . $_REQUEST['id'] . "' ORDER BY mid DESC";

SPARQL INSERT query in Protege

I'm trying to map my Data Properties in Protege to owl:Class with Sparql but it doesn't work. If any one has an example, please don't hesitate to give me an answer.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX myOnto: <http://imis/SNI/9.4.3#>
INSERT DATA
{
?s rdf:type owl:Class
}
WHERE { ?s ?o owl:DatatypeProperty}
When i was running it i got this message . Please help me to resolve this problem.
Error 61 Logged at Wed Oct 15 23:11:26 CEST 2014
SparqlReasonerException: org.openrdf.query.MalformedQueryException: Encountered " "insert" "INSERT "" at line 7, column 1.
Was expecting one of:
"base" ...
"prefix" ...
"select" ...
"construct" ...
"describe" ...
"ask" ...

Upside down text

How would you design a program that will take in a string of lower case letters and produce the string upside down?
so if I type in home
i get ǝɯoɥ upside down.
I've tried looking for in the book to get started, but nothing.
Try this, a bit of a brute-force approach but works quite well for uppercase, lowercase and number characters - all other characters are presented just as they come:
(define upside-map '#hash(
(#\a . #\ɐ) (#\b . #\q) (#\c . #\ɔ) (#\d . #\p) (#\e . #\ǝ) (#\f . #\ɟ)
(#\g . #\ƃ) (#\h . #\ɥ) (#\i . #\ı) (#\j . #\ɾ) (#\k . #\ʞ) (#\l . #\ן)
(#\m . #\ɯ) (#\n . #\u) (#\o . #\o) (#\p . #\d) (#\q . #\b) (#\r . #\ɹ)
(#\s . #\s) (#\t . #\ʇ) (#\u . #\n) (#\v . #\ʌ) (#\w . #\ʍ) (#\x . #\x)
(#\y . #\ʎ) (#\z . #\z) (#\A . #\∀) (#\B . #\𐐒) (#\C . #\Ɔ) (#\D . #\◖)
(#\E . #\Ǝ) (#\F . #\Ⅎ) (#\G . #\⅁) (#\H . #\H) (#\I . #\I) (#\J . #\s)
(#\K . #\⋊) (#\L . #\˥) (#\M . #\W) (#\N . #\N) (#\O . #\O) (#\P . #\Ԁ)
(#\Q . #\Ό) (#\R . #\ᴚ) (#\S . #\S) (#\T . #\⊥) (#\U . #\∩) (#\V . #\Λ)
(#\W . #\M) (#\X . #\X) (#\Y . #\⅄) (#\Z . #\Z) (#\0 . #\0) (#\1 . #\Ɩ)
(#\2 . #\ᄅ) (#\3 . #\Ɛ) (#\4 . #\ㄣ) (#\5 . #\ϛ) (#\6 . #\9) (#\7 . #\ㄥ)
(#\8 . #\8) (#\9 . #\6)))
(define (flip-string str)
(list->string
(map (lambda (c)
(hash-ref upside-map c (const c)))
(reverse (string->list str)))))
For example:
(flip-string "Hello World")
=> "pןɹoM oןןǝH"
For reference, I used this conversion table taken from Wikipedia. The above solution has a little wrinkle: I couldn't manage to make it work for the 𐐒 character (flipped B), with unicode value of #\u10412 - because it won't fit in a 16-bit unicode character, so it can't be represented. I wasn't aware that Racket doesn't support characters with an encoding requiring more than 16 bits.
first of all your website must support Unicode, Unicode consists thousands of characters, the first 127 of Unicode are ASCII. It is possible to create text that appears to be upside down by converting character by character to a Unicode sign that looks like the upside down version of the character, for example to convert "6" you can use "9" but the flipped version of "f" is "ɟ", which is a Latin character with Unicode number 607 (hex code 025F)
technically, you need two text area boxes, one for the original text and the other one for the flipped text, also you need a Javascript, use the onkeyup Javascript hook in the first text box to call an upsideDownText() function each time a key is released like this:
<textarea rows="5" cols="70" id="src" onkeyup="upsideDownText()"></textarea>
then do the text processing in the upsideDownText() Javascript function like this:
<script type="text/javascript">
function upsideDownText() {
var srcText = document.getElementById( 'src' ).value.toLowerCase();
var out = '';
for( var i = srcText.length - 1; i >= 0; --i ) {
var ch = srcText.charAt( i );
if( ch == 'a' ) {
out += '\u0250' }
} else if( ch == 'b' ) {
out += 'q' }
} else if( ch == 'c' ) {
out += '\u0254'
// etc....
} else {
out += ch
}
}
document.getElementById( 'dest' ).value = out;
}
</script>
get the content of the text box identified by id="src" and convert the string to lowercase using the toLowerCase() method. Then loop through the string, character by character, starting from the end of the string. A big if-then-else-if block handles the character conversion. finally push the converted string into the text box identified by id="dest", which is the lower text box.
you can find the full list of how doing this step by step from Source twiki.org

Resources