Minecraft datapack check if in block (portal) - minecraft

So this is 100% a minecraft data pack thing... (programming languages are not similar at all)
I have a function to detect if I am in a portal
execute at #a if block ~ ~ ~ blue_stained_glass_pane run say inPortal
Which looks like
So I was wondering if it would be possible to somehow either change this function into a predicate
Or if there is a way to change this into a seperate function and write
execute as #a if function???:inportal run...
A comment on any other ways to detect if they are in the portal would be cool too.

Setting into a predicate is not very practical and will just cause more issues! Doing it this way is simpler and will work fine for you!

Like RoyalQuack, I don't recommend predicate, but if you use execute at #a if block ~ ~ ~ blue_stained_glass_pane run ... you will probably have some issues.
Unless you only have this block in one place on your map, you will probably run portal somewhere else.
I recommend using a positional detection instead:
execute as #a[x=XX,y=YY,z=ZZ,dx=0,dy=1,dz=0] run function sys:yourfunction
(change XX YY ZZ by your location of course)
Using as instead of at allows to use #s in yourfunction which can be convenient. Example of yourfunction:
tp #s 10 25 10 particle flame X Y Z 0 0 0 0.02 force #a title #a [{"selector":#s, "text":" used a portal!", "color": "red"}]
If you have several portals that need to do the same action, instead of using fixed location and having to redo a different command at each portal, you can use an invisible entity to place on each portal and do the detection according to the invisible entity:
execute at #e[type=marker,tag=portal] as #a[dx=0,dy=1,dz=0] run function sys:yourfunction
Here it will run the yourfunction function for all players on the marker entity with the portal tag.

Related

How to run SPARQL queries in R (WBG Topical Taxonomy) without parallelization

I am an R user and I am interested to use the World Bank Group (WBG) Topical Taxonomy through SPARQL queries.
This can be done directly on the API https://vocabulary.worldbank.org/PoolParty/sparql/taxonomy but it can be done also through R by using the functions load.rdf (to load the taxonomy.rdf rdfxml file downloaded from https://vocabulary.worldbank.org/ ) and after using the sparql.rdf to perform the query. These functions are available on the "rrdf" package
These are the three lines of codes:
taxonomy_file <- load.rdf("taxonomy.rdf")
query <- "SELECT DISTINCT ?nodeOnPath WHERE {http://vocabulary.worldbank.org/taxonomy/435 http://www.w3.org/2004/02/skos/core#narrower* ?nodeOnPath}"
result_query_1 <- sparql.rdf(taxonomy_file,query)
What I obtain from result_query_1 is exactly the same I get through the API.
However, the load.rdf function uses all the cores available on my computer and not only one. This function is somehow parallelizing the load task over all the core available on my machine and I do not want that. I haven't found any option on that function to specify its serialized usege.
Therefore, I am trying to find other solutions. For instance, I have tried "rdf_parse" and "rdf_query" of the package "rdflib" but without any encouraging result. These are the code lines I have used.
taxonomy_file <- rdf_parse("taxonomy.rdf")
query <- "SELECT DISTINCT ?nodeOnPath WHERE {http://vocabulary.worldbank.org/taxonomy/435 http://www.w3.org/2004/02/skos/core#narrower* ?nodeOnPath}"
result_query_2 <- rdf_query(taxonomy_file , query = query)
Is there any other function that perform this task? The objective of my work is to run several queries simultaneously using foreach.
Thank you very much for any suggestion you could provide me.

Iterating over multiple model/data pairs in AMPL

