Compiler

Syndicate content 5 

As a developer, you have probably spent at least some time writing repetetive code. You may have even found yourself wishing you could generate the code programmatically. If this sounds familiar, this article is for you!

We'll start with an example. Note: the following examples use the %DynamicObject interface, which requires Caché 2016.2 or later. If you are unfamiliar with this class, check out the documentation here: Using JSON in Caché. It's really cool!

Example

You have a %Persistent class that you use to store data. Now, suppose that you are ingesting some data in JSON format, using the %DynamicObject interface. How do you map the %DynamicObject structure to your class? One solution is to simply write code to copy the values over directly

Last comment 22 November 2016
+ 15   0 7
1078

views

+ 15

rating

Class Queries in InterSystems Caché is a useful tool that separates SQL queries from Caché Object Script code. Basically, it works like this: suppose that you want to use the same SQL query with different arguments in several different places.In this case you can avoid code duplication by declaring the query body as a class query and then calling this query by name. This approach is also convenient for custom queries, in which the task of obtaining the next row is defined by a developer. Sounds interesting? Then read on!

Last comment 9 December 2016
+ 14   0 9
3792

views

+ 14

rating

This is a translation of the following article. Thanks @Evgeny Shvarov  for the help in translation.

Let's assume that you wrote a program that shows "Hello World!", for example:

  write "Hello, World!"

The program works and everyone is happy.

With time, however, your program becomes more complex, gets more features and you eventually need to show the same string in different languages. Moreover you don't know the number and names of these languages.

The spoiler below contains a description of how the task of multi-language localization is solved in Caché.

Last comment 20 May 2017
+ 8   0 1
357

views

+ 8

rating

In this article I would like to tell you about macros in InterSystems Caché. A macro is a symbolic name that is replaced with a set of instructions during compilation. A macro can “unfold” in various instruction sets each time it is called, depending on the parameters passed to it and activated scenarios. This can be both static code and the result of ObjectScript execution. Let's take a look at how you can use them in your application.

Last comment 24 July 2017
+ 8   0 5
1055

views

+ 8

rating

In my previous article, we reviewed possible use-cases for macros, so let’s now proceed to a more comprehensive example of macros usability. In this article we will design and build a logging system.

Logging system

Logging system is a useful tool for monitoring the work of an application that saves a lot of time during debugging and monitoring. Our system would consist of two parts:

  • Storage class (for log records)
  • Set of macros that automatically add a new record to the log

Last comment 24 March 2017
+ 8   3 9
939

views

+ 8

rating

Greetings! This article describes yet another simple way of creating installers for the solutions based on InterSystems Caché. The topic covers applications, which can be installed or completely removed from Caché with one action only. If you are still documenting installation instructions that have more than one step to do to install your application — it’s high time you automated this process. 

Last comment 10 May 2016
+ 5   0 6
1012

views

+ 5

rating

In most cases, if one class depends on another, the class compiler will detect this and determine the correct compilation order. For example:

  • Compiling a superclass triggers compilation of its subclasses.
  • Compiling a custom datatype class triggers compilation of any classes with a property of that type.

Yet sometimes, a developer needs to specify compilation order. InterSystems class definitions contain two keywords for this, DependsOn and CompileAfter, that are very similar but not identical. They are useful during development, and also when importing and compiling a set of classes for the first time.

The documentation for these keywords is here

Last comment 31 January 2019
+ 4   0 3
414

views

+ 4

rating

Here you have an easy way to write and execute COS code from your unix scripts. This way one does not need to write routines or even open Studio or Atelier. It can be an option for simple and small actions for instance things like installation tasks or compiling.

See sample bash script (compile.sh) to compile classes:

#!/bin/bash
#####################################################################
# Script to compile classes
# $1 = NAMESPACE
# $2 = ClassName
#
# For example:
# ./compile.sh "SAMPLES" "Sample.Person" 
#####################################################################
echo -n "username:"
read username
echo -n "password:"
read pwd

csession HSINSTANCE -U "$1" <<EOFF
$username
$pwd
set status = \$SYSTEM.OBJ.Compile("$2")
h
EOFF

This is the output from the console

Last comment 14 October 2016
+ 4   0 3
472

views

+ 4

rating

The attached file contains an example of code generation using ObjectGenerators which builds a very simple homemade RuleEngine. 

Code generation is an excellent way of increasing performance moving run-time calculations to compile-time.

We could generate code creating routines or implemeting methods using ObjectGenerators. In this example we are using ObjectGenerators.

Update: Rule Engine is now on GitHub https://github.com/intersystems-ib/cache-iat-ruleengine

A very simple code generated method

For starters, let's begin with a very basic code generated method

+ 4   0 1
0

comments

358

views

+ 4

rating

On the back of my recent post on writing bug-less code I wanted to raise a few suggestions (to ISC) that would help prevent certain types of bugs at compile time. I've probably missed a few, but these are the main ones in my mind. Please contribute more suggestions.

Btw, these also serve as potential gotchas for new COS developers.

I except that introducing these types of changes can cause legacy code problems. Particularly where developers do some interesting overloading of method arguments. Therefore I would see these as an option that would need to be enabled and that it won't prevent compilation (it would warn not error).

