Neo4j Spatial: can't run spatial - groovy

I have been trying to work with Neo4j Spatial for my project, but I can't make it work.
With limited documentation and examples I figured out how to load OSM map to the database. But to check if it is loaded, I am trying to execute a spatial query.
While trying to run my code I get this error:
import.java:69: error: cannot access GremlinGroovyPipeline
.startIntersectSearch(layer, bbox)
^
class file for com.tinkerpop.gremlin.groovy.GremlinGroovyPipeline not found
I understand what's wrong (it can't find the required library), but I don't know how to fix it. The reason is when I run Neo4j Spatial tests, LayerTest.java and TestSpatial.java do include GeoPipeline library and it works perfectly fine. However, when I created my simple java file to test Neo4j, and trying to execute commands that depend GeoPipeline library I get the error above.
I read the instructions on github for Neo4j and saw this note:
Note: neo4j-spatial has a mandatory dependency on
GremlinGroovyPipeline from the com.tinkerpop.gremlin.groovy package.
The dependency in neo4j is type 'provided', so when using
neo4j-spatial in your own Java project, make sure to add the following
dependency to your pom.xml, too.
However, I am not using Maven to build my app. It is a simple java file, that I want to run to test if I get how everything works.
here is the code from my java file:
package org.neo4j.gis.spatial;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.geotools.data.DataStore;
import org.geotools.data.neo4j.Neo4jSpatialDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.neo4j.gis.spatial.osm.OSMDataset;
import org.neo4j.gis.spatial.osm.OSMDataset.Way;
import org.neo4j.gis.spatial.osm.OSMGeometryEncoder;
import org.neo4j.gis.spatial.osm.OSMImporter;
import org.neo4j.gis.spatial.osm.OSMLayer;
import org.neo4j.gis.spatial.osm.OSMRelation;
import org.neo4j.gis.spatial.pipes.osm.OSMGeoPipeline;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;
import org.neo4j.kernel.impl.batchinsert.BatchInserter;
import org.neo4j.kernel.impl.batchinsert.BatchInserterImpl;
import org.neo4j.kernel.EmbeddedGraphDatabase;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.gis.spatial.pipes.GeoPipeline;
class SpatialOsmImport {
public static void main(String[] args)
{
OSMImporter importer = new OSMImporter("ott.osm");
Map<String, String> config = new HashMap<String, String>();
config.put("neostore.nodestore.db.mapped_memory", "90M" );
config.put("dump_configuration", "true");
config.put("use_memory_mapped_buffers", "true");
BatchInserter batchInserter = new BatchInserterImpl("target/dependency", config);
importer.setCharset(Charset.forName("UTF-8"));
try{
importer.importFile(batchInserter, "ott.osm", false);
batchInserter.shutdown();
GraphDatabaseService db = new EmbeddedGraphDatabase("target/dependency");
importer.reIndex(db, 10000);
db.shutdown();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
GraphDatabaseService database = new EmbeddedGraphDatabase("target/dependency");
try{
SpatialDatabaseService spatialService = new SpatialDatabaseService(database);
Layer layer = spatialService.getLayer("layer_roads");
LayerIndexReader spatialIndex = layer.getIndex();
System.out.println("Have " + spatialIndex.count() + " geometries in " + spatialIndex.getBoundingBox());
Envelope bbox = new Envelope(-75.80, 45.19, -75.7, 45.23);
// Search searchQuery = new SearchIntersectWindow(bbox);
// spatialIndex.executeSearch(searchQuery);
// List<SpatialDatabaseRecord> results = searchQuery.getResults();
List<SpatialDatabaseRecord> results = GeoPipeline
.startIntersectSearch(layer, bbox)
.toSpatialDatabaseRecordList();
doGeometryTestsOnResults(bbox, results);
} finally {
database.shutdown();
}
}
private static void doGeometryTestsOnResults(Envelope bbox, List<SpatialDatabaseRecord> results) {
System.out.println("Found " + results.size() + " geometries in " + bbox);
Geometry geometry = results.get(0).getGeometry();
System.out.println("First geometry is " + geometry);
geometry.buffer(2);
}
}
It is very simple right now, but I can't make it work. How do I include com.tinkerpop.gremlin.groovy.GremlinGroovyPipeline in my app, so it works?
I run everything on Ubuntu 12.04 and java version "1.7.0_25", Java(TM) SE Runtime Environment (build 1.7.0_25-b15).
Any help is greatly appreciated.

the best way to get all the required dependencies in a place where you can include them in your classpath is to run
mvn dependency:copy-dependencies
in neo4j-spatial, and find the libs to include in target/deps, see http://maven.apache.org/plugins/maven-dependency-plugin/usage.html

Related

Missing #Injectable annotation when importing json value

I'm currently trying to inject a json file into a service that I am building. This class is injecting the json via a #inject() tag in the constructor.
Below you can find the code that I'm using.
DiContainer.ts
import { Container, decorate, injectable } from "inversify";
import { IResponseBuilder, InjectionTypes, IUserIdGenerator, ILogger, IResponseRepository, IResponseService, IContextService, IFileWriter, IBoardSessionService, IIntent } from "./src/common/types";
import GoogleResponseBuilder from "./src/common/builders/googleResponseBuilder";
import UserIdGenerator from "./src/common/helpers/userIdGenerator";
import { DialogflowConversation } from "actions-on-google";
import WinstonLogger from "./src/common/winstonLogger";
import FileWriter from "./src/common/helpers/fileWriter";
import TextResponseRepository from "./src/repositories/textResponseRepository";
import SsmlResponseRepository from "./src/repositories/ssmlResponseRepository";
import ResponseSerivce from "./src/services/responseService";
import ContextService from "./src/services/contextService";
import { BoardService } from "./src/services/boardService";
import { BoardSessionService } from "./src/services/boardSessionService";
import WelcomeIntent from "./src/intents/new/welcomeIntent";
import uuid from "uuid/v4";
const sessionJson = require("./src/data/boardSessions.json");
const DIContainer = new Container();
DIContainer.bind<IResponseBuilder<DialogflowConversation>>(InjectionTypes.GoogleResponseBuilder).to(GoogleResponseBuilder);
DIContainer.bind<ILogger>(InjectionTypes.WinstonLogger).to(WinstonLogger);
DIContainer.bind<IFileWriter>(InjectionTypes.FileWriter).to(FileWriter);
DIContainer.bind<IUserIdGenerator>(InjectionTypes.UserIdGenerator).to(UserIdGenerator);
DIContainer.bind<IIntent>(InjectionTypes.WelcomeIntent).to(WelcomeIntent);
DIContainer.bind<IResponseRepository>(InjectionTypes.TextResponseRepository).to(TextResponseRepository);
DIContainer.bind<IResponseRepository>(InjectionTypes.SsmlResponseRepository).to(SsmlResponseRepository);
DIContainer.bind<IResponseService>(InjectionTypes.ResponseService).to(ResponseSerivce);
DIContainer.bind<IBoardSessionService>(InjectionTypes.BoardSessionService).to(BoardSessionService);
DIContainer.bind<IContextService>(InjectionTypes.ContextService).to(ContextService);
DIContainer.bind(InjectionTypes.SessionJSON).to(sessionJson);
DIContainer.bind(InjectionTypes.UUIDv4).toFunction(uuid);
export default DIContainer;
Every dependency you try to inject via #inject has to be marked with #injectable and registered through bind.
I don't really know how the decorate function works (so I don't know if you can use it to mark a function, and not a class, as injectable).
By the way, I think you can achieve what you want just registering your dependency as a dynamic value and returning the desired function, as stated here. In your case, something like this:
DIContainer.bind(InjectionTypes.UUIDv4).toDynamicValue((context: interfaces.Context) => { return uuid });
Alternatively, you could just directly import the function in your service without injecting it, or wrap the function in another service you can mark as injectable (let say, an uuid provider service).