Is there a good way to iterate over model/dataset pairs in AMPL in a .run file?
Say you have two different models for the same optimization problems, and four datasets. What I would do up to now was to make a .run file for each model/dataset pair, and run each individually, or keep one script for each model and manually solve for each dataset by modifying the data (file); command. But this is obviously tedious and inviable for larger projects.
So is there any good way to do this? What I've tried is something like the following (just a skeleton for clarity):
# Some global options
for {model_ in {'1.mod', '2.mod'}} {
reset;
model (model_);
for {dat in {'1.dat', '2.dat', '3.dat', '4.dat'}} {
update data;
data (dat);
solve;
# Do some post-processing
}
}
But AMPL whines about using the for loop's dummy variable in the model and data command. I've tried declaring symbolic parameters to store the name of the model and data file, but no good either.
Of course this would only be sensible if the models were similar enough, insofar as they could be post-processed in the same way. But there should at least be a way to iterate through the data files within one model without having to go like
update data;
data 1.dat;
solve;
update data;
data 2.dat;
solve;
update data;
data 3.dat;
solve;
[...]
update data;
data 427598.dat;
solve;
right?
Your example code has a reset inside a loop. This will reset the loop parameter, which is going to cause problems. For instance, if you run this:
for {modelname in {"itertest1.mod","itertest2.mod"}}{
display (modelname);
for {dataname in {"itertest_a.dat","itertest_b.dat"}}{
display (dataname);
}
}
it will print out the file names as we might hope:
modelname = itertest1.mod
dataname = itertest_a.dat
dataname = itertest_b.dat
modelname = itertest2.mod
dataname = itertest_a.dat
dataname = itertest_b.dat
But if we add a reset statement:
for {modelname in {"itertest1.mod","itertest2.mod"}}{
reset; ### this is the only line I changed ###
display (modelname);
for {dataname in {"itertest_a.dat","itertest_b.dat"}}{
display (dataname);
}
}
then we get an error: modelname is not defined (because we just reset it).
However, even without that issue, we'll still get a complaint about using the loop variable.
Solution 1: commands
Depending on exactly what you ran, you may have seen an error message advising use of the commands statement, like so:
reset;
for {scriptname in {"script1.run", "script2.run"}}{
commands (scriptname);
}
This will then run whatever commands are in the listed .run files, and you should be able to nest that to call separate scripts to define the models and the data. Since you can't use a blanket reset without killing your loop parameter, you will need to use other options for updating the model files. AMPL offers the option to define multiple "problems" and switch between them, which may or may not be helpful here; I haven't explored it.
The commands statement is documented here, although TBH I find the documentation a bit difficult to follow.
Solution 2: code generation
If all else fails, you can write an iterated AMPL script that generates (and optionally runs) a second script containing all the model/data combinations you want to run, like so:
param outfile symbolic := "runme_temp.run";
for{i in 1..3}{
for{j in 1..4}{
printf "\nreset;" > (outfile);
printf "\nmodel model%s;",i > (outfile);
printf "\ndata data%s;",j > (outfile);
printf "\nsolve;" > (outfile);
# add post-processing here as desired
}
}
close (outfile);
include runme_temp.run;
This will create and then call "runme_temp.run" which looks like this:
reset;
model model1;
data data1;
solve;
reset;
model model1;
data data2;
solve;
etc. etc. Since this does allow you to use a blanket reset; it might simplify the process of cleaning up previous models. Not the most dignified solution, but it works and can be adapted to a wide range of things.
This could be improved by de-nesting the loops:
param outfile symbolic := "runme_temp.run";
for{i in 1..3, j in 1..4}{
printf "\nreset;" > (outfile);
printf "\nmodel model%s;",i > (outfile);
printf "\ndata data%s;",j > (outfile);
printf "\nsolve;" > (outfile);
# add post-processing here as desired
}
close (outfile);
include runme_temp.run;
In this particular example it doesn't make much difference, but multiply-nested loops can run slow in AMPL; using a single multi-index for can make a big difference to performance, so it might be better to get into that habit.

Parse SQL with REGEX to find Physical Update

