I have a requirement to perform different operations if string contains any special character.
Is there any way to implement regular expression in gremlin.
Input_Name= Test#input
if Input_Name.contains( "#/$%...")
{
println " error "
}
else
{
println "sucess"
}
Currently the Gremlin language does not have a TextP.regex predicate. Some implementations, such as JanusGraph, do add custom regex extensions to Gremlin. You could also, if the database you are using allows it, use Groovy closure syntax to include a regex in a query. Within the TinkerPop community we are planning to add a TextP.regex to the Gremlin language. The code is written and on a branch that we hope will be part of the TinkerPop 3.6.0 release if all goes well.
However, in your case, perhaps the existing TextP.containing could be used if there are a finite set of special characters you are looking for but it is likely not the most optimal way to solve the problem as you will have to or several has steps together.
Another option might be to use an external index if your database implementation supports that.
Just as an example of the closure syntax, if your implementation allows it, a REGEX match would look like the example below. In general though, use of closures is not recommended, and many implementations either fully block or extremely limit their use.
gremlin> g.V().limit(20).filter {it.get().values('desc').next() ==~ "[A-Za-z]* [A-Z]'(.*)"}.values('desc')
==>Chicago O'Hare International Airport
Related
The line: g.V('1').out('knows').hasId('2').hasNext()
This exact line works in the Gremlin console.
I have not read in the documentation that hasNext does not exist in Gremlin.NET. Am I missing something, or is there simply another way to do this in Gremlin.NET?
This method is really missing in Gremlin.Net right now. While this is not explicitly stated in the documentation, the documentation does list all terminal steps implemented by Gremlin.Net:
ITraversal.Next()
ITraversal.NextTraverser()
ITraversal.ToList()
ITraversal.ToSet()
ITraversal.Iterate()
hasNext is also such a terminal step but as you can see it is missing in this list.
The only workaround I can think of for situations like this is to use the count step and then check in your application whether the returned count is greater than zero:
var count = g.V("1").Out("knows").HasId("2").Count().Next();
var exists = count > 0;
In some cases it could also make sense to limit the number of vertices going into the Count step as you aren't interested in the exact count but only want to know whether at least one vertex exists:
g.V("1").Out("knows").HasId("2").Limit<Vertex>(1).Count().Next();
This is also the proposed workaround in the ticket for this feature: TINKERPOP-1921.
It does not exist yet:
https://issues.apache.org/jira/browse/TINKERPOP-1921
The main reason is related to the fact that hasNext() is a Java Iterator semantic that was not applied to .NET. Gremlin Language Variants (GLVs) like .NET are given some latitude in terms of how they interpret the language so as to provide the most at-home feel for developers using it. In other words, if you are using the .NET GLV you shouldn't feel as though you are coding in Java, but should instead feel write at home with the standard .NET semantics.
That said, it could be argued, as it is in the issue I've referenced above, that something like hasNext() is a common form of Gremlin as a query language and should thus be available in all GLVs. So, we will consider those options as we come across them.
For .NET I guess you would try to check Current as discussed here.
I have noticed double underscore being used in some step functions in Tinkerpop Gremlin 3.3. Could someone please tell why we use this double underscore with an example ? I could not find enough information about this in the documentation.
__. allows you to define an anonymous Traversal, ie. a Traversal which is not bound to a specific TraversalSource.
In the Gremlin console, all Gremlin steps are statically imported so you never need to prefix anonymous traversals with __. unless that anonymous traversal starts with a reserved keyword in the target language. In Groovy, which is the default Gremlin flavor, this is the case with in() and as() steps: because these are reserved keywords, these two steps must be prefixed with __.
In Java, you can avoid __. prefix by statically importing all steps in your program:
import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__
See the small Note section in the documentation: http://tinkerpop.apache.org/docs/3.3.0/reference/#graph-traversal-steps
__ is used to refer to the incoming traversal (vertex or edge) in Java API. For example: in gremlin shell, one can write something like this
graph.traversal().V().repeat(out("edgeType")).until(hasLabel("label")).toList()
But in java one needs an anonymous traversal to call the functions "out" and "hasLabel" within repeat and until (or any other function like by, choose etc.). The above traversal in Java will look like:
graph.traversal().V().repeat(__.out("edgeType")).until(__.hasLabel("label")).toList()
I'm trying to add a symbol token using ParseKit below:
[t.symbolState add:#"<p style=\"margin-left: 20px;\">"];
I'm wondering if ParseKit allows for wildcards when adding a symbol, such as:
[t.symbolState add:#"<p style=\"margin-left: ##px;\">"];
I want to be able to then extract the wildcard from the token during the parsing procedure.
Is such a thing possible with ParseKit?
Developer of ParseKit here.
I think using ParseKit in this way is not a good idea.
ParseKit (and its successor PEGKit) excel at tokenizing input and then parsing at the token level.
There are several natural tokens in the example input you've provided, but what you are trying to do here is ignore those natural tokens, combine them into a blob of input, and then do fancy sub-token matching using patterns.
There is a popular, powerful tool for fancy sub-token matching using patterns: Regular Expressions. They will be a much better solution for that kind of thing than PEGKit.
However, I still don't think Regular Expressions are the tool you want to use here (or, at least not the only tool).
It looks like you want to parse XML input. Don't use Regex or PEGKit for that. Use an XML parser. Always use an XML parser for parsing XML input.
You may choose to use another XML API layered on top of the XML Parser (SAX, StAX, DOM, XSLT, XQuery, etc.) but, underneath it all, you should be parsing with an XML parser (and, of course, all of those tools I listed, do).
See here for more info.
Then, once you have the style attribute string value you are looking for, use Regex to do fancy pattern matching.
I'm writing an API that converts actions performed by a non-technical user into Salesforce.com SOQL 'SELECT', 'UPSERT', and 'DELETE' statements. Is there any resource, library, etc. out there that could validate the syntax of the generated SOQL? I'm the only one at my company with any experience with SOQL, so I'd love to place it into a set of automated tests so that other developers enhancing (or fixing) the SOQL generation algorithm know if it's still functioning properly.
I know one solution here is to just make these integration tests. However, I'd rather avoid that for three reasons:
I'd need to maintain another Salesforce.com account just for tests so we don't go over our API request cap.
We'll end up chasing false positives whenever there are connectivity issues with Salesforce.com.
Those other developers without experience will potentially need to figure out how to clean up the test Salesforce.com instance after DML operation test failures (which really means I'll need to clean up the instance whenever this occurs).
You might solve your problem by using the SoqlBuilder library. It generates SOQL for you and is capable of producing SOQL statements that would be quite error prone to create manually. The syntax is straight forward and I've used it extensively with very few issues.
I found another way to do this.
Salesforce.com posted their SOQL notation in Backus-Noir Form (BNF) here:
http://www.salesforce.com/us/developer/docs/api90/Content/sforce_api_calls_soql_bnf_notation.htm
This means you can use a BNF-aware language recognition tool to parse the SOQL. One of the most common tools, ANTLR, does this and is free. Following the ANTLR example, pass the SOQL grammar into its grammar compiler to get a Lexer and a Parser in your desired language (C#, Java, Python, etc.). Then you can pass the actual SOQL statements you want to validate into the Lexer, and then your Lexer tokens into your Parser, to break apart the SOQL statements. If your Lexer or Parser fails, you have invalid SOQL.
I can't think of a way to do this from outside of Salesforce (and even in Apex I've only got one idea right now that may not work), but I can think of two suggestions that may be of help:
Validate queries by running them, but do them in batches using a custom web service. i.e. write a web service in Apex that can accept up to 100 query strings at once, have it run them and return the results. This would drastically reduce the number of API calls but of course it won't work if you're expecting a trial-and-error type setup in the UI.
Use the metadata API to pull down information on all objects and their fields, and use those to validate that at least the fields in the query are correct. Validating other query syntax should be relatively straight forward, though conditionals may get a little tricky.
You can make use of the salesforce develop nuget packages that leverages SOAP API
I'm trying to build a search that is similar to that on Google (with regards to exact match encapsulated in double quotes).
Let's use the following phrase for an example
"phrase search" single terms [different phrase]
Currently if I use the following code
Dim searchTermsArray As String() = searchTerms.Split(New String() {" ", ",", ";"}, StringSplitOptions.RemoveEmptyEntries)
For Each entry In searchTermsArray
Response.Write(entry & "<br>")
Next
my output is
"phrase
search"
single
terms
[different
phrase]
but what I really need is to build a key value pair
phrase search | table1
single | table1
terms | table1
different phrase | table2
where table1 is a table with general info, and table2 is a table of "tags" similar to that on stackoverflow.
Can anybody point me in the right direction on how to properly capture the input?
What are you trying to do is not that trivial. Implementing a search "similar to Google's" is far beyond parsing the search string.
I'd suggest you not to reinvent the wheel and instead use production ready solutions such as Apache Lucene.NET or Apache Solr. Those cope with both parsing and fulltext search.
But if you only need to parse this kind of strings then you should really consider solution Pete pointed to.
Regex is your friend. See this question
Depending on how fancy you plan in getting, you might consider the search grammar/implementation that's included with Irony.
http://irony.codeplex.com/
Search string parsing is a non-regular problem. That means that while a regular expression can get deceptively close, it won't take you all the way there without using proprietary extensions, building an unmaintainable mess of an expression, leaving nasty edge cases open that don't work how you'd like, or some combination of the three.
Instead, there are three correct ways to handle this:
Use a third-party solution like Lucene.
Build a grammar via something like antlr.
Build your own state machine.
For a problem of this level (and assuming that search is core enough to what you're doing to really want to implement it yourself), I'd probably go with option 3. This makes more sense when you realize that regular expressions are themselves instructions for how to set up state machines. All you're doing is building that right into your code. This should give you the ability to tune performance and features as well, without requiring adding a larger lexer component into your code.
For an example of how you might do this take a look at my answer to this question:
Reading CSV files in C#
hat I would do is build a state machine to parse the string character by character. This will be the easiest way to implement a fully-correct solution, and should also result in the fastest code.