Spring state machine UML stays in memory

I've been working with Spring State machines for over a year now trying different ways to implement according to my requirements, and I've come across a serious issue when I use UML.
I use papyrus to draw the UML and I have many UML stored in a certain location. The one I need to use is selected dynamically. That has been done successfully. Now I have come across a serious problem. Below is the code on how I have called the UML.
Resource resource = new FileSystemResource(stmDir+"/"+model+".uml");
UmlStateMachineModelFactory umlBuilder = new UmlStateMachineModelFactory(resource);
umlBuilder.setStateMachineComponentResolver(resolveActionConfig(model));
StateMachineModelFactory<String, String> modelFactory = umlBuilder;
Builder<String, String> builder = StateMachineBuilder.builder();
builder.configureModel().withModel().factory(modelFactory);
builder.configureConfiguration().withConfiguration().beanFactory(new StaticListableBeanFactory());
stateMachine = builder.build();
And as you can see I use new UmlStateMachineModelFactory(resource);
UmlStateMachineModelFactory Class has the following code
#Override
public StateMachineModel<String, String> build() {
Model model = null;
try {
model = UmlUtils.getModel(getResourceUri(resolveResource()).getPath());
} catch (IOException e) {
throw new IllegalArgumentException("Cannot build build model from resource " + resource + " or location " + location, e);
}
UmlModelParser parser = new UmlModelParser(model, this);
DataHolder dataHolder = parser.parseModel();
// we don't set configurationData here, so assume null
return new DefaultStateMachineModel<String, String>(null, dataHolder.getStatesData(), dataHolder.getTransitionsData());
}
and everytime I create one UmlStateMachineModelFactory, it in turn creates one UmlModelParser.
This class has
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.uml2.uml.Activity;
import org.eclipse.uml2.uml.Constraint;
import org.eclipse.uml2.uml.Event;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.OpaqueBehavior;
import org.eclipse.uml2.uml.OpaqueExpression;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.Pseudostate;
import org.eclipse.uml2.uml.PseudostateKind;
import org.eclipse.uml2.uml.Region;
import org.eclipse.uml2.uml.Signal;
import org.eclipse.uml2.uml.SignalEvent;
import org.eclipse.uml2.uml.State;
import org.eclipse.uml2.uml.StateMachine;
import org.eclipse.uml2.uml.TimeEvent;
import org.eclipse.uml2.uml.Transition;
import org.eclipse.uml2.uml.Trigger;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.Vertex;
These remain in my memory causing it to use up a large amount of memory and doesn't get collected by the garbage collector. This is causing a lot of trouble as we are using this for a large scale application and many instances are created every few minutes.
Please suggest a workaround.
EDIT- I managed to create a singleton wrapper for this problem but regardless of that, it persists. My colleague had found out that the loaded resources do not unload. so everytime I call builder.build(),
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
resourceSet.createResource(modelUri);
Resource resource = resourceSet.getResource(modelUri, true);
this is called. I wonder is this is causing the heap build-up. Please help
I pushed some fixes per gh572 to master and 1.2.x. Hopefully those work for you. At least I was able to see garbage collection to work better. I'm planning to create releases later this week.