As an alternative I could see these being implemented as a Lint tool. This is something I (and perhaps the community) would be willing to do if we had an open source AST tree as a starting block (if anyone is aware of one in existence for COS)

Last comment 18 April 2017
+ 4   0 1
689

views

+ 4

rating

So another topic that has been of interest to a number of people since the beta was put up last week is in regard to Studio Hooks and Atelier. This requires a bit of background and then some discussion of how the architecture of Atelier necessitates some changes and then what our current thinking on the subject is.

Studio hooks were added to Cache over 10 years ago. They have two primary usages. The first is that on an appropriate action such as creating a new document, or compiling a document a callback is executed whose job is to interact with external source control systems. This is done by writing COS scripts that “do the right thing” for a given source control system based on the callback invoked. All of this code ran on the server and used all the facilities available on the server such as locks and transactions etc

Last comment 3 March 2016
+ 3   0 6
958

views

+ 3

rating

Hi all!

I'm developing a mini-framework to implement annotations in Cache. I want to support two kind of annotations: metadata and method decorators. I've got stuck trying to implement the second one.

Metadata

With metadata annotations I can add metadata to any kind of target. A target can be a method/classmethod, parameter, property and class.

For example

Last answer 25 January 2017 Last comment 26 January 2017
+ 3   0 2
443

views

+ 3

rating

Due to a new project I'm getting involved inside %Installer stuff deeper than ever I'd be happy to. Most of it was pretty easy to get, thanks to documentation, articles and SAMPLES example, while I can't dig good solution for one problem. I'm trying to import and compile some classes, willing to ignore two types of errors: 5202 (NothingToCompile) and 5373 (PredecessorClassDoesNotExist - a normal case when SNMP sampling class is compiled for the 1st time, before it was registered). 

I've tried the following:

Last answer 28 March 2017 Last comment 28 March 2017
+ 3   0 1
207

views

+ 3

rating

When I try to compile through Atelier (Compile Project), I noticed that in compilation with error it returns this message 'Failed to read /api/atelier/v1/ ...':

No additional Atelier files to synchronize.
Compile failed: Failed to read /api/atelier/v1/ERP/action/compile?flags=ck
Compile failed: Failed to read /api/atelier/v1/ERP/action/compile?flags=ck
Compile failed: Failed to read /api/atelier/v1/ERP/action/compile?flags=ck
Compile failed: Failed to read /api/atelier/v1/ERP/action/compile?flags=ck

Does anyone know what can it be?

I'm using Atelier beta (latest), with web application configured as Password Authenticated and a user with %ALL.

Last comment 24 July 2018
+ 1   0 4
0

answers

230

views

+ 1

rating

Using our cool new debugging extension for Visual Studio Code I'm trying to debug a CreateProjection method of a class, but when I compile it the work apparently gets done in one of the worker jobs, so my breakpoint never triggers.

Is there a compiler flag or qualifier to force the compilation to be done in-process rather than getting handed off to a worker job?

Last answer 2 October 2018 Last comment 4 October 2018
+ 1   0 3
196

views

+ 1

rating

