Question about custom types

68 views
Skip to first unread message

anskarl

unread,
Jun 22, 2010, 5:56:28 AM6/22/10
to HyperGraphDB
Hello!

I'm new to HypergraphDB and I would like to ask you about custom atom
types. I have a simple class with two private final fields:

public final class ExampleNode {

private final String name;
private final Boolean value;

public ExampleNode(String name, Boolean value){
this.name = name;
this.value = value;
}

public Boolean getValue(){
return this.value;
}

public String getName(){
return this.getName;
}
}

The problem is that this class isn't serializable and doesn't contain
any default constructor. As far as I understand, I need to create my
own type implementation of HGAtomType or HGAtomTypeBase (http://
code.google.com/p/hypergraphdb/wiki/IntroHGTypes). However, I haven't
found any information about writing a custom type (http://
code.google.com/p/hypergraphdb/wiki/RefCustomTypes).

Could you tell my how should I create a custom type for HGDB?

Thanks,

Anastasios

anskarl

unread,
Jun 23, 2010, 6:38:21 AM6/23/10
to HyperGraphDB
I read some source code from the project "Seco" (http://
code.google.com/p/seco/) and I found the answer to my question.

I wrote the following custom type for the class ExampleNode:

public class ExampleType extends HGAtomTypeBase {

public static HGPersistentHandle HANDLE =
HGHandleFactory.makeHandle("7025c8e3-3ae4-11dc-872e-b08ac8fa688c");

@Override
public Object make(HGPersistentHandle handle,
LazyRef<HGHandle[]> lazyRef,
IncidenceSetRef incidenceSetRef) {

HGPersistentHandle[] layout =
graph.getStore().getLink(handle);

HGAtomType stringType =
graph.getTypeSystem().getAtomType(String.class);
HGAtomType booleanType =
graph.getTypeSystem().getAtomType(Boolean.class);

String name = (String) stringType.make(layout[0], null, null);
Boolean value = (Boolean) booleanType.make(layout[1], null,
null);

return new ExampleNode(name, value);
}

@Override
public HGPersistentHandle store(Object instance) {

HGPersistentHandle[] layout = new HGPersistentHandle[2];
ExampleNode node = (ExampleNode) instance;

HGAtomType stringType =
graph.getTypeSystem().getAtomType(String.class);
HGAtomType booleanType =
graph.getTypeSystem().getAtomType(Boolean.class);

layout[0] = stringType.store(node.getName());
layout[1] = booleanType.store(node.getValue());

return graph.getStore().store(layout);
}

@Override
public void release(HGPersistentHandle handle) {

HGPersistentHandle[] layout =
graph.getStore().getLink(handle);

HGAtomType stringType =
graph.getTypeSystem().getAtomType(String.class);
HGAtomType booleanType =
graph.getTypeSystem().getAtomType(Boolean.class);

stringType.release(layout[0]);
booleanType.release(layout[1]);
}
}

After that I registered this custom type into a graph instance, as it
is shown below:

ExampleType exampleType = new ExampleType();
exampleType.setHyperGraph(graph);
graph.getTypeSystem().addPredefinedType(ExampleType.HANDLE,
exampleType, ExampleNode.class);

However, I have one more question: is there any way to avoid object
castings (i.e String name = (String) stringType.make(layout[0], null,
null);) ?

Thanks,

Anastasios

Borislav Iordanov

unread,
Jun 23, 2010, 7:22:14 AM6/23/10
to hyperg...@googlegroups.com
Hi,

Sorry about the missing custom type tutorial....been bad about that,
will write it soon. But the basics are pretty much what you've
discovered yourself, so your code below looks good. But you also need
to delete the object layout from the store in the 'release' method, in
addition to deleting the two bean properties:

public void release(HGPersistentHandle handle)
{
//....your code is good...., but at the end:
graph.getStore().removeLink(handle);
}

The way to avoid the casting from the HAtomType.make method would be
to generify that interface, and given Java's a bit sloppy generics,
that can quickly become a pain in other ways. But I'll play a bit with
it and see if it makes sense.

Cheers,
Boris

> --
> You received this message because you are subscribed to the Google Groups "HyperGraphDB" group.
> To post to this group, send email to hyperg...@googlegroups.com.
> To unsubscribe from this group, send email to hypergraphdb...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/hypergraphdb?hl=en.
>
>

--
http://www.kobrix.com - HGDB graph database, Java Scripting IDE, NLP
http://kobrix.blogspot.com - news and rants

"Frozen brains tell no tales."

-- Buckethead

anskarl

unread,
Jun 23, 2010, 9:47:17 AM6/23/10
to HyperGraphDB
Hi,

thanks for the reply, I added the line for deleting the object layout
in the release method. Now I can successfully write custom types for
simple immutable classes in Scala.

I think that it would be nice to have generics in the HGAtomType
interface.

Regards,

Anastasios

David Vendt

unread,
Jul 8, 2013, 9:44:50 AM7/8/13
to hyperg...@googlegroups.com
That post was really helpful to me. It now works for my class + HGAtomType class. But I assume that the fields of my "record" (in the example above, consisting of a String, followed by a Boolean) can not be labeled, can they? (if yes, how?)

When querying these atoms it would be much cleaner to reference the element in the record by a label (e.g.,   hg.eq("label", "highway")) instead of numbering the elements from 0..n

Tomasz Bacewicz

unread,
Dec 12, 2020, 1:43:01 AM12/12/20
to HyperGraphDB
Hello!
could you tell me please why in the example "manual" query is made in the make() method to get link's target set:
HGPersistentHandle[] layout = 
graph.getStore().getLink(handle);  

Shouldn't lazyRef.deref() give us target set of the link stored previously in the store() method?
I wrote similar example with custom type and target set in the make() method is empty. Probably I don't understand inner workings of HGDB enough but this seem counter intuitive to me.

Cheers!
Reply all
Reply to author
Forward
0 new messages