Cassandra 3.10 Create Trigger, class doesn't exist

I have DSE 5.1 with Cassandra 3.10 and cql 5.0.1 installed on CentOS 7.3.1611 (Core).
I'm following the tutorial from DSE Triggers:
http://docs.datastax.com/en/dse/5.1/cql/cql/cql_reference/cql_commands/cqlCreateTrigger.html?hl=trigger
Which send me to github:
https://github.com/apache/cassandra/tree/trunk/examples/triggers
According to the tutorial, I am compiling the jar with ANT 1.10.1. The compiled jar contain the code:
package org.apache.cassandra.triggers;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;
import org.apache.cassandra.schema.TableMetadata;
import org.apache.cassandra.schema.Schema;
import org.apache.cassandra.db.Mutation;
import org.apache.cassandra.db.partitions.Partition;
import org.apache.cassandra.db.partitions.PartitionUpdate;
import org.apache.cassandra.io.util.FileUtils;
import org.apache.cassandra.utils.FBUtilities;
import org.apache.cassandra.utils.UUIDGen;
public class AuditTrigger implements ITrigger
{
private Properties properties = loadProperties();
public Collection<Mutation> augment(Partition update)
{
String auditKeyspace = properties.getProperty("keyspace");
String auditTable = properties.getProperty("table");
TableMetadata metadata = Schema.instance.getTableMetadata(auditKeyspace, auditTable);
PartitionUpdate.SimpleBuilder audit = PartitionUpdate.simpleBuilder(metadata, UUIDGen.getTimeUUID());
audit.row()
.add("keyspace_name", update.metadata().keyspace)
.add("table_name", update.metadata().table)
.add("primary_key", update.metadata().partitionKeyType.getString(update.partitionKey().getKey()));
return Collections.singletonList(audit.buildAsMutation());
}
private static Properties loadProperties()
{
Properties properties = new Properties();
InputStream stream = AuditTrigger.class.getClassLoader().getResourceAsStream("AuditTrigger.properties");
try
{
properties.load(stream);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
finally
{
FileUtils.closeQuietly(stream);
}
return properties;
}
}
The compiled jar, I am copying to the cassandra triggers location (/etc/cassandra/triggers). Then I run from cql the command:
CREATE TRIGGER test1 ON test.test USING 'org.apache.cassandra.triggers.AuditTrigger';
And send me the error:
ConfigurationException: Trigger class 'org.apache.cassandra.triggers.AuditTrigger' doesn't exist
I copied the jar in all the nodes of my cluster, restarted the cluster and ran the command:
nodetool reloadtriggers
With no success, I also put in the jvm.options the command:
-Dcassandra.triggers_dir=/etc/dse/cassandra/triggers
And in the cassandra-env.sh, I added the command:
JVM_OPTS="$JVM_OPTS -Dcassandra.triggers_dir=/etc/dse/cassandra/triggers"
And I am getting the same error. I tryied adding the jar in the cassandra lib folder (/usr/share/dse/cassandra/lib/) and it seems to not been loading the compiled jar.
When I run the command in cql:
CREATE TRIGGER test1 ON test.test USING 'org.apache.cassandra.triggers.AuditTrigger';
I am still getting the same error
ConfigurationException: Trigger class 'org.apache.cassandra.triggers.AuditTrigger' doesn't exist
So, my question is, How can I make cassandra load my jar correctly and use it to create a trigger in cql?

Custom update listener to set subtask's fix-version

I'm developing custom listener which will update subtask's fix version to same value as it's parent issue.
Currently we are using post-function in workflow in order to set subtask's fix version according to parent on subtask creation. This however doesn't cover cases when subtask already exists and parent's fix version gets updated. New value from parent task is not propagated to subtask.
I'm using script runner and I'm creating 'Custom lisatener', for my specific project and specified Event: 'Issue Updated'. I added script as following:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.version.Version
import org.apache.log4j.Logger
class CopyFixVersionFromParentToChild extends AbstractIssueEventListener {
Logger log = Logger.getLogger(CopyFixVersionFromParentToChild.class);
SubTaskManager subTaskManager = ComponentAccessor.getComponent(SubTaskManager.class)
IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)
#Override
void issueUpdated(IssueEvent event) {
log.warn("\nIssue updated!!!\n")
try {
Issue updatedIssue = event.getIssue()
if (updatedIssue.issueTypeObject.name == "Parent issue type") {
Collection<Version> fixVersions = new ArrayList<Version>()
fixVersions = updatedIssue.getFixVersions()
Collection<Issue> subTasks = updatedIssue.getSubTaskObjects()
if (subTaskManager.subTasksEnabled && !subTasks.empty) {
subTasks.each {
if (it instanceof MutableIssue) {
((MutableIssue) it).setFixVersions(fixVersions)
issueManager.updateIssue(event.getUser(), it, EventDispatchOption.ISSUE_UPDATED, false)
}
}
}
}
} catch (ex) {
log.debug "Event: ${event.getEventTypeId()} fired for ${event.issue} and caught by script 'CopyVersionFromParentToChild'"
log.debug(ex.getMessage())
}
}
}
Problem is, that it doesn't work. I'm not sure whethe rit's problem that my script logic is encapsulated inside class. Do I have to register this in some specific way? Or am I using script runner completely wrong and I'm pasting this script to wrong section? I checked code against JIRA API and it looks like it should work, my IDE doesnt show any warnings/errors.
Also, could anyone give me hints on where to find logging output from custom scripts like this? Whatever message I put into logger, I seem to be unable to find anywhere in JIRA logs (although I'm aware that script might not work for now).
Any response is much appreciated guys, Thanks.
Martin
Well, I figure it out.
Method I posted, which implements listener as groovy class is used in different way than I expected. These kind of script files were used to be located in to specific path in JIRA installation and ScriptRunner would register them into JIRA as listeners.
In in order to create 'simple' listener script which reacts to issue updated event, I had to strip it down to this code
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.version.Version
IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)
Issue updatedIssue = event.getIssue()
Collection<Version> fixVersions = new ArrayList<Version>()
fixVersions = updatedIssue.getFixVersions()
Collection<Issue> subTasks = updatedIssue.getSubTaskObjects()
subTasks.each {
if (it instanceof MutableIssue) {
((MutableIssue) it).setFixVersions(fixVersions)
issueManager.updateIssue(event.getUser(), it, EventDispatchOption.ISSUE_UPDATED, false)
}
}
You past this to script runner interface and it works :-). Hope this helps anyone who's learning ScriptRunner. Cheers.
Matthew