I've spent a bit of time trying to bend regex to my will but its beaten me.
Here's the problem, for the following text...
--to be matched
UPDATE dbo.table
UPDATE TOP 10 PERCENT dbo.table
--do not match
UPDATE #temp
UPDATE TOP 10 PERCENT #temp
I'd like to match the first two updates statements and not match the last two update statements. So far I have the regex...
UPDATE\s?\s+[^#]
I've been trying to get the regex to ignore the TOP 10 PERCENT part as its just gets in the way. But I haven't been successful.
Thanks in advance.
I'm using .net 3.5
I assume you're trying to parse real SQL syntax (looks like SQL Server) so I've tried something that is more suitable for that (rather than just detecting the presence of #).
You can try regex like:
UPDATE\s+(TOP.*?PERCENT\s+)?(?!(#|TOP.*?PERCENT|\s)).*
It checks for UPDATE followed by optional TOP.*?PERCENT and then by something that is not TOP.*?PERCENT and doesn't start with #. It doesn't check just for the presence of # as this may legitimately appear in other position and not mean a temp table.
As I understand it, you want a regex to interact with SQL code, not actually querying a database?
You can use a negative look ahead to check if the line has #temp:
(?m)^(?!.*#temp).*UPDATE
(?!...) will fail the whole match if what's inside it matches, ^ matches the beginning of the line when combined with the m modifier. (?m) is the inline version of this modifier, as I don't know how/where you plan on using the regex.
See demo here.
#Robin's solution is much better but in case you needed regex with some simplier mechanisms employed I give you this:
UPDATE\s+(TOP\s+10\s+PERCENT\s+)?[a-z\.]+
sqlconsumer, here's a fully functioning C# .NET program. Does it do what you're looking for?
using System;
using System.Text.RegularExpressions;
class Program {
static void Main() {
string s1 = "UPDATE dbo.table";
string s2 = "UPDATE TOP 10 PERCENT dbo.table";
string s3 = "UPDATE #temp";
string s4 = "UPDATE TOP 10 PERCENT #temp";
string pattern = #"UPDATE\s+(?:TOP 10 PERCENT\s+)?dbo\.\w+";
Console.WriteLine(Regex.IsMatch(s1, pattern) );
Console.WriteLine(Regex.IsMatch(s2, pattern));
Console.WriteLine(Regex.IsMatch(s3, pattern));
Console.WriteLine(Regex.IsMatch(s4, pattern));
Console.WriteLine("\nPress Any Key to Exit.");
Console.ReadKey();
} // END Main
} // END Program
The Output:
True
True
False
False

os.execute variables

I have multiple Steam accounts that I want to launch via a single Lua script with the options I specify. I've got pretty much everything sorted, except for launching with the code provided. I have no idea how to "pass" the variable with this format.
function Steam(n, opt1, opt2, opt3)
os.execute[["start C:\Program" "Files\Sandboxie\Start.exe /box:Steam2 D:\Steam\steam.exe -login username password -opt1 -opt2 -opt3"]]
end
I have my usernames and Sandboxes setup so that only the number needs changing (fenriros2, fenriros3, Steam2, Steam3 etc) with the same password.
Basically, I want this;
Steam(3, -tf, -exit, -textmode)
to do;
os.execute[["start C:\Program" "Files\Sandboxie\Start.exe /box:Steam3 D:\Steam\steam.exe -login fenriros3 password -applaunch 440 -textmode"]]
I'll use -exit to close the lua window after it's done.
I realize my code isn't exactly efficient, but that's a worry for a later time. Right now I just need to get it working.
Any help is greatly appreciated, and I apologize if I missed something obvious, I'm still fairly new at Lua.
First obvious one. The [[ ]] delimit a string so all you need to do is to create a variable for the string and replace the contents as needed.
function Steam(n, opt1, opt2, opt3)
-- Set up execute string with placeholders for the parameters.
local strExecute = [["start C:\Program" "Files\Sandboxie\Start.exe /box:Steam{n} D:\Steam\steam.exe -login fenriros{n} password -{opt1} -{opt2} -{opt3}"]]
-- Use gsub to replace the parameters
-- You could just concat the string but I find it easier to work this way.
strExecute = strExecute:gsub('{n}',n)
strExecute = strExecute:gsub('{opt1}',opt1:gsub('%%','%%%%'))
strExecute = strExecute:gsub('{opt2}',opt2:gsub('%%','%%%%'))
strExecute = strExecute:gsub('{opt3}',opt3:gsub('%%','%%%%'))
os.execute(strExecute)
end
Steam(1,'r1','r2','r3')

How can I dry this Rails Controller Action further

Our application uses a number of environments so we can experiment with settings without breaking things. In a typical controller action, I have something like this:
def some_action
...
if #foo.development_mode == 'Production'
#settings = SomeHelper::Production.lan(bar)
elsif #foo.development_mode == 'Beta'
#settings = SomeHelper::Beta.lan(nas)
elsif #foo.development_mode == 'Experimental'
#settings = SomeHelper::Experimental.lan(nas)
end
...
end
Since we have dozens of these, I figured I could try and dry things up with something like this:
#settings = "SomeHelper::#{#foo.development_mode}.lan(bar)"
Which obviously doesn't work - I just get:
"NasHelper::Production.lan(bar)"
How can I reduce this down or do I have to stick with what I've got??
If your concern is that you're ending up with a String rather than the object, you can use String.constantize (Rails only, with standard Ruby you'd have to implement this; it uses Object.const_get(String))
Another option would be .const_get (e.g. Object.const_get(x) where x is your string), you it doesn't, on its own, nest correctly, so you would have to split at "::", etc.
Also, there's the option of using eval to evaluate the String.
But note: eval should be used with great care (it's powerful), or not at all.
Edit:
This means that instead of:
#settings = "SomeHelper::#{#foo.development_mode}.lan(bar)"
You could run:
#settings = "SomeHelper::#{#foo.development_mode}".constantize.lan(bar)
Useful Sources:
http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-constantize
http://www.ruby-forum.com/topic/183112
http://blog.grayproductions.net/articles/eval_isnt_quite_pure_evil
In the first case, #settings receives the result of the method SomeHelper::Production.lan(bar); in the second, #settings just gets a string. You could use the send method of Object linked here to fire the method, or eval, but this wouldn't be my first choice.
It looks like you might be reinventing a wheel -- Rails already has the concept of "environments" pretty well wired into everything -- they are defined in app/config/environments. You set the environment when you launch the server, and can test like Rails.env.production?. To create new environments, just copy the existing environment file of the one closest to the new one, e.g. copy production.rb to beta.rb and edit as necessary, then test Rails.env.beta?, for example.
But this still leaves you testing which one all over the place. You can add to the config hash (e.g. config.some_helper.lan = 'bar'), which value you can assign to #settings directly. You have to make sure there's either a default or it's defined in all environments, but I think this is probably the right approach ... not knowing exactly what you aim to accomplish.