The class %Compiler.UDL.TextServices arrived in 2015.1, bringing us methods for exporting a class in UDL format (i.e. looking just like we're used to seeing it in Studio), and importing a UDL format definition back into a namespace. Some source control tools including our Deltanji are now able to use UDL format, resulting in diffs that are easier to understand.

Last comment 1 February 2017
+ 1   0 1
143

views

+ 1

rating

I have just imported our Ensemble project (not a really big one, like two man-years) into my MacBook Air  Ensemble 2016.3 instance to test many things (document model, etc.), Atelier among the others.

The "Production view" is not here, and I miss it.

The opening of Diagram Editor for BP is pretty hidden. I would even like having a switch, which would default to the Diagram Editor. 

Anytime I click on the Open Diagram Editor, it waits for a while, ten produces an error and only then I can see the diagram.

The error says: Your Studio browser is no longer valid... etc.

I cannot find an easy access to the Save or Compile command, once I do some changes in the respective BP via the Diagram Editor.

Compiling larger amount of classes produces an error: 

No conflicting files to resolve.

Compile failed: Gateway Timeout

Compiling identical set of classes "per partes" goes well, however

Last answer 6 December 2018
+ 1   0 3
0

comments

289

views

+ 1

rating

I'm looking at adding multilingual support to a couple of open source projects I'm working on. The solutions are already developed in CSP so I am not looking for alternative approaches.

I'm wondering what would be the best approach for CSP and separate JavaScript files.

Initially I was wondering if I should bake the default system language text at compile time, or provide the end user with a language selection option at run time.

I came across $$$TEXT reading the docs...

https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY...

and have also discovered the localization manager which looks to be really handy...

https://community.intersystems.com/post/cach%C3%A9-localization-manager-...

Last answer 11 May 2017 Last comment 12 September 2017
+ 1   0 1
263

views

+ 1

rating

I have the following problem:

  • There are several classes with method generators, which needs to be compiled during every compilation
  • I have "ckub" qualifiers enabled in studio
  • I don't want to remove "u" qualifier because I need it very much in another namespaces

So how do I force classes with method generators to recompile when "u" qualifier is present?

Last comment 15 July 2016
0   0 2
0

answers

516

views

0

rating

During programmatic classes compilation performed by the code like this:

set status=$system.OBJ.Load(updaterFile,"cruk-d",,.resList)

strange messages appear in console log, e.g.

05/06/18-15:54:12:424 (308512) 0 oops: was expecting crc 0x21e96e85,5a4c8b2c for rtn (Update.Import.1) rbufnum-15523
...
05/06/18-16:17:45:305 (4224) 0 oops: was expecting crc 0x21e96e85,5a4c8b2c for rtn (Update.Import.1) rbufnum-15523
05/06/18-16:22:30:485 (4224) 0 oops: was expecting crc 0x21e96e85,5a4c8b2c for rtn (Update.Import.1) rbufnum-15523...(repeated 3 times)

Classes that were concerned stayed not compiled (with old object code untouched) till they were recompiled manually. To be fair, it happened only ones during several years

Last answer 7 May 2018 Last comment 8 May 2018
0   0 3
185

views

0

rating

I'm new to Cache and trying to follow the Using Cache Studio PDF. In section 2.4.3(Adding a Zen form) it tells you insert the following code;

<button caption="Save" o select="zenPage.saveRecord();" />

Unfortunately that code fails compilation. If you try and use the tools there is no o select option nor a select or onselect option. I was wondering if anyone has used this PDF before and knows what the correct attribute should be?

 

Last answer 15 July 2016 Last comment 14 July 2016
0   0 1
98

views

0

rating

Hi everyone

I'm coding a script in order to validade an installation package following the steps:

- Create environment { }
Try{
    - Import classes
    // Do some stuffs
    - Compile Classes
}Catch (ex){
    // oops ...
}
- Destroy environment { }

In some specific cases I will need to terminate the current process and start a new one with some new parameters.
With any code inside my try catch block, whenever I terminate the process I receive a RESJOB and it works perfectly.
When my job is running a compilation ($SYSTEM.OBJ.Compile(.classes)) and receive a RESJOB, it does not catch the exception. It just terminate, so the environment is not destroyed.

Any ideas on how to handle this situation

Last answer 28 May 2018 Last comment 26 May 2018
0   0 3
183

views

0

rating

Hi everyone

Is there any way to change a class definition (especifically a query definition during the compilation time)?
The idea is: 
    I have an abstract class with a parameter where I will define the ROWSPEC of a query and some methods to populate e temporary table
The implementation class will override the parameter, specifying the ROWSPEC of this implementation, and the methods will populate the rows in the same format as the ROWSPEC.

I want to change the ROWSPEC of the inherit query according to the implementation of the overriden parameter.
What I've already tried to change the parameter definition (of the query):
- Create a macro, thinking about the possibility of running it before the compilation
- Override the ClassMethod CreateProjection from the class %Projection.AbstractProjection
- Run a method in a objectgenerato

Last answer 14 July 2017 Last comment 17 July 2017
0   0 2
255

views

0

rating

Hi community!
Is there any way to create new method keywords?
For example:

ClassMethod TestOne() [ MyKeyword = MyValue ]
{
    // Implementation
}

Another doubt (maybe another topic)
How can I generate new methods at compilation time for the same class?
I tried method generators, but It only gets compiled when I compile for the second time.
I also read this comment but I could not find out how to generate the methods and get them compiled in the first compilation.

For example:

Class util.Test
{

ClassMethod TestOne()
{
    Write 1
}

ClassMethod TestTwo()
{
    Write 1
}

}

Expected

Last answer 12 June 2018 Last comment 14 June 2018
0   0 6
279

views

0

rating

Hi all,

 

I was wondering if there is any way to trigger the compilation of a class based on the compilation of another (unrelated) class. For example:

Class 1 = test.class

Class 2 = test.class.type.one

Class 3 = test.class.type.two

Every time class 2 or class 3 is compiled in Studio I would like to have class 1 automatically compiled as well.

 

The reason I need this is because class 1 has a method with CodeMode set to objectgenerator which generates some code based on what is found in classes 2 and 3.

Last answer 18 July 2017 Last comment 19 July 2017
0   0 3
252

views

0

rating

I am using &html<> to output some CSS to my page (which is a class-based and not a tag-based page), and the selectors contain the '>' character which the compiler chokes on.  How can I escape the '>' so it still displays as such in the web page source but doesn't trip up the compiler?

This is for 2012.2 btw.

Last answer 20 November 2015 Last comment 20 November 2015
0   0 2
263

views

0

rating

I have a class and I would like to dynamically change its selectivity attribute in run-time and then call $system.OBJ.Compile to

recompile the class. I have changed the selectivity to a global, but it seems like it does not work. Any suggestions? Thanks.

 

<Property name="field1">
<AverageFieldSize>8.91</AverageFieldSize>
<Selectivity>^selectivity</Selectivity>
</Property>
 

Last answer 18 July 2017 Last comment 18 July 2017
0   0 2
165

views

0

rating