How to record / capture audio with RecordControl on Java ME, SE K770i

I want to record sound on my Java ME App on K770i. So I used this:
http://java.sun.com/javame/reference/apis/jsr135/javax/microedition/media/control/RecordControl.html
example of RecordControl in my code. It goes like this:
import java.util.Vector;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.midlet.MIDlet;
import java.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;
(...)
try {
// Create a Player that captures live audio.
Player p = Manager.createPlayer("capture://audio");
p.realize();
// Get the RecordControl, set the record stream,
// start the Player and record for 5 seconds.
RecordControl rc = (RecordControl)p.getControl("RecordControl");
ByteArrayOutputStream output = new ByteArrayOutputStream();
rc.setRecordStream(output);
rc.startRecord();
p.start();
Thread.currentThread().sleep(5000);
rc.commit();
p.close();
} catch (IOException ioe) {
} catch (MediaException me) {
} catch (InterruptedException ie) { }
But unfortunately when I try to build it, it tells me:
*** Creating directories ***
*** Compiling source files ***
..\src\example\audiodemo\AudioPlayer.java:121: cannot find symbol
symbol : class RecordControl
location: class example.audiodemo.AudioPlayer
RecordControl rc = (RecordControl)p.getControl("RecordControl");
^
..\src\example\audiodemo\AudioPlayer.java:121: cannot find symbol
symbol : class RecordControl
location: class example.audiodemo.AudioPlayer
RecordControl rc = (RecordControl)p.getControl("RecordControl");
^
2 errors
So my question is: why there is no RecordControl class if in documentations it is written this class should be there. Or is there other method to record / capture audio from microfone in Java ME of Sony Ericsson?
How do you record sound?
The reason might be that it does not support audio capture.
Try to get this value from the Sony phone.
System.getProperty("supports.audio.capture")
Write a simple midlet with a form appending the above statement. like below..
frm.append("Supports Audio:" + System.getProperty("supports.audio.capture"));
If this returns the false, then you are clear...
The RecordControl wont work.
Few Sony phones dont support audio capture..like Sony p1i.
Hope this helps..
OK, so as i said I am java me newbie. So what I needed to do:
1) Added to MIDlet-Permissions in JAD and Manifest file to javax.microedition.media.control.RecordControl
2) In build.bat that I used from other demo project add inclusion of mmapi.jar, where the RecordControl is located in.
So i've added one last line shown here:
set CLDCAPI=%LIB_DIR%\cldcapi10.jar
set MIDPAPI=%LIB_DIR%\midpapi20.jar
set MMAPI=%LIB_DIR%\mmapi.jar
And according to this change updated two other lines of code:
%JAVAC% -bootclasspath %CLDCAPI%;%MIDPAPI%;%MMAPI% -source 1.3 -target 1.3 -d ..\tmpclasses -classpath ..\tmpclasses %JAVA_FILES%
%PREVERIFY% -classpath %CLDCAPI%;%MIDPAPI%;%MMAPI%;..\tmpclasses -d ..\classes ..\tmpclasses
And now everything compiles well. It was very lame isn't it?

Resources